TarsXml.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. #ifndef __TARS_XML_H__
  2. #define __TARS_XML_H__
  3. #include <netinet/in.h>
  4. #include <iostream>
  5. #include <cassert>
  6. #include <vector>
  7. #include <map>
  8. #include <string>
  9. #include <stdexcept>
  10. #include <stdint.h>
  11. #include <string.h>
  12. #include <limits.h>
  13. #include "util/tc_xml.h"
  14. #include "util/tc_common.h"
  15. namespace tars
  16. {
  17. class XmlInput
  18. {
  19. public:
  20. static void readXml(Bool& b, const XmlValuePtr & p, bool isRequire = true)
  21. {
  22. if(NULL != p.get() && p->getType() == eXmlTypeString)
  23. {
  24. b = TC_Common::lower(XmlValueStringPtr::dynamicCast(p)->value) == "true";
  25. }
  26. else if (isRequire)
  27. {
  28. char s[128];
  29. snprintf(s, sizeof(s), "read 'Bool' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  30. throw TC_Xml_Exception(s);
  31. }
  32. }
  33. static void readXml(Char& c, const XmlValuePtr & p, bool isRequire = true)
  34. {
  35. if(NULL != p.get() && p->getType() == eXmlTypeString)
  36. {
  37. c = TC_Common::strto<Char>(XmlValueStringPtr::dynamicCast(p)->value);
  38. }
  39. else if (isRequire)
  40. {
  41. char s[128];
  42. snprintf(s, sizeof(s), "read 'Char' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  43. throw TC_Xml_Exception(s);
  44. }
  45. }
  46. static void readXml(UInt8& n, const XmlValuePtr & p, bool isRequire = true)
  47. {
  48. if(NULL != p.get() && p->getType() == eXmlTypeString)
  49. {
  50. n = TC_Common::strto<UInt8>(XmlValueStringPtr::dynamicCast(p)->value);
  51. }
  52. else if (isRequire)
  53. {
  54. char s[128];
  55. snprintf(s, sizeof(s), "read 'Uint8' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  56. throw TC_Xml_Exception(s);
  57. }
  58. }
  59. static void readXml(Short& n, const XmlValuePtr & p, bool isRequire = true)
  60. {
  61. if(NULL != p.get() && p->getType() == eXmlTypeString)
  62. {
  63. n = TC_Common::strto<Short>(XmlValueStringPtr::dynamicCast(p)->value);
  64. }
  65. else if (isRequire)
  66. {
  67. char s[128];
  68. snprintf(s, sizeof(s), "read 'Short' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  69. throw TC_Xml_Exception(s);
  70. }
  71. }
  72. static void readXml(UInt16& n, const XmlValuePtr & p, bool isRequire = true)
  73. {
  74. if(NULL != p.get() && p->getType() == eXmlTypeString)
  75. {
  76. n = TC_Common::strto<UInt16>(XmlValueStringPtr::dynamicCast(p)->value);
  77. }
  78. else if (isRequire)
  79. {
  80. char s[128];
  81. snprintf(s, sizeof(s), "read 'Uint16' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  82. throw TC_Xml_Exception(s);
  83. }
  84. }
  85. static void readXml(Int32& n, const XmlValuePtr & p, bool isRequire = true)
  86. {
  87. if(NULL != p.get() && p->getType() == eXmlTypeString)
  88. {
  89. n = TC_Common::strto<Int32>(XmlValueStringPtr::dynamicCast(p)->value);
  90. }
  91. else if (isRequire)
  92. {
  93. char s[128];
  94. snprintf(s, sizeof(s), "read 'Int32' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  95. throw TC_Xml_Exception(s);
  96. }
  97. }
  98. static void readXml(UInt32& n, const XmlValuePtr & p, bool isRequire = true)
  99. {
  100. if(NULL != p.get() && p->getType() == eXmlTypeString)
  101. {
  102. n = TC_Common::strto<UInt32>(XmlValueStringPtr::dynamicCast(p)->value);
  103. }
  104. else if (isRequire)
  105. {
  106. char s[128];
  107. snprintf(s, sizeof(s), "read 'UInt32' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  108. throw TC_Xml_Exception(s);
  109. }
  110. }
  111. static void readXml(Int64& n, const XmlValuePtr & p, bool isRequire = true)
  112. {
  113. if(NULL != p.get() && p->getType() == eXmlTypeString)
  114. {
  115. n = TC_Common::strto<Int64>(XmlValueStringPtr::dynamicCast(p)->value);
  116. }
  117. else if (isRequire)
  118. {
  119. char s[128];
  120. snprintf(s, sizeof(s), "read 'Int64' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  121. throw TC_Xml_Exception(s);
  122. }
  123. }
  124. static void readXml(Float& n, const XmlValuePtr & p, bool isRequire = true)
  125. {
  126. if(NULL != p.get() && p->getType() == eXmlTypeString)
  127. {
  128. n = TC_Common::strto<Float>(XmlValueStringPtr::dynamicCast(p)->value);
  129. }
  130. else if (isRequire)
  131. {
  132. char s[128];
  133. snprintf(s, sizeof(s), "read 'Float' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  134. throw TC_Xml_Exception(s);
  135. }
  136. }
  137. static void readXml(Double& n, const XmlValuePtr & p, bool isRequire = true)
  138. {
  139. if(NULL != p.get() && p->getType() == eXmlTypeString)
  140. {
  141. n = TC_Common::strto<Double>(XmlValueStringPtr::dynamicCast(p)->value);
  142. }
  143. else if (isRequire)
  144. {
  145. char s[128];
  146. snprintf(s, sizeof(s), "read 'Double' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  147. throw TC_Xml_Exception(s);
  148. }
  149. }
  150. static void readXml(std::string& s, const XmlValuePtr & p, bool isRequire = true)
  151. {
  152. if(NULL != p.get() && p->getType() == eXmlTypeString)
  153. {
  154. s = XmlValueStringPtr::dynamicCast(p)->value;
  155. }
  156. else if (isRequire)
  157. {
  158. char s[128];
  159. snprintf(s, sizeof(s), "read 'string' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  160. throw TC_Xml_Exception(s);
  161. }
  162. }
  163. static void readXml(char *buf, const UInt32 bufLen, UInt32 & readLen, const XmlValuePtr & p, bool isRequire = true)
  164. {
  165. if(NULL != p.get() && p->getType() == eXmlTypeString)
  166. {
  167. XmlValueStringPtr pString=XmlValueStringPtr::dynamicCast(p);
  168. if((UInt32)pString->value.size()>bufLen)
  169. {
  170. char s[128];
  171. snprintf(s, sizeof(s), "invalid char * size, size: %u", (UInt32)pString->value.size());
  172. throw TC_Xml_Exception(s);
  173. }
  174. memcpy(buf,pString->value.c_str(),pString->value.size());
  175. readLen = pString->value.size();
  176. }
  177. else if (isRequire)
  178. {
  179. char s[128];
  180. snprintf(s, sizeof(s), "read 'char *' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  181. throw TC_Xml_Exception(s);
  182. }
  183. }
  184. template<typename V, typename Cmp, typename Alloc>
  185. static void readXml(std::map<string, V, Cmp, Alloc>& m, const XmlValuePtr & p, bool isRequire = true)
  186. {
  187. if(NULL != p.get() && p->getType() == eXmlTypeObj)
  188. {
  189. XmlValueObjPtr pObj=XmlValueObjPtr::dynamicCast(p);
  190. map<string,XmlValuePtr>::iterator iter;
  191. iter=pObj->value.begin();
  192. for(;iter!=pObj->value.end();++iter)
  193. {
  194. std::pair<string, V> pr;
  195. pr.first=iter->first;
  196. readXml(pr.second,iter->second);
  197. m.insert(pr);
  198. }
  199. }
  200. else if (isRequire)
  201. {
  202. char s[128];
  203. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  204. throw TC_Xml_Exception(s);
  205. }
  206. }
  207. template<typename V, typename Cmp, typename Alloc>
  208. static void readXml(std::map<bool, V, Cmp, Alloc>& m, const XmlValuePtr & p, bool isRequire = true)
  209. {
  210. if(NULL != p.get() && p->getType() == eXmlTypeObj)
  211. {
  212. XmlValueObjPtr pObj=XmlValueObjPtr::dynamicCast(p);
  213. map<string,XmlValuePtr>::iterator iter;
  214. iter=pObj->value.begin();
  215. for(;iter!=pObj->value.end();++iter)
  216. {
  217. std::pair<bool, V> pr;
  218. pr.first=TC_Common::strto<bool>(iter->first);
  219. readXml(pr.second,iter->second);
  220. m.insert(pr);
  221. }
  222. }
  223. else if (isRequire)
  224. {
  225. char s[128];
  226. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  227. throw TC_Xml_Exception(s);
  228. }
  229. }
  230. template<typename V, typename Cmp, typename Alloc>
  231. static void readXml(std::map<Char, V, Cmp, Alloc>& m, const XmlValuePtr & p, bool isRequire = true)
  232. {
  233. if(NULL != p.get() && p->getType() == eXmlTypeObj)
  234. {
  235. XmlValueObjPtr pObj=XmlValueObjPtr::dynamicCast(p);
  236. map<string,XmlValuePtr>::iterator iter;
  237. iter=pObj->value.begin();
  238. for(;iter!=pObj->value.end();++iter)
  239. {
  240. std::pair<Char, V> pr;
  241. pr.first=TC_Common::strto<Int32>(iter->first);
  242. readXml(pr.second,iter->second);
  243. m.insert(pr);
  244. }
  245. }
  246. else if (isRequire)
  247. {
  248. char s[128];
  249. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  250. throw TC_Xml_Exception(s);
  251. }
  252. }
  253. template<typename V, typename Cmp, typename Alloc>
  254. static void readXml(std::map<UInt8, V, Cmp, Alloc>& m, const XmlValuePtr & p, bool isRequire = true)
  255. {
  256. if(NULL != p.get() && p->getType() == eXmlTypeObj)
  257. {
  258. XmlValueObjPtr pObj=XmlValueObjPtr::dynamicCast(p);
  259. map<string,XmlValuePtr>::iterator iter;
  260. iter=pObj->value.begin();
  261. for(;iter!=pObj->value.end();++iter)
  262. {
  263. std::pair<UInt8, V> pr;
  264. pr.first=TC_Common::strto<UInt8>(iter->first);
  265. readXml(pr.second,iter->second);
  266. m.insert(pr);
  267. }
  268. }
  269. else if (isRequire)
  270. {
  271. char s[128];
  272. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  273. throw TC_Xml_Exception(s);
  274. }
  275. }
  276. template<typename V, typename Cmp, typename Alloc>
  277. static void readXml(std::map<Short, V, Cmp, Alloc>& m, const XmlValuePtr & p, bool isRequire = true)
  278. {
  279. if(NULL != p.get() && p->getType() == eXmlTypeObj)
  280. {
  281. XmlValueObjPtr pObj=XmlValueObjPtr::dynamicCast(p);
  282. map<string,XmlValuePtr>::iterator iter;
  283. iter=pObj->value.begin();
  284. for(;iter!=pObj->value.end();++iter)
  285. {
  286. std::pair<Short, V> pr;
  287. pr.first=TC_Common::strto<Short>(iter->first);
  288. readXml(pr.second,iter->second);
  289. m.insert(pr);
  290. }
  291. }
  292. else if (isRequire)
  293. {
  294. char s[128];
  295. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  296. throw TC_Xml_Exception(s);
  297. }
  298. }
  299. template<typename V, typename Cmp, typename Alloc>
  300. static void readXml(std::map<UInt16, V, Cmp, Alloc>& m, const XmlValuePtr & p, bool isRequire = true)
  301. {
  302. if(NULL != p.get() && p->getType() == eXmlTypeObj)
  303. {
  304. XmlValueObjPtr pObj=XmlValueObjPtr::dynamicCast(p);
  305. map<string,XmlValuePtr>::iterator iter;
  306. iter=pObj->value.begin();
  307. for(;iter!=pObj->value.end();++iter)
  308. {
  309. std::pair<UInt16, V> pr;
  310. pr.first=TC_Common::strto<UInt16>(iter->first);
  311. readXml(pr.second,iter->second);
  312. m.insert(pr);
  313. }
  314. }
  315. else if (isRequire)
  316. {
  317. char s[128];
  318. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  319. throw TC_Xml_Exception(s);
  320. }
  321. }
  322. template<typename V, typename Cmp, typename Alloc>
  323. static void readXml(std::map<Int32, V, Cmp, Alloc>& m, const XmlValuePtr & p, bool isRequire = true)
  324. {
  325. if(NULL != p.get() && p->getType() == eXmlTypeObj)
  326. {
  327. XmlValueObjPtr pObj=XmlValueObjPtr::dynamicCast(p);
  328. map<string,XmlValuePtr>::iterator iter;
  329. iter=pObj->value.begin();
  330. for(;iter!=pObj->value.end();++iter)
  331. {
  332. std::pair<Int32, V> pr;
  333. pr.first=TC_Common::strto<Int32>(iter->first);
  334. readXml(pr.second,iter->second);
  335. m.insert(pr);
  336. }
  337. }
  338. else if (isRequire)
  339. {
  340. char s[128];
  341. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  342. throw TC_Xml_Exception(s);
  343. }
  344. }
  345. template<typename V, typename Cmp, typename Alloc>
  346. static void readXml(std::map<UInt32, V, Cmp, Alloc>& m, const XmlValuePtr & p, bool isRequire = true)
  347. {
  348. if(NULL != p.get() && p->getType() == eXmlTypeObj)
  349. {
  350. XmlValueObjPtr pObj=XmlValueObjPtr::dynamicCast(p);
  351. map<string,XmlValuePtr>::iterator iter;
  352. iter=pObj->value.begin();
  353. for(;iter!=pObj->value.end();++iter)
  354. {
  355. std::pair<UInt32, V> pr;
  356. pr.first=TC_Common::strto<UInt32>(iter->first);
  357. readXml(pr.second,iter->second);
  358. m.insert(pr);
  359. }
  360. }
  361. else if (isRequire)
  362. {
  363. char s[128];
  364. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  365. throw TC_Xml_Exception(s);
  366. }
  367. }
  368. template<typename V, typename Cmp, typename Alloc>
  369. static void readXml(std::map<Int64, V, Cmp, Alloc>& m, const XmlValuePtr & p, bool isRequire = true)
  370. {
  371. if(NULL != p.get() && p->getType() == eXmlTypeObj)
  372. {
  373. XmlValueObjPtr pObj=XmlValueObjPtr::dynamicCast(p);
  374. map<string,XmlValuePtr>::iterator iter;
  375. iter=pObj->value.begin();
  376. for(;iter!=pObj->value.end();++iter)
  377. {
  378. std::pair<Int64, V> pr;
  379. pr.first=TC_Common::strto<Int64>(iter->first);
  380. readXml(pr.second,iter->second);
  381. m.insert(pr);
  382. }
  383. }
  384. else if (isRequire)
  385. {
  386. char s[128];
  387. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  388. throw TC_Xml_Exception(s);
  389. }
  390. }
  391. template<typename V, typename Cmp, typename Alloc>
  392. static void readXml(std::map<Float, V, Cmp, Alloc>& m, const XmlValuePtr & p, bool isRequire = true)
  393. {
  394. if(NULL != p.get() && p->getType() == eXmlTypeObj)
  395. {
  396. XmlValueObjPtr pObj=XmlValueObjPtr::dynamicCast(p);
  397. map<string,XmlValuePtr>::iterator iter;
  398. iter=pObj->value.begin();
  399. for(;iter!=pObj->value.end();++iter)
  400. {
  401. std::pair<Float, V> pr;
  402. pr.first=TC_Common::strto<Float>(iter->first);
  403. readXml(pr.second,iter->second);
  404. m.insert(pr);
  405. }
  406. }
  407. else if (isRequire)
  408. {
  409. char s[128];
  410. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  411. throw TC_Xml_Exception(s);
  412. }
  413. }
  414. template<typename V, typename Cmp, typename Alloc>
  415. static void readXml(std::map<Double, V, Cmp, Alloc>& m, const XmlValuePtr & p, bool isRequire = true)
  416. {
  417. if(NULL != p.get() && p->getType() == eXmlTypeObj)
  418. {
  419. XmlValueObjPtr pObj=XmlValueObjPtr::dynamicCast(p);
  420. map<string,XmlValuePtr>::iterator iter;
  421. iter=pObj->value.begin();
  422. for(;iter!=pObj->value.end();++iter)
  423. {
  424. std::pair<Double, V> pr;
  425. pr.first=TC_Common::strto<Double>(iter->first);
  426. readXml(pr.second,iter->second);
  427. m.insert(pr);
  428. }
  429. }
  430. else if (isRequire)
  431. {
  432. char s[128];
  433. snprintf(s, sizeof(s), "read 'map' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  434. throw TC_Xml_Exception(s);
  435. }
  436. }
  437. template<typename K, typename V, typename Cmp, typename Alloc>
  438. static void readXml(std::map<K, V, Cmp, Alloc>& m, const XmlValuePtr & p, bool isRequire = true)
  439. {
  440. char s[128];
  441. snprintf(s, sizeof(s), "map key is not Basic type. map key is only string|bool|num");
  442. throw TC_Xml_Exception(s);
  443. }
  444. template<typename T, typename Alloc>
  445. static void readXml(std::vector<T, Alloc>& v, const XmlValuePtr & p, bool isRequire = true)
  446. {
  447. if (NULL != p.get() && p->getType() == eXmlTypeArray)
  448. {
  449. XmlValueArrayPtr pArray=XmlValueArrayPtr::dynamicCast(p);
  450. v.resize(pArray->value.size());
  451. for(size_t i=0;i<pArray->value.size();++i)
  452. {
  453. readXml(v[i],pArray->value[i]);
  454. }
  455. }
  456. else if (isRequire)
  457. {
  458. char s[128];
  459. snprintf(s, sizeof(s), "read 'vector' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  460. throw TC_Xml_Exception(s);
  461. }
  462. }
  463. /// 读取结构数组
  464. template<typename T>
  465. static void readXml(T* v, const UInt32 len, UInt32 & readLen, const XmlValuePtr & p, bool isRequire = true)
  466. {
  467. if(NULL != p.get() && p->getType() == eXmlTypeArray)
  468. {
  469. XmlValueArrayPtr pArray=XmlValueArrayPtr::dynamicCast(p);
  470. if(pArray->value.size()>len)
  471. {
  472. char s[128];
  473. snprintf(s, sizeof(s), "read 'T *' invalid size, size: %u", (uint32_t)pArray->value.size());
  474. throw TC_Xml_Exception(s);
  475. }
  476. for(size_t i=0;i<pArray->value.size();++i)
  477. {
  478. readXml(v[i],pArray->value[i]);
  479. }
  480. readLen=pArray->value.size();
  481. }
  482. else if (isRequire)
  483. {
  484. char s[128];
  485. snprintf(s, sizeof(s), "read 'T *' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  486. throw TC_Xml_Exception(s);
  487. }
  488. }
  489. template<typename T>
  490. static void readXml(T& v, const XmlValuePtr & p, bool isRequire = true, typename detail::disable_if<detail::is_convertible<T*, TarsStructBase*>, void ***>::type dummy = 0)
  491. {
  492. Int32 n = 0;
  493. readXml(n, p, isRequire);
  494. v = (T) n;
  495. }
  496. /// 读取结构
  497. template<typename T>
  498. static void readXml(T& v, const XmlValuePtr & p, bool isRequire = true, typename detail::enable_if<detail::is_convertible<T*, TarsStructBase*>, void ***>::type dummy = 0)
  499. {
  500. if(NULL != p.get() && p->getType() == eXmlTypeObj)
  501. {
  502. XmlValueObjPtr pObj=XmlValueObjPtr::dynamicCast(p);
  503. v.readFromXml(pObj);
  504. }
  505. else if (isRequire)
  506. {
  507. char s[128];
  508. snprintf(s, sizeof(s), "read 'Char' type mismatch, get type: %d.", (p.get() ? p->getType() : 0));
  509. throw TC_Xml_Exception(s);
  510. }
  511. }
  512. };
  513. class XmlOutput
  514. {
  515. public:
  516. static XmlValueStringPtr writeXml(Bool b, bool cdata = false)
  517. {
  518. return (new XmlValueString(b ? "true" : "false", cdata));
  519. }
  520. static XmlValueStringPtr writeXml(Char n, bool cdata = false)
  521. {
  522. return (new XmlValueString(TC_Common::tostr(n), cdata));
  523. }
  524. static XmlValueStringPtr writeXml(UInt8 n, bool cdata = false)
  525. {
  526. return (new XmlValueString(TC_Common::tostr(n), cdata));
  527. }
  528. static XmlValueStringPtr writeXml(Short n, bool cdata = false)
  529. {
  530. return (new XmlValueString(TC_Common::tostr(n), cdata));
  531. }
  532. static XmlValueStringPtr writeXml(UInt16 n, bool cdata = false)
  533. {
  534. return (new XmlValueString(TC_Common::tostr(n), cdata));
  535. }
  536. static XmlValueStringPtr writeXml(Int32 n, bool cdata = false)
  537. {
  538. return (new XmlValueString(TC_Common::tostr(n), cdata));
  539. }
  540. static XmlValueStringPtr writeXml(UInt32 n, bool cdata = false)
  541. {
  542. return (new XmlValueString(TC_Common::tostr(n), cdata));
  543. }
  544. static XmlValueStringPtr writeXml(Int64 n, bool cdata = false)
  545. {
  546. return (new XmlValueString(TC_Common::tostr(n), cdata));
  547. }
  548. static XmlValueStringPtr writeXml(Float n, bool cdata = false)
  549. {
  550. return (new XmlValueString(TC_Common::tostr(n), cdata));
  551. }
  552. static XmlValueStringPtr writeXml(Double n, bool cdata = false)
  553. {
  554. return (new XmlValueString(TC_Common::tostr(n), cdata));
  555. }
  556. static XmlValueStringPtr writeXml(const std::string& s, bool cdata = false)
  557. {
  558. return (new XmlValueString(s, cdata));
  559. }
  560. static XmlValueStringPtr writeXml(const char *buf, const UInt32 len, bool cdata = false)
  561. {
  562. return (new XmlValueString(string(buf,len), cdata));
  563. }
  564. template<typename V, typename Cmp, typename Alloc>
  565. static XmlValueObjPtr writeXml(const std::map<string, V, Cmp, Alloc>& m)
  566. {
  567. XmlValueObjPtr pObj=new XmlValueObj();
  568. typedef typename std::map<string, V, Cmp, Alloc>::const_iterator IT;
  569. for (IT i = m.begin(); i != m.end(); ++i)
  570. {
  571. pObj->value[i->first]=writeXml(i->second);
  572. }
  573. return pObj;
  574. }
  575. template<typename V, typename Cmp, typename Alloc>
  576. static XmlValueObjPtr writeXml(const std::map<Bool, V, Cmp, Alloc>& m)
  577. {
  578. XmlValueObjPtr pObj=new XmlValueObj();
  579. typedef typename std::map<Bool, V, Cmp, Alloc>::const_iterator IT;
  580. for (IT i = m.begin(); i != m.end(); ++i)
  581. {
  582. pObj->value[TC_Common::tostr(i->first)]=writeXml(i->second);
  583. }
  584. return pObj;
  585. }
  586. template<typename V, typename Cmp, typename Alloc>
  587. static XmlValueObjPtr writeXml(const std::map<Char, V, Cmp, Alloc>& m)
  588. {
  589. XmlValueObjPtr pObj=new XmlValueObj();
  590. typedef typename std::map<Char, V, Cmp, Alloc>::const_iterator IT;
  591. for (IT i = m.begin(); i != m.end(); ++i)
  592. {
  593. pObj->value[TC_Common::tostr((Int32)i->first)]=writeXml(i->second);
  594. }
  595. return pObj;
  596. }
  597. template<typename V, typename Cmp, typename Alloc>
  598. static XmlValueObjPtr writeXml(const std::map<UInt8, V, Cmp, Alloc>& m)
  599. {
  600. XmlValueObjPtr pObj=new XmlValueObj();
  601. typedef typename std::map<UInt8, V, Cmp, Alloc>::const_iterator IT;
  602. for (IT i = m.begin(); i != m.end(); ++i)
  603. {
  604. pObj->value[TC_Common::tostr(i->first)]=writeXml(i->second);
  605. }
  606. return pObj;
  607. }
  608. template<typename V, typename Cmp, typename Alloc>
  609. static XmlValueObjPtr writeXml(const std::map<Short, V, Cmp, Alloc>& m)
  610. {
  611. XmlValueObjPtr pObj=new XmlValueObj();
  612. typedef typename std::map<Short, V, Cmp, Alloc>::const_iterator IT;
  613. for (IT i = m.begin(); i != m.end(); ++i)
  614. {
  615. pObj->value[TC_Common::tostr(i->first)]=writeXml(i->second);
  616. }
  617. return pObj;
  618. }
  619. template<typename V, typename Cmp, typename Alloc>
  620. static XmlValueObjPtr writeXml(const std::map<UInt16, V, Cmp, Alloc>& m)
  621. {
  622. XmlValueObjPtr pObj=new XmlValueObj();
  623. typedef typename std::map<UInt16, V, Cmp, Alloc>::const_iterator IT;
  624. for (IT i = m.begin(); i != m.end(); ++i)
  625. {
  626. pObj->value[TC_Common::tostr(i->first)]=writeXml(i->second);
  627. }
  628. return pObj;
  629. }
  630. template<typename V, typename Cmp, typename Alloc>
  631. static XmlValueObjPtr writeXml(const std::map<Int32, V, Cmp, Alloc>& m)
  632. {
  633. XmlValueObjPtr pObj=new XmlValueObj();
  634. typedef typename std::map<Int32, V, Cmp, Alloc>::const_iterator IT;
  635. for (IT i = m.begin(); i != m.end(); ++i)
  636. {
  637. pObj->value[TC_Common::tostr(i->first)]=writeXml(i->second);
  638. }
  639. return pObj;
  640. }
  641. template<typename V, typename Cmp, typename Alloc>
  642. static XmlValueObjPtr writeXml(const std::map<UInt32, V, Cmp, Alloc>& m)
  643. {
  644. XmlValueObjPtr pObj=new XmlValueObj();
  645. typedef typename std::map<UInt32, V, Cmp, Alloc>::const_iterator IT;
  646. for (IT i = m.begin(); i != m.end(); ++i)
  647. {
  648. pObj->value[TC_Common::tostr(i->first)]=writeXml(i->second);
  649. }
  650. return pObj;
  651. }
  652. template<typename V, typename Cmp, typename Alloc>
  653. static XmlValueObjPtr writeXml(const std::map<Int64, V, Cmp, Alloc>& m)
  654. {
  655. XmlValueObjPtr pObj=new XmlValueObj();
  656. typedef typename std::map<Int64, V, Cmp, Alloc>::const_iterator IT;
  657. for (IT i = m.begin(); i != m.end(); ++i)
  658. {
  659. pObj->value[TC_Common::tostr(i->first)]=writeXml(i->second);
  660. }
  661. return pObj;
  662. }
  663. template<typename V, typename Cmp, typename Alloc>
  664. static XmlValueObjPtr writeXml(const std::map<Float, V, Cmp, Alloc>& m)
  665. {
  666. XmlValueObjPtr pObj=new XmlValueObj();
  667. typedef typename std::map<Float, V, Cmp, Alloc>::const_iterator IT;
  668. for (IT i = m.begin(); i != m.end(); ++i)
  669. {
  670. pObj->value[TC_Common::tostr(i->first)]=writeXml(i->second);
  671. }
  672. return pObj;
  673. }
  674. template<typename V, typename Cmp, typename Alloc>
  675. static XmlValueObjPtr writeXml(const std::map<Double, V, Cmp, Alloc>& m)
  676. {
  677. XmlValueObjPtr pObj=new XmlValueObj();
  678. typedef typename std::map<Double, V, Cmp, Alloc>::const_iterator IT;
  679. for (IT i = m.begin(); i != m.end(); ++i)
  680. {
  681. pObj->value[TC_Common::tostr(i->first)]=writeXml(i->second);
  682. }
  683. return pObj;
  684. }
  685. template<typename K, typename V, typename Cmp, typename Alloc>
  686. static XmlValueObjPtr writeXml(const std::map<K, V, Cmp, Alloc>& m)
  687. {
  688. char s[128];
  689. snprintf(s, sizeof(s), "map key is not Basic type. map key is only string|bool|num");
  690. throw TC_Xml_Exception(s);
  691. }
  692. template<typename T, typename Alloc>
  693. static XmlValueArrayPtr writeXml(const std::vector<T, Alloc>& v)
  694. {
  695. XmlValueArrayPtr pArray=new XmlValueArray();
  696. typedef typename std::vector<T, Alloc>::const_iterator IT;
  697. for (IT i = v.begin(); i != v.end(); ++i)
  698. pArray->value.push_back(writeXml(*i));
  699. return pArray;
  700. }
  701. template<typename T>
  702. static XmlValueArrayPtr writeXml(const T *v, const UInt32 len)
  703. {
  704. XmlValueArrayPtr pArray=new XmlValueArray();
  705. for (size_t i = 0; i < len; ++i)
  706. {
  707. pArray->value.push_back(writeXml(v[i]));
  708. }
  709. return pArray;
  710. }
  711. template<typename T>
  712. static XmlValueStringPtr writeXml(const T& v, typename detail::disable_if<detail::is_convertible<T*, TarsStructBase*>, void ***>::type dummy = 0)
  713. {
  714. return writeXml((Int32) v);
  715. }
  716. template<typename T>
  717. static XmlValueObjPtr writeXml(const T& v, typename detail::enable_if<detail::is_convertible<T*, TarsStructBase*>, void ***>::type dummy = 0)
  718. {
  719. return v.writeToXml();
  720. }
  721. };
  722. ////////////////////////////////////////////////////////////////////////////////////////////////////
  723. }
  724. #endif