Orxonox  0.0.5 Codename: Arcturus
Public Member Functions | Private Attributes | List of all members
orxonox::mbool Struct Reference

mbool is a small helper class that acts like a bool, but keeps track of the number of its state changes. More...

#include </home/jenkins/workspace/orxonox_doxygen_trunk/src/libraries/util/mbool.h>

Public Member Functions

constexpr mbool (bool value=false)
 Constructor: Creates the mbool and initializes the boolean value (default to false). More...
 
constexpr mbool (const mbool &value)
 Copy-constructor, copies state and memory. More...
 
 ~mbool ()=default
 Destructor does nothing but not defining it might create a symbol (class is header only) More...
 
unsigned char & getMemory ()
 Returns the memory value. More...
 
constexpr operator bool () const
 Implicitly converts the mbool to a bool. More...
 
constexpr bool operator! () const
 Returns the inverted state of the bool (doesn't change the internal state). More...
 
constexpr bool operator!= (bool other) const
 Compares the mbool to a bool, returns true if the bool has a different value than the state of the mbool. More...
 
constexpr bool operator!= (const mbool &other) const
 Compares two mbools, returns true if they have a different memory value. More...
 
mbooloperator++ ()
 Increases the memory which also inverts it's state (++mbool). More...
 
mbool operator++ (int)
 Increases the memory which also inverts it's state (mbool++). More...
 
mbooloperator= (bool value)
 Assigns a boolean value (and increases the memory value if the value is different to the old value). More...
 
mbooloperator= (const mbool &value)
 Assigns another mbool, copies state and memory. More...
 
constexpr bool operator== (bool other) const
 Compares the mbool to a bool, returns true if the bool has the same value as the state of the mbool. More...
 
constexpr bool operator== (const mbool &other) const
 Compares two mbools, returns true if their memory matches. More...
 

Private Attributes

union {
   bool   bool_: 1
 The boolean state of the mbool, is located on the first bit of the memory variable. More...
 
   unsigned char   memory_
 The memory of the mbool, counts the state-changes (and the first bit represents also the boolean value) More...
 
value_
 A union containing the state and the memory of the mbool. More...
 

Detailed Description

mbool is a small helper class that acts like a bool, but keeps track of the number of its state changes.

The mbool class acts like a bool, but it has an internal counter that counts the number state changes (i.e. when the bool changes from true to false or back). This is used in the network if a boolean value is synchronized, because if a value changes quickly from false to true and back in the same tick, the clients will never be notified of this action. By using mbool however this behaviour is fixed, which is important for triggers and other objects.

Note
This is efficiently solved by using a union that combines a counter and a boolean bitfield of size 1. The boolean value corresponds always to the first bit of the counter - this means, if the counter is incremented, the boolean state changes. On the other hand, if you want to change the state, you can simply increase the counter.

Constructor & Destructor Documentation

constexpr orxonox::mbool::mbool ( bool  value = false)
inline

Constructor: Creates the mbool and initializes the boolean value (default to false).

constexpr orxonox::mbool::mbool ( const mbool value)
inline

Copy-constructor, copies state and memory.

orxonox::mbool::~mbool ( )
inlinedefault

Destructor does nothing but not defining it might create a symbol (class is header only)

Member Function Documentation

unsigned char& orxonox::mbool::getMemory ( )
inline

Returns the memory value.

constexpr orxonox::mbool::operator bool ( ) const
inline

Implicitly converts the mbool to a bool.

constexpr bool orxonox::mbool::operator! ( ) const
inline

Returns the inverted state of the bool (doesn't change the internal state).

constexpr bool orxonox::mbool::operator!= ( bool  other) const
inline

Compares the mbool to a bool, returns true if the bool has a different value than the state of the mbool.

constexpr bool orxonox::mbool::operator!= ( const mbool other) const
inline

Compares two mbools, returns true if they have a different memory value.

mbool& orxonox::mbool::operator++ ( )
inline

Increases the memory which also inverts it's state (++mbool).

mbool orxonox::mbool::operator++ ( int  )
inline

Increases the memory which also inverts it's state (mbool++).

mbool& orxonox::mbool::operator= ( bool  value)
inline

Assigns a boolean value (and increases the memory value if the value is different to the old value).

mbool& orxonox::mbool::operator= ( const mbool value)
inline

Assigns another mbool, copies state and memory.

constexpr bool orxonox::mbool::operator== ( bool  other) const
inline

Compares the mbool to a bool, returns true if the bool has the same value as the state of the mbool.

constexpr bool orxonox::mbool::operator== ( const mbool other) const
inline

Compares two mbools, returns true if their memory matches.

Member Data Documentation

bool orxonox::mbool::bool_

The boolean state of the mbool, is located on the first bit of the memory variable.

unsigned char orxonox::mbool::memory_

The memory of the mbool, counts the state-changes (and the first bit represents also the boolean value)

union { ... } orxonox::mbool::value_

A union containing the state and the memory of the mbool.


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