Skip to content

Commit

Permalink
[mq]: 2011-12-03_00-29-53_r165+.diff
Browse files Browse the repository at this point in the history
  • Loading branch information
ruifig committed Dec 4, 2011
1 parent d5135fc commit de0e2c7
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 127 deletions.
9 changes: 5 additions & 4 deletions inc/clcpp/Database.h
Expand Up @@ -284,15 +284,16 @@ namespace clcpp
// Does this type derive from the specified type, by hash?
bool DerivesFrom(unsigned int type_name_hash) const
{
// TODO RVF: Test this

// Search in immediate bases
for (int i=0; i<base_types.size(); i++)
{
// Search in immediate bases
if (base_types[i]->name.hash == type_name_hash)
return true;
}

// Search up the inheritance tree
// Search up the inheritance tree
for (int i=0; i<base_types.size(); i++)
{
if (base_types[i]->DerivesFrom(type_name_hash))
return true;
}
Expand Down
16 changes: 11 additions & 5 deletions release/inc/clcpp/Database.h
Expand Up @@ -270,25 +270,31 @@ namespace clcpp
Type()
: Primitive(KIND)
, size(0)
, base_type(0)
, ci(0)
{
}

Type(Kind k)
: Primitive(k)
, size(0)
, base_type(0)
, ci(0)
{
}

// Does this type derive from the specified type, by hash?
bool DerivesFrom(unsigned int type_name_hash) const
{
for (const Type* type = base_type; type; type = type->base_type)
// Search in immediate bases
for (int i=0; i<base_types.size(); i++)
{
if (type->name.hash == type_name_hash)
if (base_types[i]->name.hash == type_name_hash)
return true;
}

// Search up the inheritance tree
for (int i=0; i<base_types.size(); i++)
{
if (base_types[i]->DerivesFrom(type_name_hash))
return true;
}

Expand All @@ -304,7 +310,7 @@ namespace clcpp
unsigned int size;

// Single type this one derives from. Can be either a Class or TemplateType.
const Type* base_type;
CArray<const Type*> base_types;

// This is non-null if the type is a registered container
ContainerInfo* ci;
Expand Down
14 changes: 9 additions & 5 deletions release/src/clReflectUtil/FieldVisitor.cpp
Expand Up @@ -66,8 +66,10 @@ namespace
}

// Template types have no fields; just bases
if (template_type->base_type != 0)
VisitField(object, template_type->base_type, clcpp::Qualifier(), visitor);
for (int i = 0; i < template_type->base_types.size(); i++)
{
VisitField(object, template_type->base_types[i], clcpp::Qualifier(), visitor);
}
}


Expand All @@ -91,9 +93,11 @@ namespace
VisitField(object + field->offset, field->type, field->qualifier, visitor);
}

// Visit the base type at the same offset
if (class_type->base_type != 0)
VisitField(object, class_type->base_type, clcpp::Qualifier(), visitor);
// Visit the base types at the same offset
for (int i = 0; i < class_type->base_types.size(); i++)
{
VisitField(object, class_type->base_types[i], clcpp::Qualifier(), visitor);
}
}
}

Expand Down
59 changes: 37 additions & 22 deletions release/src/clReflectUtil/SerialiseJSON.cpp
Expand Up @@ -868,6 +868,28 @@ namespace
}


const clcpp::Field* FindFieldsRecursive(const clcpp::Class* class_type, unsigned int hash)
{
const clcpp::Field* field = 0;
field = clcpp::FindPrimitive(class_type->fields, hash);

// Search up through the inheritance hierarchy
if (field==0)
{
for (int i = 0; i < class_type->base_types.size(); i++)
{
if (class_type->base_types[i]->kind == clcpp::Primitive::KIND_CLASS)
{
field = FindFieldsRecursive(class_type->base_types[i]->AsClass(), hash);
if (field)
return field;
}
}
}

return 0;
}

