framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
optional.hpp
Go to the documentation of this file.
1 // Copyright (C) 2013 iwg molw5
2 // For conditions of distribution and use, see copyright notice in COPYING
3 
13 #pragma once
14 
17 
18 namespace framework
19 {
20  namespace protocol_buffers
21  {
29  template <typename T>
31  {
32  private:
33  using value_type = typename T::value_type;
34  using reference_type =
35  typename std::conditional <
36  std::is_scalar <value_type>::value,
37  value_type,
38  value_type const&
39  >::type;
40 
41  public:
42  default_optional_value () = default;
43 
44  default_optional_value (value_type value)
45  : p_tValue {std::move(value)},
46  p_bSet {true}
47  {
48  }
49 
50  default_optional_value (std::tuple <>)
52  {
53  }
54 
55  template <typename... Args>
56  default_optional_value (std::tuple <Args...>&& args)
57  : default_optional_value (
58  std::forward <std::tuple <Args...>> (args),
59  static_cast <make_indices <sizeof... (Args)>*> (nullptr))
60  {
61  }
62 
63  private:
64  template <typename... Args, typename... Indices>
65  default_optional_value (std::tuple <Args...> args, pack_container <Indices...>*)
66  : p_tValue{std::forward <Args> (std::get <Indices::value> (args))...},
67  p_bSet {true}
68  {
69  }
70 
71  public:
73  reference_type get () const
74  {
75  return p_tValue;
76  }
77 
79  void set (value_type value)
80  {
81  p_bSet = true;
82  p_tValue = std::move(value);
83  }
84 
86  bool check () const
87  {
88  return p_bSet;
89  }
90 
92  value_type& mutate ()
93  {
94  p_bSet = true;
95  return p_tValue;
96  }
97 
98  protected:
99  ~default_optional_value () = default;
100 
101  protected:
102  value_type p_tValue {};
103  bool p_bSet {false};
104  };
105 
109  template <
110  typename Name,
111  uint32_t Number,
112  typename Specification,
113  template <typename> class Implementation = default_optional_value>
114  struct optional :
115  public field_base <
116  std::integral_constant <e_field_type, e_field_type::optional>,
117  std::integral_constant <uint32_t, Number>>,
118  public serializable::value_type <Name, Specification,
119  protocol_buffer_implementation_wrapper <
120  Name,
121  std::integral_constant <uint32_t, Number>,
122  Specification,
123  Implementation
124  >::template type,
125  false>
126  {
127  };
128  }
129 }
130 
131 #include <framework/protocol_buffers/optional.inl>