All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
gears::utility::tribool Struct Reference

Implements Kleene's logic for three-state booleans. More...

Public Member Functions

constexpr tribool () noexcept
 Creates a tribool with an indeterminate value.
 
constexpr tribool (indeterminate_t) noexcept
 Creates a tribool with an indeterminate value.
 
constexpr tribool (bool v) noexcept
 Creates a tribool with a boolean value.
 
constexpr operator bool () const noexcept
 Turns the tribool into a boolean. More...
 
constexpr tribool operator== (const tribool &rhs) const noexcept
 Checks if both tribool are equal. More...
 
constexpr tribool operator!= (const tribool &rhs) const noexcept
 Checks if both tribool are not equal. More...
 
constexpr tribool operator! () const noexcept
 Negates a tribool value. More...
 

Friends

constexpr bool is_true (const tribool &tri) noexcept
 Checks if the tribool is true. More...
 
constexpr bool is_false (const tribool &tri) noexcept
 Checks if the tribool is false. More...
 
constexpr bool is_indeterminate (const tribool &tri) noexcept
 Checks if the tribool is indeterminate. More...
 

Related Functions

(Note that these are not member functions.)

constexpr tribool operator&& (const tribool &lhs, const tribool &rhs) noexcept
 Computes the logical and of two tribool. More...
 
constexpr tribool operator|| (const tribool &lhs, const tribool &rhs) noexcept
 Computes the logical or of two tribool. More...
 
template<typename CharT >
constexpr const CharT * get_default_indeterminate_name () noexcept
 Returns the default indeterminate name. More...
 
template<typename CharT , typename Traits >
auto operator<< (std::basic_ostream< CharT, Traits > &out, const tribool &tri) -> decltype(out)
 Outputs the value of the tribool to the stream provided. More...
 
template<typename CharT , typename Traits >
auto operator>> (std::basic_istream< CharT, Traits > &in, tribool &tri) -> decltype(in)
 Reads a tribool value from an input stream. More...
 

Detailed Description

Implements Kleene's logic for three-state booleans. This class should be preferred to gears::utility::maybe<bool> as it implements true three-state logic. The three states for the boolean are true, false, and indeterminate. So for example all three are valid:

namespace util = gears::utility;
util::tribool x = true;
x = false;

The input and output operators are supported by default. In order to disable it, you have to define the macro GEARS_NO_IOSTREAM. The input/output operators behave similarly to bool values, except indeterminate can be read or output as 2. When std::boolalpha is active, they can be read and written in the same similar manner except the default string would be "indeterminate". For example:

namespace util = gears::utility;
util::tribool x = util::indeterminate;
std::cout << std::boolalpha << x << '\n';

Output:

indeterminate

You can change the indeterminate value through instantiating a value of type indeterminate_t. Likewise, you can change the default string read or written through the use of the indeterminate_name facet. For example:

namespace util = gears::utility;
constexpr util::indeterminate_t null{};
util::tribool x = null; // defined our own "keyword"
// change the default string to null
std::locale myloc(std::cout.getloc(), new util::indeterminate_name<char>("null"));
std::cout.imbue(myloc);
std::cout << std::boolalpha << x << '\n';

Output:

null

Definition at line 99 of file tribool.hpp.

Member Function Documentation

constexpr gears::utility::tribool::operator bool ( ) const
inlineexplicitnoexcept
Returns
true if the tribool stores true, false otherwise.

Definition at line 124 of file tribool.hpp.

constexpr tribool gears::utility::tribool::operator! ( ) const
inlinenoexcept

Negates a tribool value based on the table below.

! Result
true false
false true
indeterminate indeterminate
Returns
A tribool based on the table above.

Definition at line 207 of file tribool.hpp.

constexpr tribool gears::utility::tribool::operator!= ( const tribool rhs) const
inlinenoexcept

Checks if both tribool are not equal based on the following truth table.

!= true false indeterminate
true false true indeterminate
false true false indeterminate
indeterminate indeterminate indeterminate indeterminate
Parameters
rhsOther tribool to check.
Returns
A tribool according to the table above.

