标签:
class IContainer { public: virtual RESULT Insert(const std::string& key, const ExportData& data) = 0; virtual RESULT Delete(const std::string& key) = 0; virtual RESULT Find(const std::string& key, ExportData& data) = 0; };
class ExportData { public: long GetData() { return m_lData; } void SetData(long data) { m_lData = data; } string GetData() { return m_strData; } void SetData(string data) { m_strData = data; } // ...... overload the other two types private: long m_lData; string m_strData; // ...... overload the other two types };
void GetData(long& data) { data = m_lData; } void SetData(long data) { m_lData = data; } void GetData(string& data) { data = m_strData; } void SetData(const string& data) { m_strData = data; }
template<typename T> class ExportData { public: T GetData() { return m_lData; } void SetData(T data) { m_data = data; } private: T m_data; };
template<typename T> class ExportData { public: RESULT GetData(T& data) { return ERROR; } RESULT SetData(const T& data) { return ERROR; } }; template<> class ExportData<long> { public: RESULT GetData(long& data) { data = m_data; return OK; } RESULT SetData(const long& data) { m_data = data; return OK; } private: long m_data; }; template<> class ExportData<double> ...... // just like the implementation of long template<> class ExportData<string> ...... // just like the implementation of long template<> class ExportData<Binary> ...... // just like the implementation of long
template<typename T> struct TypeTraits { static const DATA_TYPE field_type = TYPE_UNSUPPORTED; }; template<> struct TypeTraits<std::string> { static const DATA_TYPE field_type = TYPE_UTF8; }; template<> struct TypeTraits<long> { static const DATA_TYPE field_type = TYPE_INEGER; }; template<> struct TypeTraits<double> { static const DATA_TYPE field_type = TYPE_REAL; }; template<> struct TypeTraits<Binary> { static const DATA_TYPE field_type = TYPE_BINARY; };
TypeTraits<long>::field_type == TYPE_UNSUPPORTED
template<typename T> class ExportData { public: RESULT GetData(T& data) { return ERROR; } RESULT SetData(const T& data) { return ERROR; } }; template<> class ExportData<long> { public: RESULT GetData(long& data) { if (TypeTraits<long>::field_type == TYPE_UNSUPPORTED) { return ERROR; } data = m_data; return OK; } RESULT SetData(const long& data) { m_data = data; return OK; } private: long m_data; };
class IContainer { public: template<typename T> virtual RESULT Insert(const std::string& key, const ExportData<T>& data) = 0; virtual RESULT Delete(const std::string& key) = 0; template<typename T> virtual RESULT Find(const std::string& key, ExportData<T>& data) = 0; };
class ExportData { public: ExportData() : _data(NULL), _size(0){} ExportData(const ExportData& data) { _data = NULL; _size = 0; AssignData(data._data, data._size); _type = data._type; } ~ExportData() { if (_data) { delete[] _data; _data = NULL; } } ExportData& operator=(const ExportData& data) { this->AssignData(data._data, data._size); this->_type = data._type; return *this; template<typename T> RESULT SetData(const T& data) { if (TypeTraits<T>::field_type == TYPE_UNSUPPORTED) { return ERROR; } AssignData((const char*)&data, sizeof(T)); _type = TypeTraits<T>::field_type; return OK; } template<> RESULT SetData<std::string>(const std::string& data) { AssignData(data.c_str(), data.size()); _type = TYPE_UTF8; return OK; } template<> RESULT SetData<Binary>(const Binary& data) { AssignData(data.GetBlobAddr(), data.GetSize()); _type = TYPE_BLOB; return OK; } template<typename T> RESULT GetData(T& data) const { if (TypeTraits<T>::field_type == TYPE_UNSUPPORTED || _data == NULL || TypeTraits<T>::field_type != _type) { return ERROR; } memcpy(&data, _data, _size); return OK; } template<> RESULT GetData<std::string>(std::string& data) const { if (TYPE_UTF8 != _type || _data == NULL) { data = ""; return ERROR; } data.assign(_data, _size); return OK; } template<> RESULT GetData<Binary>(Binary& data) const { if (TYPE_BLOB != _type || _data == NULL) { data.SetBlobData(NULL, 0); return ERROR; } data.SetBlobData(_data, _size); return OK; } private: void AssignData(const char* data, unsigned int size) { if (_data) { delete[] _data; _data = NULL; } _size = size; _data = new char[size]; memcpy(_data, data, _size); } char* _data; unsigned long _size; DATA_TYPE _type; };
ExportData data; RESULT res = OK; res = data.SetData<string>("DataTest"); assert(OK == res); string str; res = data.GetData<string>(str); assert(OK == res); res = data.SetData<long>(111); long ldata = 0; res = data.GetData<long>(ldata); assert(OK == res);
【C++模版之旅】项目中一次活用C++模板(traits)的经历
标签:
原文地址:http://www.cnblogs.com/mengfanrong/p/4199130.html