GY-63_MS5611/libraries/Troolean/Troolean.cpp

179 lines
3.0 KiB
C++
Raw Normal View History

2018-01-28 09:34:55 -05:00
//
// FILE: Troolean.cpp
// AUTHOR: Rob Tillaart
2022-12-02 08:12:22 -05:00
// VERSION: 0.1.7
2021-12-29 07:05:17 -05:00
// PURPOSE: Arduino Library for a three state logic data type supporting {true false unknown}
2020-11-27 05:33:55 -05:00
// URL: https://github.com/RobTillaart/Troolean
2021-12-29 07:05:17 -05:00
2018-01-28 09:34:55 -05:00
#include "Troolean.h"
/////////////////////////////////////////////////////
//
2022-11-26 10:50:57 -05:00
// PUBLIC
2018-01-28 09:34:55 -05:00
//
Troolean::Troolean()
{
_value = -1;
}
2021-12-29 07:05:17 -05:00
2018-01-28 09:34:55 -05:00
Troolean::Troolean(const int8_t val)
{
if (val == 0) _value = 0;
else if (val == -1) _value = -1;
else _value = 1;
}
2021-12-29 07:05:17 -05:00
2018-01-28 09:34:55 -05:00
Troolean::Troolean(const Troolean &t)
{
_value = t._value;
}
2021-12-29 07:05:17 -05:00
2022-11-26 10:50:57 -05:00
// PRINTING
2018-01-28 09:34:55 -05:00
size_t Troolean::printTo(Print& p) const
{
size_t n = 0;
if (_value == 0) n += p.print("false");
else if (_value == -1) n += p.print("unknown");
else n += p.print("true");
return n;
};
2021-12-29 07:05:17 -05:00
//////////////////////////////////////////////////
//
2022-11-26 10:50:57 -05:00
// EQUALITIES
// t == t
// f == f
// u == u
2021-12-29 07:05:17 -05:00
//
2018-01-28 09:34:55 -05:00
bool Troolean::operator == (const Troolean &t)
{
return (_value == t._value);
}
2021-12-29 07:05:17 -05:00
2018-01-28 09:34:55 -05:00
bool Troolean::operator == (const bool &b)
{
if (_value == 0 && !b) return true;
if (_value == 1 && b) return true;
return false;
}
2021-12-29 07:05:17 -05:00
2018-01-28 09:34:55 -05:00
bool Troolean::operator == (const int &i)
{
2022-12-02 08:12:22 -05:00
if (_value == 0 && i == 0) return true;
2018-01-28 09:34:55 -05:00
if (_value == -1 && i == -1) return true;
2022-12-02 08:12:22 -05:00
if (_value == 1 && i != 0 && i != -1) return true;
2018-01-28 09:34:55 -05:00
return false;
}
2021-12-29 07:05:17 -05:00
2018-01-28 09:34:55 -05:00
bool Troolean::operator != (const Troolean &t)
{
return (_value != t._value);
}
2021-12-29 07:05:17 -05:00
2018-01-28 09:34:55 -05:00
bool Troolean::operator != (const bool &b)
{
if (_value == 0 && !b) return false;
if (_value == 1 && b) return false;
return true;
}
2021-12-29 07:05:17 -05:00
2018-01-28 09:34:55 -05:00
bool Troolean::operator != (const int &i)
{
2022-12-02 08:12:22 -05:00
if (_value == 0 && i != 0) return true;
2018-01-28 09:34:55 -05:00
if (_value == -1 && i != -1) return true;
2022-12-02 08:12:22 -05:00
if (_value == 1 && (i == 0 || i == -1)) return true;
2018-01-28 09:34:55 -05:00
return false;
}
2021-12-29 07:05:17 -05:00
2018-01-28 09:34:55 -05:00
Troolean::operator bool() const
{
if (_value == 1) return true;
if (_value == 0) return false;
return false;
}
2021-12-29 07:05:17 -05:00
//////////////////////////////////////////////////////////
//
2022-11-26 10:50:57 -05:00
// NEGATE
// t -> f
// f -> t
// u -> u
2021-12-29 07:05:17 -05:00
//
2018-01-28 09:34:55 -05:00
Troolean Troolean::operator ! ()
{
2022-12-02 08:12:22 -05:00
if (_value == -1) return Troolean(-1);
2018-01-28 09:34:55 -05:00
if (_value == 1) return Troolean(0);
return Troolean(1);
}
2021-12-29 07:05:17 -05:00
//////////////////////////////////////////////////////////
//
2022-11-26 10:50:57 -05:00
// LOGICAL OPERATORS
2021-12-29 07:05:17 -05:00
//
2018-01-28 09:34:55 -05:00
Troolean Troolean::operator && (const Troolean &t)
{
if (_value == 0 || t._value == 0) return Troolean(0);
2022-12-02 08:12:22 -05:00
if (_value == -1 || t._value == -1) return Troolean(-1);
return Troolean(1);
2018-01-28 09:34:55 -05:00
}
2021-12-29 07:05:17 -05:00
2018-01-28 09:34:55 -05:00
Troolean Troolean::operator && (const bool &b)
{
if (_value == 0 || !b) return Troolean(0);
if (_value == 1 && b) return Troolean(1);
return Troolean(-1);
}
2021-12-29 07:05:17 -05:00
2018-01-28 09:34:55 -05:00
Troolean Troolean::operator || (const Troolean &t)
{
if (_value == 1 || t._value == 1) return Troolean(1);
if (_value == 0 && t._value == 0) return Troolean(0);
return Troolean(-1);
}
2021-12-29 07:05:17 -05:00
2018-01-28 09:34:55 -05:00
Troolean Troolean::operator || (const bool &b)
{
2022-12-02 08:12:22 -05:00
if (_value == 1 || b) return Troolean(1);
if (_value == 0 && !b) return Troolean(0);
2018-01-28 09:34:55 -05:00
return Troolean(-1);
}
2021-12-29 07:05:17 -05:00
2022-12-02 08:12:22 -05:00
bool Troolean::isTrue()
{
return ((_value != 0) && (_value != -1));
}
bool Troolean::isFalse()
{
return _value == 0;
}
bool Troolean::isUnknown()
{
return _value == -1;
}
2022-11-26 10:50:57 -05:00
// -- END OF FILE --
2021-12-29 07:05:17 -05:00