4 * Created on: Sep 4, 2012
8 #include "TypeDescription.h"
20 void TypeDescription::AddField(const std::string &n, const FieldDescription &f) {
22 throw std::invalid_argument("duplicate definition of field " + n + " of type " + name);
24 fields.insert(std::make_pair(n, f));
28 bool TypeDescription::HasField(const std::string &name) const {
29 return fields.count(name);
32 const FieldDescription &TypeDescription::GetField(const std::string &n) const {
33 map<string, FieldDescription>::const_iterator result(fields.find(n));
34 if (result != fields.end()) {
35 return result->second;
37 throw std::invalid_argument("undefined field " + n + " of type " + name);
41 void TypeDescription::Construct(void *data) const {
45 std::memset(data, 0, Size());
50 void TypeDescription::AddSupertype(int id, std::ptrdiff_t offset) {
51 supertypes[id] = offset;
54 bool TypeDescription::IsSubtypeOf(int id) const {
55 return supertypes.count(id);
58 std::ptrdiff_t TypeDescription::SupertypeOffset(int id) const {
59 return supertypes.at(id);
63 vector<TypeDescription> TypeDescription::typeDescriptions;
65 TypeDescription &TypeDescription::CreateOrGet(const std::string &name) {
66 for (vector<TypeDescription>::iterator i(typeDescriptions.begin()), end(typeDescriptions.end()); i != end; ++i) {
67 if (i->name == name) {
71 typeDescriptions.push_back(TypeDescription(typeDescriptions.size(), name));
72 return typeDescriptions.back();
75 int TypeDescription::GetTypeId(const std::string &name) {
76 for (vector<TypeDescription>::size_type i(0), end(typeDescriptions.size()); i < end; ++i) {
77 if (typeDescriptions[i].name == name) {
81 typeDescriptions.push_back(TypeDescription(typeDescriptions.size(), name));
82 return typeDescriptions.size() - 1;
85 const TypeDescription &TypeDescription::Get(int id) {
86 assert(id >= 0 && id < int(typeDescriptions.size()));
87 return typeDescriptions[id];