[2155] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[7401] | 29 | /** |
---|
| 30 | @file |
---|
| 31 | @ingroup Util |
---|
| 32 | @brief Declaration and implementation of the @ref orxonox::mbool class. |
---|
| 33 | */ |
---|
| 34 | |
---|
[2155] | 35 | #ifndef _mbool_H__ |
---|
| 36 | #define _mbool_H__ |
---|
| 37 | |
---|
| 38 | #include "UtilPrereqs.h" |
---|
| 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
[7401] | 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 | */ |
---|
[8400] | 58 | struct mbool |
---|
[2155] | 59 | { |
---|
| 60 | public: |
---|
[7401] | 61 | /// Constructor: Creates the mbool and initializes the boolean value (default to false). |
---|
[2155] | 62 | inline mbool(bool value = false) |
---|
| 63 | { this->value_.memory_ = 0; this->value_.bool_ = value; } |
---|
[7401] | 64 | /// Copy-constructor, copies state and memory. |
---|
[2155] | 65 | inline mbool(const mbool& value) |
---|
| 66 | { this->value_.memory_ = value.value_.memory_; } |
---|
[8400] | 67 | /// Destructor does nothing but not defining it might create a symbol (class is header only) |
---|
| 68 | inline ~mbool() |
---|
| 69 | { } |
---|
[2155] | 70 | |
---|
[7401] | 71 | /// Assigns a boolean value (and increases the memory value if the value is different to the old value). |
---|
[7268] | 72 | inline mbool& operator=(bool value) |
---|
[2155] | 73 | { if (value != this->value_.bool_) { ++this->value_.memory_; } return (*this); } |
---|
[7401] | 74 | /// Assigns another mbool, copies state and memory. |
---|
[7268] | 75 | inline mbool& operator=(const mbool& value) |
---|
[2155] | 76 | { this->value_.memory_ = value.value_.memory_; return (*this); } |
---|
| 77 | |
---|
[7401] | 78 | /// Increases the memory which also inverts it's state (++mbool). |
---|
[2155] | 79 | inline mbool& operator++() |
---|
| 80 | { ++this->value_.memory_; return (*this); } |
---|
[7401] | 81 | /// Increases the memory which also inverts it's state (mbool++). |
---|
| 82 | inline mbool operator++(int) |
---|
[2155] | 83 | { mbool temp = (*this); ++this->value_.memory_; return temp; } |
---|
| 84 | |
---|
[7401] | 85 | /// Implicitly converts the mbool to a bool. |
---|
[2155] | 86 | inline operator bool() const |
---|
| 87 | { return this->value_.bool_; } |
---|
| 88 | |
---|
[7401] | 89 | /// Compares the mbool to a bool, returns true if the bool has the same value as the state of the mbool. |
---|
[2155] | 90 | inline bool operator==(bool other) const |
---|
| 91 | { return this->value_.bool_ == other; } |
---|
[7401] | 92 | /// Compares the mbool to a bool, returns true if the bool has a different value than the state of the mbool. |
---|
[2155] | 93 | inline bool operator!=(bool other) const |
---|
| 94 | { return this->value_.bool_ != other; } |
---|
| 95 | |
---|
[7401] | 96 | /// Compares two mbools, returns true if their memory matches. |
---|
[2155] | 97 | inline bool operator==(const mbool& other) const |
---|
| 98 | { return this->value_.memory_ == other.value_.memory_; } |
---|
[7401] | 99 | /// Compares two mbools, returns true if they have a different memory value. |
---|
[2155] | 100 | inline bool operator!=(const mbool& other) const |
---|
| 101 | { return this->value_.memory_ != other.value_.memory_; } |
---|
| 102 | |
---|
[7401] | 103 | /// Returns the inverted state of the bool (doesn't change the internal state). |
---|
[2155] | 104 | inline bool operator!() const |
---|
| 105 | { return (!this->value_.bool_); } |
---|
[6417] | 106 | |
---|
[7401] | 107 | /// Returns the memory value. |
---|
[2662] | 108 | inline unsigned char& getMemory(){ return value_.memory_; } |
---|
[2155] | 109 | |
---|
| 110 | private: |
---|
| 111 | union |
---|
| 112 | { |
---|
[7401] | 113 | bool bool_ : 1; ///< The boolean state of the mbool, is located on the first bit of the memory variable |
---|
| 114 | unsigned char memory_; ///< The memory of the mbool, counts the state-changes (and the first bit represents also the boolean value) |
---|
| 115 | } value_; ///< A union containing the state and the memory of the mbool |
---|
[2155] | 116 | }; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | #endif /* _mbool_H__ */ |
---|