]> git.localhorst.tv Git - l2e.git/blob - src/loader/TypeDescription.cpp
fixed some issues to get it to compile again
[l2e.git] / src / loader / TypeDescription.cpp
1 /*
2  * TypeDescription.cpp
3  *
4  *  Created on: Sep 4, 2012
5  *      Author: holy
6  */
7
8 #include "TypeDescription.h"
9
10 #include <cassert>
11 #include <stdexcept>
12
13 using std::map;
14 using std::string;
15 using std::vector;
16
17 namespace loader {
18
19 void TypeDescription::AddField(const std::string &n, const FieldDescription &f) {
20         if (HasField(n)) {
21                 throw std::invalid_argument("duplicate definition of field " + n + " of type " + name);
22         } else {
23                 fields.insert(std::make_pair(n, f));
24         }
25 }
26
27 bool TypeDescription::HasField(const std::string &name) const {
28         return fields.count(name);
29 }
30
31 const FieldDescription &TypeDescription::GetField(const std::string &n) const {
32         map<string, FieldDescription>::const_iterator result(fields.find(n));
33         if (result != fields.end()) {
34                 return result->second;
35         } else {
36                 throw std::invalid_argument("undefined field " + n + " of type " + name);
37         }
38 }
39
40
41 void TypeDescription::AddSupertype(int id, std::ptrdiff_t offset) {
42         supertypes[id] = offset;
43 }
44
45 bool TypeDescription::IsSubtypeOf(int id) const {
46         return supertypes.count(id);
47 }
48
49 std::ptrdiff_t TypeDescription::SupertypeOffset(int id) const {
50         return supertypes.at(id);
51 }
52
53
54 vector<TypeDescription> TypeDescription::typeDescriptions;
55
56 TypeDescription &TypeDescription::CreateOrGet(const std::string &name) {
57         for (vector<TypeDescription>::iterator i(typeDescriptions.begin()), end(typeDescriptions.end()); i != end; ++i) {
58                 if (i->name == name) {
59                         return *i;
60                 }
61         }
62         typeDescriptions.push_back(TypeDescription(typeDescriptions.size(), name));
63         return typeDescriptions.back();
64 }
65
66 int TypeDescription::GetTypeId(const std::string &name) {
67         for (vector<TypeDescription>::size_type i(0), end(typeDescriptions.size()); i < end; ++i) {
68                 if (typeDescriptions[i].name == name) {
69                         return i;
70                 }
71         }
72         typeDescriptions.push_back(TypeDescription(typeDescriptions.size(), name));
73         return typeDescriptions.size() - 1;
74 }
75
76 const TypeDescription &TypeDescription::Get(int id) {
77         assert(id >= 0 && id < int(typeDescriptions.size()));
78         return typeDescriptions[id];
79 }
80
81 }