📄 reflection.h
字号:
#ifndef __Reflection_H
#define __Reflection_H
#include <string>
#include <vector>
using namespace std;
#ifndef null
#define null 0
#endif
#include "TypesWrapper.h"
#include "Destroyer.h"
class IReflection;
class ExchangeManager;
class object_ptr;
typedef void (*Function) (object_ptr& object, void* value);
class ExchangeManager
{
// Methods
public:
static ExchangeManager* Instance()
{
// Uses "Lazy initialization"
if (!instance_)
{
instance_ = new ExchangeManager();
destroyer_.SetObject(instance_);
}
return instance_;
}
int ObjectsNumber () { return static_cast<int>(reflection_objects_.size()); }
void Add (IReflection* reflected)
{
reflection_objects_.push_back(reflected);
}
void Remove (IReflection* reflected)
{
vector <IReflection*>::iterator i;
for (i = reflection_objects_.begin(); i != reflection_objects_.end(); ++i)
{
if (*i == reflected)
{
reflection_objects_.erase(i);
break;
}
}
}
bool Initiation (IReflection* reflected);
bool Distribution(IReflection* reflected, void* object);
// Constructor
protected:
ExchangeManager() {}
private:
static ExchangeManager* instance_;
static Destroyer<ExchangeManager> destroyer_;
vector<IReflection*> reflection_objects_;
};
class object_ptr
{
public:
object_ptr (const char* name, const char* type, void* value)
: name_(name), type_(type), value_(value), call_(null)
{
}
object_ptr (const char* name, const char* type, void* value, Function call)
: name_(name), type_(type), value_(value), call_(call)
{
}
string& type () { return type_; }
string& name () { return name_; }
void* value () { return value_; }
Function call () { return call_; }
private:
string type_;
string name_;
void* value_;
Function call_;
};
class IReflection
{
public:
IReflection ()
{
objects_ptr_.clear();
server_ = ExchangeManager::Instance();
server_->Add(this);
}
virtual void InitReflection () = 0;
int ObjectsNumber () { return static_cast<int>(objects_ptr_.size()); }
bool Get (void* value, object_ptr& ptr)
{
bool result = false;
for (int i = 0; i < ObjectsNumber(); i++)
{
if (objects_ptr_[i].value() == value)
{
ptr = objects_ptr_[i];
result = true;
break;
}
}
return result;
}
void* Value(int index)
{
void* result = null;
if (index < 0 || index >= ObjectsNumber()
) return result
;
result = objects_ptr_[index].value();
return result;
}
void Update (void* object);
void Exchange (IReflection* reflection);
void Exchange (IReflection* reflection, void* object);
void Distribution(void* object)
{
ExchangeManager::Instance()->Distribution(this,object);
}
void Bootstrap()
{
ExchangeManager::Instance()->Initiation(this);
}
virtual ~IReflection()
{
server_->Remove(this);
}
protected:
vector<object_ptr> objects_ptr_;
ExchangeManager* server_;
};
#define REFLECTED_OBJECT(ident)\
objects_ptr_.push_back(object_ptr(#ident,typeid(ident).name(),&ident));\
ident.Parent() = this;\
ident.Parent()->Bootstrap();
#define REFLECTED_OBJECT_ACTION(ident,func)\
objects_ptr_.push_back(object_ptr(#ident,typeid(ident).name(),&ident,func));\
ident.Parent() = this;\
ident.Parent()->Bootstrap();
#define REFLECTION_START\
void InitReflection() {
#define REFLECTION_FINAL\
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -