/*
* @Filename:CMetaDataManager.h
* @Date:2016-10-21
* @Author:yuanzuochao
* @Description:
*
* @History:
* Date Author Records
* 2016-10-21 yuanzuochao Create
*/
#ifndef CMETADATAMANAGER_H
#define CMETADATAMANAGER_H
#include <iostream>
using namespace std;
#include <string>
#include <vector>
#include <typeinfo>
#include <map>
struct IInfo
{
virtual std::string getType() const = 0;
virtual std::string getName() const = 0;
virtual ~IInfo(){}
};
template<class _Class, class _Type> class CField;
template<class _Class, class _Type>
struct ISetter
{
virtual void accecpt(CField<_Class, _Type>* field, _Class* ins, _Type value) = 0;
virtual ~ISetter(){}
};
template<class _Class, class _Type>
struct IGetter
{
virtual _Type accecpt(const CField<_Class, _Type>* field, _Class* ins) const = 0;
virtual ~IGetter(){}
};
template<class _Class, class _Type>
class const_ref_param_setter:public ISetter<_Class, _Type>
{
public:
typedef void (_Class::*Setter) (const _Type&);
public:
const_ref_param_setter(Setter setter):
m_pSetter(setter)
{
}
void set(_Class* ins, _Type value)
{
(ins->*m_pSetter)(value);
}
void accecpt(CField<_Class, _Type>* field, _Class* ins, _Type value)
{
field->set(this, ins, value);
}
private:
Setter m_pSetter;
};
template<class _Class, class _Type>
class ordinary_setter:public ISetter<_Class, _Type>
{
public:
typedef void (_Class::*Setter) (_Type);
public:
ordinary_setter(Setter setter):
m_pSetter(setter)
{
}
void set(_Class* ins, _Type value)
{
(ins->*m_pSetter)(value);
}
void accecpt(CField<_Class, _Type>* field, _Class* ins, _Type value)
{
field->set(this, ins, value);
}
private:
Setter m_pSetter;
};
template<class _Class, class _Type>
class const_ref_ret_getter:public IGetter<_Class, _Type>
{
public:
typedef const _Type& (_Class::*Getter) () const;
public:
const_ref_ret_getter(Getter getter):
m_pGetter(getter)
{
}
const _Type& get(_Class* ins) const
{
return (ins->*m_pGetter)();
}
_Type accecpt(const CField<_Class, _Type>* field, _Class* ins) const
{
return field->get(this, ins);
}
private:
Getter m_pGetter;
};
template<class _Class, class _Type>
class ordinary_getter:public IGetter<_Class, _Type>
{
public:
typedef _Type (_Class::*Getter) () const;
public:
ordinary_getter(Getter getter):
m_pGetter(getter)
{
}
_Type get(_Class* ins) const
{
return (ins->*m_pGetter)();
}
_Type accecpt(const CField<_Class, _Type>* field, _Class* ins) const
{
return field->get(this, ins);
}
private:
Getter m_pGetter;
};
template<class _Class, class _Type>
class CField:public IInfo
{
public:
typedef _Type _Class::* FieldType;
typedef typename const_ref_param_setter<_Class, _Type>::Setter type_const_ref_param_setter;
typedef typename ordinary_setter<_Class, _Type>::Setter type_ordinary_setter;
typedef typename const_ref_ret_getter<_Class, _Type>::Getter type_const_ref_ret_getter;
typedef typename ordinary_getter<_Class, _Type>::Getter type_ordinary_getter;
public:
CField(std::string name, type_const_ref_param_setter setter, type_const_ref_ret_getter getter):
m_pField(NULL),
m_strName(name)
{
m_pSetter = new const_ref_param_setter<_Class,_Type>(setter);
m_pGetter = new const_ref_ret_getter<_Class, _Type>(getter);
}
CField(std::string name, type_ordinary_setter setter, type_ordinary_getter getter):
m_pField(NULL),
m_strName(name)
{
m_pSetter = new ordinary_setter<_Class,_Type>(setter);
m_pGetter = new ordinary_getter<_Class, _Type>(getter);
}
CField(std::string name, FieldType field):
m_pField(field),
m_strName(name),
m_pSetter(NULL),
m_pGetter(NULL)
{
}
~CField()
{
if(m_pGetter)
{
delete m_pGetter;
m_pGetter = NULL;
}
if(m_pSetter)
{
delete m_pSetter;
m_pSetter = NULL;
}
}
std::string getType() const
{
return typeid(_Type).name();
}
std::string getName() const
{
return m_strName;
}
void set(_Class* ins, _Type value)
{
if(m_pField)
{
ins->*m_pField = value;
}else if(m_pSetter)
{
m_pSetter->accecpt(this, ins, value);
}else
{
std::cout<<"no setter matched!"<<std::endl;
}
}
_Type get(_Class* ins) const
{
_Type value;
if(m_pField)
{
value = ins->*m_pField;
}else if(m_pGetter)
{
value = m_pGetter->accecpt(this, ins);
}else
{
std::cout<<"no getter matched!"<<std::endl;
}
return value;
}
void set(const_ref_param_setter<_Class, _Type>* setter, _Class* ins, _Type value)
{
setter->set(ins, value);
}
_Type get(const const_ref_ret_getter<_Class, _Type>* getter, _Class* ins) const
{
return getter->get(ins);
}
void set(ordinary_setter<_Class, _Type>* setter, _Class* ins, _Type value)
{
setter->set(ins, value);
}
_Type get(const ordinary_getter<_Class, _Type>* getter, _Class* ins) const
{
return getter->get(ins);
}
private:
FieldType m_pField;
std::string m_strName;
ISetter<_Class, _Type>* m_pSetter;
IGetter<_Class, _Type>* m_pGetter;
};
template <class _Class>
class CClass:public IInfo
{
typedef std::map<std::string, IInfo*>::value_type type_map;
typedef std::map<std::string, IInfo*>::iterator type_iter;
typedef std::map<std::string, IInfo*>::const_iterator type_const_iter;
public:
~CClass()
{
type_iter iter = m_mapFieldInfo.begin();
for(; iter != m_mapFieldInfo.end(); ++iter)
{
delete (iter->second);
}
m_mapFieldInfo.clear();
}
std::string getType() const
{
return typeid(_Class).name();
}
std::string getName() const
{
return typeid(_Class).name();
}
public:
//typedef typename CField<_Class, _Type>::type_const_ref_param_setter type_const_ref_param_setter;
//typedef typename CField<_Class, _Type>::type_const_ref_ret_getter type_const_ref_ret_getter;
template<class _Type>
void addField(std::string name, void (_Class::*setter)(const _Type&), const _Type& (_Class::*getter)() const )
{
IInfo* pField = new CField<_Class, _Type>(name, setter, getter);
m_mapFieldInfo.insert(type_map(name, pField));
}
template<class _Type>
void addField(std::string name, void (_Class::*setter)(_Type), _Type (_Class::*getter)() const)
{
IInfo* pField = new CField<_Class, _Type>(name, setter, getter);
m_mapFieldInfo.insert(type_map(name, pField));
}
template<class _Type>
void addField(std::string name, _Type (_Class::*director))
{
IInfo* pField = new CField<_Class, _Type>(name, director);
m_mapFieldInfo.insert(type_map(name, pField));
}
template<class _Type>
void set(std::string name, _Class* ins, _Type value) const
{
type_const_iter iter = m_mapFieldInfo.find(name);
if(iter == m_mapFieldInfo.end())
{
return;
}
CField<_Class, _Type>* pField = dynamic_cast<CField<_Class, _Type>*>(iter->second);
if(NULL == pField)
{
return;
}
pField->set(ins, value);
}
template<class _Type>
_Type get(std::string name, _Class* ins) const
{
_Type value;
type_const_iter iter = m_mapFieldInfo.find(name);
do
{
if(iter == m_mapFieldInfo.end())
{
std::cout<<"name: "<<name<<" not be found"<<std::endl;
break;
}
CField<_Class, _Type>* pField = dynamic_cast<CField<_Class, _Type>*>(iter->second);
if(NULL == pField)
{
std::cout<<"dynamic_cast failed"<<std::endl;
break;
}
value = pField->get(ins);
}while(0);
return value;
}
std::string getFieldType(std::string name) const
{
std::string type;
type_const_iter iter = m_mapFieldInfo.find(name);
if(iter == m_mapFieldInfo.end())
{
std::cout<<"name: "<<name<<" not be found"<<std::endl;
return type;
}
type = (iter->second)->getType();
return type;
}
private:
std::map<std::string, IInfo*> m_mapFieldInfo;
};
class CMetaDataManager
{
typedef std::map<std::string, IInfo*>::value_type type_map;
typedef std::map<std::string, IInfo*>::iterator type_iter;
typedef std::map<std::string, IInfo*>::const_iterator type_const_iter;
public:
template<class _Class>
void registry()
{
IInfo* pClass = new CClass<_Class>();
m_mapClassInfo.insert(type_map(typeid(_Class).name(), pClass));
}
template<class _Class, class _Type>
void addField(std::string name, void (_Class::*setter)(const _Type&), const _Type& (_Class::*getter)() const )
{
type_iter iter = m_mapClassInfo.find(typeid(_Class).name());
if(iter == m_mapClassInfo.end())
{
return;
}
CClass<_Class>* pClass = dynamic_cast<CClass<_Class>*>(iter->second);
if(NULL == pClass)
{
return;
}
pClass->addField(name, setter, getter);
}
template<class _Class, class _Type>
void addField(std::string name, void (_Class::*setter)(_Type), _Type (_Class::*getter)() const)
{
type_iter iter = m_mapClassInfo.find(typeid(_Class).name());
if(iter == m_mapClassInfo.end())
{
return;
}
CClass<_Class>* pClass = dynamic_cast<CClass<_Class>*>(iter->second);
if(NULL == pClass)
{
return;
}
pClass->addField(name, setter, getter);
}
template<class _Class, class _Type>
void addField(std::string name, _Type (_Class::*director))
{
type_iter iter = m_mapClassInfo.find(typeid(_Class).name());
if(iter == m_mapClassInfo.end())
{
return;
}
CClass<_Class>* pClass = dynamic_cast<CClass<_Class>*>(iter->second);
if(NULL == pClass)
{
return;
}
pClass->addField(name, director);
}
template<class _Class, class _Type>
void set(std::string name, _Class* ins, _Type value) const
{
type_const_iter iter = m_mapClassInfo.find(typeid(_Class).name());
if(iter == m_mapClassInfo.end())
{
return;
}
CClass<_Class>* pClass = dynamic_cast<CClass<_Class>*>(iter->second);
if(NULL == pClass)
{
return;
}
pClass->set(name, ins, value);
}
template<class _Class, class _Type>
_Type get(std::string name, _Class* ins) const
{
_Type value;
type_const_iter iter = m_mapClassInfo.find(typeid(_Class).name());
do
{
if(iter == m_mapClassInfo.end())
{
break;
}
CClass<_Class>* pClass = dynamic_cast<CClass<_Class>*>(iter->second);
if(NULL == pClass)
{
break;
}
value = pClass->get<_Type>(name, ins);
}while(0);
return value ;
}
template<class _Class>
std::string getFieldType(std::string name) const
{
std::string type="";
type_const_iter iter = m_mapClassInfo.find(typeid(_Class).name());
if(iter == m_mapClassInfo.end())
{
return type;
}
CClass<_Class>* pClass = dynamic_cast<CClass<_Class>*>(iter->second);
if(NULL == pClass)
{
return type;
}
return pClass->getFieldType(name);
}
private:
std::map<std::string, IInfo*> m_mapClassInfo;
};
#endif //CMETADATAMANAGER_H
相关推荐
C++ 获取串口的详细信息接口工程代码,已在项目中成功使用 详细描述:https://blog.csdn.net/ZuoYueXian/article/details/86606444
//获取属性值 _bstr_t bstrValue=CVB::get(pDispatch,L\"Value\"); //设置属性值 CVB::put(pDispatch,L\"Value\",bstrValue); //调用方法 _bstr_t bstrMessage2=CVB::Invoke1( pDispatch, L"Echo", L"hello world!...
9.5.10 为库存属性设置缺省值 9.5.11 实现属性表 小结 第10章 Internet COM组件 10.1 ActiveX控件与Internet 10.1.1 Internet对ActiveX控件提出的新要求 10.1.2 ActiveX控件的安全设置 10.1.2.1 设置初始化安全性 ...
实例173 获得Windows和System的路径 实例174 控制光驱的弹开与关闭 实例175 启动控制面板 实例176 为程序添加快捷键 6.5 系统监控 实例177 检测系统启动模式 实例178 内存使用状态 实例179 监视剪贴板内容 ...
0575 获取CPU ID值 380 0576 获取CPU时钟频率 381 0577 获得Windows和System的路径 381 0578 获取特殊文件夹路径 382 0579 检测系统启动模式 382 0580 判断操作系统类型 383 0581 获取当前系统的运行...
利用C++的派生性开发者可以减少开发自定义窗口的时间和创造出可重用的代码;利用虚拟性可以在必要时更好的控制窗口的活动。而且C++本身所具备的超越C语言的特性都可以使开发者编写出更易用,更灵活的代码。 在MFC...
第1章 语言基础 1 1.1 基础语句 2 0001 注释的使用方法及注意事项 2 0002 使用汇编语言 2 0003 如何使用内联函数 2 0004 如何使用#define自定义宏 3 ...0083 在C++.NET中如何获取系统信息 34
cc实例173 获得Windows和System的路径 cc实例174 控制光驱的弹开与关闭 cc实例175 启动控制面板 cc实例176 为程序添加快捷键 6.5 系统监控 cc实例177 检测系统启动模式 cc实例178 ...
cc实例172 隐藏.c显示Windows任务栏 6.4 系统相关设置 cc实例173 获得Windows和System的路径 cc实例174 控制光驱的弹开与关闭 cc实例175 启动控制面板 cc实例176 为程序添加快捷键 6.5 系统监控 cc...
cc实例173 获得Windows和System的路径 cc实例174 控制光驱的弹开与关闭 cc实例175 启动控制面板 cc实例176 为程序添加快捷键 6.5 系统监控 cc实例177 检测系统启动模式 cc实例178 ...
算法主要利用把像素的颜色拆解出来 ARGB,之后获取每一个像素点的颜色值,并且把色彩图转成黑白图,通过黑白图求3-4个颜色的平均值实现png图片的透明贴图效果。 算法还可以实现修正贴图的位置,并处理超出边界的情况...
1.6.5 设置Visual C++ 2010的选项 23 1.6.6 创建和执行Windows应用程序 23 1.6.7 创建Windows Forms应用程序 26 1.7 小结 27 1.8 本章主要内容 28 第2章 数据、变量和计算 29 2.1 C++程序结构 29 2.1.1 main...
实例173 获得Windows和System的路径 实例174 控制光驱的弹开与关闭 实例175 启动控制面板 实例176 为程序添加快捷键 6.5 系统监控 实例177 检测系统启动模式 实例178 内存使用状态 实例179 监视剪贴板内容 ...
0575 获取CPU ID值 380 0576 获取CPU时钟频率 381 0577 获得Windows和System的路径 381 0578 获取特殊文件夹路径 382 0579 检测系统启动模式 382 0580 判断操作系统类型 383 0581 获取当前系统的运行...
如果其它对象可以获得该属性的非常量引用(或指针),那么对该属性的单纯赋值就会破坏业务规则的完整性。 (4)流操作符重载返回值申明为“引用”的作用: 流操作符和>>,这两个操作符常常希望被连续使用,...
18、文件属性信息的获取与设置 ①、属性信息的获取:大小、时间、隐藏属性等; ②、属性信息的设置 [删除只读属性文件]; ③、判断文件是否存在以及是否为目录; 19、文件夹操作 ①、文件夹的创建与删除; ②...