#pragma once #ifdef RESDATAOBJECT_EXPORTS #define RESDATAOBJECT_API #define RESDATAOBJECT_C_API extern "C" #else #define RESDATAOBJECT_API #define RESDATAOBJECT_C_API extern "C" #endif #include #include #include #include #include #include #include // 对于 std::tolower #include using namespace std; using DWORD = unsigned long; // Linux 兼容的 DWORD 类型 //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; class RESDATAOBJECT_API ResDataObjectExption { std::unique_ptr m_ExpContext; public: ResDataObjectExption(const ResDataObjectExption &tValue); ResDataObjectExption(const char *pExp); ~ResDataObjectExption(void); ResDataObjectExption& operator = (const ResDataObjectExption &tValue); const char* what() const; }; //缺省的值和字符串以外不要用 class RESDATAOBJECT_API EDATAOBJECT { std::unique_ptr m_value; public: EDATAOBJECT() : m_value(std::make_unique()) {} ~EDATAOBJECT() = default; //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() { try { return std::stof(*m_value); } catch (...) { return 0.0f; } }; operator double() { try { return std::stod(*m_value); } catch (...) { return 0.0; } }; 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::unique_ptr first; std::unique_ptr second; ResDataObject_Pair(); ~ResDataObject_Pair() = default; ResDataObject_Pair(const ResDataObject_Pair& other); // 添加移动构造函数 ResDataObject_Pair(ResDataObject_Pair&& other) noexcept : first(std::move(other.first)) , second(std::move(other.second)) { // 移动后重置源对象,确保其成员非空 if (!other.first) { other.first = std::make_unique(); } if (!other.second) { other.second = std::make_unique(); } } ResDataObject_Pair& operator=(const ResDataObject_Pair& other); ResDataObject_Pair& operator=(ResDataObject_Pair&& other) noexcept; }; class RESDATAOBJECT_API ResDataObject { protected: std::unique_ptr m_encode; std::unique_ptr m_value; std::unique_ptr> m_vec; //std::string *m_encode;//serialize //std::string *m_value; //vector *m_vec; public: ResDataObject(void); ResDataObject(const ResDataObject &tValue); ResDataObject(ResDataObject&& tValue) noexcept; ~ResDataObject(void); ResDataObject& operator=(ResDataObject&& tValue) noexcept; 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 { std::unique_ptr m_pKey; std::unique_ptr m_ValString; std::unique_ptr m_pTargetObject; public: ExJsonDataObject(); virtual ~ExJsonDataObject() = default; 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() = default; virtual ~BaseJsonDataObject() = default; 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) { try { if constexpr (std::is_same_v) { m_Data = std::stof(pValString); } else if constexpr (std::is_same_v) { m_Data = std::stod(pValString); } else if constexpr (std::is_integral_v) { std::stringstream strm(pValString); strm >> m_Data; } else { std::stringstream strm(pValString); strm >> m_Data; } return true; } catch (...) { return false; } }; 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); }; }; //Ccos协议包中的数据区对象 //数据区用此对象进行操作, //数据区的数据保存在CcosPacket中,而ResRawObject是引用这个数据,并非拷贝!!! //关系为 CcosPacket 1->n ResRawObject,注意CcosPacket被删除的时候,不要继续使用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 [](CCOSKEYTYPE 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(CCOSKEYTYPE Key, bool tValue); // bool add(CCOSKEYTYPE Key, char tValue); // bool add(CCOSKEYTYPE Key, unsigned char tValue); // bool add(CCOSKEYTYPE Key, short tValue); // bool add(CCOSKEYTYPE Key, unsigned short tValue); // bool add(CCOSKEYTYPE Key, int tValue); // bool add(CCOSKEYTYPE Key, unsigned int tValue); // bool add(CCOSKEYTYPE Key, long tValue); // bool add(CCOSKEYTYPE Key, unsigned long tValue); // bool add(CCOSKEYTYPE Key, long long tValue); // bool add(CCOSKEYTYPE Key, unsigned long long tValue); // bool add(CCOSKEYTYPE Key, float tValue); // bool add(CCOSKEYTYPE Key, double tValue); // bool add(CCOSKEYTYPE Key, size_t KeySize, const char *pData, size_t datasize);//Key的上限