示例#1
0
        public void WriteReflection(TextOut i_output)
        {
            i_output.Append("void setup_type(TypeSetupContext<" + FullName + "> & i_context)");
            i_output.NewLine();
            i_output.AppendLine("{");
            i_output.Tab();
            i_output.AppendLine("using ThisClass = " + FullName + ";");

            // bases
            ClassEntry[] bases = m_bases.ToArray();
            int baseCount = bases.Length;

            if (baseCount > 0)
            {
                i_output.AppendLine("i_context.type()->set_base_types( {");
                i_output.Tab();

                for (int baseIndex = 0; baseIndex < baseCount; baseIndex++)
                {
                    ClassEntry baseClass = bases[baseIndex];
                    string term;
                    if (baseIndex + 1 < baseCount)
                        term = ",";
                    else
                        term = " });";
                    if( m_virtualBases.Contains( baseClass) )
                        i_output.AppendLine("BaseType::from_types_virtual<ThisClass, " + baseClass.FullName + ">()" + term);
                    else
                        i_output.AppendLine("BaseType::from_types<ThisClass, " + baseClass.FullName + ">()" + term);
                }
                i_output.Untab();
            }

            // gmdt function
            i_output.AppendLine("i_context.type()->set_most_derived_type_func(&ThisClass::static_get_type);");

            // test base_types container
            i_output.AppendLine("");
            i_output.AppendLine("// test base_types container");
            i_output.AppendLine("std::vector<const reflective::Type*> test_bases = {");
            for (int baseIndex = 0; baseIndex < baseCount; baseIndex++)
            {
                i_output.AppendLine("&reflective::get_naked_type<" + bases[baseIndex].FullName + ">(), ");
            }
            i_output.AppendLine("};");
            i_output.AppendLine("for( const auto & base : i_context.type()->base_types() )");
            i_output.AppendLine("{");
            i_output.Tab();
            i_output.AppendLine("auto it = reflective::Ext::find_if(test_bases, ");
            i_output.Tab();
            i_output.AppendLine("[&base] (const reflective::Type * i_s) { return i_s == base.base_type(); } );");
            i_output.Untab();
            i_output.AppendLine("REFLECTIVE_INTERNAL_ASSERT( it != test_bases.end() );");
            i_output.AppendLine("test_bases.erase(it);");
            i_output.Untab();
            i_output.AppendLine("}");
            i_output.AppendLine("REFLECTIVE_INTERNAL_ASSERT( test_bases.size() == 0 );");

            i_output.Untab();
            i_output.AppendLine("}");
        }
示例#2
0
        public void WriteDefinition(TextOut i_output, bool i_emitTypeChecks)
        {
            i_output.Append("class " + Name);

            bool first = true;
            foreach( ClassEntry baseEntry in m_bases )
            {
                if( first )
                {
                    i_output.Append( " : public " );
                }
                else
                {
                    i_output.Append(", public ");
                }
                if( m_virtualBases.Contains(baseEntry) )
                {
                    i_output.Append("virtual ");
                }
                i_output.Append(baseEntry.Name);
                first = false;
            }
            i_output.NewLine();
            i_output.AppendLine("{");
            i_output.Tab();

            i_output.AppendLine("public:");

            // vtable
            if (m_hasVTable)
            {
                i_output.AppendLine("virtual ~" + Name + "() {}");
                i_output.AppendLine("virtual int vfunct_" + Name + "() { return 42; }");
            }

            if (i_emitTypeChecks)
            {
                i_output.AppendLine("std::string type_name = \"" + Name + "\";");
            }

            if(m_hasMDTFunc)
            {
                i_output.AppendLine("virtual const reflective::Type & virtual_get_type() const");
                i_output.AppendLine("\t{ return reflective::get_naked_type<" +Name + ">(); }");
                i_output.AppendLine("");
                i_output.AppendLine("static const reflective::Type & static_get_type(const void * i_object)");
                i_output.AppendLine("\t{ return static_cast<const " + Name + " *>(i_object)->virtual_get_type(); }");
            }

            // properties
            foreach (ClassProperty prop in m_properties)
            {
                i_output.AppendLine(prop.Type + " " + prop.Name + ";" );
            }

            i_output.Untab();
            i_output.AppendLine("};");
        }