]> git.localhorst.tv Git - l2e.git/blob - src/loader/TypeDescription.h
edd0177c418fe3f344834165d7638d278749276b
[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 <map>
12 #include <memory>
13 #include <string>
14 #include <vector>
15
16 namespace loader {
17
18 class FieldDescription {
19
20 public:
21         FieldDescription(std::ptrdiff_t offset, int type, bool reference = true, bool aggregate = false)
22         : offset(offset), type(type), reference(reference), aggregate(aggregate) { }
23
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; }
28
29 private:
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 Load(void *) const;
44
45         void SetConstructor(void (*ctor)(void *)) { constructor = ctor; }
46         void SetLoader(void (*ld)(void *)) { loader = ld; }
47         void AddSupertype(int id, std::ptrdiff_t offset);
48         bool IsSubtypeOf(int id) const;
49         bool IsSubtypeOf(const TypeDescription &other) const { return IsSubtypeOf(other.TypeId()); }
50         std::ptrdiff_t SupertypeOffset(int id) const;
51         std::ptrdiff_t SupertypeOffset(const TypeDescription &other) const { return SupertypeOffset(other.TypeId()); }
52
53         int TypeId() const { return id; }
54         const std::string &TypeName() const { return name; }
55
56         void SetSize(int s) { size = s; }
57         int Size() const { return size; }
58
59         typedef std::map<std::string, FieldDescription>::const_iterator FieldIterator;
60         FieldIterator FieldsBegin() const { return fields.begin(); }
61         FieldIterator FieldsEnd() const { return fields.end(); }
62
63         static TypeDescription &CreateOrGet(const std::string &name);
64         static int GetTypeId(const std::string &);
65         static const TypeDescription &Get(int id);
66
67 private:
68         TypeDescription(int id, const std::string &name) : constructor(0), loader(0), name(name), id(id), size(0) { }
69
70 private:
71         void (*constructor)(void *);
72         void (*loader)(void *);
73         std::string name;
74         std::map<std::string, FieldDescription> fields;
75         std::map<int, std::ptrdiff_t> supertypes;
76         int id;
77         int size;
78
79         static std::vector<TypeDescription> typeDescriptions;
80
81 };
82
83 }
84
85 #endif /* LOADER_TYPEDESCRIPTION_H_ */