Remote Call Framework 3.3
SerializeStl.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2022, 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.3
14 // Contact: support <at> deltavsoft.com
15 //
16 //******************************************************************************
17 
18 #ifndef INCLUDE_SF_SERIALIZESTL_HPP
19 #define INCLUDE_SF_SERIALIZESTL_HPP
20 
21 #include <SF/Archive.hpp>
22 
23 namespace SF {
24 
25  class PushBackSemantics
26  {
27  public:
28  template<typename Container, typename Value>
29  void add(Container &container, const Value &value)
30  {
31  container.push_back(value);
32  }
33  };
34 
35  class InsertSemantics
36  {
37  public:
38  template<typename Container, typename Value>
39  void add(Container &container, const Value &value)
40  {
41  container.insert(value);
42  }
43  };
44 
45  class ReserveSemantics
46  {
47  public:
48  template<typename Container>
49  void reserve(Container &container, std::size_t newSize)
50  {
51  container.reserve(newSize);
52  }
53  };
54 
55  class NoReserveSemantics
56  {
57  public:
58  template<typename Container>
59  void reserve(Container &container, std::size_t newSize)
60  {
61  RCF_UNUSED_VARIABLE(container);
62  RCF_UNUSED_VARIABLE(newSize);
63  }
64  };
65 
66  template<typename AddFunc, typename ReserveFunc, typename StlContainer>
67  void serializeStlContainer(Archive &ar, StlContainer &t)
68  {
69 
70  typedef typename StlContainer::iterator Iterator;
71  typedef typename StlContainer::value_type Value;
72 
73  if (ar.isRead())
74  {
75  t.clear();
76  unsigned int count = 0;
77  ar & count;
78 
79  std::size_t minSerializedLength = sizeof(Value);
80  if (ar.verifyAgainstArchiveSize(count*minSerializedLength))
81  {
82  ReserveFunc().reserve(t, count);
83  }
84 
85  for (unsigned int i=0; i<count; i++)
86  {
87  Value value;
88  ar & value;
89  AddFunc().add(t, value);
90  }
91  }
92  else if (ar.isWrite())
93  {
94  unsigned int count = static_cast<unsigned int>(t.size());
95  ar & count;
96  Iterator it = t.begin();
97  for (unsigned int i=0; i<count; i++)
98  {
99  ar & *it;
100  it++;
101  }
102  }
103  }
104 
105 }
106 
107 #endif // ! INCLUDE_SF_SERIALIZESTL_HPP
Definition: ByteBuffer.hpp:188