| 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 | |
| 27 | #include <tribool.hpp> |
| 28 | |
| 29 | namespace I2n |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | * @brief Constructor |
| 34 | * Default to Undef state |
| 35 | * |
| 36 | **/ |
| 37 | Tribool::Tribool() |
| 38 | : Value(Undef) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @brief Constructor |
| 44 | * |
| 45 | * @param v Initial state |
| 46 | **/ |
| 47 | Tribool::Tribool(const State &v) |
| 48 | : Value(v) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @brief Copy constructor |
| 54 | * |
| 55 | * @param src Class to copy the value from |
| 56 | **/ |
| 57 | Tribool::Tribool(const Tribool &src) |
| 58 | : Value(src.Value) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @brief Assignment operator |
| 64 | * |
| 65 | * @param v New state |
| 66 | * @return :Tribool& Pointer to this class |
| 67 | **/ |
| 68 | Tribool& Tribool::operator= (const State &v) |
| 69 | { |
| 70 | Value=v; |
| 71 | return *this; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @brief Assignment operator |
| 76 | * |
| 77 | * @param src Class to copy the state from |
| 78 | * @return :Tribool& Pointer to this class |
| 79 | **/ |
| 80 | Tribool& Tribool::operator= (const Tribool& src) |
| 81 | { |
| 82 | Value=src.Value; |
| 83 | return *this; |
| 84 | } //lint !e1529 |
| 85 | |
| 86 | /** |
| 87 | * @brief Element comparator |
| 88 | * |
| 89 | * @param x Class to compare against |
| 90 | * @return bool True if the are in 100% the same state, false otherwise |
| 91 | * Will return true if both elements are "Undef". |
| 92 | **/ |
| 93 | bool Tribool::operator==(const Tribool& x) const |
| 94 | { |
| 95 | return Value==x.Value; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @brief Element comparator, NOT version |
| 100 | * |
| 101 | * @param x Class to compare against |
| 102 | * @return bool True if they are not in the same state, false otherwise |
| 103 | **/ |
| 104 | bool Tribool::operator!=(const Tribool& x) const |
| 105 | { |
| 106 | return Value!=x.Value; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @brief Logical NOT operator - operating in tristate space |
| 111 | * |
| 112 | * @return :Tribool Tribool state as the result |
| 113 | **/ |
| 114 | Tribool Tribool::operator !() const |
| 115 | { |
| 116 | enum State rtn = Undef; |
| 117 | |
| 118 | switch (Value) |
| 119 | { |
| 120 | case True: |
| 121 | rtn = False; |
| 122 | break; |
| 123 | case False: |
| 124 | rtn = True; |
| 125 | break; |
| 126 | case Undef: |
| 127 | default: |
| 128 | break; |
| 129 | } //lint !e787 |
| 130 | |
| 131 | return rtn; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @brief Logical OR - operating in tristate space |
| 136 | * |
| 137 | * @param x Object to compare against |
| 138 | * @return :Tribool Tribool state as result |
| 139 | **/ |
| 140 | Tribool Tribool::operator ||(const Tribool& x) const //lint !e1753 |
| 141 | { |
| 142 | if ((True==Value) || (True==x.Value)) |
| 143 | return True; |
| 144 | |
| 145 | if ((False==Value) && (False==x.Value)) |
| 146 | return False; |
| 147 | |
| 148 | return Undef; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @brief Logical AND operator - operating in tristate space |
| 153 | * |
| 154 | * @param x Object to compare against |
| 155 | * @return :Tribool Tristate state as result |
| 156 | **/ |
| 157 | Tribool Tribool::operator &&(const Tribool& x) const //lint !e1753 |
| 158 | { |
| 159 | if ((True==Value) && (True==x.Value)) |
| 160 | return True; |
| 161 | |
| 162 | if ((False==Value) || (False==x.Value)) |
| 163 | return False; |
| 164 | |
| 165 | return Undef; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @brief Constructor with bool conversion |
| 170 | * |
| 171 | * @param v Initial value in bistate space: true or false |
| 172 | **/ |
| 173 | Tribool::Tribool(const bool &v) |
| 174 | { |
| 175 | if (v == true) |
| 176 | Value = True; |
| 177 | else |
| 178 | Value = False; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * @brief Assignment operator |
| 183 | * |
| 184 | * @param v Value in bistate space: true or false |
| 185 | * @return :Tribool& Pointer to this class |
| 186 | **/ |
| 187 | Tribool& Tribool::operator=(const bool &v) |
| 188 | { |
| 189 | if (v == true) |
| 190 | Value = True; |
| 191 | else |
| 192 | Value = False; |
| 193 | |
| 194 | return *this; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @brief Element comparision for easier access in bistate space |
| 199 | * |
| 200 | * This is lossy! |
| 201 | * |
| 202 | * @param v Boolean value to compare against |
| 203 | * @return bool True if it's exact the bool value, false otherwise. |
| 204 | * Undef state will return false. |
| 205 | **/ |
| 206 | bool Tribool::is_exact(const bool &v) const |
| 207 | { |
| 208 | if (Value==Undef) |
| 209 | return false; |
| 210 | |
| 211 | if (v == true && Value == True) |
| 212 | return true; |
| 213 | else if(v == false && Value == False) |
| 214 | return true; |
| 215 | |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | } |