framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
variable_container.hpp
Go to the documentation of this file.
1 // Copyright (C) 2012 iwg molw5
2 // For conditions of distribution and use, see copyright notice in COPYING
3 
13 #pragma once
14 
15 #include <iterator>
16 
19 
20 namespace framework
21 {
22  namespace serializable
23  {
34  template <
35  typename Size,
36  typename Value,
37  typename Container,
38  bool Continuous = false>
39  struct variable_container : public mutator_type <Container>
40  {
41  };
42 
53  template <
54  typename Size,
55  typename Value,
56  typename Container,
57  bool Continuous,
58  typename Input,
59  typename Output>
63  Input& in, Output& out)
64  {
66  if (!dispatch_read <Size> (in, size))
67  return false;
68 
69  Container result;
70  auto inserter = std::insert_iterator <Container> (result, result.begin());
71  for (type_extractor <Size> i=0; i < size; ++i)
72  {
74  if (!dispatch_read <Value> (in, x))
75  return false;
76 
77  inserter = std::move(x);
78  }
79 
80  out = std::move(result);
81  return true;
82  }
83 
94  template <
95  typename Size,
96  typename Value,
97  typename Container,
98  bool Continuous,
99  typename Input,
100  typename Output>
104  Input const& in, Output& out)
105  {
106  type_extractor <Size> const& size = std::distance(in.begin(), in.end());
107  if (!dispatch_write <Size> (size, out))
108  return false;
109 
110  for (auto const& x : in)
111  if (!dispatch_write <Value> (static_cast <type_extractor <Value> const&> (x), out))
112  return false;
113 
114  return true;
115  }
116 
124  template <
125  typename Size,
126  typename Value,
127  typename Container,
128  typename Input,
129  typename Output>
131  typename std::enable_if <
132  std::is_same <Value, type_extractor <Value>>::value &&
133  std::is_scalar <Value>::value,
134  bool
135  >::type read_dispatch (
137  Input& in, Output& out)
138  {
140  if (!dispatch_read <Size> (in, size))
141  return false;
142 
143 #if false
144  Container result;
145  result.resize(size);
146  if (!stream_read(in, &out[0], sizeof(out[0])*size))
147  return false;
148 
149  out = std::move(result);
150  return true;
151 #else
152  out.resize(size);
153  if (!stream_read(in, &out[0], sizeof(out[0])*size))
154  return false;
155 
156  return true;
157 #endif
158  }
159 
167  template <
168  typename Size,
169  typename Value,
170  typename Container,
171  typename Input,
172  typename Output>
174  typename std::enable_if <
175  std::is_same <Value, type_extractor <Value>>::value &&
176  std::is_scalar <Value>::value,
177  bool
178  >::type write_dispatch (
180  Input const& in, Output& out)
181  {
182  type_extractor <Size> const& size = std::distance(in.begin(), in.end());
183  if (!dispatch_write <Size> (size, out))
184  return false;
185 
186  if (!stream_write(out, &in[0], sizeof(in[0])*in.size()))
187  return false;
188 
189  return true;
190  }
191  }
192 }