void ParserPair(Context& ctx, Token& t, char*& object, const clcpp::Type*& type)
{
// Get the field name
Expand Down Expand Up @@ -915,17 +937,8 @@ namespace
{
const clcpp::Class* class_type = type->AsClass();
unsigned int field_hash = clcpp::internal::HashData(name.val.string, name.length);
field = clcpp::FindPrimitive(class_type->fields, field_hash);

// Search up through the inheritance hierarchy for the field
const clcpp::Type* base_type = class_type->base_type;
while (field == 0 && base_type)
{
// Only classes have fields that can be inspected
if (base_type->kind == clcpp::Primitive::KIND_CLASS)
field = clcpp::FindPrimitive(base_type->AsClass()->fields, field_hash);
base_type = base_type->base_type;
}
field = FindFieldsRecursive(class_type, field_hash);

// Don't load values for transient/nullstr fields
if (field && (field->flag_attributes & (clcpp::FlagAttribute::TRANSIENT | clcpp::FlagAttribute::NULLSTR)))
Expand All @@ -938,7 +951,6 @@ namespace
ParserValue(ctx, t, object + field->offset, field->type, field->qualifier.op, field);
}


void ParserMembers(Context& ctx, Token& t, char* object, const clcpp::Type* type)
{
ParserPair(ctx, t, object, type);
Expand Down Expand Up @@ -1364,7 +1376,7 @@ namespace
}


void SaveClass(clutl::WriteBuffer& out, const char* object, const clcpp::Class* class_type, unsigned int flags, bool& field_written)
void DoSaveClass(clutl::WriteBuffer& out, const char* object, const clcpp::Class* class_type, unsigned int flags, bool& field_written)
{
// Emit any create object requests
if (flags & clutl::JSONFlags::EMIT_CREATE_OBJECT &&
Expand Down Expand Up @@ -1421,21 +1433,24 @@ namespace
field_written = true;
}

// Locate the most immediate base class (skipping template types, for example)
const clcpp::Type* base_type = class_type->base_type;
while (base_type && base_type->kind != clcpp::Primitive::KIND_CLASS)
base_type = base_type->base_type;
}

// Recurse into base class
if (base_type)
void SaveClass(clutl::WriteBuffer& out, const char* object, const clcpp::Type* type, unsigned int flags, bool& field_written)
{
for (int i = 0; i< type->base_types.size(); i++)
{
const clcpp::Class* base_class = base_type->AsClass();
NewLine(out, flags);
SaveClass(out, object, base_class, flags, field_written);
const clcpp::Type* base_type = type->base_types[i];
if (base_type->kind == clcpp::Primitive::KIND_CLASS)
{
NewLine(out, flags);
DoSaveClass(out, object, base_type->AsClass(), flags, field_written);
}

if (base_type->base_types.size())
SaveClass(out, object, base_type, flags, field_written);
}
}


void SaveTemplateType(clutl::WriteBuffer& out, const char* object, const clcpp::TemplateType* template_type, unsigned int flags)
{
// Construct a read iterator and leave early if there are no elements
Expand Down
1 change: 0 additions & 1 deletion src/clReflectCore/Database.h
Expand Up @@ -254,7 +254,6 @@ namespace cldb

//
// A basic built-in type that classes/structs can also inherit from
// Only one base type is supported until it becomes necessary to do otherwise.
//
struct Type : public Primitive
{
Expand Down
5 changes: 3 additions & 2 deletions src/clReflectCore/DatabaseMetadata.cpp
Expand Up @@ -97,8 +97,9 @@ cldb::meta::DatabaseTypes::DatabaseTypes()
DatabaseField(&cldb::ContainerInfo::count),
};

DatabaseField bases_fields[] =
DatabaseField inheritance_fields[] =
{
DatabaseField(&cldb::TypeInheritance::name),
DatabaseField(&cldb::TypeInheritance::derived_type),
DatabaseField(&cldb::TypeInheritance::base_type),
};
Expand Down Expand Up @@ -126,5 +127,5 @@ cldb::meta::DatabaseTypes::DatabaseTypes()
m_ContainerInfoType.Type<cldb::ContainerInfo>().Fields(container_info_fields);

// Create descriptions of inheritance
m_TypeInheritanceType.Type<cldb::TypeInheritance>().Fields(bases_fields);
m_InheritanceType.Type<cldb::TypeInheritance>().Fields(inheritance_fields);
}
7 changes: 4 additions & 3 deletions src/clReflectCore/DatabaseMetadata.h
Expand Up @@ -125,7 +125,7 @@ namespace cldb
struct DatabaseType
{
// An empty type
DatabaseType() : size(0), packed_size(0) { }
DatabaseType() : size(0), packed_size(0), base_type(0) { }

// Set the type
template <typename TYPE>
Expand Down Expand Up @@ -191,7 +191,7 @@ namespace cldb
template <> const DatabaseType& GetType<NameAttribute>() const { return m_NameAttributeType; }
template <> const DatabaseType& GetType<TextAttribute>() const { return m_TextAttributeType; }
template <> const DatabaseType& GetType<ContainerInfo>() const { return m_ContainerInfoType; }
template <> const DatabaseType& GetType<TypeInheritance>() const { return m_TypeInheritanceType; }
template <> const DatabaseType& GetType<TypeInheritance>() const { return m_InheritanceType; }

// All type descriptions
DatabaseType m_PrimitiveType;
Expand All @@ -215,7 +215,8 @@ namespace cldb
// Container type description
DatabaseType m_ContainerInfoType;

DatabaseType m_TypeInheritanceType;
// Inheritance type description
DatabaseType m_InheritanceType;
};
}
}
49 changes: 28 additions & 21 deletions src/clReflectCore/DatabaseTextSerialiser.cpp
Expand Up @@ -100,20 +100,6 @@ namespace
WritePrimitive(fp, primitive, db);
fputs("\t", fp);
fputs(itohex(primitive.size), fp);

