C++ Any 任意基础类型封装

下面是本人使用C++封装的一个针对任意基础类型以及用户自定义类型指针的通用类型。目的是为方便常用类型使用统一化及便利化。该类型的使用就与平时使用基础类型基本没什么差别。具体可参看以下代码及测试代码。

 1 /******************************************************************************
 2 
 3                              I'm jacc.kim
 4 
 5     CreateDate: 2017-05-21 14:20:56 
 6     FileName  : JKMsgDefine.h
 7     Version   : 1.00
 8     Author    : jacc.kim
 9     Summary   : 消息数据定义
10 
11 ******************************************************************************/
12 #pragma once
13 
14 enum class eJKMsgDataValueType
15 {
16     eUndefined                  = 0,    // 未定义类型
17     eChar                       = 1,    // = char
18     eShort                      = 2,    // = short
19     eInt                        = 3,    // = int
20     eLong                       = 4,    // = long
21     eInt64                      = 5,    // = long long
22 
23     eUChar                      = 6,    // = unsigned char
24     eUShort                     = 7,    // = unsigned short
25     eUInt                       = 8,    // = unsigned int
26     eULong                      = 9,    // = unsigned long
27     eUInt64                     = 10,   // = unsigned long long
28 
29     eFloat                      = 11,   // = float
30     eDouble                     = 12,   // = double
31 
32     eString                     = 13,   // = string
33 
34     eCustom                     = 14,   // = 用户自定义类型
35 
36 };//enum class eJKMsgDataValueType
  1 /******************************************************************************
  2 
  3                              I'm jacc.kim
  4 
  5     CreateDate: 2017-05-22 09:30:36 
  6     FileName  : JKMsgData.h
  7     Version   : 1.00
  8     Author    : jacc.kim
  9     Summary   : 消息数据类型定义
 10 
 11 ******************************************************************************/
 12 #pragma once
 13 
 14 #include <string>
 15 #include <map>
 16 
 17 #include "JKMsgDefine.h"
 18 
 19 /******************************************************************************
 20  * create   : (jacc.kim) [05-21-2017]
 21  * summary  : class JKMsgDataValue.消息数据值类型
 22 ******************************************************************************/
 23 class JKMsgDataValue
 24 {
 25 public:
 26     JKMsgDataValue(const char v);
 27     JKMsgDataValue(const short v);
 28     JKMsgDataValue(const int v);
 29     JKMsgDataValue(const long v);
 30     JKMsgDataValue(const long long v);
 31 
 32     JKMsgDataValue(const unsigned char v);
 33     JKMsgDataValue(const unsigned short v);
 34     JKMsgDataValue(const unsigned int v);
 35     JKMsgDataValue(const unsigned long v);
 36     JKMsgDataValue(const unsigned long long v);
 37 
 38     JKMsgDataValue(const float v);
 39     JKMsgDataValue(const double v);
 40 
 41     //JKMsgDataValue(const char* v);
 42     JKMsgDataValue(const std::string& v);
 43 
 44     template<typename Tx>
 45     JKMsgDataValue(Tx* pCustomPtr);
 46 
 47     template<typename Tx>
 48     JKMsgDataValue(const Tx* pCustomPtr);
 49 
 50     template<>
 51     JKMsgDataValue(char* pCustomPtr);
 52 
 53     template<>
 54     JKMsgDataValue(const char* pCustomPtr);
 55 
 56     JKMsgDataValue(const JKMsgDataValue& v);
 57     JKMsgDataValue& operator=(const JKMsgDataValue& v);
 58 
 59     ~JKMsgDataValue();
 60 
 61 public:
 62     char                        valueChar() const;
 63     short                       valueShort() const;
 64     int                         valueInt() const;
 65     long                        valueLong() const;
 66     long long                   valueLongLong() const;
 67 
 68     unsigned char               valueUChar() const;
 69     unsigned short              valueUShort() const;
 70     unsigned int                valueUInt() const;
 71     unsigned long               valueULong() const;
 72     unsigned long long          valueULongLong() const;
 73 
 74     float                       valueFloat() const;
 75     double                      valueDouble() const;
 76     const std::string&          valueString() const;
 77 
 78     template<typename Tx>
 79     bool                        valueCustom(Tx*& pCustomPtr) const;
 80     template<typename Tx>
 81     Tx*                         valueCustom() const;
 82 
 83     template<typename Tx>
 84     bool                        valueCustomConst(const Tx*& pCustomPtr) const;
 85     template<typename Tx>
 86     const Tx*                   valueCustomConst() const;
 87 
 88     // 
 89     // !!!note     : 该版本不建议使用.功能上是可以强转为char*,但实际上,出于语言语义上的考虑,强行将一个const char*脱掉const特性
 90     //               ,在逻辑上是不合理的.因此,该版本的实现内部将直接返回空指针!!!
 91     template<>
 92     bool                        valueCustom(char*& pCustomPtr) const;
 93     template<>
 94     char*                       valueCustom() const;
 95 
 96     template<>
 97     bool                        valueCustomConst(const char*& pCustomPtr) const;
 98     template<>
 99     const char*                 valueCustomConst() const;
100 
101 public:
102     operator                    char() const;
103     operator                    short() const;
104     operator                    int() const;
105     operator                    long() const;
106     operator                    long long() const;
107     
108     operator                    unsigned char() const;
109     operator                    unsigned short() const;
110     operator                    unsigned int() const;
111     operator                    unsigned long() const;
112     operator                    unsigned long long() const;
113  
114     operator                    float() const;
115     operator                    double() const;
116     operator                    const std::string&() const;
117  
118     template<typename Tx>
119     operator                    Tx*() const;
120     template<typename Tx>
121     operator                    const Tx*() const;
122     
123     //template<>
124     //operator                    char*() const;
125     //template<>
126     //operator                    const char*() const;
127 
128 public:
129     // 转换为字符串
130     bool                        toString(std::string& strOut) const;
131     bool                        toString(std::string& strOut, const unsigned int nDigits) const;
132     std::string                 toString() const;
133     std::string                 toString(const unsigned int nDigits) const;
134     
135 private:
136     void                        resetData();
137 
138 private:
139     int                         m_eType;
140     union
141     {
142         char                    charData;
143         short                   shortData;
144         int                     intData;
145         long                    longData;
146         long long               longlongData;
147 
148         unsigned char           ucharData;
149         unsigned short          ushortData;
150         unsigned int            uintData;
151         unsigned long           ulongData;
152         unsigned long long      ulonglongData;
153 
154         float                   floatData;
155         double                  doubleData;
156 
157         std::string*            stringData;         // will be delete in destructor
158 
159         void*                   customData;         // only reference.
160         const void*             const_customData;   // only reference.
161     }                           m_Data;
162 
163 };//class JKMsgDataValue
  1 #include <sstream>
  2 
  3 #include "src/GameCore/MsgDispatcher/JKMsgData.h"
  4 #include "src/Common/GlobalDefine.h"
  5 
  6 ///////////////////////////////////////////////////////////////////////////////
  7 // class JKMsgDataValue
  8 JKMsgDataValue::JKMsgDataValue(const char v) {
  9     this->resetData();
 10     m_eType = static_cast<int>(eJKMsgDataValueType::eChar);
 11     m_Data.charData = v;
 12 }
 13 
 14 JKMsgDataValue::JKMsgDataValue(const short v) {
 15     this->resetData();
 16     m_eType = static_cast<int>(eJKMsgDataValueType::eShort);
 17     m_Data.shortData = v;
 18 }
 19 
 20 JKMsgDataValue::JKMsgDataValue(const int v) {
 21     this->resetData();
 22     m_eType = static_cast<int>(eJKMsgDataValueType::eInt);
 23     m_Data.intData = v;
 24 }
 25 
 26 JKMsgDataValue::JKMsgDataValue(const long v) {
 27     this->resetData();
 28     m_eType = static_cast<int>(eJKMsgDataValueType::eLong);
 29     m_Data.longData = v;
 30 }
 31 
 32 JKMsgDataValue::JKMsgDataValue(const long long v) {
 33     this->resetData();
 34     m_eType = static_cast<int>(eJKMsgDataValueType::eInt64);
 35     m_Data.longlongData = v;
 36 }
 37 
 38 JKMsgDataValue::JKMsgDataValue(const unsigned char v) {
 39     this->resetData();
 40     m_eType = static_cast<int>(eJKMsgDataValueType::eUChar);
 41     m_Data.ucharData = v;
 42 }
 43 
 44 JKMsgDataValue::JKMsgDataValue(const unsigned short v) {
 45     this->resetData();
 46     m_eType = static_cast<int>(eJKMsgDataValueType::eUShort);
 47     m_Data.ushortData = v;
 48 }
 49 
 50 JKMsgDataValue::JKMsgDataValue(const unsigned int v) {
 51     this->resetData();
 52     m_eType = static_cast<int>(eJKMsgDataValueType::eUInt);
 53     m_Data.uintData = v;
 54 }
 55 
 56 JKMsgDataValue::JKMsgDataValue(const unsigned long v) {
 57     this->resetData();
 58     m_eType = static_cast<int>(eJKMsgDataValueType::eULong);
 59     m_Data.ulongData = v;
 60 }
 61 
 62 JKMsgDataValue::JKMsgDataValue(const unsigned long long v) {
 63     this->resetData();
 64     m_eType = static_cast<int>(eJKMsgDataValueType::eUInt64);
 65     m_Data.ulonglongData = v;
 66 }
 67 
 68 JKMsgDataValue::JKMsgDataValue(const float v) {
 69     this->resetData();
 70     m_eType = static_cast<int>(eJKMsgDataValueType::eFloat);
 71     m_Data.floatData = v;
 72 }
 73 
 74 JKMsgDataValue::JKMsgDataValue(const double v) {
 75     this->resetData();
 76     m_eType = static_cast<int>(eJKMsgDataValueType::eDouble);
 77     m_Data.doubleData = v;
 78 }
 79 
 80 //JKMsgDataValue::JKMsgDataValue(const char* v) {
 81 //    this->resetData();
 82 //    if (nullptr == v) {
 83 //        m_eType = static_cast<int>(eJKMsgDataValueType::eUndefined);
 84 //        return;
 85 //    }
 86 //    m_eType = static_cast<int>(eJKMsgDataValueType::eString);
 87 //    m_Data.stringData = new std::string(v);
 88 //}
 89 
 90 JKMsgDataValue::JKMsgDataValue(const std::string& v) {
 91     this->resetData();
 92     m_eType = static_cast<int>(eJKMsgDataValueType::eString);
 93     m_Data.stringData = new std::string(v);
 94 }
 95 
 96 JKMsgDataValue::JKMsgDataValue(const JKMsgDataValue& v) {
 97     *this = v;
 98 }
 99 
