How to use the cog.out function in cog

To help you get started, weโ€™ve selected a few cog examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jgarvin / Cogflect / cog-recipes / cogflect / Enum.py View on Github external
"    // it to be a function using the 'constexpr' keyword.\n")
        cog.out("    static const unsigned size = %d;\n\n" % len(self.fields))

        cog.out("};\n\n") # close class

        for f in self.fields:
            cog.out("template<>\n"
                    "struct type::info_with_hash<%du>\n"
                    "{\n"
                    "    typedef %s_INFO type;\n"
                    "};\n\n" % (self.__get_name_hash(f.name), f.name))

        cog.out("namespace {\n\n")
        for f in self.fields:
            cog.out("const type %s(type::make_from_info<%s_INFO>());\n" % (f.name, f.name))
        cog.out("\n}\n\n") # close anonymous namespace

        cog.out("}\n") # close enum namespace
github jdf / processing.py / buildtime / py / processing_parser.py View on Github external
def emit(self):
        cog.outl('\t\t\tcase %d: {' % self.arity)
        if len(self.methods) == 1 and not is_builtin(self.name):
            cog.out('\t\t\t\t')
            self.emit_method(self.methods[0])
        else:
            for i in range(self.arity):
                cog.outl('\t\t\t\tfinal PyType t%d = args[%d].getType();' % (i, i))
            self.methods.sort(key=lambda p: [m.getSimpleName()
                                             for m in p.getParameterTypes()],
                               reverse=True)
            for i in range(len(self.methods)):
                m = self.methods[i]
                if i > 0:
                    cog.out(' else ')
                else:
                    cog.out('\t\t\t\t')
                if self.arity > 0:
                    cog.out('if (')
                    for j in range(self.arity):
                        if j > 0:
                            cog.out(' && ')
                        emit_typecheck_expression(m.getParameterTypes()[j], 
                                                  't%d' % j)
                    cog.out(') {\n\t\t\t\t\t')
                self.emit_method(m)
                if self.arity > 0:
                    cog.out('\t\t\t\t}') 
            if is_builtin(self.name):
                cog.outl(' else { return %s_builtin.__call__(args, kws); }' % self.name)
            elif self.arity > 0:
github jdf / processing.py / buildtime / py / processing_parser.py View on Github external
def emit_method(self, m):
        if m.getReturnType() is not Void.TYPE:
            cog.out('return ')
            emit_python_prefix(m.getReturnType())
        cog.out('%s(' % self.name)
        for i in range(self.arity):
            if i > 0:
                cog.out(', ')
            emit_java_expression(m.getParameterTypes()[i], 'args[%d]' % i)
        cog.out(')')
        if m.getReturnType() is Void.TYPE:
            cog.outl(';')
            cog.outl('\t\t\t\treturn Py.None;')
        else:
            cog.outl(');')
github jgarvin / Cogflect / cog-recipes / cogflect / Enum.py View on Github external
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")
github jgarvin / Cogflect / cog-recipes / cogflect / Enum.py View on Github external
"        {\n")

        for f in self.fields:
            cog.out("            case %s_INFO::value:\n" % f.name)
            cog.out("                action.template action< %s_INFO >();\n" % f.name)
            cog.out("                break;\n")

        cog.out("            default:\n"
                "                action.default_action();\n"
                "                break;\n")


        cog.out("        }\n" # close switch
                "    }\n\n") # close value_switcher

        cog.out("    template\n"
                "    static inline void index_switcher(unsigned index, Action& action)\n"
                "    {\n"
                "        switch(index)\n"
                "        {\n")

        for f in self.fields:
            cog.out("            case %s_INFO::index:\n" % f.name)
            cog.out("                action.template action< %s_INFO >();\n" % f.name)
            cog.out("                break;\n")

        cog.out("            default:\n"
                "                action.default_action();\n"
                "                break;\n")


        cog.out("        }\n" # close switch
github jgarvin / Cogflect / cog-recipes / cogflect / common.py View on Github external
def generate_enum_common():
    global __generated_enum_common
    if __generated_enum_common:
        return

    generate_common_common()

    __generated_enum_common = True

    cog.out(__enum_big_string)
github jgarvin / Cogflect / cog-recipes / cogflect / Enum.py View on Github external
cog.out("namespace " + self.name + " {")
        cog.out("\n\n")

        cog.out("class type;\n")
        cog.out("class data;\n\n")

        # Forward declare info_index templates
        cog.out("template\n"
                "struct info_index;\n\n")

        for i, f in enumerate(self.fields):
            self.__gen_info_index(f, i)
            cog.out("\n\n")

        cog.out("class type\n"
                "{\n"
                "private:\n"
                "    int val_;\n"
                "\n"
                "    explicit inline type(int val) : val_(val) {}\n"
                "\n"
                "public:\n"
                "    inline type(type const& other) : val_(other.val_) {}\n"
                "    inline type& operator=(type other) { val_ = other.val_; }\n"
                "\n"
                "    template\n"
                "    static inline type make_from_info()\n"
                "    {\n"
                "        return type(Info::value);\n"
                "    }\n"
                "\n")
github jgarvin / Cogflect / cog-recipes / cogflect / Enum.py View on Github external
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)