]> git.localhorst.tv Git - l2e.git/blob - src/loader/TypeDescription.h
0f530d8a6ab1910f6dd43f9b9610f116817e0f94
[l2e.git] / src / loader / TypeDescription.h
1 /*
2  * TypeDescription.h
3  *
4  *  Created on: Sep 4, 2012
5  *      Author: holy
6  */
7
8 #ifndef LOADER_TYPEDESCRIPTION_H_
9 #define LOADER_TYPEDESCRIPTION_H_
10
11 #include <iosfwd>
12 #include <map>
13 #include <memory>
14 #include <string>
15 #include <vector>
16
17 namespace loader {
18
19 class FieldDescription {
20
21 public:
22         FieldDescription(std::ptrdiff_t offset, int type)
23         : description(0), offset(offset), type(type), reference(false), aggregate(false) { }
24
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; }
30
31         FieldDescription &SetReferenced() { reference = true; return *this; }
32         FieldDescription &SetAggregate() { aggregate = true; return *this; }
33         FieldDescription &SetDescription(const char *d) { description = d; return *this; }
34
35 private:
36         const char *description;
37         std::ptrdiff_t offset;
38         int type;
39         bool reference;
40         bool aggregate;
41 };
42
43 class TypeDescription {
44
45 public:
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;
51
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()); }
59
60         int TypeId() const { return id; }
61         const std::string &TypeName() const { return name; }
62
63         void SetSize(int s) { size = s; }
64         int Size() const { return size; }
65
66         void SetDescription(const char *d) { description = d; }
67         const char *Description() const { return description; }
68
69         typedef std::map<std::string, FieldDescription>::const_iterator FieldIterator;
70         FieldIterator FieldsBegin() const { return fields.begin(); }
71         FieldIterator FieldsEnd() const { return fields.end(); }
72
73         static TypeDescription &Create(int id, const std::string &name);
74         static int GetTypeId(const std::string &);
75         static const TypeDescription &Get(int id);
76
77         static void WriteSourceWiki(std::ostream &);
78
79 private:
80         TypeDescription(int id, const std::string &name) : constructor(0), loader(0), description(0), name(name), id(id), size(0) { }
81
82 private:
83         void (*constructor)(void *);
84         void (*loader)(void *);
85         const char *description;
86         std::string name;
87         std::map<std::string, FieldDescription> fields;
88         std::map<int, std::ptrdiff_t> supertypes;
89         int id;
90         int size;
91
92         static std::map<int, TypeDescription> typeDescriptions;
93         static std::map<std::string, int> typeName2ID;
94
95 };
96
97 }
98
99 #endif /* LOADER_TYPEDESCRIPTION_H_ */