framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
terminated_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 <cassert>
16 
19 
20 namespace framework
21 {
22  namespace serializable
23  {
33  template <
34  typename Value,
35  type_extractor <Value> Terminator,
36  typename Container>
37  struct terminated_container : public mutator_type <Container>
38  {
39  };
40 
52  template <
53  typename Value,
54  type_extractor <Value> Terminator,
55  typename Container,
56  typename Input,
57  typename Output>
60  Input& in, Output& out)
61  {
63  if (!dispatch_read <Value> (in, x))
64  return false;
65 
66  Container result;
67  auto inserter = std::insert_iterator <Container> (result, result.begin());
68  while (x != Terminator)
69  {
70  inserter = std::move(x);
71 
72  if (!dispatch_read <Value> (in, x))
73  return false;
74  }
75 
76  out = std::move(result);
77  return true;
78  }
79 
92  template <
93  typename Value,
94  type_extractor <Value> Terminator,
95  typename Container,
96  typename Input,
97  typename Output>
100  Input const& in, Output& out)
101  {
102  for (auto const& x : in)
103  {
104  assert(x != Terminator);
105  if (!dispatch_write <Value> (x, out))
106  return false;
107  }
108 
109  if (!dispatch_write <Value> (Terminator, out))
110  return false;
111 
112  return true;
113  }
114  }
115 }