| 1 | /* |
| 2 | The software in this package is distributed under the GNU General |
| 3 | Public License version 2 (with a special exception described below). |
| 4 | |
| 5 | A copy of GNU General Public License (GPL) is included in this distribution, |
| 6 | in the file COPYING.GPL. |
| 7 | |
| 8 | As a special exception, if other files instantiate templates or use macros |
| 9 | or inline functions from this file, or you compile this file and link it |
| 10 | with other works to produce a work based on this file, this file |
| 11 | does not by itself cause the resulting work to be covered |
| 12 | by the GNU General Public License. |
| 13 | |
| 14 | However the source code for this file must still be made available |
| 15 | in accordance with section (3) of the GNU General Public License. |
| 16 | |
| 17 | This exception does not invalidate any other reasons why a work based |
| 18 | on this file might be covered by the GNU General Public License. |
| 19 | */ |
| 20 | /** @file |
| 21 | * @brief Implementation of intuitive tristate logic |
| 22 | * |
| 23 | * (c) Copyright 2011 by Intra2net AG |
| 24 | * Written by Erich Willems and Thomas Jarosch |
| 25 | * |
| 26 | * After we had serious trouble with the current boost::logic::tribool implementation |
| 27 | * overloading the comparison operators, we rolled our own class. |
| 28 | */ |
| 29 | |
| 30 | #ifndef __TRIBOOL_HPP |
| 31 | #define __TRIBOOL_HPP |
| 32 | |
| 33 | #include <iostream> |
| 34 | |
| 35 | namespace I2n |
| 36 | { |
| 37 | |
| 38 | class Tribool |
| 39 | { |
| 40 | public: |
| 41 | enum State { False, True, Undef }; |
| 42 | |
| 43 | Tribool(); |
| 44 | Tribool(const State &v); |
| 45 | Tribool(const Tribool& src); |
| 46 | |
| 47 | // set element |
| 48 | Tribool& operator= (const State &v); |
| 49 | Tribool& operator= (const Tribool& src); |
| 50 | |
| 51 | // Element comparison |
| 52 | bool operator==(const Tribool& v) const; |
| 53 | bool operator!=(const Tribool& v) const; |
| 54 | |
| 55 | // logic operators |
| 56 | Tribool operator !() const; |
| 57 | Tribool operator ||(const Tribool& x) const; |
| 58 | Tribool operator &&(const Tribool& x) const; |
| 59 | |
| 60 | // Bistate logic to tristate conversion |
| 61 | Tribool(const bool &v); |
| 62 | Tribool& operator= (const bool &v); |
| 63 | |
| 64 | // comparision with bool (bi) |
| 65 | bool is_exact(const bool &v) const; |
| 66 | |
| 67 | private: |
| 68 | State Value; |
| 69 | }; |
| 70 | |
| 71 | } |
| 72 | |
| 73 | namespace std |
| 74 | { |
| 75 | |
| 76 | // allow Tribools to be easily printed to ostreams |
| 77 | template <class charT, class traits> |
| 78 | inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, |
| 79 | const I2n::Tribool& x) |
| 80 | { |
| 81 | if (x == I2n::Tribool::False) |
| 82 | os << '0'; |
| 83 | else if (x == I2n::Tribool::True) |
| 84 | os << '1'; |
| 85 | else |
| 86 | os << '?'; |
| 87 | |
| 88 | return os; |
| 89 | } |
| 90 | |
| 91 | } |
| 92 | |
| 93 | #endif |