From 49f91b40dabf14305266362abc325ec199744171 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Fri, 11 Mar 2011 16:35:29 +0100 Subject: [PATCH] Own implementation of intuitive tristate logic --- src/Makefile.am | 4 +- src/tribool.cpp | 195 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tribool.hpp | 49 ++++++++++++++ 3 files changed, 246 insertions(+), 2 deletions(-) create mode 100644 src/tribool.cpp create mode 100644 src/tribool.hpp diff --git a/src/Makefile.am b/src/Makefile.am index c3a14ed..1a7a023 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,12 +8,12 @@ include_HEADERS = cron.hpp daemonfunc.hpp exception.hxx filefunc.hxx \ i2n_configdata.hpp i2n_configfile.hpp insocketstream.hxx ip_type.hxx ipfunc.hxx \ log_macros.hpp logfunc.hpp logread.hxx oftmpstream.hxx pidfile.hpp pipestream.hxx \ pointer_func.hpp source_track_basics.hpp stringfunc.hxx timefunc.hxx tmpfstream.hpp \ - tmpfstream_impl.hpp tracefunc.hpp userfunc.hpp week.hpp + tmpfstream_impl.hpp tracefunc.hpp userfunc.hpp week.hpp tribool.hpp libi2ncommon_la_SOURCES = cron.cpp daemonfunc.cpp filefunc.cpp \ i2n_configfile.cpp ipfunc.cpp logfunc.cpp logread.cpp oftmpstream.cpp pidfile.cpp \ pointer_func.cpp source_track_basics.cpp stringfunc.cpp timefunc.cpp tmpfstream.cpp \ - tracefunc.cpp userfunc.cpp week.cpp + tracefunc.cpp userfunc.cpp week.cpp tribool.cpp # Note: If you specify a:b:c as the version in the next line, # the library that is made has version (a-c).c.b. In this diff --git a/src/tribool.cpp b/src/tribool.cpp new file mode 100644 index 0000000..f69e545 --- /dev/null +++ b/src/tribool.cpp @@ -0,0 +1,195 @@ +/** @file + * @brief Implementation of intuitive tristate logic + * + * (c) Copyright 2011 by Intra2net AG + * Written by Erich Willems and Thomas Jarosch + * + * info@intra2net.com + */ + +#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 +{ + switch (Value) + { + case True: + return False; + case False: + return True; + } //lint !e787 + + return Undef; +} + +/** + * @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; +} + +} diff --git a/src/tribool.hpp b/src/tribool.hpp new file mode 100644 index 0000000..adc3dba --- /dev/null +++ b/src/tribool.hpp @@ -0,0 +1,49 @@ +/** @file + * @brief Implementation of intuitive tristate logic + * + * (c) Copyright 2011 by Intra2net AG + * Written by Erich Willems and Thomas Jarosch + * + * After we had serious trouble with the current boost::logic::tribool implementation + * overloading the comparison operators, we rolled our own class. + * + * info@intra2net.com + */ + +namespace I2n +{ + +class Tribool +{ +public: + enum State { False, True, Undef }; + + Tribool(); + Tribool(const State &v); + Tribool(const Tribool& src); + + // set element + Tribool& operator= (const State &v); + Tribool& operator= (const Tribool& src); + + // Element comparison + bool operator==(const Tribool& v) const; + bool operator!=(const Tribool& v) const; + + // logic operators + Tribool operator !() const; + Tribool operator ||(const Tribool& x) const; + Tribool operator &&(const Tribool& x) const; + + // Bistate logic to tristate conversion + Tribool(const bool &v); + Tribool& operator= (const bool &v); + + // comparision with bool (bi) + bool is_exact(const bool &v) const; + +private: + State Value; +}; + +} -- 1.7.1