Value.hpp
Go to the documentation of this file.
00001 #ifndef VALUE_HPP
00002 #define VALUE_HPP
00003 
00004 #include <iostream>
00005 #include <utility>
00006 
00007 namespace msp {
00008 
00009 template <class T> class Value {
00010 public:
00015     Value<T>& operator=(const Value<T>& rhs) {
00016         data.first  = rhs();
00017         data.second = rhs.set();
00018         return *this;
00019     }
00020 
00024     operator T() const { return data.first; }
00025 
00029     operator T&() { return data.first; }
00030 
00035     Value<T>& operator=(const T rhs) {
00036         data.first  = rhs;
00037         data.second = true;
00038         return *this;
00039     }
00040 
00045     T& operator()() { return data.first; }
00046 
00051     T operator()() const { return data.first; }
00052 
00057     bool set() const { return data.second; }
00058 
00063     bool& set() { return data.second; }
00064 
00065 private:
00066     std::pair<T, bool> data;
00067 };
00068 
00069 }  // namespace msp
00070 
00071 template <class T>
00072 inline std::ostream& operator<<(std::ostream& s, const msp::Value<T>& val) {
00073     if(val.set())
00074         s << val();
00075     else
00076         s << "<unset>";
00077     return s;
00078 }
00079 
00080 template <>
00081 inline std::ostream& operator<<(std::ostream& s,
00082                                 const msp::Value<uint8_t>& val) {
00083     if(val.set())
00084         s << uint32_t(val());
00085     else
00086         s << "<unset>";
00087     return s;
00088 }
00089 
00090 template <>
00091 inline std::ostream& operator<<(std::ostream& s,
00092                                 const msp::Value<int8_t>& val) {
00093     if(val.set())
00094         s << int32_t(val());
00095     else
00096         s << "<unset>";
00097     return s;
00098 }
00099 
00100 #endif


msp
Author(s): Christian Rauch
autogenerated on Thu Jun 20 2019 19:40:38