Пример #1
0
    def __gen_info_index(self, field, idx):
        cog.out("template<>\n" "struct info_index<%d>\n" % idx)
        cog.out("{\n")
        cog.out("    static const unsigned index = %d;\n" % idx)

        # TODO: Add test for this
        value = None
        if field.value != None:
            value = field.value
        else:
            value = idx

        cog.out("    static const int value = %d;\n" % value)
        cog.out(
            "    inline static const char* string() { return \"%s::%s\"; }\n" %
            (self.name, field.name))

        if field.type != None:
            cog.out("    typedef %s type;\n" % sanitizeTypename(field.type))

        cog.out("    typedef %s::type enum_type;\n" % self.name)
        cog.out("    typedef %s::data data_type;\n" % self.name)

        # SHA1 has is 160-bits, but 'unsigned long long' is only
        # guaranteed by the standard to be 64-bits, so we chop off
        # the 96 bit difference. Collisions are extremely unlikely with SHA1 already,
        # but it's even less likely that you'd get a collision and *not* get a C++
        # type error, and we're only dealing with strings that are legit C++ variable
        # names.
        name_hash = self.__get_name_hash(field.name)
        cog.out("    static const unsigned long long name_hash = %du;\n" %
                name_hash)

        if self.possible_tags:
            cog.out("    struct tags\n" "    {\n")
            for t in self.possible_tags:
                if t in field.tags:
                    cog.out("        typedef cogflect::true_t %s;\n" % t)
                else:
                    cog.out("        typedef cogflect::false_t %s;\n" % t)
            cog.out("    };\n")

        if field.metadata:
            cog.out("    struct metadata\n" "    {\n")
            for e in field.metadata:
                cog.out("        ")
                e.out()
                cog.out("\n")
            cog.out("    };\n")

        cog.out("};\n\n")

        cog.out("typedef info_index<%d> %s_INFO;" % (idx, field.name))
Пример #2
0
    def __gen_info_index(self, field, idx):
        cog.out("template<>\n"
                "struct info_index<%d>\n" % idx)
        cog.out("{\n")
        cog.out("    static const unsigned index = %d;\n" % idx)

        # TODO: Add test for this
        value = None
        if field.value != None:
            value = field.value
        else:
            value = idx

        cog.out("    static const int value = %d;\n" % value)
        cog.out("    inline static const char* string() { return \"%s::%s\"; }\n" % (self.name, field.name))

        if field.type != None:
            cog.out("    typedef %s type;\n" % sanitizeTypename(field.type))

        cog.out("    typedef %s::type enum_type;\n" % self.name)
        cog.out("    typedef %s::data data_type;\n" % self.name)

        # SHA1 has is 160-bits, but 'unsigned long long' is only
        # guaranteed by the standard to be 64-bits, so we chop off
        # the 96 bit difference. Collisions are extremely unlikely with SHA1 already,
        # but it's even less likely that you'd get a collision and *not* get a C++
        # type error, and we're only dealing with strings that are legit C++ variable
        # names.
        name_hash = self.__get_name_hash(field.name)
        cog.out("    static const unsigned long long name_hash = %du;\n" % name_hash)

        if self.possible_tags:
            cog.out("    struct tags\n"
                    "    {\n")
            for t in self.possible_tags:
                if t in field.tags:
                    cog.out("        typedef cogflect::true_t %s;\n" % t)
                else:
                    cog.out("        typedef cogflect::false_t %s;\n" % t)
            cog.out("    };\n")

        if field.metadata:
            cog.out("    struct metadata\n"
                    "    {\n")
            for e in field.metadata:
                cog.out("        ")
                e.out()
                cog.out("\n")
            cog.out("    };\n")

        cog.out("};\n\n")

        cog.out("typedef info_index<%d> %s_INFO;" % (idx, field.name))
Пример #3
0
    def generate(self):
        generate_cppclass_common()

        cog.out("namespace %s {\n" "\n" % self.name)

        cog.out("class data\n" "{\n" "public:")

        cog.out(_body % {
            "name": self.name,
            "forAllMembersBody": self.__gen_for_all_members()
        })

        cog.out("private:\n")

        for f in self.fields:
            if f.type:
                cog.out("    %s %s_;\n" %
                        (sanitizeTypename(f.type), f.name.lower()))

        cog.out("}; // class data\n\n")

        for f in self.fields:
            if f.type:
                cog.out(
                    "template<>\n"
                    "inline %(name)s_INFO::type& data::get_member<%(name)s_INFO>()\n"
                    "{\n"
                    "    return %(lower_name)s_;\n"
                    "}\n\n" % {
                        "name": f.name,
                        "lower_name": f.name.lower()
                    })

        for f in self.fields:
            if f.type:
                cog.out(
                    "template<>\n"
                    "inline %(name)s_INFO::type const& data::get_member<%(name)s_INFO>() const\n"
                    "{\n"
                    "    return %(lower_name)s_;\n"
                    "}\n\n" % {
                        "name": f.name,
                        "lower_name": f.name.lower()
                    })

        cog.out("} // namespace %s\n" % self.name)
Пример #4
0
    def generate(self):
        generate_cppclass_common()

        cog.out("namespace %s {\n" "\n" % self.name)

        cog.out("class data\n" "{\n" "public:")

        cog.out(_body % {"name": self.name, "forAllMembersBody": self.__gen_for_all_members()})

        cog.out("private:\n")

        for f in self.fields:
            if f.type:
                cog.out("    %s %s_;\n" % (sanitizeTypename(f.type), f.name.lower()))

        cog.out("}; // class data\n\n")

        for f in self.fields:
            if f.type:
                cog.out(
                    "template<>\n"
                    "inline %(name)s_INFO::type& data::get_member<%(name)s_INFO>()\n"
                    "{\n"
                    "    return %(lower_name)s_;\n"
                    "}\n\n" % {"name": f.name, "lower_name": f.name.lower()}
                )

        for f in self.fields:
            if f.type:
                cog.out(
                    "template<>\n"
                    "inline %(name)s_INFO::type const& data::get_member<%(name)s_INFO>() const\n"
                    "{\n"
                    "    return %(lower_name)s_;\n"
                    "}\n\n" % {"name": f.name, "lower_name": f.name.lower()}
                )

        cog.out("} // namespace %s\n" % self.name)