+/*
+ * TypeDescription.h
+ *
+ * Created on: Sep 4, 2012
+ * Author: holy
+ */
+
+#ifndef LOADER_TYPEDESCRIPTION_H_
+#define LOADER_TYPEDESCRIPTION_H_
+
+#include <map>
+#include <memory>
+#include <string>
+#include <vector>
+
+namespace loader {
+
+class FieldDescription {
+
+public:
+ FieldDescription(std::ptrdiff_t offset, int type, bool reference = true, bool aggregate = false)
+ : offset(offset), type(type), reference(reference), aggregate(aggregate) { }
+
+ std::ptrdiff_t Offset() const { return offset; };
+ int TypeId() const { return type; }
+ bool IsReferenced() const { return reference; }
+ bool IsAggregate() const { return aggregate; }
+
+private:
+ std::ptrdiff_t offset;
+ int type;
+ bool reference;
+ bool aggregate;
+};
+
+class TypeDescription {
+
+public:
+ void AddField(const std::string &name, const FieldDescription &f);
+ bool HasField(const std::string &name) const;
+ const FieldDescription &GetField(const std::string &name) const;
+
+ void AddSupertype(int id, std::ptrdiff_t offset);
+ bool IsSubtypeOf(int id) const;
+ bool IsSubtypeOf(const TypeDescription &other) const { return IsSubtypeOf(other.TypeId()); }
+ std::ptrdiff_t SupertypeOffset(int id) const;
+ std::ptrdiff_t SupertypeOffset(const TypeDescription &other) const { return SupertypeOffset(other.TypeId()); }
+
+ int TypeId() const { return id; }
+ const std::string &TypeName() const { return name; }
+
+ void SetSize(int s) { size = s; }
+ int Size() const { return size; }
+
+ static TypeDescription &CreateOrGet(const std::string &name);
+ static int GetTypeId(const std::string &);
+ static const TypeDescription &Get(int id);
+
+private:
+ TypeDescription(int id, const std::string &name) : name(name), id(id), size(0) { }
+
+private:
+ std::string name;
+ std::map<std::string, FieldDescription> fields;
+ std::map<int, std::ptrdiff_t> supertypes;
+ int id;
+ int size;
+
+ static std::vector<TypeDescription> typeDescriptions;
+
+};
+
+}
+
+#endif /* LOADER_TYPEDESCRIPTION_H_ */