C++ 存储多种类型的结构

来源:互联网 发布:123软件是什么 编辑:程序博客网 时间:2024/05/16 07:40

自己写的代码 有点粗糙,有时间,好好整整。

template<class T>class JBuffer{};template<>class JBuffer<char>{public:JBuffer():size(0){}JBuffer(size_t size){Resize(size);}template<class T>JBuffer(const JBuffer<T>& j){size = j.size;buffer.resize(size);memcpy(buffer,j,size);}void Resize(size_t size){this->size = size;buffer.resize(size,0);}        void Add(const char* data,size_t size){memcpy(&buffer[0],data,size);}void Add(const char* data,size_t datasize,int offset){if( (offset + datasize) > size){throw exception("buffer 溢出");}memcpy(&buffer[0]+ offset,data,datasize);}char At(size_t pos){return buffer[pos];}void Set(size_t pos,char value){if(pos > buffer.size()) throw exception("buffer 溢出!");buffer[pos] = value;}operator char*(){return & buffer[0];}size_t Size(){return buffer.size();}private:size_t size;vector<char> buffer;};enum ValueType{UNSET_VALUE,CHAR_VALUE,SHORT_VALUE,INT_VALUE,LONG_VALUE,FLOAT_VALUE,DOUBLE_VALUE,ASTR_VALUE,WSTR_VALUE,BINARY_VALUE};class JValue{public:JValue():type(UNSET_VALUE){_long = 0;}JValue(const JValue& v){type = v.type;_long= v._long;}void Set(u8& data){_check();_char = data; type = CHAR_VALUE;}void Set(s8& data){u8 d = data;Set(d);}void Set(u16& data){_check();_short = data;type = SHORT_VALUE;}void Set(s16& data){u16 d = data;Set(d);}void Set(u32& data){_check();_int = data;type = INT_VALUE;}void Set(s32& data){u32 d = data;Set(d);}void Set(u64& data){_check();_long = data;type = LONG_VALUE;}void Set(s64& data){u64 d = data;Set(d);}void Set(float& data){_check();_float = data; type = FLOAT_VALUE;}void Set(double& data){_check();_double = data;type = DOUBLE_VALUE;}void Set(string str){_check(); astr = new string;*astr = str;type = ASTR_VALUE;}void Set(wstring str){_check(); wstr = new wstring(); *wstr = str; type = WSTR_VALUE;}void Set(JBuffer<char> b){_check();binary = new JBuffer<char>(b) ;type = BINARY_VALUE;}template<class T>T Value(){switch (type){case JByteHandle::CHAR_VALUE:return  *(T*)&_char;break;case JByteHandle::SHORT_VALUE:return  *(T*)&_short;break;case JByteHandle::INT_VALUE:return  *(T*)&_int;break;case JByteHandle::LONG_VALUE:return  *(T*)&_long;break;case JByteHandle::FLOAT_VALUE:return  *(T*)&_float;break;case JByteHandle::DOUBLE_VALUE:return  *(T*)&_double;break;case JByteHandle::ASTR_VALUE:return  *(T*)astr;break;case JByteHandle::WSTR_VALUE:return  *(T*)wstr;break;case JByteHandle::BINARY_VALUE:return  *(T*)binary;break;default:throw exception("类型未初始化!");break;}}private:void _check(){switch (type){case JByteHandle::UNSET_VALUE:break;default:_destroy();break;}}void _destroy(){switch (type){case JByteHandle::ASTR_VALUE:delete astr;break;case JByteHandle::WSTR_VALUE:delete wstr;break;case JByteHandle::BINARY_VALUE:delete binary;break;default:break;}}private:union{u8 _char;u16 _short;u32 _int;u64 _long;float _float;double _double;string* astr;wstring* wstr;JBuffer<char>* binary;};public:ValueType type;};

这个类能过存储多种类型,int long double 二进制等一些常见的数据。

自己用的,写个可以不够合理,希望能够指点下。

原创粉丝点击