中间件底层,websocket

SerializeStl.hpp 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //******************************************************************************
  2. // RCF - Remote Call Framework
  3. //
  4. // Copyright (c) 2005 - 2020, Delta V Software. All rights reserved.
  5. // http://www.deltavsoft.com
  6. //
  7. // RCF is distributed under dual licenses - closed source or GPL.
  8. // Consult your particular license for conditions of use.
  9. //
  10. // If you have not purchased a commercial license, you are using RCF
  11. // under GPL terms.
  12. //
  13. // Version: 3.2
  14. // Contact: support <at> deltavsoft.com
  15. //
  16. //******************************************************************************
  17. #ifndef INCLUDE_SF_SERIALIZESTL_HPP
  18. #define INCLUDE_SF_SERIALIZESTL_HPP
  19. #include <SF/Archive.hpp>
  20. namespace SF {
  21. class PushBackSemantics
  22. {
  23. public:
  24. template<typename Container, typename Value>
  25. void add(Container &container, const Value &value)
  26. {
  27. container.push_back(value);
  28. }
  29. };
  30. class InsertSemantics
  31. {
  32. public:
  33. template<typename Container, typename Value>
  34. void add(Container &container, const Value &value)
  35. {
  36. container.insert(value);
  37. }
  38. };
  39. class ReserveSemantics
  40. {
  41. public:
  42. template<typename Container>
  43. void reserve(Container &container, std::size_t newSize)
  44. {
  45. container.reserve(newSize);
  46. }
  47. };
  48. class NoReserveSemantics
  49. {
  50. public:
  51. template<typename Container>
  52. void reserve(Container &container, std::size_t newSize)
  53. {
  54. RCF_UNUSED_VARIABLE(container);
  55. RCF_UNUSED_VARIABLE(newSize);
  56. }
  57. };
  58. template<typename AddFunc, typename ReserveFunc, typename StlContainer>
  59. void serializeStlContainer(Archive &ar, StlContainer &t)
  60. {
  61. typedef typename StlContainer::iterator Iterator;
  62. typedef typename StlContainer::value_type Value;
  63. if (ar.isRead())
  64. {
  65. t.clear();
  66. unsigned int count = 0;
  67. ar & count;
  68. std::size_t minSerializedLength = sizeof(Value);
  69. if (ar.verifyAgainstArchiveSize(count*minSerializedLength))
  70. {
  71. ReserveFunc().reserve(t, count);
  72. }
  73. for (unsigned int i=0; i<count; i++)
  74. {
  75. Value value;
  76. ar & value;
  77. AddFunc().add(t, value);
  78. }
  79. }
  80. else if (ar.isWrite())
  81. {
  82. unsigned int count = static_cast<unsigned int>(t.size());
  83. ar & count;
  84. Iterator it = t.begin();
  85. for (unsigned int i=0; i<count; i++)
  86. {
  87. ar & *it;
  88. it++;
  89. }
  90. }
  91. }
  92. }
  93. #endif // ! INCLUDE_SF_SERIALIZESTL_HPP