📄 creg.h
字号:
/*
creg - Code compoment registration system
Copyright 2005 Jelmer Cnossen
*/
#ifndef jc_CR_HEADER
#define jc_CR_HEADER
#include <vector>
#include <string>
#include "ISerializer.h"
#include "Sync/SyncedPrimitive.h"
namespace creg {
class IType;
class Class;
class ClassBinder;
typedef unsigned int uint;
// Fundamental/basic types
enum BasicTypeID
{
crInt, crUInt,
crShort, crUShort,
crChar, crUChar,
crFloat,
crDouble,
crBool,
#if defined(SYNCDEBUG) || defined(SYNCCHECK)
crSyncedSint, crSyncedUint,
crSyncedSshort, crSyncedUshort,
crSyncedSchar, crSyncedUchar,
crSyncedFloat,
crSyncedDouble,
crSyncedBool,
#endif
};
class IType
{
public:
// Type interface can go here...
virtual ~IType();
virtual void Serialize (ISerializer* s, void *instance) = 0;
virtual std::string GetName () = 0;
static IType* CreateBasicType (BasicTypeID t);
static IType* CreateStringType ();
static IType* CreateObjInstanceType (Class *objectType);
static IType* CreateEnumeratedType (size_t size);
};
class IMemberRegistrator
{
public:
virtual ~IMemberRegistrator();
virtual void RegisterMembers (Class *cls) = 0;
};
enum ClassFlags {
CF_None = 0,
CF_Abstract = 4
};
struct _DummyStruct {};
/** Class member flags to use with CR_MEMBER_SETFLAG */
enum ClassMemberFlag {
CM_NoSerialize = 1, /// Make the serializers skip the member
CM_Config = 2, // Exposed in config
};
/**
* Stores class bindings such as constructor/destructor
*/
class ClassBinder
{
public:
ClassBinder (const char *className, unsigned int cf, ClassBinder* base, IMemberRegistrator **mreg, int instanceSize, void (*constructorProc)(void *instance), void (*destructorProc)(void *instance));
Class *class_;
ClassBinder *base;
ClassFlags flags;
IMemberRegistrator **memberRegistrator;
const char *name;
int size; // size of an instance in bytes
void (*constructor)(void *instance);
void (*destructor)(void *instance); // needed for classes without virtual destructor (classes/structs declared with CR_DECLARE_STRUCT)
ClassBinder* nextBinder;
};
class System
{
public:
/// Return the global list of classes
static std::vector<Class*> GetClasses() { return classes; }
/// Initialization of creg, collects all the classes and initializes metadata
static void InitializeClasses ();
/// Shutdown of creg
static void FreeClasses ();
/// Find a class by name
static Class* GetClass(const std::string& name);
static void AddClassBinder(ClassBinder* cb);
protected:
static ClassBinder *binderList;
static std::vector<Class*> classes;
};
/** Represents a C++ class or struct, declared with CR_DECLARE/CR_DECLARE_STRUCT */
class Class
{
public:
struct Member
{
const char* name;
IType* type;
unsigned int offset;
int flags; // combination of ClassMemberFlag's
};
Class ();
~Class ();
/// Returns true if this class is equal to or derived from other
bool IsSubclassOf (Class* other);
/// Serialize all the registered members
void SerializeInstance (ISerializer* s, void *instance);
void DeleteInstance (void *inst);
/// Allocate an instance of the class
void* CreateInstance ();
/// Calculate a checksum from the class metadata
void CalculateChecksum (unsigned int& checksum);
void AddMember (const char *name, IType* type, unsigned int offset);
void SetMemberFlag (const char *name, ClassMemberFlag f);
Member* FindMember (const char *name);
void BeginFlag (ClassMemberFlag flag);
void EndFlag (ClassMemberFlag flag);
bool IsAbstract() { return (binder->flags & CF_Abstract) != 0; }
std::vector<Class*> GetImplementations(); // get all concrete classes that implement this class
std::vector <Member*> members;
std::vector <Class*> derivedClasses; // all classes that derive from this class
ClassBinder* binder;
std::string name;
Class *base;
void (_DummyStruct::*serializeProc)(ISerializer& s);
void (_DummyStruct::*postLoadProc)();
friend class ClassBinder;
};
// -------------------------------------------------------------------
// Container Type templates
// -------------------------------------------------------------------
// vector,deque container
template<typename T>
class DynamicArrayType : public IType
{
public:
typedef typename T::iterator iterator;
typedef typename T::value_type ElemT;
IType *elemType;
DynamicArrayType (IType *elemType) : elemType(elemType) {}
~DynamicArrayType () { if (elemType) delete elemType; }
void Serialize (ISerializer *s, void *inst) {
T& ct = *(T*)inst;
if (s->IsWriting ()) {
int size = (int)ct.size();
s->Serialize (&size,sizeof(int));
for (int a=0;a<size;a++)
elemType->Serialize (s, &ct[a]);
} else {
int size;
s->Serialize (&size, sizeof(int));
ct.resize (size);
for (int a=0;a<size;a++)
elemType->Serialize (s, &ct[a]);
}
}
std::string GetName() { return elemType->GetName() + "[]"; }
};
class StaticArrayBaseType : public IType
{
public:
IType *elemType;
int size, elemSize;
StaticArrayBaseType(IType *et, int Size, int ElemSize) :
elemType(et), size(Size), elemSize(ElemSize) {}
~StaticArrayBaseType() { if (elemType) delete elemType; }
std::string GetName();
};
template<typename T, int Size>
class StaticArrayType : public StaticArrayBaseType
{
public:
typedef T ArrayType[Size];
StaticArrayType(IType *elemType) : StaticArrayBaseType(elemType, Size, sizeof(ArrayType)/Size) {}
void Serialize (ISerializer *s, void *instance)
{
T* array = (T*)instance;
for (int a=0;a<Size;a++)
elemType->Serialize (s, &array[a]);
}
};
class EmptyType : public IType
{
public:
int size;
EmptyType(int Size) {size=Size;}
~EmptyType() {}
void Serialize (ISerializer* s, void *instance)
{
for (int a=0;a<size;a++) {
char c=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -