123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578 |
- #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 <utility>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <sstream>
- #include <assert.h>
- #include <cctype> // 对于 std::tolower
- #include <memory>
- using namespace std;
- using DWORD = unsigned long; // Linux 兼容的 DWORD 类型
- //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;
- class RESDATAOBJECT_API ResDataObjectExption
- {
- std::unique_ptr<std::string> 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<std::string> m_value;
- public:
- EDATAOBJECT() : m_value(std::make_unique<std::string>()) {}
- ~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<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()
- {
- 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<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::unique_ptr<std::string> first;
- std::unique_ptr<ResDataObject> 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<std::string>();
- }
- if (!other.second) {
- other.second = std::make_unique<ResDataObject>();
- }
- }
- ResDataObject_Pair& operator=(const ResDataObject_Pair& other);
- ResDataObject_Pair& operator=(ResDataObject_Pair&& other) noexcept;
-
- };
- class RESDATAOBJECT_API ResDataObject
- {
- protected:
- std::unique_ptr<std::string> m_encode;
- std::unique_ptr<std::string> m_value;
- std::unique_ptr<std::vector<ResDataObject_Pair>> m_vec;
- //std::string *m_encode;//serialize
- //std::string *m_value;
- //vector<ResDataObject_Pair*> *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<std::string> m_pKey;
- std::unique_ptr<std::string> m_ValString;
- std::unique_ptr<ResDataObject> 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<typename T> 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<T, float>) {
- m_Data = std::stof(pValString);
- }
- else if constexpr (std::is_same_v<T, double>) {
- m_Data = std::stod(pValString);
- }
- else if constexpr (std::is_integral_v<T>) {
- 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的上限<size的上限,失败返回
- //
- // //可变长度资源的添加,12BIT的KEY+高位1是不变,3bit的内容由LenSize来决定
- // bool addFlexible(CCOSKEYTYPE Key, size_t KeySize, const char* pData, size_t datasize);//Flexible key
- //
- //
- //
- //};
|