Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 11096 for code/trunk


Ignore:
Timestamp:
Jan 25, 2016, 9:33:11 PM (8 years ago)
Author:
muemart
Message:

Constexpr for some util things

Location:
code/trunk/src/libraries/util
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/libraries/util/OrxEnum.h

    r7401 r11096  
    6161    {
    6262        public:
    63             OrxEnum() { }
    64             OrxEnum(int type)            { type_ = type; }
    65             OrxEnum(const T& instance)   { type_ = instance.type_; }
     63            constexpr OrxEnum()                   : type_()                {}
     64            constexpr OrxEnum(int type)           : type_(type)            {}
     65            constexpr OrxEnum(const T& instance)  : type_(instance.type_)  {}
     66            constexpr OrxEnum(const OrxEnum& instance) = delete;
    6667
    67             operator int()               { return type_; }
    68             T& operator =(int type)      { type_ = type; return *this; }
    69             bool operator <(const T& right) const { return (type_ < right.type_); }
    70             bool operator >(const T& right) const { return (type_ > right.type_); }
     68            constexpr operator int() const                  { return type_; }
     69            T& operator =(int type)                         { type_ = type; return *this; }
     70            constexpr bool operator <(const T& right) const { return (type_ < right.type_); }
     71            constexpr bool operator >(const T& right) const { return (type_ > right.type_); }
    7172
    7273        private:
    73             OrxEnum(const OrxEnum& instance);
    7474            int type_;
    7575    };
     
    7777
    7878/// See orxonox::OrxEnum for more info
    79 #define OrxEnumConstructors(enumName)                        \
    80 enumName() { }                                               \
    81 enumName(int type) : OrxEnum<enumName>(type)             { } \
    82 enumName(const enumName& inst) : OrxEnum<enumName>(inst) { } \
     79#define OrxEnumConstructors(enumName)                                  \
     80constexpr enumName() { }                                               \
     81constexpr enumName(int type) : OrxEnum<enumName>(type)             { } \
     82constexpr enumName(const enumName& inst) : OrxEnum<enumName>(inst) { } \
    8383void dummyFunction()
    8484
  • code/trunk/src/libraries/util/mbool.h

    r8400 r11096  
    6060        public:
    6161            /// Constructor: Creates the mbool and initializes the boolean value (default to false).
    62             inline mbool(bool value = false)
    63                 { this->value_.memory_ = 0; this->value_.bool_ = value; }
     62            constexpr mbool(bool value = false) : value_{ static_cast<unsigned char>(value ? 1 : 0) }
     63                {}
    6464            /// Copy-constructor, copies state and memory.
    65             inline mbool(const mbool& value)
    66                 { this->value_.memory_ = value.value_.memory_; }
     65            constexpr mbool(const mbool& value) : value_{ value.value_.memory_ }
     66                {}
    6767            /// Destructor does nothing but not defining it might create a symbol (class is header only)
    68             inline ~mbool()
    69                 { }
     68            inline ~mbool() = default;
    7069
    7170            /// Assigns a boolean value (and increases the memory value if the value is different to the old value).
     
    8483
    8584            /// Implicitly converts the mbool to a bool.
    86             inline operator bool() const
     85            constexpr operator bool() const
    8786                { return this->value_.bool_; }
    8887
    8988            /// Compares the mbool to a bool, returns true if the bool has the same value as the state of the mbool.
    90             inline bool operator==(bool other) const
     89            constexpr bool operator==(bool other) const
    9190                { return this->value_.bool_ == other; }
    9291            /// Compares the mbool to a bool, returns true if the bool has a different value than the state of the mbool.
    93             inline bool operator!=(bool other) const
     92            constexpr bool operator!=(bool other) const
    9493                { return this->value_.bool_ != other; }
    9594
    9695            /// Compares two mbools, returns true if their memory matches.
    97             inline bool operator==(const mbool& other) const
     96            constexpr bool operator==(const mbool& other) const
    9897                { return this->value_.memory_ == other.value_.memory_; }
    9998            /// Compares two mbools, returns true if they have a different memory value.
    100             inline bool operator!=(const mbool& other) const
     99            constexpr bool operator!=(const mbool& other) const
    101100                { return this->value_.memory_ != other.value_.memory_; }
    102101
    103102            /// Returns the inverted state of the bool (doesn't change the internal state).
    104             inline bool operator!() const
     103            constexpr bool operator!() const
    105104                { return (!this->value_.bool_); }
    106105
     
    111110            union
    112111            {
     112                unsigned char memory_;  ///< The memory of the mbool, counts the state-changes (and the first bit represents also the boolean value)
    113113                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)
    115114            } value_;                   ///< A union containing the state and the memory of the mbool
    116115    };
  • code/trunk/src/libraries/util/tribool.h

    r8729 r11096  
    1010// so that (dontcare == dontcare).
    1111// Also removed all logic operators except for == and !=
     12// Added C++11 constexpr
    1213
    1314
     
    2223 * The type of the 'dontcare' keyword.
    2324 */
    24 struct dontcare_keyword_t { };
     25struct dontcare_keyword_t {};
    2526
    2627/**
    2728 * \brief Keyword for the dontcare tribool value
    2829 */
    29 const dontcare_keyword_t dontcare = dontcare_keyword_t();
     30constexpr dontcare_keyword_t dontcare = dontcare_keyword_t();
    3031
    3132/**
     
    4344   * \throws nothrow
    4445   */
    45   tribool() : value(false_value) {}
     46  constexpr tribool() : value(false_value) {}
    4647
    4748  /**
     
    5152   * \throws nothrow
    5253   */
    53   tribool(bool value) : value(value? true_value : false_value) {}
     54  constexpr tribool(bool value) : value(value? true_value : false_value) {}
    5455
    5556  /**
     
    5859   * \throws nothrow
    5960   */
    60   tribool(dontcare_keyword_t) : value(dontcare_value) {}
     61  constexpr tribool(dontcare_keyword_t) : value(dontcare_value) {}
    6162
    6263  /**
     
    6667   * \throws nothrow
    6768   */
    68   inline bool operator==(tribool y)
     69  constexpr bool operator==(tribool y) const
    6970  {
    7071    return (this->value == y.value);
     
    7475   * \overload
    7576   */
    76   inline bool operator==(bool y) { return (*this) == tribool(y); }
     77  constexpr bool operator==(bool y) const { return (*this) == tribool(y); }
    7778
    7879  /**
    7980   * \overload
    8081   */
    81   inline bool operator==(dontcare_keyword_t)
     82  constexpr bool operator==(dontcare_keyword_t) const
    8283  { return tribool(dontcare) == (*this); }
    8384
     
    8889   * \throws nothrow
    8990   */
    90   inline bool operator!=(tribool y)
     91  constexpr bool operator!=(tribool y) const
    9192  {
    9293    return !((*this) == y);
     
    9697   * \overload
    9798   */
    98   inline bool operator!=(bool y) { return (*this) != tribool(y); }
     99  constexpr bool operator!=(bool y) const { return (*this) != tribool(y); }
    99100
    100101  /**
    101102   * \overload
    102103   */
    103   inline bool operator!=(dontcare_keyword_t)
     104  constexpr bool operator!=(dontcare_keyword_t) const
    104105  { return (*this) != tribool(dontcare); }
    105106
     
    114115 * \overload
    115116 */
    116 inline bool operator==(bool x, tribool y) { return tribool(x) == y; }
     117constexpr bool operator==(bool x, tribool y) { return tribool(x) == y; }
    117118
    118119/**
    119120 * \overload
    120121 */
    121 inline bool operator==(dontcare_keyword_t, tribool x)
     122constexpr bool operator==(dontcare_keyword_t, tribool x)
    122123{ return tribool(dontcare) == x; }
    123124
     
    125126 * \overload
    126127 */
    127 inline bool operator!=(bool x, tribool y) { return tribool(x) != y; }
     128constexpr bool operator!=(bool x, tribool y) { return tribool(x) != y; }
    128129
    129130/**
    130131 * \overload
    131132 */
    132 inline bool operator!=(dontcare_keyword_t, tribool x)
     133constexpr bool operator!=(dontcare_keyword_t, tribool x)
    133134{ return tribool(dontcare) != x; }
    134135
Note: See TracChangeset for help on using the changeset viewer.