Changeset 7401 for code/trunk/src/libraries/util/mbool.h
- Timestamp:
- Sep 11, 2010, 12:34:00 AM (14 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/doc (added) merged: 7290-7292,7296-7300,7302-7304,7306-7312,7315-7318,7323,7325,7327,7331-7332,7334-7335,7345-7347,7352-7353,7356-7357,7361,7363-7367,7371-7375,7388
- Property svn:mergeinfo changed
-
code/trunk/src/libraries/util/mbool.h
r7268 r7401 27 27 */ 28 28 29 /** 30 @file 31 @ingroup Util 32 @brief Declaration and implementation of the @ref orxonox::mbool class. 33 */ 34 29 35 #ifndef _mbool_H__ 30 36 #define _mbool_H__ … … 34 40 namespace orxonox 35 41 { 42 /** 43 @brief mbool is a small helper class that acts like a bool, but keeps track of the number of its state changes. 44 45 The mbool class acts like a bool, but it has an internal counter that counts 46 the number state changes (i.e. when the bool changes from true to false or 47 back). This is used in the network if a boolean value is synchronized, because 48 if a value changes quickly from false to true and back in the same tick, the 49 clients will never be notified of this action. By using mbool however this 50 behaviour is fixed, which is important for triggers and other objects. 51 52 @note This is efficiently solved by using a union that combines a counter and a 53 boolean bitfield of size 1. The boolean value corresponds always to the first 54 bit of the counter - this means, if the counter is incremented, the boolean state 55 changes. On the other hand, if you want to change the state, you can simply increase 56 the counter. 57 */ 36 58 struct _UtilExport mbool 37 59 { 38 60 public: 61 /// Constructor: Creates the mbool and initializes the boolean value (default to false). 39 62 inline mbool(bool value = false) 40 63 { this->value_.memory_ = 0; this->value_.bool_ = value; } 64 /// Copy-constructor, copies state and memory. 41 65 inline mbool(const mbool& value) 42 66 { this->value_.memory_ = value.value_.memory_; } 43 67 68 /// Assigns a boolean value (and increases the memory value if the value is different to the old value). 44 69 inline mbool& operator=(bool value) 45 70 { if (value != this->value_.bool_) { ++this->value_.memory_; } return (*this); } 71 /// Assigns another mbool, copies state and memory. 46 72 inline mbool& operator=(const mbool& value) 47 73 { this->value_.memory_ = value.value_.memory_; return (*this); } 48 74 75 /// Increases the memory which also inverts it's state (++mbool). 49 76 inline mbool& operator++() 50 77 { ++this->value_.memory_; return (*this); } 51 inline mbool operator++(int i) 78 /// Increases the memory which also inverts it's state (mbool++). 79 inline mbool operator++(int) 52 80 { mbool temp = (*this); ++this->value_.memory_; return temp; } 53 81 82 /// Implicitly converts the mbool to a bool. 54 83 inline operator bool() const 55 84 { return this->value_.bool_; } 56 85 86 /// Compares the mbool to a bool, returns true if the bool has the same value as the state of the mbool. 57 87 inline bool operator==(bool other) const 58 88 { return this->value_.bool_ == other; } 89 /// Compares the mbool to a bool, returns true if the bool has a different value than the state of the mbool. 59 90 inline bool operator!=(bool other) const 60 91 { return this->value_.bool_ != other; } 61 92 93 /// Compares two mbools, returns true if their memory matches. 62 94 inline bool operator==(const mbool& other) const 63 95 { return this->value_.memory_ == other.value_.memory_; } 96 /// Compares two mbools, returns true if they have a different memory value. 64 97 inline bool operator!=(const mbool& other) const 65 98 { return this->value_.memory_ != other.value_.memory_; } 66 99 100 /// Returns the inverted state of the bool (doesn't change the internal state). 67 101 inline bool operator!() const 68 102 { return (!this->value_.bool_); } 69 103 104 /// Returns the memory value. 70 105 inline unsigned char& getMemory(){ return value_.memory_; } 71 106 … … 73 108 union 74 109 { 75 bool bool_ : 1; 76 unsigned char memory_; 77 } value_; 110 bool bool_ : 1; ///< The boolean state of the mbool, is located on the first bit of the memory variable 111 unsigned char memory_; ///< The memory of the mbool, counts the state-changes (and the first bit represents also the boolean value) 112 } value_; ///< A union containing the state and the memory of the mbool 78 113 }; 79 114 }
Note: See TracChangeset
for help on using the changeset viewer.