/* The software in this package is distributed under the GNU General Public License version 2 (with a special exception described below). A copy of GNU General Public License (GPL) is included in this distribution, in the file COPYING.GPL. As a special exception, if other files instantiate templates or use macros or inline functions from this file, or you compile this file and link it with other works to produce a work based on this file, this file does not by itself cause the resulting work to be covered by the GNU General Public License. However the source code for this file must still be made available in accordance with section (3) of the GNU General Public License. This exception does not invalidate any other reasons why a work based on this file might be covered by the GNU General Public License. */ /** @file * @brief Implementation of intuitive tristate logic * * (c) Copyright 2011 by Intra2net AG * Written by Erich Willems and Thomas Jarosch */ #include namespace I2n { /** * @brief Constructor * Default to Undef state * **/ Tribool::Tribool() : Value(Undef) { } /** * @brief Constructor * * @param v Initial state **/ Tribool::Tribool(const State &v) : Value(v) { } /** * @brief Copy constructor * * @param src Class to copy the value from **/ Tribool::Tribool(const Tribool &src) : Value(src.Value) { } /** * @brief Assignment operator * * @param v New state * @return :Tribool& Pointer to this class **/ Tribool& Tribool::operator= (const State &v) { Value=v; return *this; } /** * @brief Assignment operator * * @param src Class to copy the state from * @return :Tribool& Pointer to this class **/ Tribool& Tribool::operator= (const Tribool& src) { Value=src.Value; return *this; } //lint !e1529 /** * @brief Element comparator * * @param x Class to compare against * @return bool True if the are in 100% the same state, false otherwise * Will return true if both elements are "Undef". **/ bool Tribool::operator==(const Tribool& x) const { return Value==x.Value; } /** * @brief Element comparator, NOT version * * @param x Class to compare against * @return bool True if they are not in the same state, false otherwise **/ bool Tribool::operator!=(const Tribool& x) const { return Value!=x.Value; } /** * @brief Logical NOT operator - operating in tristate space * * @return :Tribool Tribool state as the result **/ Tribool Tribool::operator !() const { enum State rtn = Undef; switch (Value) { case True: rtn = False; break; case False: rtn = True; break; case Undef: default: break; } //lint !e787 return rtn; } /** * @brief Logical OR - operating in tristate space * * @param x Object to compare against * @return :Tribool Tribool state as result **/ Tribool Tribool::operator ||(const Tribool& x) const //lint !e1753 { if ((True==Value) || (True==x.Value)) return True; if ((False==Value) && (False==x.Value)) return False; return Undef; } /** * @brief Logical AND operator - operating in tristate space * * @param x Object to compare against * @return :Tribool Tristate state as result **/ Tribool Tribool::operator &&(const Tribool& x) const //lint !e1753 { if ((True==Value) && (True==x.Value)) return True; if ((False==Value) || (False==x.Value)) return False; return Undef; } /** * @brief Constructor with bool conversion * * @param v Initial value in bistate space: true or false **/ Tribool::Tribool(const bool &v) { if (v == true) Value = True; else Value = False; } /** * @brief Assignment operator * * @param v Value in bistate space: true or false * @return :Tribool& Pointer to this class **/ Tribool& Tribool::operator=(const bool &v) { if (v == true) Value = True; else Value = False; return *this; } /** * @brief Element comparision for easier access in bistate space * * This is lossy! * * @param v Boolean value to compare against * @return bool True if it's exact the bool value, false otherwise. * Undef state will return false. **/ bool Tribool::is_exact(const bool &v) const { if (Value==Undef) return false; if (v == true && Value == True) return true; else if(v == false && Value == False) return true; return false; } }