kexi

tristate Class Reference

#include <tristate.h>

Public Member Functions

 tristate ()
 
 tristate (bool boolValue)
 
 tristate (char c)
 
 tristate (int intValue)
 
bool operator! () const
 
bool operator~ () const
 
tristate & operator= (const tristate &tsValue)
 
TQString toString () const
 

Friends

bool operator== (bool boolValue, tristate tsValue)
 
bool operator== (tristate tsValue, bool boolValue)
 
bool operator!= (bool boolValue, tristate tsValue)
 
bool operator!= (tristate tsValue, bool boolValue)
 

Detailed Description

3-state logical type with three values: true, false and cancelled and convenient operators.

cancelled state can be also called dontKnow, it behaves as null in SQL. A main goal of this class is to improve readibility when there's a need for storing third, cancelled, state, especially in case C++ exceptions are not in use. With it, developer can forget about declaring a specific enum type having just three values: true, false, cancelled.

Objects of this class can be used with similar convenience as standard bool type:

  • use as return value when 'cancelled'
    tristate doSomething();
    3-state logical type with three values: true, false and cancelled and convenient operators.
    Definition tristate.h:100
  • convert from bool (1) or to bool (2)
    tristate t = true; //(1)
    setVisible(t); //(2)
  • clear comparisons
    tristate t = doSomething();
    if (t) doSomethingIfTrue();
    if (!t) doSomethingIfFalse();
    if (~t) doSomethingIfCancelled();

"! ~" can be used as "not cancelled".

With tristate class, developer can also forget about it's additional meaning and treat it just as a bool, if the third state is irrelevant to the current situation.

Other use for tristate class could be to allow cancellation within a callback function or a TQt slot. Example:

public slots:
void validateData(tristate& result);

Having the single parameter, signals and slots have still simple look. Developers can alter their code (by replacing 'bool& result' with 'tristate& result') in case when a possibility of canceling of, say, data provessing needs to be implemented. Let's say validateData() function uses a TQDialog to get some validation from a user. While TQDialog::Rejected is returned after cancellation of the validation process, the information about cancellation needs to be transferred up to a higher level of the program. Storing values of type TQDialog::DialogCode there could be found as unreadable, and casting these to int is not typesafe. With tristate class it's easier to make it obvious that cancellation should be taken into account.

Author
Jaroslaw Staniek

Definition at line 99 of file tristate.h.

Constructor & Destructor Documentation

◆ tristate() [1/4]

tristate::tristate ( )
inline

Default constructor, object has cancelled value set.

Definition at line 105 of file tristate.h.

◆ tristate() [2/4]

tristate::tristate ( bool  boolValue)
inline

Constructor accepting a boolean value.

Definition at line 113 of file tristate.h.

◆ tristate() [3/4]

tristate::tristate ( char  c)
inline

Constructor accepting a char value.

It is converted in the following way:

  • 2 -> cancelled
  • 1 -> true
  • other -> false

Definition at line 125 of file tristate.h.

◆ tristate() [4/4]

tristate::tristate ( int  intValue)
inline

Constructor accepting an integer value.

It is converted in the following way:

  • 2 -> cancelled
  • 1 -> true
  • other -> false

Definition at line 136 of file tristate.h.

Member Function Documentation

◆ operator!()

bool tristate::operator! ( ) const
inline

Casting to bool type with negation: true is only returned if the original tristate value is equal to false.

Definition at line 145 of file tristate.h.

◆ operator=()

tristate & tristate::operator= ( const tristate &  tsValue)
inline

Definition at line 153 of file tristate.h.

◆ operator~()

bool tristate::operator~ ( ) const
inline

Special casting to bool type: true is only returned if the original tristate value is equal to cancelled.

Definition at line 151 of file tristate.h.

◆ toString()

TQString tristate::toString ( ) const
inline
Returns
text representation of the value: "true", "false" or "cancelled".

Definition at line 166 of file tristate.h.

Friends And Related Symbol Documentation

◆ operator!= [1/2]

bool operator!= ( bool  boolValue,
tristate  tsValue 
)
friend

Inequality operator comparing a bool value boolValue and a tristate value tsValue.

Returns
false if both boolValue and tsValue are true or if both boolValue and tsValue are false. Else, returns true.

Definition at line 196 of file tristate.h.

◆ operator!= [2/2]

bool operator!= ( tristate  tsValue,
bool  boolValue 
)
friend

Inequality operator comparing a tristate value tsValue and a bool value boolValue.

See also
bool operator!=(bool boolValue, tristate tsValue)

Definition at line 206 of file tristate.h.

◆ operator== [1/2]

bool operator== ( bool  boolValue,
tristate  tsValue 
)
friend

Equality operator comparing a bool value boolValue and a tristate value tsValue.

Returns
true if both
  • both tsValue value and boolValue are true, or
  • both tsValue value and boolValue are false If the tristate value has value of cancelled, false is returned.

Definition at line 232 of file tristate.h.

◆ operator== [2/2]

bool operator== ( tristate  tsValue,
bool  boolValue 
)
friend

Equality operator comparing a tristate value tsValue and a bool value boolValue.

Returns
true if both
  • both tsValue value and boolValue are true, or
  • both tsValue value and boolValue are false If the tristate value has value of cancelled, false is returned.

Definition at line 219 of file tristate.h.


The documentation for this class was generated from the following file: