Imported Upstream version 17.10.1
[platform/upstream/libzypp.git] / zypp / TriBool.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/TriBool.h
10  *
11 */
12 #ifndef ZYPP_TRIBOOL_H
13 #define ZYPP_TRIBOOL_H
14
15 #include <iosfwd>
16 #include <string>
17 #include <boost/logic/tribool.hpp>
18
19 ///////////////////////////////////////////////////////////////////
20 namespace zypp
21 { /////////////////////////////////////////////////////////////////
22
23   /** 3-state boolean logic (\c true, \c false and \c indeterminate).
24    * \code
25    * namespace zypp
26    * {
27    *   typedef boost::logic::tribool TriBool;
28    *   using   boost::logic::tribool;
29    *   using   boost::logic::indeterminate;
30    * }
31    * \endcode
32    *
33    * \warning Be carefull.esp. when comparing \ref TriBool using
34    * \c operator==, as <b><tt>( indeterminate == indeterminate )</tt></b>
35    * does \b not evaluate \b true. It's \c indeterminate.
36    *
37    * \see http://www.boost.org/doc/html/tribool.html
38    * \ingroup BOOST
39   */
40   typedef boost::logic::tribool TriBool;
41   using   boost::logic::tribool;
42   using   boost::logic::indeterminate;
43
44   inline std::string asString( const TriBool & val_r, const std::string & istr_r = std::string(),
45                                                       const std::string & tstr_r = std::string(),
46                                                       const std::string & fstr_r = std::string() )
47   {
48     std::string ret;
49     if (indeterminate(val_r))
50       ret = ( istr_r.empty() ? "indeterminate" : istr_r );
51     else if (val_r)
52       ret = ( tstr_r.empty() ? "true" : tstr_r );
53     else
54       ret = ( fstr_r.empty() ? "false" : fstr_r );
55     return ret;
56   }
57
58   /////////////////////////////////////////////////////////////////
59 } // namespace zypp
60 ///////////////////////////////////////////////////////////////////
61 namespace boost
62 {
63     namespace logic
64     {
65       /** \relates TriBool stream output */
66       inline std::ostream & operator<<(std::ostream & s, const tribool & obj)
67       { return s << zypp::asString( obj ); }
68
69       /** \relates TriBool whether 2 tribool have the same state (this is NOT ==) */
70       inline bool sameTriboolState( tribool lhs, tribool rhs )
71       { return( ( indeterminate(lhs) && indeterminate(rhs) ) || ( bool )( lhs == rhs ) ); }
72     }
73 }
74 #endif // ZYPP_TRIBOOL_H