1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
/*
* creg - Code compoment registration system
* Classes for serialization of registrated class instances
*/
#include <unordered_map>
#include <vector>
#include <string>
#include <string.h>
#include "creg_cond.h"
#include "System/Util.h"
#include "System/Sync/HsiehHash.h"
using namespace creg;
// -------------------------------------------------------------------
// some local statics, needed cause we work with global static vars
// -------------------------------------------------------------------
static std::unordered_map<const Class*, std::vector<Class*>>& derivedClasses()
{
// note: we cannot save this in `class Class`, cause those are created with
// global statics, and those have an arbitrary init order. And when a Class
// gets created, it doesn't mean its parent class already has. (Still its
// pointer is already valid!)
// So we cannot access other's Class' members that early in the loading
// stage, that's solved with these local static vars, which get created
// on access.
static std::unordered_map<const Class*, std::vector<Class*>> m;
return m;
}
static std::unordered_map<std::string, Class*>& mapNameToClass()
{
static std::unordered_map<std::string, Class*> m;
return m;
}
static std::vector<Class*>& classes()
{
static std::vector<Class*> v;
return v;
}
// -------------------------------------------------------------------
// Class Binder
// -------------------------------------------------------------------
ClassBinder::ClassBinder(const char* className, ClassFlags cf,
ClassBinder* baseClsBinder, void (*memberRegistrator)(creg::Class*), int instanceSize, int instanceAlignment, bool hasVTable, bool isCregStruct,
void (*constructorProc)(void* inst), void (*destructorProc)(void* inst))
: class_(className)
, base(baseClsBinder)
, flags(cf)
, name(className)
, size(instanceSize)
, alignment(instanceAlignment)
, hasVTable(hasVTable)
, isCregStruct(isCregStruct)
, constructor(constructorProc)
, destructor(destructorProc)
{
class_.binder = this;
class_.size = instanceSize;
class_.alignment = instanceAlignment;
System::AddClass(&class_);
if (base) {
derivedClasses()[&base->class_].push_back(&class_);
}
// Register members
assert(memberRegistrator);
memberRegistrator(&class_);
}
// -------------------------------------------------------------------
// System
// -------------------------------------------------------------------
const std::vector<Class*>& System::GetClasses()
{
return classes();
}
Class* System::GetClass(const std::string& name)
{
const auto it = mapNameToClass().find(name);
if (it == mapNameToClass().end()) {
return NULL;
}
return it->second;
}
void System::AddClass(Class* c)
{
classes().push_back(c);
mapNameToClass()[c->name] = c;
}
// ------------------------------------------------------------------
// creg::Class: Class description
// ------------------------------------------------------------------
Class::Class(const char* _name)
: binder(nullptr)
, size(0)
, alignment(0)
, currentMemberFlags(0)
{
name = _name;
}
bool Class::IsSubclassOf(Class* other) const
{
for (const Class* c = this; c; c = c->base()) {
if (c == other) {
return true;
}
}
return false;
}
std::vector<Class*> Class::GetImplementations()
{
std::vector<Class*> classes;
for (Class* dc: derivedClasses()[this]) {
if (!dc->IsAbstract()) {
classes.push_back(dc);
}
const std::vector<Class*>& impl = dc->GetImplementations();
classes.insert(classes.end(), impl.begin(), impl.end());
}
return classes;
}
const std::vector<Class*>& Class::GetDerivedClasses() const
{
return derivedClasses()[this];
}
void Class::BeginFlag(ClassMemberFlag flag)
{
currentMemberFlags |= (int)flag;
}
void Class::EndFlag(ClassMemberFlag flag)
{
currentMemberFlags &= ~(int)flag;
}
void Class::SetFlag(ClassFlags flag)
{
binder->flags = (ClassFlags) (binder->flags | flag);
}
void Class::AddMember(const char* name, boost::shared_ptr<IType> type, unsigned int offset, int alignment)
{
assert(!FindMember(name, false));
members.emplace_back();
Member& m = members.back();
m.name = name;
m.offset = offset;
m.type = type;
m.alignment = alignment;
m.flags = currentMemberFlags;
}
Class::Member* Class::FindMember(const char* name, const bool inherited)
{
for (Class* c = this; c; c = c->base()) {
for (Member& m: members) {
if (!STRCASECMP(m.name, name))
return &m;
}
if (!inherited)
return nullptr;
}
return nullptr;
}
void Class::SetMemberFlag(const char* name, ClassMemberFlag f)
{
for (Member& m: members) {
if (!strcmp(m.name, name)) {
m.flags |= (int)f;
break;
}
}
}
void* Class::CreateInstance()
{
void* inst = ::operator new(binder->size);
if (binder->constructor) {
binder->constructor(inst);
}
return inst;
}
void Class::DeleteInstance(void* inst)
{
if (binder->destructor) {
binder->destructor(inst);
}
::operator delete(inst);
}
void Class::CalculateChecksum(unsigned int& checksum)
{
for (Member& m: members) {
checksum += m.flags;
checksum = HsiehHash(m.name, strlen(m.name), checksum);
checksum = HsiehHash(m.type->GetName().data(), m.type->GetName().size(), checksum);
checksum += m.type->GetSize();
}
if (base()) {
base()->CalculateChecksum(checksum);
}
}
|