my_encoder.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <iostream>
  2. #include <string>
  3. #include "xpack/json.h"
  4. using namespace std;
  5. template<bool b> struct booltype {};
  6. template<typename T, typename B = booltype<true> >
  7. struct isxpack {static const bool value = false;};
  8. template<typename T>
  9. struct isxpack<T, booltype<T::__x_pack_value> > {static const bool value = true; };
  10. // without parents
  11. class MemberList {
  12. public:
  13. std::vector<std::string> members;
  14. template<class T>
  15. bool encode(const char *key, const T&val, const xpack::Extend *ext) {
  16. (void)val;
  17. (void)ext;
  18. if (NULL != key) {
  19. members.push_back(std::string(key));
  20. }
  21. return true;
  22. }
  23. };
  24. // with parents
  25. class MemberListP {
  26. public:
  27. std::vector<std::string> members;
  28. // class without xpack
  29. template<class T>
  30. typename xpack::x_enable_if<!isxpack<T>::value&&!xpack::is_xpack_out<T>::value, bool>::type encode(const char *key, const T&val, const xpack::Extend *ext) {
  31. (void)val;
  32. (void)ext;
  33. if (NULL != key) {
  34. members.push_back(std::string(key));
  35. }
  36. return true;
  37. }
  38. // class with xpack
  39. template<class T>
  40. typename xpack::x_enable_if<T::__x_pack_value && !xpack::is_xpack_out<T>::value, bool>::type encode(const char *key, const T&val, const xpack::Extend *ext) {
  41. if (NULL != key) {
  42. members.push_back(std::string(key));
  43. } else {
  44. val.__x_pack_encode(*this, val, ext);
  45. }
  46. return true;
  47. }
  48. // class with xpack_out
  49. template<class T>
  50. typename xpack::x_enable_if<xpack::is_xpack_out<T>::value, bool>::type encode(const char *key, const T&val, const xpack::Extend *ext) {
  51. if (NULL != key) {
  52. members.push_back(std::string(key));
  53. } else {
  54. __x_pack_encode_out(*this, val, ext);
  55. }
  56. return true;
  57. }
  58. };
  59. template <class T>
  60. std::string GetMembers(const T&val) {
  61. MemberList m;
  62. val.__x_pack_encode(m, val, 0);
  63. return xpack::json::encode(m.members);
  64. }
  65. template <class T>
  66. std::string GetMembersP(const T&val) {
  67. MemberListP m;
  68. val.__x_pack_encode(m, val, 0);
  69. return xpack::json::encode(m.members);
  70. }
  71. class Base1 {
  72. public:
  73. string b1s;
  74. int b1i;
  75. XPACK(O(b1s, b1i));
  76. };
  77. struct Base2 {
  78. string b2s;
  79. int b2i;
  80. };
  81. XPACK_OUT(Base2, O(b2s, b2i));
  82. struct N {
  83. string ns;
  84. int ni;
  85. XPACK(O(ns, ni));
  86. };
  87. struct Test:public Base1, Base2 {
  88. int a;
  89. int b;
  90. N n;
  91. XPACK(I(Base1, Base2), O(a,b, n));
  92. };
  93. int main(int argc, char *argv[]) {
  94. (void)argc;
  95. (void)argv;
  96. Test t;
  97. std::cout<<GetMembers(t)<<std::endl;
  98. std::cout<<GetMembersP(t)<<std::endl;
  99. return 0;
  100. }