00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef MTL_MAYBE_INCLUDE
00013 #define MTL_MAYBE_INCLUDE
00014
00015 #include <iostream>
00016
00017 namespace mtl { namespace utilities {
00018
00019 template <class Value>
00020 struct maybe : public std::pair<Value, bool>
00021 {
00022 typedef std::pair<Value, bool> base;
00023 typedef maybe<Value> self;
00024
00025 maybe(bool b) : base(Value(), b) {}
00026 maybe(Value v) : base(v, true) {}
00027 maybe(Value v, bool b) : base(v, b) {}
00028 maybe(base b) : base(b) {}
00029
00030 operator bool() const
00031 {
00032 return this->second;
00033 }
00034 operator Value() const
00035 {
00036 return this->first;
00037 }
00038 bool has_value() const
00039 {
00040 return this->second;
00041 }
00042 Value value() const
00043 {
00044 return this->first;
00045 }
00046 };
00047
00048 template <class Value>
00049 inline std::ostream& operator<< (std::ostream& os, maybe<Value> const& m)
00050 {
00051 return os << '(' << m.value() << ", " << (m ? "true" : "false") << ')';
00052 }
00053
00054 }
00055
00056 using utilities::maybe;
00057
00058 }
00059
00060 #endif // MTL_MAYBE_INCLUDE