4 * Created on: Sep 4, 2012
8 #ifndef LOADER_TYPEDESCRIPTION_H_
9 #define LOADER_TYPEDESCRIPTION_H_
18 class FieldDescription {
21 FieldDescription(std::ptrdiff_t offset, int type, bool reference = true, bool aggregate = false)
22 : offset(offset), type(type), reference(reference), aggregate(aggregate) { }
24 std::ptrdiff_t Offset() const { return offset; };
25 int TypeId() const { return type; }
26 bool IsReferenced() const { return reference; }
27 bool IsAggregate() const { return aggregate; }
30 std::ptrdiff_t offset;
36 class TypeDescription {
39 void AddField(const std::string &name, const FieldDescription &f);
40 bool HasField(const std::string &name) const;
41 const FieldDescription &GetField(const std::string &name) const;
42 void Construct(void *) const;
44 void SetConstructor(void (*ctor)(void *)) { constructor = ctor; }
45 void AddSupertype(int id, std::ptrdiff_t offset);
46 bool IsSubtypeOf(int id) const;
47 bool IsSubtypeOf(const TypeDescription &other) const { return IsSubtypeOf(other.TypeId()); }
48 std::ptrdiff_t SupertypeOffset(int id) const;
49 std::ptrdiff_t SupertypeOffset(const TypeDescription &other) const { return SupertypeOffset(other.TypeId()); }
51 int TypeId() const { return id; }
52 const std::string &TypeName() const { return name; }
54 void SetSize(int s) { size = s; }
55 int Size() const { return size; }
57 typedef std::map<std::string, FieldDescription>::const_iterator FieldIterator;
58 FieldIterator FieldsBegin() const { return fields.begin(); }
59 FieldIterator FieldsEnd() const { return fields.end(); }
61 static TypeDescription &CreateOrGet(const std::string &name);
62 static int GetTypeId(const std::string &);
63 static const TypeDescription &Get(int id);
66 TypeDescription(int id, const std::string &name) : constructor(0), name(name), id(id), size(0) { }
69 void (*constructor)(void *);
71 std::map<std::string, FieldDescription> fields;
72 std::map<int, std::ptrdiff_t> supertypes;
76 static std::vector<TypeDescription> typeDescriptions;
82 #endif /* LOADER_TYPEDESCRIPTION_H_ */