Definition at line 188 of file tribool.hpp.

constexpr tribool gears::utility::tribool::operator== ( const tribool rhs) const
inlinenoexcept

Checks if both tribool are equal based on the following truth table.

== true false indeterminate
true true false indeterminate
false false true indeterminate
indeterminate indeterminate indeterminate indeterminate
Parameters
rhsOther tribool to check.
Returns
A tribool according to the table above.

Definition at line 169 of file tribool.hpp.

Friends And Related Function Documentation

template<typename CharT >
constexpr const CharT * get_default_indeterminate_name ( )
related

Returns the default indeterminate name based on the character type provided. Only char and wchar_t specialisations are provided. The default value is the string "indeterminate".

This function is only available if GEARS_NO_IOSTREAM is not defined.

Template Parameters
CharTThe character type of the string.
Returns
The default indeterminate name, "indeterminate".
constexpr bool is_false ( const tribool tri)
friend
Parameters
triThe tribool to check.
Returns
true if the tribool stores false, false otherwise.

Definition at line 142 of file tribool.hpp.

constexpr bool is_indeterminate ( const tribool tri)
friend
Parameters
triThe tribool to check.
Returns
true if the tribool stores indeterminate, false otherwise.

Definition at line 151 of file tribool.hpp.

constexpr bool is_true ( const tribool tri)
friend
Parameters
triThe tribool to check.
Returns
true if the tribool stores true, false otherwise.

Definition at line 133 of file tribool.hpp.

constexpr tribool operator&& ( const tribool lhs,
const tribool rhs 
)
related

Computes the logical and of two tribool. Due to the nature of C++'s short circuiting, no short circuiting is done through overloading of this operators. The value returned is based on the following table.

AND true false indeterminate
true true false indeterminate
false false false false
indeterminate indeterminate false indeterminate
Parameters
lhsLeft hand side of the expression.
rhsRight hand side of the expression.
Returns
A tribool based on the truth table above.

Definition at line 231 of file tribool.hpp.

template<typename CharT , typename Traits >
auto operator<< ( std::basic_ostream< CharT, Traits > &  out,
const tribool tri 
) -> decltype(out)
related

Outputs the value of the tribool to the stream provided.

The value written when std::boolalpha is not active is as follows:

internal value output
false 0
true 1
indeterminate 2

If std::boolalpha is written then the name written to depends on the facet provided. The true and false values are provided by the std::numpunct functions that implement do_truename and do_falsename. The indeterminate value is provided by the indeterminate_name facet.

Assuming everything is defaulted, the values written are as follows:

internal value output
false "false"
true "true"
indeterminate "indeterminate"

This operator overload is only available if GEARS_NO_IOSTREAM is not defined.

Parameters
outOutput stream to write to.
triTribool to read from.
Returns
The output stream.

Definition at line 381 of file tribool.hpp.

template<typename CharT , typename Traits >
auto operator>> ( std::basic_istream< CharT, Traits > &  in,
tribool tri 
) -> decltype(in)
related

Reads a tribool value from an input stream. This function works similarly to the output stream except inverted. That is, if std::boolalpha is not active, then the values of 0, 1, and 2 will set the tribool appropriately otherwise failure will be set.

If std::boolalpha is set then the string is read from the appropriate facets. Which are std::numpunct for true and false and indeterminate_name for the indeterminate value.

This operator overload is only available if GEARS_NO_IOSTREAM is not defined.

Parameters
inThe input stream to read from.
triThe tribool to place the value to.
Returns
The input stream.

Definition at line 425 of file tribool.hpp.

constexpr tribool operator|| ( const tribool lhs,
const tribool rhs 
)
related

Computes the logical or of two tribool. Due to the nature of C++'s short circuiting, no short circuiting is done through overloading of this operators. The value returned is based on the following table.

OR true false indeterminate
true true true true
false true false indeterminate
indeterminate true indeterminate indeterminate
Parameters
lhsLeft hand side of the expression.
rhsRight hand side of the expression.
Returns
A tribool based on the truth table above.

Definition at line 263 of file tribool.hpp.