100 JKMsgDataValue& JKMsgDataValue::operator=(const JKMsgDataValue& v) {
101     if (&v != this) {
102         m_eType = v.m_eType;
103         m_Data  = v.m_Data;
104         if (static_cast<eJKMsgDataValueType>(m_eType) == eJKMsgDataValueType::eString) {
105             m_Data.stringData = new std::string(*v.m_Data.stringData);
106         }
107     }
108     return *this;
109 }
110 
111 JKMsgDataValue::~JKMsgDataValue() {
112     this->resetData();
113 }
114 
115 void JKMsgDataValue::resetData() {
116     if (static_cast<int>(eJKMsgDataValueType::eString) == m_eType) {
117         delete m_Data.stringData;
118         m_Data.stringData = nullptr;
119     }
120 }
121 
122 char JKMsgDataValue::valueChar() const {
123     return m_Data.charData;
124 }
125 
126 short JKMsgDataValue::valueShort() const {
127     return m_Data.shortData;
128 }
129 
130 int JKMsgDataValue::valueInt() const {
131     return m_Data.intData;
132 }
133 
134 long JKMsgDataValue::valueLong() const {
135     return m_Data.longData;
136 }
137 
138 long long JKMsgDataValue::valueLongLong() const {
139     return m_Data.longlongData;
140 }
141 
142 unsigned char JKMsgDataValue::valueUChar() const {
143     return m_Data.ucharData;
144 }
145 
146 unsigned short JKMsgDataValue::valueUShort() const {
147     return m_Data.ushortData;
148 }
149 
150 unsigned int JKMsgDataValue::valueUInt() const {
151     return m_Data.uintData;
152 }
153 
154 unsigned long JKMsgDataValue::valueULong() const {
155     return m_Data.ulongData;
156 }
157 
158 unsigned long long JKMsgDataValue::valueULongLong() const {
159     return m_Data.ulonglongData;
160 }
161 
162 float JKMsgDataValue::valueFloat() const {
163     return m_Data.floatData;
164 }
165 
166 double JKMsgDataValue::valueDouble() const {
167     return m_Data.doubleData;
168 }
169 
170 const std::string& JKMsgDataValue::valueString() const {
171     return *m_Data.stringData;
172 }
173 
174 JKMsgDataValue::operator char() const {
175     return this->valueChar();
176 }
177 
178 JKMsgDataValue::operator short() const {
179     return this->valueShort();
180 }
181 
182 JKMsgDataValue::operator int() const {
183     return this->valueInt();
184 }
185 
186 JKMsgDataValue::operator long()const {
187     return this->valueLongLong();
188 }
189 
190 JKMsgDataValue::operator long long() const {
191     return this->valueLongLong();
192 }
193 
194 JKMsgDataValue::operator unsigned char() const {
195     return this->valueUChar();
196 }
197 
198 JKMsgDataValue::operator unsigned short() const {
199     return this->valueUShort();
200 }
201 
202 JKMsgDataValue::operator unsigned int() const {
203     return this->valueUInt();
204 }
205 
206 JKMsgDataValue::operator unsigned long()const {
207     return this->valueULong();
208 }
209 
210 JKMsgDataValue::operator unsigned long long() const {
211     return this->valueULongLong();
212 }
213 
214 JKMsgDataValue::operator float() const {
215     return this->valueFloat();
216 }
217 
218 JKMsgDataValue::operator double() const {
219     return this->valueDouble();
220 }
221 
222 JKMsgDataValue::operator const std::string&() const {
223     return valueString();
224 }
225 
226 bool JKMsgDataValue::toString(std::string& strOut) const {
227     return this->toString(strOut, 0);
228 }
229 
230 bool JKMsgDataValue::toString(std::string& strOut, const unsigned int nDigits) const {
231     auto bIsSuccess = true;
232 
233     auto doubleToString = [](const double v, const int nDigits, std::stringstream& strOutOS) {
234                               const auto nFormatSize = 8;
235                               char szFormatBuff[nFormatSize];
236                               memset((void*)szFormatBuff, 0x0L, nFormatSize);
237                               sprintf(szFormatBuff, "%%.%df", nDigits);
238                           
239                               const auto nSizeDouble = sizeof(double);
240                               char szBuff[nSizeDouble];
241                               memset((void*)szBuff, 0x0L, nSizeDouble);
242                               sprintf(szBuff, szFormatBuff, v);
243 
244                               strOutOS << szBuff;
245                           };
246 
247     std::stringstream os;
248     switch (static_cast<eJKMsgDataValueType>(m_eType)) {
249     case eJKMsgDataValueType::eChar   : os << m_Data.charData;                          break; // = char
250     case eJKMsgDataValueType::eShort  : os << m_Data.shortData;                         break; // = short
251     case eJKMsgDataValueType::eInt    : os << m_Data.intData;                           break; // = int
252     case eJKMsgDataValueType::eLong   : os << m_Data.longData;                          break; // = long
253     case eJKMsgDataValueType::eInt64  : os << m_Data.longlongData;                      break; // = long long
254 
255     case eJKMsgDataValueType::eUChar  : os << m_Data.ucharData;                         break; // = unsigned char
256     case eJKMsgDataValueType::eUShort : os << m_Data.ushortData;                        break; // = unsigned short
257     case eJKMsgDataValueType::eUInt   : os << m_Data.uintData;                          break; // = unsigned int
258     case eJKMsgDataValueType::eULong  : os << m_Data.ulongData;                         break; // = unsigned long
259     case eJKMsgDataValueType::eUInt64 : os << m_Data.ulonglongData;                     break; // = unsigned long long
260 
261     case eJKMsgDataValueType::eFloat  : doubleToString(m_Data.floatData, nDigits, os);  break; // = float
262     case eJKMsgDataValueType::eDouble : doubleToString(m_Data.doubleData, nDigits, os); break; // = double
263 
264     case eJKMsgDataValueType::eString : os << m_Data.stringData->c_str();               break; // = string
265 
266     default                           : DDZAssert(false, "Convert error!"); bIsSuccess = false;                   break;
267     }
268 
269     if (bIsSuccess) {
270         os >> strOut;
271     }
272 
273     return bIsSuccess;
274 }
275 
276 std::string JKMsgDataValue::toString() const {
277     std::string strOut;
278     if (!this->toString(strOut)) {
279         strOut = "";
280     }
281     return strOut;
282 }
283 
284 std::string JKMsgDataValue::toString(const unsigned int nDigits) const {
285     std::string strOut;
286     if (!this->toString(strOut, nDigits)) {
287         strOut = "";
288     }
289     return strOut;
290 }
  1 #include "JKMsgDefine.h"
  2 
  3 ///////////////////////////////////////////////////////////////////////////////
  4 // class JKMsgDataValue
  5 template<typename Tx>
  6 JKMsgDataValue::JKMsgDataValue(Tx* pCustomPtr) {
  7     this->resetData();
  8     m_eType                 = static_cast<int>(eJKMsgDataValueType::eUndefined);
  9     if (nullptr != pCustomPtr) {
 10         m_eType             = static_cast<int>(eJKMsgDataValueType::eCustom);
 11         m_Data.customData   = static_cast<void*>(pCustomPtr);
 12     }
 13 }
 14 
 15 template<typename Tx>
 16 JKMsgDataValue::JKMsgDataValue(const Tx* pCustomPtr) {
 17     this->resetData();
 18     m_eType                 = static_cast<int>(eJKMsgDataValueType::eUndefined);
 19     if (nullptr != pCustomPtr) {
 20         m_eType             = static_cast<int>(eJKMsgDataValueType::eCustom);
 21         m_Data.const_customData = static_cast<const void*>(pCustomPtr);
 22     }
 23 }
 24 
 25 template<>
 26 JKMsgDataValue::JKMsgDataValue(char* pCustomPtr) {
 27     this->resetData();
 28     if (nullptr == pCustomPtr) {
 29         m_eType             = static_cast<int>(eJKMsgDataValueType::eUndefined);
 30         return;
 31     }
 32     m_eType                 = static_cast<int>(eJKMsgDataValueType::eString);
 33     m_Data.stringData       = new std::string(pCustomPtr);
 34 }
 35 
 36 template<>
 37 JKMsgDataValue::JKMsgDataValue(const char* pCustomPtr) {
 38     this->resetData();
 39     if (nullptr == pCustomPtr) {
 40         m_eType             = static_cast<int>(eJKMsgDataValueType::eUndefined);
 41         return;
 42     }
 43     m_eType                 = static_cast<int>(eJKMsgDataValueType::eString);
 44     m_Data.stringData       = new std::string(pCustomPtr);
 45 }
 46 
 47 template<typename Tx>
 48 bool JKMsgDataValue::valueCustom(Tx*& pCustomPtr) const {
 49     auto bIsSuccess         = false;
 50     pCustomPtr              = nullptr;
 51     if (static_cast<eJKMsgDataValueType>(m_eType) == eJKMsgDataValueType::eCustom) {
 52         pCustomPtr          = static_cast<Tx*>(m_Data.customData);
 53         bIsSuccess          = true;
 54     }
 55     return bIsSuccess;
 56 }
 57 
 58 template<typename Tx>
 59 typename Tx* JKMsgDataValue::valueCustom() const {
 60     Tx* pCustomPtr          = nullptr;
 61     if (!this->valueCustom(pCustomPtr)) {
 62         pCustomPtr          = nullptr;
 63     }
 64     return pCustomPtr;
 65 }
 66 
 67 template<typename Tx>
 68 bool JKMsgDataValue::valueCustomConst(const Tx*& pCustomPtr) const {
 69     auto bIsSuccess         = false;
 70     pCustomPtr              = nullptr;
 71     if (static_cast<eJKMsgDataValueType>(m_eType) == eJKMsgDataValueType::eCustom) {
 72         pCustomPtr          = static_cast<const Tx*>(m_Data.const_customData);
 73         bIsSuccess          = true;
 74     }
 75     return bIsSuccess;
 76 }
 77 template<typename Tx>
 78 typename const Tx* JKMsgDataValue::valueCustomConst() const {
 79     const Tx* pCustomPtr    = nullptr;
 80     if (!this->valueCustomConst(pCustomPtr)) {
 81         pCustomPtr = nullptr;
 82     }
 83     return pCustomPtr;
 84 }
 85 
 86 template<>
 87 bool JKMsgDataValue::valueCustom(char*& pCustomPtr) const {
 88     auto bIsSuccess         = false;
 89     pCustomPtr              = nullptr;
 90     //if (static_cast<eJKMsgDataValueType>(m_eType) == eJKMsgDataValueType::eString) {
 91     //    const char* pConstCharPtr = m_Data.stringData->c_str();
 92     //    pCustomPtr          = const_cast<char*>(pConstCharPtr);
 93     //    bIsSuccess          = true;
 94     //}
 95     return bIsSuccess;
 96 }
 97 
 98 template<>
 99 char* JKMsgDataValue::valueCustom() const {
100     char* pCustomPtr        = nullptr;
101     //if (!this->valueCustom(pCustomPtr)) {
102     //    pCustomPtr          = nullptr;
103     //}
104     return pCustomPtr;
105 }
106 
107 template<>
108 bool JKMsgDataValue::valueCustomConst(const char*& pCustomPtr) const {
109     auto bIsSuccess         = false;
110     pCustomPtr              = nullptr;
111     if (static_cast<eJKMsgDataValueType>(m_eType) == eJKMsgDataValueType::eString) {
112         pCustomPtr          = m_Data.stringData->c_str();
113         bIsSuccess          = true;
114     }
115     return bIsSuccess;
116 }
117 
118 template<>
119 const char* JKMsgDataValue::valueCustomConst() const {
120     const char* pCustomPtr  = nullptr;
121     if (!this->valueCustomConst(pCustomPtr)) {
122         pCustomPtr          = nullptr;
123     }
124     return pCustomPtr;
125 }
126 
127 template<typename Tx>
128 JKMsgDataValue::operator Tx*() const {
129     return this->valueCustom<Tx>();
130 }
131 
132 template<typename Tx>
133 JKMsgDataValue::operator const Tx*() const {
134     return this->valueCustomConst<Tx>();
135 }
136 
137 //template<>
138 //JKMsgDataValue::operator char*() const {
139 //    auto pConstCharPtr = static_cast<const char*>(*this);
140 //    return const_cast<char*>(pConstCharPtr);
141 //}
142 //
143 //template<>
144 //JKMsgDataValue::operator const char*() const {
145 //    return this->valueCustomConst<char>();
146 //}

