Remote Call Framework 3.4
vector.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2023, Delta V Software. All rights reserved.
6 // https://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF under GPL terms.
12 //
13 // Version: 3.4
14 // Contact: support <at> deltavsoft.com
15 //
16 //******************************************************************************
17 
18 #ifndef INCLUDE_SF_VECTOR_HPP
19 #define INCLUDE_SF_VECTOR_HPP
20 
21 #include <type_traits>
22 #include <vector>
23 
24 #include <SF/Serializer.hpp>
25 #include <SF/SerializeStl.hpp>
26 
27 namespace SF {
28 
29  // std::vector
30 
31  template<typename T, typename A>
32  inline void serializeVector(
33  SF::Archive & ar,
34  std::vector<T,A> & vec,
35  RCF::FalseType *)
36  {
37  serializeStlContainer<PushBackSemantics, ReserveSemantics>(ar, vec);
38  }
39 
40  template<typename T, typename A>
41  inline void serializeVector(
42  SF::Archive & ar,
43  std::vector<T,A> & vec,
44  RCF::TrueType *)
45  {
46  serializeVectorFast(ar, vec);
47  }
48 
49  template<typename T, typename A>
50  inline void serialize(
51  SF::Archive & ar,
52  std::vector<T,A> & vec)
53  {
54  // We want fast vector serialization for vector<T>, if T is fundamental.
55  // Don't need to cover the case where T is a bool, as vector<bool> has
56  // its own serialize() function (see below).
57 
58  const bool IsBool = std::is_same<T, bool>::value;
59  static_assert( !IsBool, "This serialization function cannot be used for vector<bool>." );
60 
61  typedef typename std::is_fundamental<T>::type type;
62  serializeVector(ar, vec, (type *) 0);
63  }
64 
65  // Special case serialization for vector<bool>.
66  RCF_EXPORT void serialize(SF::Archive & ar, std::vector<bool> & bits);
67 
68  class I_VecWrapper
69  {
70  public:
71  virtual ~I_VecWrapper() {}
72 
73  virtual void resize(std::size_t newSize) = 0;
74  virtual std::uint32_t size() = 0;
75  virtual char * addressOfElement(std::size_t idx) = 0;
76  virtual std::uint32_t sizeofElement() = 0;
77  };
78 
79  template<typename Vec>
80  class VecWrapper : public I_VecWrapper
81  {
82  public:
83  VecWrapper(Vec & vec) : mVec(vec)
84  {
85  }
86 
87  void resize(std::size_t newSize)
88  {
89  mVec.resize(newSize);
90  }
91 
92  std::uint32_t size()
93  {
94  return static_cast<std::uint32_t>(mVec.size());
95  }
96 
97  char * addressOfElement(std::size_t idx)
98  {
99  return reinterpret_cast<char *>( &mVec[idx] );
100  }
101 
102  std::uint32_t sizeofElement()
103  {
104  typedef typename Vec::value_type ValueType;
105  return sizeof(ValueType);
106  }
107 
108  private:
109  Vec & mVec;
110  };
111 
112  RCF_EXPORT void serializeVectorFastImpl(
113  SF::Archive & ar,
114  I_VecWrapper & vec);
115 
116  template<typename T, typename A>
117  inline void serializeVectorFast(
118  SF::Archive & ar,
119  std::vector<T,A> & vec)
120  {
121  VecWrapper< std::vector<T,A> > vecWrapper(vec);
122  serializeVectorFastImpl(ar, vecWrapper);
123  }
124 
125 } // namespace SF
126 
127 #endif // ! INCLUDE_SF_VECTOR_HPP
Represents an archive, in which serialized objects are stored.
Definition: Archive.hpp:31
Definition: ByteBuffer.hpp:188