1 #ifndef LOADER_TYPEDESCRIPTION_H_
2 #define LOADER_TYPEDESCRIPTION_H_
12 class FieldDescription {
15 FieldDescription(std::ptrdiff_t offset, int type)
16 : description(0), offset(offset), type(type), reference(false), aggregate(false) { }
18 std::ptrdiff_t Offset() const { return offset; };
19 int TypeId() const { return type; }
20 bool IsReferenced() const { return reference; }
21 bool IsAggregate() const { return aggregate; }
22 const char *Description() const { return description; }
24 FieldDescription &SetReferenced() { reference = true; return *this; }
25 FieldDescription &SetAggregate() { aggregate = true; return *this; }
26 FieldDescription &SetDescription(const char *d) { description = d; return *this; }
29 const char *description;
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;
43 void Load(void *) const;
44 bool NeedsLinking() const { return link; }
46 void SetConstructor(void (*ctor)(void *)) { constructor = ctor; }
47 void SetLoader(void (*ld)(void *)) { loader = ld; }
48 void AddSupertype(int id, std::ptrdiff_t offset);
49 bool IsSubtypeOf(int id) const;
50 bool IsSubtypeOf(const TypeDescription &other) const { return IsSubtypeOf(other.TypeId()); }
51 std::ptrdiff_t SupertypeOffset(int id) const;
52 std::ptrdiff_t SupertypeOffset(const TypeDescription &other) const { return SupertypeOffset(other.TypeId()); }
54 int TypeId() const { return id; }
55 const std::string &TypeName() const { return name; }
57 void SetSize(int s) { size = s; }
58 int Size() const { return size; }
60 void SetDescription(const char *d) { description = d; }
61 const char *Description() const { return description; }
63 typedef std::map<std::string, FieldDescription>::const_iterator FieldIterator;
64 FieldIterator FieldsBegin() const { return fields.begin(); }
65 FieldIterator FieldsEnd() const { return fields.end(); }
67 static TypeDescription &Create(int id, const std::string &name);
68 static int GetTypeId(const std::string &);
69 static const TypeDescription &Get(int id);
71 static int GetMaxSize();
73 static void WriteSourceWiki(std::ostream &);
76 TypeDescription(int id, const std::string &name) : constructor(0), loader(0), description(0), name(name), id(id), size(0), link(false) { }
79 void (*constructor)(void *);
80 void (*loader)(void *);
81 const char *description;
83 std::map<std::string, FieldDescription> fields;
84 std::map<int, std::ptrdiff_t> supertypes;
89 static std::map<int, TypeDescription> typeDescriptions;
90 static std::map<std::string, int> typeName2ID;