4 * Created on: Sep 4, 2012
8 #ifndef LOADER_TYPEDESCRIPTION_H_
9 #define LOADER_TYPEDESCRIPTION_H_
19 class FieldDescription {
22 FieldDescription(std::ptrdiff_t offset, int type)
23 : description(0), offset(offset), type(type), reference(false), aggregate(false) { }
25 std::ptrdiff_t Offset() const { return offset; };
26 int TypeId() const { return type; }
27 bool IsReferenced() const { return reference; }
28 bool IsAggregate() const { return aggregate; }
29 const char *Description() const { return description; }
31 FieldDescription &SetReferenced() { reference = true; return *this; }
32 FieldDescription &SetAggregate() { aggregate = true; return *this; }
33 FieldDescription &SetDescription(const char *d) { description = d; return *this; }
36 const char *description;
37 std::ptrdiff_t offset;
43 class TypeDescription {
46 void AddField(const std::string &name, const FieldDescription &f);
47 bool HasField(const std::string &name) const;
48 const FieldDescription &GetField(const std::string &name) const;
49 void Construct(void *) const;
50 void Load(void *) const;
52 void SetConstructor(void (*ctor)(void *)) { constructor = ctor; }
53 void SetLoader(void (*ld)(void *)) { loader = ld; }
54 void AddSupertype(int id, std::ptrdiff_t offset);
55 bool IsSubtypeOf(int id) const;
56 bool IsSubtypeOf(const TypeDescription &other) const { return IsSubtypeOf(other.TypeId()); }
57 std::ptrdiff_t SupertypeOffset(int id) const;
58 std::ptrdiff_t SupertypeOffset(const TypeDescription &other) const { return SupertypeOffset(other.TypeId()); }
60 int TypeId() const { return id; }
61 const std::string &TypeName() const { return name; }
63 void SetSize(int s) { size = s; }
64 int Size() const { return size; }
66 void SetDescription(const char *d) { description = d; }
67 const char *Description() const { return description; }
69 typedef std::map<std::string, FieldDescription>::const_iterator FieldIterator;
70 FieldIterator FieldsBegin() const { return fields.begin(); }
71 FieldIterator FieldsEnd() const { return fields.end(); }
73 static TypeDescription &Create(int id, const std::string &name);
74 static int GetTypeId(const std::string &);
75 static const TypeDescription &Get(int id);
77 static void WriteSourceWiki(std::ostream &);
80 TypeDescription(int id, const std::string &name) : constructor(0), loader(0), description(0), name(name), id(id), size(0) { }
83 void (*constructor)(void *);
84 void (*loader)(void *);
85 const char *description;
87 std::map<std::string, FieldDescription> fields;
88 std::map<int, std::ptrdiff_t> supertypes;
92 static std::map<int, TypeDescription> typeDescriptions;
93 static std::map<std::string, int> typeName2ID;
99 #endif /* LOADER_TYPEDESCRIPTION_H_ */