中间件底层,websocket

vector.hpp 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_VECTOR_HPP
  18. #define INCLUDE_SF_VECTOR_HPP
  19. #include <type_traits>
  20. #include <vector>
  21. #include <SF/Serializer.hpp>
  22. #include <SF/SerializeStl.hpp>
  23. namespace SF {
  24. // std::vector
  25. template<typename T, typename A>
  26. inline void serializeVector(
  27. SF::Archive & ar,
  28. std::vector<T,A> & vec,
  29. RCF::FalseType *)
  30. {
  31. serializeStlContainer<PushBackSemantics, ReserveSemantics>(ar, vec);
  32. }
  33. template<typename T, typename A>
  34. inline void serializeVector(
  35. SF::Archive & ar,
  36. std::vector<T,A> & vec,
  37. RCF::TrueType *)
  38. {
  39. serializeVectorFast(ar, vec);
  40. }
  41. template<typename T, typename A>
  42. inline void serialize(
  43. SF::Archive & ar,
  44. std::vector<T,A> & vec)
  45. {
  46. // We want fast vector serialization for vector<T>, if T is fundamental.
  47. // Don't need to cover the case where T is a bool, as vector<bool> has
  48. // its own serialize() function (see below).
  49. const bool IsBool = std::is_same<T, bool>::value;
  50. static_assert( !IsBool, "This serialization function cannot be used for vector<bool>." );
  51. typedef typename std::is_fundamental<T>::type type;
  52. serializeVector(ar, vec, (type *) 0);
  53. }
  54. // Special case serialization for vector<bool>.
  55. RCF_EXPORT void serialize(SF::Archive & ar, std::vector<bool> & bits);
  56. class I_VecWrapper
  57. {
  58. public:
  59. virtual ~I_VecWrapper() {}
  60. virtual void resize(std::size_t newSize) = 0;
  61. virtual std::uint32_t size() = 0;
  62. virtual char * addressOfElement(std::size_t idx) = 0;
  63. virtual std::uint32_t sizeofElement() = 0;
  64. };
  65. template<typename Vec>
  66. class VecWrapper : public I_VecWrapper
  67. {
  68. public:
  69. VecWrapper(Vec & vec) : mVec(vec)
  70. {
  71. }
  72. void resize(std::size_t newSize)
  73. {
  74. mVec.resize(newSize);
  75. }
  76. std::uint32_t size()
  77. {
  78. return static_cast<std::uint32_t>(mVec.size());
  79. }
  80. char * addressOfElement(std::size_t idx)
  81. {
  82. return reinterpret_cast<char *>( &mVec[idx] );
  83. }
  84. std::uint32_t sizeofElement()
  85. {
  86. typedef typename Vec::value_type ValueType;
  87. return sizeof(ValueType);
  88. }
  89. private:
  90. Vec & mVec;
  91. };
  92. RCF_EXPORT void serializeVectorFastImpl(
  93. SF::Archive & ar,
  94. I_VecWrapper & vec);
  95. template<typename T, typename A>
  96. inline void serializeVectorFast(
  97. SF::Archive & ar,
  98. std::vector<T,A> & vec)
  99. {
  100. VecWrapper< std::vector<T,A> > vecWrapper(vec);
  101. serializeVectorFastImpl(ar, vecWrapper);
  102. }
  103. } // namespace SF
  104. #endif // ! INCLUDE_SF_VECTOR_HPP