#pragma once #ifndef RESDATAOBJECT_EXPORTS #ifdef _WIN64 #ifdef _DEBUG #pragma comment(lib, "Dios.Dev.ResDataObjectX64D.lib") #else #pragma comment(lib, "Dios.Dev.ResDataObjectX64.lib") #endif #else #ifdef _DEBUG #pragma comment(lib, "Dios.Dev.ResDataObjectD.lib") #else #pragma comment(lib, "Dios.Dev.ResDataObject.lib") #endif #endif #endif // 下列 ifdef 块是创建使从 DLL 导出更简单的 // 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 RESDATAOBJECT_EXPORTS // 符号编译的。在使用此 DLL 的 // 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将 // RESDATAOBJECT_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的 // 符号视为是被导出的。 #ifdef RESDATAOBJECT_EXPORTS #define RESDATAOBJECT_API __declspec(dllexport) #define RESDATAOBJECT_C_API extern "C" __declspec(dllexport) #else #define RESDATAOBJECT_API __declspec(dllimport) #define RESDATAOBJECT_C_API extern "C" __declspec(dllimport) #endif #include #include #include #include #include #include using namespace std; //help note //数据对象是JSON格式的树状结构 //对象有俩个成员,m_value 还有 vector> //它俩当中必须有一个是有效的,先检查vector,若空 m_value有效. //JSON定义中数组的定义不要用,因为无法转换到XML对象. //XML定义中不要用属性,因为无法转换到JSON对象. //JSON定义为主,XML支持为辅. //ini定义只有俩层. //不要在key定义中使用 【.】 ,因为Boost会把【.】看成是模块的切换点. //get 用法 //["key"],[idx],[xx].get("key"),[xx].get(); //add 用法:不解释 //update 用法:key,val,idx的形式,没有匹配值的情况用add,有值的情况update class RESDATAOBJECT_API ResDataObjectExption { std::string *m_ExpContext; public: ResDataObjectExption(const ResDataObjectExption &tValue); ResDataObjectExption(const char *pExp); ~ResDataObjectExption(void); ResDataObjectExption& operator = (const ResDataObjectExption &tValue); const char *what(); }; //缺省的值和字符串以外不要用 class RESDATAOBJECT_API EDATAOBJECT { std::string *m_value; public: EDATAOBJECT() { m_value = new string; }; ~EDATAOBJECT() { delete m_value; }; //set EDATAOBJECT& operator = (const char* pVal) { (*m_value) = (pVal); return (*this); }; EDATAOBJECT& operator = (const float tValue) { unsigned int temp,*pF ; std::stringstream strm; //strm.precision(numeric_limits::digits10); assert(sizeof(float) == sizeof(unsigned int)); pF = (unsigned int*)&tValue; temp = (*pF); strm << temp; (*this) = strm.str().c_str(); return (*this); }; EDATAOBJECT& operator = (const double tValue) { unsigned long long temp,*pF ; std::stringstream strm; //strm.precision(numeric_limits::digits10); assert(sizeof(double) == sizeof(unsigned long long)); pF = (unsigned long long*)&tValue; temp = (*pF); strm << temp; (*this) = strm.str().c_str(); return (*this); }; template EDATAOBJECT& operator = (const T tValue) { std::stringstream strm; strm << tValue; (*this) = strm.str().c_str(); return (*this); }; //get operator float() { unsigned int ret1 = 0; std::stringstream strm; strm << (*m_value); strm >> ret1; float *pF,ret; pF = (float*)&ret1; ret = (*pF); return ret; }; operator double() { unsigned long long ret1 = 0; std::stringstream strm; strm << (*m_value); strm >> ret1; double *pD,ret; pD = (double*)&ret1; ret = (*pD); return ret; }; operator const char*() { return m_value->c_str(); } template operator T() { T ret1 = 0; std::stringstream strm; strm << m_value->c_str(); strm >> ret1; return ret1; }; }; class RESDATAOBJECT_API ResDataObject_Pair { public: std::string *first; PVOID second; ResDataObject_Pair(); ~ResDataObject_Pair(); }; class RESDATAOBJECT_API ResDataObject { protected: std::string *m_encode;//serialize std::string *m_value; vector *m_vec; public: ResDataObject(void); ResDataObject(const ResDataObject &tValue); ~ResDataObject(void); bool DumpJsonFile(const char *pfileName); //open close bool loadFile(const char *pfileName);//支持xml,json,ini扩展名 bool SaveFile( const char *pfileName );//支持xml,json,ini扩展名 //serialize const char *encode(); bool decode(const char *pdata); //commons void clear(); size_t size(); bool IsObject(); void MakeKeyLower(); size_t GetKeyCount(const char *pKey); //erase bool eraseAllOf(const char *pKey); bool eraseOneOf(const char *pKey,size_t idx = 0); //set ResDataObject& operator = (const ResDataObject &tValue); //add ResDataObject& operator += (const ResDataObject &tValue); ResDataObject& operator = (const bool tValue); ResDataObject& operator = (const char tValue); ResDataObject& operator = (const unsigned char tValue); ResDataObject& operator = (const short tValue); ResDataObject& operator = (const unsigned short tValue); ResDataObject& operator = (const int tValue); ResDataObject& operator = (const unsigned int tValue); ResDataObject& operator = (const long tValue); ResDataObject& operator = (const unsigned long tValue); ResDataObject& operator = (const long long tValue); ResDataObject& operator = (const unsigned long long tValue); ResDataObject& operator = (const float tValue); ResDataObject& operator = (const double tValue); ResDataObject& operator = (const char* pVal); //get ResDataObject &operator [](size_t idx); ResDataObject &operator [](int idx); ResDataObject &operator [](const char *pKey); bool SetKey(size_t idx,const char *pKey); const char* GetKey(size_t idx); const char* GetKey(int idx); //get operator bool(); operator char(); operator unsigned char(); operator short(); operator unsigned short(); operator int(); operator unsigned int(); operator long(); operator unsigned long(); operator long long(); operator unsigned long long(); operator float(); operator double(); operator const char*(); //same name in the row int GetFirstOf(const char *pKey); int GetNextOf(const char *pKey,int PrevIdx); //add bool add(const char* pKey,bool tValue); bool add(const char* pKey,char tValue); bool add(const char* pKey,unsigned char tValue); bool add(const char* pKey,short tValue); bool add(const char* pKey,unsigned short tValue); bool add(const char* pKey,int tValue); bool add(const char* pKey,unsigned int tValue); bool add(const char* pKey,long tValue); bool add(const char* pKey,unsigned long tValue); bool add(const char* pKey,long long tValue); bool add(const char* pKey,unsigned long long tValue); bool add(const char* pKey,float tValue); bool add(const char* pKey,double tValue); bool add(const char* pKey,const char* pValue); bool add(const char* pKey,ResDataObject &dataobj); //bool add(const char* pKey,ResDataObject dataobj); bool update(const char* pKey,bool tValue); bool update(const char* pKey,char tValue); bool update(const char* pKey,unsigned char tValue); bool update(const char* pKey,short tValue); bool update(const char* pKey,unsigned short tValue); bool update(const char* pKey,int tValue); bool update(const char* pKey,unsigned int tValue); bool update(const char* pKey,long tValue); bool update(const char* pKey,unsigned long tValue); bool update(const char* pKey,long long tValue); bool update(const char* pKey,unsigned long long tValue); bool update(const char* pKey,float tValue); bool update(const char* pKey,double tValue); bool update(const char* pKey,const char* pValue); bool update(const char* pKey,ResDataObject &dataobj); //bool add(const char* pKey,ResDataObject dataobj); bool operator == (const char* pVal); bool operator == (const ResDataObject &Obj); }; RESDATAOBJECT_C_API bool TryGetValue(ResDataObject &obj, const char *pKey, ResDataObject &res); class RESDATAOBJECT_API ExJsonDataObject { string *m_pKey; string *m_ValString; ResDataObject * m_pTargetObject; public: ExJsonDataObject(); virtual ~ExJsonDataObject(); ExJsonDataObject(const ExJsonDataObject &tValue); //base void SetKey(const char *pKey); const char *GetKey(); //基于对象 virtual ResDataObject &GetResDataObject(); virtual bool SetResDataObject(ResDataObject &obj); //基于字符串 //virtual const char *GetVal(); //virtual bool SetVal(const char* pValString); //基于下标读取 ResDataObject &operator [](const char *pKey); ExJsonDataObject& operator = (const ExJsonDataObject &tValue); }; template class BaseJsonDataObject { protected: T m_Data; std::string m_KeyString; std::string m_ValString; public: BaseJsonDataObject() { }; //BaseJsonDataObject(BaseJsonDataObject &tValue) //{ // m_pKeyString = new string(); // m_pValString = new string(); // m_pData = new T; // (*m_pKeyString) = (*tValue.m_pKeyString); // (*m_pValString) = (*tValue.m_pValString); // (*m_pData) = (*tValue.m_pData); //}; ~BaseJsonDataObject() { }; virtual void GetResDataObject(ResDataObject &obj) { obj.add(GetKey(), GetVal()); }; virtual bool SetResDataObject(ResDataObject &obj) { return SetVal((const char *)obj); }; virtual const char *encode() { ResDataObject obj; if(obj.add(GetKey(),GetVal()) == false) { return NULL; } (m_ValString) = obj.encode(); return m_ValString.c_str(); }; virtual bool Decode(const char *pString) { bool ret = true; ResDataObject obj; try { if(obj.decode(pString) == false) { return false; } ret = SetVal(obj[m_KeyString.c_str()]); } catch(...) { ret = false; } return ret; }; virtual const char *GetKey() { return m_KeyString.c_str(); }; virtual const char *GetVal() { std::stringstream strm; strm << (m_Data); strm >> (m_ValString); return m_ValString.c_str(); }; virtual void SetKey(const char* pKeyString) { (m_KeyString) = pKeyString; }; virtual bool SetVal(const char* pValString) { #pragma warning( push ) #pragma warning( disable : 4800 4244 ) bool ret = true; try { std::stringstream strm; std::string typenameT = typeid(T).name(); transform(typenameT.begin(), typenameT.end(), typenameT.begin(), tolower); if(typenameT == "float") { assert(sizeof(float) == sizeof(unsigned int)); unsigned int ret1 = 0; strm << (pValString); strm >> ret1; float *pF = (float*)&ret1; (m_Data) = (*pF); } else if(typenameT == "double") { assert(sizeof(double) == sizeof(unsigned long long)); unsigned long long ret1 = 0; strm << (pValString); strm >> ret1; double *pF = (double*)&ret1; (m_Data) = (*pF); } else { strm << (pValString); strm >> (m_Data); } return true; } catch(...) { ret = false; } return ret; #pragma warning( pop ) }; virtual bool AddVal(const char* pValString) { return false; }; virtual bool DelVal(const char* pValString) { return false; }; //BaseJsonDataObject& operator = (BaseJsonDataObject &tValue) //{ // if (this != &tValue) // { // (*m_pKeyString) = (*tValue.m_pKeyString); // (*m_pValString) = (*tValue.m_pValString); // (*m_pData) = (*tValue.m_pData); // } // return (*this); //} BaseJsonDataObject& operator = (T Val) { (m_Data) = Val; return (*this); }; operator T() { return (m_Data); }; }; //Dios协议包中的数据区对象 //数据区用此对象进行操作, //数据区的数据保存在DiosPacket中,而ResRawObject是引用这个数据,并非拷贝!!! //关系为 DiosPacket 1->n ResRawObject,注意DiosPacket被删除的时候,不要继续使用ResRawObject!!! //线程安全,非进程安全,多线程抢占自己加锁!! //class RESDATAOBJECT_API ResRawObject //{ // size_t m_BuffLen; // size_t m_DataLen; // char *m_pszDataPosition; // //public: // ResRawObject(); // ResRawObject(char *pData, size_t BuffSize,size_t DataSize); // virtual ~ResRawObject(); // // //init op // void ClearData(); // // //Get operation // // //尽量用在读取,不要用在更新上,因为数据是一个缓存,前面更新的话,将有可能破坏后面数据区 // ResRawObject &operator [](DIOSKEYTYPE key); // operator bool(); // operator char(); // operator unsigned char(); // operator short(); // operator unsigned short(); // operator int(); // operator unsigned int(); // operator long(); // operator unsigned long(); // operator long long(); // operator unsigned long long(); // operator float(); // operator double(); // operator const char*(); // // //Set operation // // //固定长度资源的添加,12BIT的KEY是不变,3bit的内容由参数来决定 // bool add(DIOSKEYTYPE Key, bool tValue); // bool add(DIOSKEYTYPE Key, char tValue); // bool add(DIOSKEYTYPE Key, unsigned char tValue); // bool add(DIOSKEYTYPE Key, short tValue); // bool add(DIOSKEYTYPE Key, unsigned short tValue); // bool add(DIOSKEYTYPE Key, int tValue); // bool add(DIOSKEYTYPE Key, unsigned int tValue); // bool add(DIOSKEYTYPE Key, long tValue); // bool add(DIOSKEYTYPE Key, unsigned long tValue); // bool add(DIOSKEYTYPE Key, long long tValue); // bool add(DIOSKEYTYPE Key, unsigned long long tValue); // bool add(DIOSKEYTYPE Key, float tValue); // bool add(DIOSKEYTYPE Key, double tValue); // bool add(DIOSKEYTYPE Key, size_t KeySize, const char *pData, size_t datasize);//Key的上限