Value.hpp
Go to the documentation of this file.
1 #ifndef VALUE_HPP
2 #define VALUE_HPP
3 
4 #include <iostream>
5 #include <utility>
6 
7 namespace msp {
8 
9 template <class T> class Value {
10 public:
15  Value<T>& operator=(const Value<T>& rhs) = default;
16 
20  operator T() const { return data.first; }
21 
25  operator T&() { return data.first; }
26 
31  Value<T>& operator=(const T rhs) {
32  data.first = rhs;
33  data.second = true;
34  return *this;
35  }
36 
41  T& operator()() { return data.first; }
42 
47  T operator()() const { return data.first; }
48 
53  bool set() const { return data.second; }
54 
59  bool& set() { return data.second; }
60 
61 private:
62  std::pair<T, bool> data;
63 };
64 
65 } // namespace msp
66 
67 template <class T>
68 inline std::ostream& operator<<(std::ostream& s, const msp::Value<T>& val) {
69  if(val.set())
70  s << val();
71  else
72  s << "<unset>";
73  return s;
74 }
75 
76 template <>
77 inline std::ostream& operator<<(std::ostream& s,
78  const msp::Value<uint8_t>& val) {
79  if(val.set())
80  s << uint32_t(val());
81  else
82  s << "<unset>";
83  return s;
84 }
85 
86 template <>
87 inline std::ostream& operator<<(std::ostream& s,
88  const msp::Value<int8_t>& val) {
89  if(val.set())
90  s << int32_t(val());
91  else
92  s << "<unset>";
93  return s;
94 }
95 
96 #endif
std::pair< T, bool > data
Definition: Value.hpp:62
T operator()() const
Gets a copy of the data.
Definition: Value.hpp:47
T & operator()()
Gets a reference to the internal data.
Definition: Value.hpp:41
Value< T > & operator=(const T rhs)
Assignment operator for non-Value objects.
Definition: Value.hpp:31
std::ostream & operator<<(std::ostream &s, const msp::Value< T > &val)
Definition: Value.hpp:68
bool set() const
Queries if the data has been set.
Definition: Value.hpp:53
Value< T > & operator=(const Value< T > &rhs)=default
Copy assignment operator.


msp
Author(s): Christian Rauch
autogenerated on Tue Oct 6 2020 03:39:02