PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/buffer/struct_codec_macros.hpp

https://github.com/lianic/ardb
C++ Header | 928 lines | 813 code | 67 blank | 48 comment | 58 complexity | e10620adaa554e622ae8f3a5cddd4f45 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. /*
  2. *Copyright (c) 2013-2013, yinqiwen <yinqiwen@gmail.com>
  3. *All rights reserved.
  4. *
  5. *Redistribution and use in source and binary forms, with or without
  6. *modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of Redis nor the names of its contributors may be used
  14. * to endorse or promote products derived from this software without
  15. * specific prior written permission.
  16. *
  17. *THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. *AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. *IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. *ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  21. *BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. *CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. *SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. *INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. *CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. *ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  27. *THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /*
  30. * struct_codec_macros.hpp
  31. *
  32. * Created on: 2011-9-15
  33. * Author: yinqiwen
  34. */
  35. #ifndef STRUCT_CODEC_MACROS_HPP_
  36. #define STRUCT_CODEC_MACROS_HPP_
  37. #include "buffer/buffer_helper.hpp"
  38. #include "buffer/struct_code_templates.hpp"
  39. #include <string>
  40. #include <list>
  41. #include <map>
  42. #include <vector>
  43. #include <deque>
  44. #include <utility>
  45. #include <btree_set.h>
  46. #include <btree_map.h>
  47. using ardb::Buffer;
  48. #define ENCODE_DEFINE(...) \
  49. public: \
  50. bool Encode(Buffer& buf) \
  51. { \
  52. return ardb::encode_arg(buf,__VA_ARGS__); \
  53. } \
  54. #define DECODE_DEFINE(...) \
  55. public: \
  56. bool Decode(Buffer& buf) \
  57. { \
  58. return ardb::decode_arg(buf, __VA_ARGS__); \
  59. } \
  60. #define ENCODE2_DEFINE(...) \
  61. public: \
  62. bool Encode(Buffer& buf) \
  63. { \
  64. return ardb::encode_arg_with_tag(buf,__VA_ARGS__); \
  65. } \
  66. #define DECODE2_DEFINE(...) \
  67. public: \
  68. bool Decode(Buffer& buf) \
  69. { \
  70. return ardb::decode_arg_with_tag(buf, __VA_ARGS__); \
  71. } \
  72. #define CODEC2_DEFINE(...) \
  73. ENCODE2_DEFINE(__VA_ARGS__)\
  74. DECODE2_DEFINE(__VA_ARGS__)
  75. #define ENCODE_DEFINE_P(...) \
  76. public: \
  77. bool Encode(Buffer* buf) \
  78. { \
  79. RETURN_FALSE_IF_NULL(buf); \
  80. return ardb::encode_arg(*buf,__VA_ARGS__); \
  81. }
  82. #define DECODE_DEFINE_P(...) \
  83. public: \
  84. bool Decode(Buffer* buf) \
  85. { \
  86. RETURN_FALSE_IF_NULL(buf); \
  87. return ardb::decode_arg(*buf, __VA_ARGS__); \
  88. }
  89. #define FIX_ENCODE_DEFINE_P(...) \
  90. public: \
  91. bool Encode(Buffer* buf) \
  92. { \
  93. RETURN_FALSE_IF_NULL(buf); \
  94. return ardb::fix_encode_arg(*buf,__VA_ARGS__); \
  95. }
  96. #define FIX_DECODE_DEFINE_P(...) \
  97. public: \
  98. bool Decode(Buffer* buf) \
  99. { \
  100. RETURN_FALSE_IF_NULL(buf); \
  101. return ardb::fix_decode_arg(*buf, __VA_ARGS__); \
  102. }
  103. #define CODEC_DEFINE(...) \
  104. ENCODE_DEFINE(__VA_ARGS__)\
  105. DECODE_DEFINE(__VA_ARGS__)
  106. #define CODEC_DEFINE_P(...) \
  107. ENCODE_DEFINE_P(__VA_ARGS__)\
  108. DECODE_DEFINE_P(__VA_ARGS__)
  109. #define FIX_CODEC_DEFINE_P(...) \
  110. FIX_ENCODE_DEFINE_P(__VA_ARGS__)\
  111. FIX_DECODE_DEFINE_P(__VA_ARGS__)
  112. namespace ardb
  113. {
  114. inline bool encode_arg(Buffer& buf)
  115. {
  116. return true;
  117. }
  118. inline bool encode_arg(Buffer& buf, std::string& a0)
  119. {
  120. BufferHelper::WriteVarString(buf, a0);
  121. return true;
  122. }
  123. inline bool encode_arg(Buffer& buf, float& a0)
  124. {
  125. BufferHelper::WriteFixFloat(buf, a0);
  126. return true;
  127. }
  128. inline bool encode_arg(Buffer& buf, double& a0)
  129. {
  130. BufferHelper::WriteFixDouble(buf, a0);
  131. return true;
  132. }
  133. inline bool encode_arg(Buffer& buf, uint8& a0)
  134. {
  135. BufferHelper::WriteFixUInt8(buf, a0);
  136. return true;
  137. }
  138. inline bool encode_arg(Buffer& buf, const uint8& a0)
  139. {
  140. BufferHelper::WriteFixUInt8(buf, a0);
  141. return true;
  142. }
  143. inline bool encode_arg(Buffer& buf, int8& a0)
  144. {
  145. BufferHelper::WriteFixInt8(buf, a0);
  146. return true;
  147. }
  148. inline bool encode_arg(Buffer& buf, const int8& a0)
  149. {
  150. BufferHelper::WriteFixInt8(buf, a0);
  151. return true;
  152. }
  153. inline bool encode_arg(Buffer& buf, bool& a0)
  154. {
  155. BufferHelper::WriteBool(buf, a0);
  156. return true;
  157. }
  158. inline bool encode_arg(Buffer& buf, const bool& a0)
  159. {
  160. BufferHelper::WriteBool(buf, a0);
  161. return true;
  162. }
  163. inline bool encode_arg(Buffer& buf, uint16& a0)
  164. {
  165. BufferHelper::WriteVarUInt16(buf, a0);
  166. return true;
  167. }
  168. inline bool encode_arg(Buffer& buf, const uint16& a0)
  169. {
  170. BufferHelper::WriteVarUInt16(buf, a0);
  171. return true;
  172. }
  173. inline bool encode_arg(Buffer& buf, int16& a0)
  174. {
  175. BufferHelper::WriteVarInt16(buf, a0);
  176. return true;
  177. }
  178. inline bool encode_arg(Buffer& buf, const int16& a0)
  179. {
  180. BufferHelper::WriteVarInt16(buf, a0);
  181. return true;
  182. }
  183. inline bool encode_arg(Buffer& buf, uint32& a0)
  184. {
  185. BufferHelper::WriteVarUInt32(buf, a0);
  186. return true;
  187. }
  188. inline bool encode_arg(Buffer& buf, const uint32& a0)
  189. {
  190. BufferHelper::WriteVarUInt32(buf, a0);
  191. return true;
  192. }
  193. inline bool encode_arg(Buffer& buf, int32& a0)
  194. {
  195. BufferHelper::WriteVarInt32(buf, a0);
  196. return true;
  197. }
  198. inline bool encode_arg(Buffer& buf, const int32& a0)
  199. {
  200. BufferHelper::WriteVarInt32(buf, a0);
  201. return true;
  202. }
  203. inline bool encode_arg(Buffer& buf, uint64& a0)
  204. {
  205. BufferHelper::WriteVarUInt64(buf, a0);
  206. return true;
  207. }
  208. inline bool encode_arg(Buffer& buf, const uint64& a0)
  209. {
  210. BufferHelper::WriteVarUInt64(buf, a0);
  211. return true;
  212. }
  213. inline bool encode_arg(Buffer& buf, int64& a0)
  214. {
  215. BufferHelper::WriteVarInt64(buf, a0);
  216. return true;
  217. }
  218. inline bool encode_arg(Buffer& buf, const int64& a0)
  219. {
  220. BufferHelper::WriteVarInt64(buf, a0);
  221. return true;
  222. }
  223. inline bool encode_arg(Buffer& buf, const std::string& a0)
  224. {
  225. BufferHelper::WriteVarString(buf, a0);
  226. return true;
  227. }
  228. inline bool encode_arg(Buffer& buf, const char*& a0)
  229. {
  230. BufferHelper::WriteVarString(buf, a0);
  231. return true;
  232. }
  233. //fix encode
  234. inline bool fix_encode_arg(Buffer& buf)
  235. {
  236. return true;
  237. }
  238. template<typename A0>
  239. inline bool fix_encode_arg(Buffer& buf, A0& a0)
  240. {
  241. return a0.Encode(buf);
  242. }
  243. template<typename T>
  244. inline bool fix_encode_arg(Buffer& buf, std::list<T>& a0)
  245. {
  246. BufferHelper::WriteFixUInt32(buf, a0.size());
  247. typename std::list<T>::iterator iter = a0.begin();
  248. while (iter != a0.end())
  249. {
  250. fix_encode_arg(buf, *iter);
  251. iter++;
  252. }
  253. return true;
  254. }
  255. template<typename T>
  256. inline bool fix_encode_arg(Buffer& buf, std::vector<T>& a0)
  257. {
  258. BufferHelper::WriteFixUInt32(buf, a0.size());
  259. typename std::vector<T>::iterator iter = a0.begin();
  260. while (iter != a0.end())
  261. {
  262. fix_encode_arg(buf, *iter);
  263. iter++;
  264. }
  265. return true;
  266. }
  267. template<typename T>
  268. inline bool fix_encode_arg(Buffer& buf, std::deque<T>& a0)
  269. {
  270. BufferHelper::WriteFixUInt32(buf, a0.size());
  271. typename std::deque<T>::iterator iter = a0.begin();
  272. while (iter != a0.end())
  273. {
  274. fix_encode_arg(buf, *iter);
  275. iter++;
  276. }
  277. return true;
  278. }
  279. template<typename K, typename V>
  280. inline bool fix_encode_arg(Buffer& buf, std::map<K, V>& a0)
  281. {
  282. BufferHelper::WriteFixUInt32(buf, a0.size());
  283. typename std::map<K, V>::iterator iter = a0.begin();
  284. while (iter != a0.end())
  285. {
  286. fix_encode_arg(buf, iter->first);
  287. fix_encode_arg(buf, iter->second);
  288. iter++;
  289. }
  290. return true;
  291. }
  292. inline bool fix_encode_arg(Buffer& buf, float& a0)
  293. {
  294. BufferHelper::WriteFixFloat(buf, a0);
  295. return true;
  296. }
  297. inline bool fix_encode_arg(Buffer& buf, double& a0)
  298. {
  299. BufferHelper::WriteFixDouble(buf, a0);
  300. return true;
  301. }
  302. inline bool fix_encode_arg(Buffer& buf, uint8_t& a0)
  303. {
  304. BufferHelper::WriteFixUInt8(buf, a0);
  305. return true;
  306. }
  307. inline bool fix_encode_arg(Buffer& buf, const uint8_t& a0)
  308. {
  309. BufferHelper::WriteFixUInt8(buf, a0);
  310. return true;
  311. }
  312. inline bool fix_encode_arg(Buffer& buf, int8_t& a0)
  313. {
  314. BufferHelper::WriteFixInt8(buf, a0);
  315. return true;
  316. }
  317. inline bool fix_encode_arg(Buffer& buf, const int8_t& a0)
  318. {
  319. BufferHelper::WriteFixInt8(buf, a0);
  320. return true;
  321. }
  322. inline bool fix_encode_arg(Buffer& buf, bool& a0)
  323. {
  324. BufferHelper::WriteBool(buf, a0);
  325. return true;
  326. }
  327. inline bool fix_encode_arg(Buffer& buf, const bool& a0)
  328. {
  329. BufferHelper::WriteBool(buf, a0);
  330. return true;
  331. }
  332. inline bool fix_encode_arg(Buffer& buf, uint16_t& a0)
  333. {
  334. BufferHelper::WriteFixUInt16(buf, a0);
  335. return true;
  336. }
  337. inline bool fix_encode_arg(Buffer& buf, const uint16_t& a0)
  338. {
  339. BufferHelper::WriteFixUInt16(buf, a0);
  340. return true;
  341. }
  342. inline bool fix_encode_arg(Buffer& buf, int16_t& a0)
  343. {
  344. BufferHelper::WriteFixInt16(buf, a0);
  345. return true;
  346. }
  347. inline bool fix_encode_arg(Buffer& buf, const int16_t& a0)
  348. {
  349. BufferHelper::WriteFixInt16(buf, a0);
  350. return true;
  351. }
  352. inline bool fix_encode_arg(Buffer& buf, uint32_t& a0)
  353. {
  354. BufferHelper::WriteFixUInt32(buf, a0);
  355. return true;
  356. }
  357. inline bool fix_encode_arg(Buffer& buf, const uint32_t& a0)
  358. {
  359. BufferHelper::WriteFixUInt32(buf, a0);
  360. return true;
  361. }
  362. inline bool fix_encode_arg(Buffer& buf, int32_t& a0)
  363. {
  364. BufferHelper::WriteFixInt32(buf, a0);
  365. return true;
  366. }
  367. inline bool fix_encode_arg(Buffer& buf, const int32_t& a0)
  368. {
  369. BufferHelper::WriteFixInt32(buf, a0);
  370. return true;
  371. }
  372. inline bool fix_encode_arg(Buffer& buf, uint64_t& a0)
  373. {
  374. BufferHelper::WriteFixUInt64(buf, a0);
  375. return true;
  376. }
  377. inline bool fix_encode_arg(Buffer& buf, const uint64_t& a0)
  378. {
  379. BufferHelper::WriteFixUInt64(buf, a0);
  380. return true;
  381. }
  382. inline bool fix_encode_arg(Buffer& buf, int64_t& a0)
  383. {
  384. BufferHelper::WriteFixInt64(buf, a0);
  385. return true;
  386. }
  387. inline bool fix_encode_arg(Buffer& buf, const int64_t& a0)
  388. {
  389. BufferHelper::WriteFixInt64(buf, a0);
  390. return true;
  391. }
  392. inline bool fix_encode_arg(Buffer& buf, std::string& a0)
  393. {
  394. BufferHelper::WriteFixString(buf, a0);
  395. return true;
  396. }
  397. inline bool fix_encode_arg(Buffer& buf, const std::string& a0)
  398. {
  399. BufferHelper::WriteFixString(buf, a0);
  400. return true;
  401. }
  402. inline bool fix_encode_arg(Buffer& buf, const char*& a0)
  403. {
  404. BufferHelper::WriteFixString(buf, a0);
  405. return true;
  406. }
  407. inline bool decode_arg(Buffer& buf)
  408. {
  409. return true;
  410. }
  411. template<typename A0>
  412. inline bool decode_arg(Buffer& buf, A0& a0)
  413. {
  414. return a0.Decode(buf);
  415. }
  416. //template<>
  417. inline bool decode_arg(Buffer& buf, float& a0)
  418. {
  419. return BufferHelper::ReadFixFloat(buf, a0);
  420. }
  421. inline bool decode_arg(Buffer& buf, double& a0)
  422. {
  423. return BufferHelper::ReadFixDouble(buf, a0);
  424. }
  425. inline bool decode_arg(Buffer& buf, uint8& a0)
  426. {
  427. return BufferHelper::ReadFixUInt8(buf, a0);
  428. }
  429. inline bool decode_arg(Buffer& buf, int8& a0)
  430. {
  431. return BufferHelper::ReadFixInt8(buf, a0);
  432. }
  433. inline bool decode_arg(Buffer& buf, bool& a0)
  434. {
  435. return BufferHelper::ReadBool(buf, a0);
  436. }
  437. //template<>
  438. inline bool decode_arg(Buffer& buf, uint16& a0)
  439. {
  440. return BufferHelper::ReadVarUInt16(buf, a0);
  441. }
  442. inline bool decode_arg(Buffer& buf, int16& a0)
  443. {
  444. return BufferHelper::ReadVarInt16(buf, a0);
  445. }
  446. //template<>
  447. inline bool decode_arg(Buffer& buf, uint32& a0)
  448. {
  449. return BufferHelper::ReadVarUInt32(buf, a0);
  450. }
  451. inline bool decode_arg(Buffer& buf, int32& a0)
  452. {
  453. return BufferHelper::ReadVarInt32(buf, a0);
  454. }
  455. //template<>
  456. inline bool decode_arg(Buffer& buf, uint64& a0)
  457. {
  458. return BufferHelper::ReadVarUInt64(buf, a0);
  459. }
  460. inline bool decode_arg(Buffer& buf, int64& a0)
  461. {
  462. return BufferHelper::ReadVarInt64(buf, a0);
  463. }
  464. //template<>
  465. inline bool decode_arg(Buffer& buf, std::string& a0)
  466. {
  467. return BufferHelper::ReadVarString(buf, a0);
  468. }
  469. //template<>
  470. inline bool decode_arg(Buffer& buf, char*& a0)
  471. {
  472. return BufferHelper::ReadVarString(buf, a0);
  473. }
  474. //Fix decode
  475. template<typename A0>
  476. inline bool fix_decode_arg(Buffer& buf, A0& a0)
  477. {
  478. return a0.Decode(buf);
  479. }
  480. template<typename T>
  481. inline bool fix_decode_arg(Buffer& buf, std::list<T>& a0)
  482. {
  483. uint32 size;
  484. if (!BufferHelper::ReadFixUInt32(buf, size))
  485. {
  486. return false;
  487. }
  488. uint32 i = 0;
  489. while (i < size)
  490. {
  491. T t;
  492. if (!fix_decode_arg(buf, t))
  493. {
  494. return false;
  495. }
  496. a0.push_back(t);
  497. i++;
  498. }
  499. return true;
  500. }
  501. template<typename T>
  502. inline bool fix_decode_arg(Buffer& buf, std::vector<T>& a0)
  503. {
  504. uint32 size;
  505. if (!BufferHelper::ReadFixUInt32(buf, size))
  506. {
  507. return false;
  508. }
  509. uint32 i = 0;
  510. while (i < size)
  511. {
  512. T t;
  513. if (!fix_decode_arg(buf, t))
  514. {
  515. return false;
  516. }
  517. a0.push_back(t);
  518. i++;
  519. }
  520. return true;
  521. }
  522. template<typename T>
  523. inline bool fix_decode_arg(Buffer& buf, std::deque<T>& a0)
  524. {
  525. uint32 size;
  526. if (!BufferHelper::ReadFixUInt32(buf, size))
  527. {
  528. return false;
  529. }
  530. uint32 i = 0;
  531. while (i < size)
  532. {
  533. T t;
  534. if (!fix_decode_arg(buf, t))
  535. {
  536. return false;
  537. }
  538. a0.push_back(t);
  539. i++;
  540. }
  541. return true;
  542. }
  543. template<typename K, typename V>
  544. inline bool fix_decode_arg(Buffer& buf, std::map<K, V>& a0)
  545. {
  546. uint32 size;
  547. if (!BufferHelper::ReadFixUInt32(buf, size))
  548. {
  549. return false;
  550. }
  551. uint32 i = 0;
  552. while (i < size)
  553. {
  554. K k;
  555. V v;
  556. if (!fix_decode_arg(buf, k) || !fix_decode_arg(buf, v))
  557. {
  558. return false;
  559. }
  560. a0.insert(std::make_pair(k, v));
  561. i++;
  562. }
  563. return true;
  564. }
  565. //template<>
  566. inline bool fix_decode_arg(Buffer& buf, uint8_t& a0)
  567. {
  568. return BufferHelper::ReadFixUInt8(buf, a0);
  569. }
  570. inline bool fix_decode_arg(Buffer& buf, int8_t& a0)
  571. {
  572. return BufferHelper::ReadFixInt8(buf, a0);
  573. }
  574. inline bool fix_decode_arg(Buffer& buf, bool& a0)
  575. {
  576. return BufferHelper::ReadBool(buf, a0);
  577. }
  578. //template<>
  579. inline bool fix_decode_arg(Buffer& buf, uint16_t& a0)
  580. {
  581. return BufferHelper::ReadFixUInt16(buf, a0);
  582. }
  583. inline bool fix_decode_arg(Buffer& buf, int16_t& a0)
  584. {
  585. return BufferHelper::ReadFixInt16(buf, a0);
  586. }
  587. //template<>
  588. inline bool fix_decode_arg(Buffer& buf, uint32_t& a0)
  589. {
  590. return BufferHelper::ReadFixUInt32(buf, a0);
  591. }
  592. inline bool fix_decode_arg(Buffer& buf, int32_t& a0)
  593. {
  594. return BufferHelper::ReadFixInt32(buf, a0);
  595. }
  596. //template<>
  597. inline bool fix_decode_arg(Buffer& buf, uint64_t& a0)
  598. {
  599. return BufferHelper::ReadFixUInt64(buf, a0);
  600. }
  601. inline bool fix_decode_arg(Buffer& buf, int64_t& a0)
  602. {
  603. return BufferHelper::ReadFixInt64(buf, a0);
  604. }
  605. //template<>
  606. inline bool fix_decode_arg(Buffer& buf, std::string& a0)
  607. {
  608. return BufferHelper::ReadFixString(buf, a0);
  609. }
  610. //template<>
  611. inline bool fix_decode_arg(Buffer& buf, char*& a0)
  612. {
  613. return BufferHelper::ReadFixString(buf, a0);
  614. }
  615. template<typename A0>
  616. inline bool encode_arg(Buffer& buf, A0* a0)
  617. {
  618. return a0->Encode(buf);
  619. }
  620. template<typename A0>
  621. inline bool encode_arg(Buffer& buf, A0& a0)
  622. {
  623. return a0.Encode(buf);
  624. }
  625. template<typename T>
  626. inline bool encode_arg(Buffer& buf, std::list<T>& a0)
  627. {
  628. BufferHelper::WriteVarUInt32(buf, a0.size());
  629. typename std::list<T>::iterator iter = a0.begin();
  630. while (iter != a0.end())
  631. {
  632. encode_arg(buf, *iter);
  633. iter++;
  634. }
  635. return true;
  636. }
  637. template<typename T>
  638. inline bool encode_arg(Buffer& buf, std::set<T>& a0)
  639. {
  640. BufferHelper::WriteVarUInt32(buf, a0.size());
  641. typename std::set<T>::iterator iter = a0.begin();
  642. while (iter != a0.end())
  643. {
  644. encode_arg(buf, *iter);
  645. iter++;
  646. }
  647. return true;
  648. }
  649. template<typename T>
  650. inline bool encode_arg(Buffer& buf, btree::btree_set<T>& a0)
  651. {
  652. BufferHelper::WriteVarUInt32(buf, a0.size());
  653. typename btree::btree_set<T>::iterator iter = a0.begin();
  654. while (iter != a0.end())
  655. {
  656. encode_arg(buf, *iter);
  657. iter++;
  658. }
  659. return true;
  660. }
  661. template<typename T>
  662. inline bool encode_arg(Buffer& buf, std::vector<T>& a0)
  663. {
  664. BufferHelper::WriteVarUInt32(buf, a0.size());
  665. typename std::vector<T>::iterator iter = a0.begin();
  666. while (iter != a0.end())
  667. {
  668. encode_arg(buf, *iter);
  669. iter++;
  670. }
  671. return true;
  672. }
  673. template<typename T>
  674. inline bool encode_arg(Buffer& buf, std::deque<T>& a0)
  675. {
  676. BufferHelper::WriteVarUInt32(buf, a0.size());
  677. typename std::deque<T>::iterator iter = a0.begin();
  678. while (iter != a0.end())
  679. {
  680. encode_arg(buf, *iter);
  681. iter++;
  682. }
  683. return true;
  684. }
  685. template<typename K, typename V>
  686. inline bool encode_arg(Buffer& buf, std::map<K, V>& a0)
  687. {
  688. BufferHelper::WriteVarUInt32(buf, a0.size());
  689. typename std::map<K, V>::iterator iter = a0.begin();
  690. while (iter != a0.end())
  691. {
  692. encode_arg(buf, iter->first);
  693. encode_arg(buf, iter->second);
  694. iter++;
  695. }
  696. return true;
  697. }
  698. template<typename K, typename V>
  699. inline bool encode_arg(Buffer& buf, btree::btree_map<K, V>& a0)
  700. {
  701. BufferHelper::WriteVarUInt32(buf, a0.size());
  702. typename btree::btree_map<K, V>::iterator iter = a0.begin();
  703. while (iter != a0.end())
  704. {
  705. encode_arg(buf, iter->first);
  706. encode_arg(buf, iter->second);
  707. iter++;
  708. }
  709. return true;
  710. }
  711. template<typename T>
  712. inline bool decode_arg(Buffer& buf, std::list<T>& a0)
  713. {
  714. uint32 size;
  715. if (!BufferHelper::ReadVarUInt32(buf, size))
  716. {
  717. return false;
  718. }
  719. uint32 i = 0;
  720. while (i < size)
  721. {
  722. T t;
  723. if (!decode_arg(buf, t))
  724. {
  725. return false;
  726. }
  727. a0.push_back(t);
  728. i++;
  729. }
  730. return true;
  731. }
  732. template<typename T>
  733. inline bool decode_arg(Buffer& buf, std::set<T>& a0)
  734. {
  735. uint32 size;
  736. if (!BufferHelper::ReadVarUInt32(buf, size))
  737. {
  738. return false;
  739. }
  740. uint32 i = 0;
  741. while (i < size)
  742. {
  743. T t;
  744. if (!decode_arg(buf, t))
  745. {
  746. return false;
  747. }
  748. a0.insert(t);
  749. i++;
  750. }
  751. return true;
  752. }
  753. template<typename T>
  754. inline bool decode_arg(Buffer& buf, btree::btree_set<T>& a0)
  755. {
  756. uint32 size;
  757. if (!BufferHelper::ReadVarUInt32(buf, size))
  758. {
  759. return false;
  760. }
  761. uint32 i = 0;
  762. while (i < size)
  763. {
  764. T t;
  765. if (!decode_arg(buf, t))
  766. {
  767. return false;
  768. }
  769. a0.insert(t);
  770. i++;
  771. }
  772. return true;
  773. }
  774. template<typename T>
  775. inline bool decode_arg(Buffer& buf, std::vector<T>& a0)
  776. {
  777. uint32 size;
  778. if (!BufferHelper::ReadVarUInt32(buf, size))
  779. {
  780. return false;
  781. }
  782. uint32 i = 0;
  783. while (i < size)
  784. {
  785. T t;
  786. if (!decode_arg(buf, t))
  787. {
  788. return false;
  789. }
  790. a0.push_back(t);
  791. i++;
  792. }
  793. return true;
  794. }
  795. template<typename T>
  796. inline bool decode_arg(Buffer& buf, std::deque<T>& a0)
  797. {
  798. uint32 size;
  799. if (!BufferHelper::ReadVarUInt32(buf, size))
  800. {
  801. return false;
  802. }
  803. uint32 i = 0;
  804. while (i < size)
  805. {
  806. T t;
  807. if (!decode_arg(buf, t))
  808. {
  809. return false;
  810. }
  811. a0.push_back(t);
  812. i++;
  813. }
  814. return true;
  815. }
  816. template<typename K, typename V>
  817. inline bool decode_arg(Buffer& buf, std::map<K, V>& a0)
  818. {
  819. uint32 size;
  820. if (!BufferHelper::ReadVarUInt32(buf, size))
  821. {
  822. return false;
  823. }
  824. uint32 i = 0;
  825. while (i < size)
  826. {
  827. K k;
  828. V v;
  829. if (!decode_arg(buf, k) || !decode_arg(buf, v))
  830. {
  831. return false;
  832. }
  833. a0.insert(std::make_pair(k, v));
  834. i++;
  835. }
  836. return true;
  837. }
  838. template<typename K, typename V>
  839. inline bool decode_arg(Buffer& buf, btree::btree_map<K, V>& a0)
  840. {
  841. uint32 size;
  842. if (!BufferHelper::ReadVarUInt32(buf, size))
  843. {
  844. return false;
  845. }
  846. uint32 i = 0;
  847. while (i < size)
  848. {
  849. K k;
  850. V v;
  851. if (!decode_arg(buf, k) || !decode_arg(buf, v))
  852. {
  853. return false;
  854. }
  855. a0.insert(std::make_pair(k, v));
  856. i++;
  857. }
  858. return true;
  859. }
  860. }
  861. #endif /* STRUCT_CODEC_MACROS_HPP_ */