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