// TODO RVF : Refactor this
/*
fputs("\t", fp);
fputs(HexStringFromName(primitive.base_type, db), fp);
*/
/*
for(int i = 0; i < primitive.base_types.size(); i++)
{
if (i>0)
fputs(",", fp);
fputs(HexStringFromName(primitive.base_types[i], db), fp);
}
*/
}


Expand Down Expand Up @@ -214,6 +200,14 @@ namespace
fputs(itohex(ci.count), fp);
}

void WriteTypeInheritance(FILE *fp, const cldb::TypeInheritance& ti, const cldb::Database& db)
{
fputs(HexStringFromName(ti.name, db), fp);
fputs("\t", fp);
fputs(HexStringFromName(ti.derived_type, db), fp);
fputs("\t", fp);
fputs(HexStringFromName(ti.base_type, db), fp);
}

void WriteTextAttribute(FILE* fp, const cldb::TextAttribute& primitive, const cldb::Database& db)
{
Expand Down Expand Up @@ -290,6 +284,8 @@ void cldb::WriteTextDatabase(const char* filename, const Database& db)

WritePrimitives<ContainerInfo>(fp, db, WriteContainerInfo, "Containers", "Name\t\tRead\t\tWrite\t\tFlags\t\tCount");

WritePrimitives<TypeInheritance>(fp, db, WriteTypeInheritance, "Inheritance", "Name\t\tDerived\t\tBase");

fclose(fp);
}

Expand Down Expand Up @@ -420,8 +416,7 @@ namespace
cldb::u32 name, parent;
tok.GetNameAndParent(name, parent);

// Type parsing - discard the size and base type
tok.GetHexInt();
// Type parsing - discard the size
tok.GetHexInt();

// Add a new class to the database
Expand Down Expand Up @@ -503,7 +498,6 @@ namespace
}


// TODO RVF : Refactor this
void ParseClass(char* line, cldb::Database& db)
{
StringTokeniser tok(line, "\t");
Expand All @@ -514,7 +508,6 @@ namespace

// Type parsing
cldb::u32 size = tok.GetHexInt();
cldb::u32 base = tok.GetHexInt();

// Add a new class to the database
cldb::Class primitive(
Expand All @@ -526,8 +519,6 @@ namespace
}



// TODO RVF : Refactor this to remove "base" ?
void ParseTemplateType(char* line, cldb::Database& db)
{
StringTokeniser tok(line, "\t");
Expand All @@ -538,7 +529,6 @@ namespace

// Type parsing - discard size
tok.GetHexInt();
cldb::u32 base = tok.GetHexInt();

// Template type argument parsing
cldb::TemplateType primitive(db.GetName(name), db.GetName(parent));
Expand Down Expand Up @@ -664,6 +654,22 @@ namespace
db.Add(ci.name, ci);
}

void ParseInheritance(char* line, cldb::Database&db)
{
StringTokeniser tok(line, "\t");

// Parse the inheritance
cldb::u32 name = tok.GetHexInt();
cldb::u32 derived_type = tok.GetHexInt();
cldb::u32 base_type = tok.GetHexInt();

// Construct and add to the database
cldb::TypeInheritance ti;
ti.name = db.GetName(name);
ti.derived_type = db.GetName(derived_type);
ti.base_type = db.GetName(base_type);
db.Add(ti.name, ti);
}

template <typename PARSE_FUNC>
void ParseTable(FILE* fp, char* line, cldb::Database& db, const char* table_name, PARSE_FUNC parse_func)
Expand Down Expand Up @@ -728,6 +734,7 @@ bool cldb::ReadTextDatabase(const char* filename, Database& db)
ParseTable(fp, line, db, "Name Attributes", ParseNameAttribute);
ParseTable(fp, line, db, "Text Attributes", ParseTextAttribute);
ParseTable(fp, line, db, "Containers", ParseContainerInfo);
ParseTable(fp, line, db, "Inheritance", ParseInheritance);
}

fclose(fp);
Expand Down

0 comments on commit de0e2c7

Please sign in to comment.