tribool.hxx

Go to the documentation of this file.
00001 #pragma once
00002 #ifndef OPENGM_TRIBOOL_HXX
00003 #define OPENGM_TRIBOOL_HXX
00004 
00005 namespace opengm {
00006 
00008    class Tribool
00009    {
00010    public:
00011       enum State {True=1, False=0, Maybe=-1};
00012 
00013       Tribool();
00014       Tribool(const Tribool&);
00015       template<class T>
00016          Tribool(const T);
00017       Tribool(Tribool::State state);
00018 
00019       Tribool& operator=(const Tribool&);
00020       template<class T>
00021          Tribool& operator=(T);
00022       Tribool& operator=(Tribool::State state);
00023 
00024       bool operator==(const bool a) const;
00025       template<class T>
00026          bool operator==(T a) const;
00027       bool operator!=(const bool a) const;
00028       operator bool() const;
00029       bool operator!() const;
00030       bool maybe() const;
00031       
00032       void operator&=(Tribool::State state);
00033 
00034    private:
00035       char value_;
00036       friend std::ostream& operator<<(std::ostream& out, const Tribool& t );
00037    };
00038 
00039    inline Tribool::Tribool()
00040    :  value_(Tribool::Maybe)
00041    {}
00042 
00043    inline Tribool::Tribool
00044    (
00045       const Tribool& val
00046    )
00047    :  value_(val.value_)
00048    {}
00049 
00050    inline Tribool::Tribool
00051    (
00052       Tribool::State state
00053    )
00054    :  value_(state)
00055    {}
00056 
00057    template<class T>
00058    inline Tribool::Tribool
00059    (
00060       const T val
00061    )
00062    :  value_(static_cast<char>(val) == Tribool::Maybe 
00063              ? Tribool::Maybe 
00064              : static_cast<char>(static_cast<bool>(val)))
00065    {}
00066 
00067    inline Tribool& 
00068    Tribool::operator=
00069    (
00070       const Tribool& rhs
00071    )
00072    {
00073       if(this != &rhs) {
00074          value_ = rhs.value_;
00075       }
00076       return *this;
00077    }
00078 
00079    template<class T>
00080    inline Tribool& 
00081    Tribool::operator=
00082    (
00083       const T val
00084    )
00085    {
00086       static_cast<char>(val) == Tribool::Maybe 
00087          ? value_ = Tribool::Maybe 
00088          : value_ = static_cast<char>(static_cast<bool>(val));
00089       return *this;
00090    }
00091 
00092    inline Tribool& 
00093    Tribool::operator=
00094    (
00095       Tribool::State val
00096    )
00097    {
00098       value_ = static_cast<char>(val);
00099       return *this;
00100    }
00101 
00102    inline bool 
00103    Tribool::operator==
00104    (
00105       const bool a
00106    ) const
00107    {
00108       return bool( (value_ == Tribool::True && a == true)
00109          || (value_ == Tribool::False && a == false));
00110    }
00111 
00112    template<class T>
00113    inline bool 
00114    Tribool::operator==
00115    (
00116       T a
00117    ) const
00118    {
00119       return static_cast<char>(a) == value_;
00120    }
00121 
00122    inline bool 
00123    Tribool::operator!=
00124    (
00125       const bool a
00126    ) const
00127    {
00128       return (value_ != Tribool::True && a == true)
00129          || (value_ != Tribool::True && a == false);
00130    }
00131 
00132    inline Tribool::operator bool() const
00133    {
00134       return value_ == Tribool::True;
00135    }
00136 
00137    inline bool 
00138    Tribool::operator!() const
00139    {
00140       return value_ == Tribool::False;
00141    }
00142 
00143    inline bool 
00144    Tribool::maybe() const
00145    {
00146       return value_ == Tribool::Maybe;
00147    }
00148 
00149    inline std::ostream& 
00150    operator<<
00151    (
00152       std::ostream& out, 
00153       const Tribool& t
00154    )
00155    {
00156       out << static_cast<int>(t.value_);
00157       return out;
00158    } 
00159 
00160    inline void 
00161    Tribool::operator&=(Tribool::State state)
00162    {
00163       if(state==Tribool::True && value_!=Tribool::False) value_=Tribool::True;
00164       if(state==Tribool::False) value_=Tribool::False;                
00165    }
00166 
00167 } // namespace opengm
00168 
00169 #endif // #ifndef OPENGM_TRIBOOL_HXX
00170 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Mon Jun 17 16:31:06 2013 for OpenGM by  doxygen 1.6.3