以下是测试代码。目前在 VS2013 中测试 Ok。

 1     std::list<JKMsgDataValue> listTest;
 2 
 3     JKMsgDataValue vChar                    = 'c';                                  // test ok.
 4     listTest.emplace_back(vChar);                                                   // test ok.
 5     JKMsgDataValue vShort                   = static_cast<short>(2);                // test ok.
 6     listTest.emplace_back(vShort);                                                  // test ok.
 7     JKMsgDataValue vInt                     = static_cast<int>(3);                  // test ok.
 8     listTest.emplace_back(vInt);                                                    // test ok.
 9     JKMsgDataValue vLong                    = static_cast<long>(4);                 // test ok.
10     listTest.emplace_back(vLong);                                                   // test ok.
11     JKMsgDataValue vLongLong                = static_cast<long long>(5);            // test ok.
12     listTest.emplace_back(vLongLong);                                               // test ok.
13 
14     JKMsgDataValue vUChar                   = static_cast<unsigned char>('b');      // test ok.
15     listTest.emplace_back(vUChar);                                                  // test ok.
16     JKMsgDataValue vUShort                  = static_cast<unsigned short>(6);       // test ok.
17     listTest.emplace_back(vUShort);                                                 // test ok.
18     JKMsgDataValue vUInt                    = static_cast<unsigned int>(7);         // test ok.
19     listTest.emplace_back(vUInt);                                                   // test ok.
20     JKMsgDataValue vULong                   = static_cast<unsigned long>(8);        // test ok.
21     listTest.emplace_back(vULong);                                                  // test ok.
22     JKMsgDataValue vULongLong               = static_cast<unsigned long long>(9);   // test ok.
23     listTest.emplace_back(vULongLong);
24 
25     JKMsgDataValue fFloat                   = 10.1f;                                // test ok.
26     listTest.emplace_back(fFloat);                                                  // test ok.
27     JKMsgDataValue vDouble                  = 11.2;                                 // test ok.
28     listTest.emplace_back(vDouble);                                                 // test ok.
29 
30     const double ddddddd1                   = vDouble;                              // test ok.
31     const double ddddddd2                   = (double)vDouble;                      // test ok.
32     const double ddddddd3                   = double(vDouble);                      // test ok.
33     const double ddddddd4                   = (double)(vDouble);                    // test ok.
34 
35     JKMsgDataValue vString_1                = "this is a string char*";             // test ok.
36     listTest.emplace_back(vString_1);                                               // test ok.
37     JKMsgDataValue vString_2                = std::string("abc");                   // test ok.
38     listTest.emplace_back(vString_2);                                               // test ok.
39 
40     const std::string strTTTTTTTT1          = vString_2;                            // test ok.
41     const auto strTTTTTTTT2                 = std::string(vString_2);               // test ok.
42     const auto strTTTTTTTT3                 = (std::string)vString_2;               // test ok.
43     const auto strTTTTTTTT4                 = (std::string)(vString_2);             // test ok.
44 
45     JKMsgDataValue vv                       = 'm';                                  // test ok.
46     listTest.emplace_back(vv);                                                      // test ok.
47     vv                                      = 7;                                    // test ok.
48     listTest.emplace_back(vv);                                                      // test ok.
49     vv                                      = "jacc.kim";                           // test ok.
50     listTest.emplace_back(vv);                                                      // test ok.
51     vv                                      = 666.66;                               // test ok.
52     listTest.emplace_back(vv);                                                      // test ok.
53 
54     std::for_each(listTest.begin(),
55                   listTest.end(),
56                   [](const JKMsgDataValue& v){
57                       std::string strOut;
58                       const bool bIsSuccess = v.toString(strOut, 2);                // test ok.
59                       CCLOG("-------- %s --------", strOut.c_str());                // test ok.
60                       if (!bIsSuccess) {
61                           int asdfasdf      = 0;
62                       }                      
63                   });
64 
65     JKMsgDataValue vv2_0                    = this;                                 // test ok.
66     JKMsgDataValue vv2(this);                                                       // test ok.
67     HelloWorld* pThisObj                    = nullptr;                              // test ok.
68     if (vv2.valueCustom(pThisObj)) {                                                // test ok.
69         int asfdasf                         = 0;                                    // test ok.
70     }                                                                               // test ok.
71     
72     HelloWorld* pMyPtr                      = vv2;                                  // test ok.
73 
74     const HelloWorld* pTestHelloWorld       = this;                                 // test ok.
75     JKMsgDataValue vv4                      = pTestHelloWorld;                      // test ok.
76     auto pVV4                               = static_cast<const HelloWorld*>(vv4);  // test ok.
77 
78     char* pCharTest                         = "ak47";                               // test ok.
79     JKMsgDataValue vv5                      = pCharTest;                            // test ok.
80     auto pVV5                               = static_cast<char*>(vv5);              // test ok.
81     auto pVV5_2                             = static_cast<const char*>(vv5);        // test ok.
82     
83     char* pVV5_3                            = nullptr;
84     vv5.valueCustom(pVV5_3);                                                        // test ok.
85 
86     const char* pVV5_4                      = nullptr;
87     vv5.valueCustomConst(pVV5_4);                                                   // test ok.
88 
89     const char* pCharTestConst              = "ak47";
90     JKMsgDataValue vv6                      = pCharTestConst;                       // test ok.
91     auto pVV6                               = static_cast<const char*>(vv6);        // test ok.

        以上如果有什么问题,欢迎大家指正、交流、共同探讨!

posted @ 2017-05-23 09:54  Jacc.Kim  阅读(1265)  评论(0编辑  收藏  举报