framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
optional_field.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 
16 
17 namespace framework
18 {
19  namespace serializable
20  {
26  template <typename Specification>
28  {
29  template <typename Output>
30  friend bool write_dispatch (
32  output_flags_frame <Specification> const& in, Output& out)
33  {
34  return dispatch_write <Specification> (in.p_iFlags, out);
35  }
36 
37  private:
38  using type = type_extractor <Specification>;
39 
40  public:
44  void set_flag (type const flag)
45  {
46  p_iFlags |= flag;
47  }
48 
52  bool write (char const*, std::size_t)
53  {
54  return true;
55  }
56 
57  private:
58  type p_iFlags {};
59  };
60 
61  template <typename Specification, typename Stream>
62  class input_flags_frame : std::reference_wrapper <Stream>
63  {
64  private:
65  using type = type_extractor <Specification>;
66 
67  public:
74  input_flags_frame (Stream& stream, type flags)
75  : std::reference_wrapper <Stream> (stream),
76  p_iFlags(flags)
77  {
78  }
79 
83  bool check_flag (type const flag) const
84  {
85  return p_iFlags & flag;
86  }
87 
91  template <std::size_t N>
92  bool read (char* s)
93  {
94  return stream_read <N> (this->get(), s);
95  }
96 
100  bool read (char* s, std::size_t n)
101  {
102  return stream_read(this->get(), s, n);
103  }
104 
105  private:
106  type const p_iFlags;
107  };
108 
113  template <typename Type, typename... Specification>
114  struct optional_field : public container_type <
115  pack_container <Specification...>,
116  pack_container <Specification...>,
117  false>
118  {
119  };
120 
128  template <
129  typename Type,
130  typename... Specification,
131  typename Input,
132  typename Output>
135  Input& in, Output& out)
136  {
137  type_extractor <Type> flags;
138  if (!dispatch_read <Type> (in, flags))
139  return false;
140 
141  input_flags_frame <Type, Input> frame {in, flags};
142  return dispatch_read <alias <Specification...>> (frame, out);
143  }
144 
152  template <
153  typename Type,
154  typename... Specification,
155  typename Input,
156  typename Output>
159  Input const& in, Output& out)
160  {
161  // Write the object state to the flags stream
163  if (!dispatch_write <alias <Specification...>> (in, frame))
164  return false;
165 
166  // Write the flags stream to the output stream
167  if (!write(frame, out))
168  return false;
169 
170  // Write the object state to the output stream
171  if (!dispatch_write <alias <Specification...>> (in, out))
172  return false;
173 
174  return true;
175  }
176  }
177 }