Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 11, 2010, 12:34:00 AM (14 years ago)
Author:
landauf
Message:

merged doc branch back to trunk

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/libraries/util/mbool.h

    r7268 r7401  
    2727 */
    2828
     29/**
     30    @file
     31    @ingroup Util
     32    @brief Declaration and implementation of the @ref orxonox::mbool class.
     33*/
     34
    2935#ifndef _mbool_H__
    3036#define _mbool_H__
     
    3440namespace orxonox
    3541{
     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    */
    3658    struct _UtilExport mbool
    3759    {
    3860        public:
     61            /// Constructor: Creates the mbool and initializes the boolean value (default to false).
    3962            inline mbool(bool value = false)
    4063                { this->value_.memory_ = 0; this->value_.bool_ = value; }
     64            /// Copy-constructor, copies state and memory.
    4165            inline mbool(const mbool& value)
    4266                { this->value_.memory_ = value.value_.memory_; }
    4367
     68            /// Assigns a boolean value (and increases the memory value if the value is different to the old value).
    4469            inline mbool& operator=(bool value)
    4570                { if (value != this->value_.bool_) { ++this->value_.memory_; } return (*this); }
     71            /// Assigns another mbool, copies state and memory.
    4672            inline mbool& operator=(const mbool& value)
    4773                { this->value_.memory_ = value.value_.memory_; return (*this); }
    4874
     75            /// Increases the memory which also inverts it's state (++mbool).
    4976            inline mbool& operator++()
    5077                { ++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)
    5280                { mbool temp = (*this); ++this->value_.memory_; return temp; }
    5381
     82            /// Implicitly converts the mbool to a bool.
    5483            inline operator bool() const
    5584                { return this->value_.bool_; }
    5685
     86            /// Compares the mbool to a bool, returns true if the bool has the same value as the state of the mbool.
    5787            inline bool operator==(bool other) const
    5888                { 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.
    5990            inline bool operator!=(bool other) const
    6091                { return this->value_.bool_ != other; }
    6192
     93            /// Compares two mbools, returns true if their memory matches.
    6294            inline bool operator==(const mbool& other) const
    6395                { return this->value_.memory_ == other.value_.memory_; }
     96            /// Compares two mbools, returns true if they have a different memory value.
    6497            inline bool operator!=(const mbool& other) const
    6598                { return this->value_.memory_ != other.value_.memory_; }
    6699
     100            /// Returns the inverted state of the bool (doesn't change the internal state).
    67101            inline bool operator!() const
    68102                { return (!this->value_.bool_); }
    69103
     104            /// Returns the memory value.
    70105            inline unsigned char& getMemory(){ return value_.memory_; }
    71106
     
    73108            union
    74109            {
    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
    78113    };
    79114}
Note: See TracChangeset for help on using the changeset viewer.