]> git.localhorst.tv Git - l2e.git/blob - src/loader/TypeDescription.h
new language, new compiler
[l2e.git] / src / loader / TypeDescription.h
1 #ifndef LOADER_TYPEDESCRIPTION_H_
2 #define LOADER_TYPEDESCRIPTION_H_
3
4 #include <iosfwd>
5 #include <map>
6 #include <memory>
7 #include <string>
8 #include <vector>
9
10 namespace loader {
11
12 class FieldDescription {
13
14 public:
15         FieldDescription(std::ptrdiff_t offset, int type)
16         : description(0), offset(offset), type(type), reference(false), aggregate(false) { }
17
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; }
23
24         FieldDescription &SetReferenced() { reference = true; return *this; }
25         FieldDescription &SetAggregate() { aggregate = true; return *this; }
26         FieldDescription &SetDescription(const char *d) { description = d; return *this; }
27
28 private:
29         const char *description;
30         std::ptrdiff_t offset;
31         int type;
32         bool reference;
33         bool aggregate;
34 };
35
36 class TypeDescription {
37
38 public:
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 Init(void *) const;
44         void Load(void *) const;
45
46         void SetConstructor(void (*ctor)(void *)) { constructor = ctor; }
47         void SetInitializer(void (*init)(void *)) { initializer = init; }
48         void SetLoader(void (*ld)(void *)) { loader = ld; }
49         void AddSupertype(int id, std::ptrdiff_t offset);
50         bool IsSubtypeOf(int id) const;
51         bool IsSubtypeOf(const TypeDescription &other) const { return IsSubtypeOf(other.TypeId()); }
52         std::ptrdiff_t SupertypeOffset(int id) const;
53         std::ptrdiff_t SupertypeOffset(const TypeDescription &other) const { return SupertypeOffset(other.TypeId()); }
54
55         int TypeId() const { return id; }
56         const std::string &TypeName() const { return name; }
57
58         void SetSize(int s) { size = s; }
59         int Size() const { return size; }
60
61         void SetDescription(const char *d) { description = d; }
62         const char *Description() const { return description; }
63
64         typedef std::map<std::string, FieldDescription>::const_iterator FieldIterator;
65         FieldIterator FieldsBegin() const { return fields.begin(); }
66         FieldIterator FieldsEnd() const { return fields.end(); }
67
68         static TypeDescription &Create(int id, const std::string &name);
69         static int GetTypeId(const std::string &);
70         static const TypeDescription &Get(int id);
71
72         static int GetMaxSize();
73
74         static void WriteSourceWiki(std::ostream &);
75
76 private:
77         TypeDescription(int id, const std::string &name)
78         : constructor(0)
79         , initializer(0)
80         , loader(0)
81         , description(0)
82         , name(name)
83         , id(id)
84         , size(0)
85         { }
86
87 private:
88         void (*constructor)(void *);
89         void (*initializer)(void *);
90         void (*loader)(void *);
91         const char *description;
92         std::string name;
93         std::map<std::string, FieldDescription> fields;
94         std::map<int, std::ptrdiff_t> supertypes;
95         int id;
96         int size;
97
98         static std::map<int, TypeDescription> typeDescriptions;
99         static std::map<std::string, int> typeName2ID;
100
101 };
102
103 }
104
105 #endif