123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644 |
- #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 <utility>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <sstream>
- #include <assert.h>
- using namespace std;
- //help note
- //数据对象是JSON格式的树状结构
- //对象有俩个成员,m_value 还有 vector<pair<string,数据对象>>
- //它俩当中必须有一个是有效的,先检查vector,若空 m_value有效.
- //JSON定义中数组的定义不要用,因为无法转换到XML对象.
- //XML定义中不要用属性,因为无法转换到JSON对象.
- //JSON定义为主,XML支持为辅.
- //ini定义只有俩层.
- //不要在key定义中使用 【.】 ,因为Boost会把【.】看成是模块的切换点.
- //get 用法
- //["key"],[idx],[xx].get<double>("key"),[xx].get<double>();
- //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<float>::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<double>::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<typename T> 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<typename T> 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<ResDataObject_Pair*> *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<typename T> 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的上限<size的上限,失败返回
- //
- // //可变长度资源的添加,12BIT的KEY+高位1是不变,3bit的内容由LenSize来决定
- // bool addFlexible(DIOSKEYTYPE Key, size_t KeySize, const char* pData, size_t datasize);//Flexible key
- //
- //
- //
- //};
|