[Orxonox-commit 6439] r11096 - code/trunk/src/libraries/util
muemart at orxonox.net
muemart at orxonox.net
Mon Jan 25 21:33:12 CET 2016
Author: muemart
Date: 2016-01-25 21:33:11 +0100 (Mon, 25 Jan 2016)
New Revision: 11096
Modified:
code/trunk/src/libraries/util/OrxEnum.h
code/trunk/src/libraries/util/mbool.h
code/trunk/src/libraries/util/tribool.h
Log:
Constexpr for some util things
Modified: code/trunk/src/libraries/util/OrxEnum.h
===================================================================
--- code/trunk/src/libraries/util/OrxEnum.h 2016-01-24 22:28:05 UTC (rev 11095)
+++ code/trunk/src/libraries/util/OrxEnum.h 2016-01-25 20:33:11 UTC (rev 11096)
@@ -60,26 +60,26 @@
struct OrxEnum
{
public:
- OrxEnum() { }
- OrxEnum(int type) { type_ = type; }
- OrxEnum(const T& instance) { type_ = instance.type_; }
+ constexpr OrxEnum() : type_() {}
+ constexpr OrxEnum(int type) : type_(type) {}
+ constexpr OrxEnum(const T& instance) : type_(instance.type_) {}
+ constexpr OrxEnum(const OrxEnum& instance) = delete;
- operator int() { return type_; }
- T& operator =(int type) { type_ = type; return *this; }
- bool operator <(const T& right) const { return (type_ < right.type_); }
- bool operator >(const T& right) const { return (type_ > right.type_); }
+ constexpr operator int() const { return type_; }
+ T& operator =(int type) { type_ = type; return *this; }
+ constexpr bool operator <(const T& right) const { return (type_ < right.type_); }
+ constexpr bool operator >(const T& right) const { return (type_ > right.type_); }
private:
- OrxEnum(const OrxEnum& instance);
int type_;
};
}
/// See orxonox::OrxEnum for more info
-#define OrxEnumConstructors(enumName) \
-enumName() { } \
-enumName(int type) : OrxEnum<enumName>(type) { } \
-enumName(const enumName& inst) : OrxEnum<enumName>(inst) { } \
+#define OrxEnumConstructors(enumName) \
+constexpr enumName() { } \
+constexpr enumName(int type) : OrxEnum<enumName>(type) { } \
+constexpr enumName(const enumName& inst) : OrxEnum<enumName>(inst) { } \
void dummyFunction()
#endif /* _OrxEnum_H__ */
Modified: code/trunk/src/libraries/util/mbool.h
===================================================================
--- code/trunk/src/libraries/util/mbool.h 2016-01-24 22:28:05 UTC (rev 11095)
+++ code/trunk/src/libraries/util/mbool.h 2016-01-25 20:33:11 UTC (rev 11096)
@@ -59,14 +59,13 @@
{
public:
/// Constructor: Creates the mbool and initializes the boolean value (default to false).
- inline mbool(bool value = false)
- { this->value_.memory_ = 0; this->value_.bool_ = value; }
+ constexpr mbool(bool value = false) : value_{ static_cast<unsigned char>(value ? 1 : 0) }
+ {}
/// Copy-constructor, copies state and memory.
- inline mbool(const mbool& value)
- { this->value_.memory_ = value.value_.memory_; }
+ constexpr mbool(const mbool& value) : value_{ value.value_.memory_ }
+ {}
/// Destructor does nothing but not defining it might create a symbol (class is header only)
- inline ~mbool()
- { }
+ inline ~mbool() = default;
/// Assigns a boolean value (and increases the memory value if the value is different to the old value).
inline mbool& operator=(bool value)
@@ -83,25 +82,25 @@
{ mbool temp = (*this); ++this->value_.memory_; return temp; }
/// Implicitly converts the mbool to a bool.
- inline operator bool() const
+ constexpr operator bool() const
{ return this->value_.bool_; }
/// Compares the mbool to a bool, returns true if the bool has the same value as the state of the mbool.
- inline bool operator==(bool other) const
+ constexpr bool operator==(bool other) const
{ return this->value_.bool_ == other; }
/// Compares the mbool to a bool, returns true if the bool has a different value than the state of the mbool.
- inline bool operator!=(bool other) const
+ constexpr bool operator!=(bool other) const
{ return this->value_.bool_ != other; }
/// Compares two mbools, returns true if their memory matches.
- inline bool operator==(const mbool& other) const
+ constexpr bool operator==(const mbool& other) const
{ return this->value_.memory_ == other.value_.memory_; }
/// Compares two mbools, returns true if they have a different memory value.
- inline bool operator!=(const mbool& other) const
+ constexpr bool operator!=(const mbool& other) const
{ return this->value_.memory_ != other.value_.memory_; }
/// Returns the inverted state of the bool (doesn't change the internal state).
- inline bool operator!() const
+ constexpr bool operator!() const
{ return (!this->value_.bool_); }
/// Returns the memory value.
@@ -110,8 +109,8 @@
private:
union
{
- bool bool_ : 1; ///< The boolean state of the mbool, is located on the first bit of the memory variable
unsigned char memory_; ///< The memory of the mbool, counts the state-changes (and the first bit represents also the boolean value)
+ bool bool_ : 1; ///< The boolean state of the mbool, is located on the first bit of the memory variable
} value_; ///< A union containing the state and the memory of the mbool
};
}
Modified: code/trunk/src/libraries/util/tribool.h
===================================================================
--- code/trunk/src/libraries/util/tribool.h 2016-01-24 22:28:05 UTC (rev 11095)
+++ code/trunk/src/libraries/util/tribool.h 2016-01-25 20:33:11 UTC (rev 11096)
@@ -9,6 +9,7 @@
// of 'indeterminate'. The difference is that 'dontcare' is actually a value
// so that (dontcare == dontcare).
// Also removed all logic operators except for == and !=
+// Added C++11 constexpr
// For more information, see http://www.boost.org
@@ -21,12 +22,12 @@
* INTERNAL ONLY
* The type of the 'dontcare' keyword.
*/
-struct dontcare_keyword_t { };
+struct dontcare_keyword_t {};
/**
* \brief Keyword for the dontcare tribool value
*/
-const dontcare_keyword_t dontcare = dontcare_keyword_t();
+constexpr dontcare_keyword_t dontcare = dontcare_keyword_t();
/**
* \brief A 3-state boolean type.
@@ -42,7 +43,7 @@
*
* \throws nothrow
*/
- tribool() : value(false_value) {}
+ constexpr tribool() : value(false_value) {}
/**
* Construct a new 3-state boolean value with the given boolean
@@ -50,14 +51,14 @@
*
* \throws nothrow
*/
- tribool(bool value) : value(value? true_value : false_value) {}
+ constexpr tribool(bool value) : value(value? true_value : false_value) {}
/**
* Construct a new 3-state boolean value with an dontcare value.
*
* \throws nothrow
*/
- tribool(dontcare_keyword_t) : value(dontcare_value) {}
+ constexpr tribool(dontcare_keyword_t) : value(dontcare_value) {}
/**
* \brief Compare tribools for equality
@@ -65,7 +66,7 @@
* \returns the result of comparing two tribool values.
* \throws nothrow
*/
- inline bool operator==(tribool y)
+ constexpr bool operator==(tribool y) const
{
return (this->value == y.value);
}
@@ -73,12 +74,12 @@
/**
* \overload
*/
- inline bool operator==(bool y) { return (*this) == tribool(y); }
+ constexpr bool operator==(bool y) const { return (*this) == tribool(y); }
/**
* \overload
*/
- inline bool operator==(dontcare_keyword_t)
+ constexpr bool operator==(dontcare_keyword_t) const
{ return tribool(dontcare) == (*this); }
/**
@@ -87,7 +88,7 @@
* \returns the result of comparing two tribool values for inequality.
* \throws nothrow
*/
- inline bool operator!=(tribool y)
+ constexpr bool operator!=(tribool y) const
{
return !((*this) == y);
}
@@ -95,12 +96,12 @@
/**
* \overload
*/
- inline bool operator!=(bool y) { return (*this) != tribool(y); }
+ constexpr bool operator!=(bool y) const { return (*this) != tribool(y); }
/**
* \overload
*/
- inline bool operator!=(dontcare_keyword_t)
+ constexpr bool operator!=(dontcare_keyword_t) const
{ return (*this) != tribool(dontcare); }
/**
@@ -113,23 +114,23 @@
/**
* \overload
*/
-inline bool operator==(bool x, tribool y) { return tribool(x) == y; }
+constexpr bool operator==(bool x, tribool y) { return tribool(x) == y; }
/**
* \overload
*/
-inline bool operator==(dontcare_keyword_t, tribool x)
+constexpr bool operator==(dontcare_keyword_t, tribool x)
{ return tribool(dontcare) == x; }
/**
* \overload
*/
-inline bool operator!=(bool x, tribool y) { return tribool(x) != y; }
+constexpr bool operator!=(bool x, tribool y) { return tribool(x) != y; }
/**
* \overload
*/
-inline bool operator!=(dontcare_keyword_t, tribool x)
+constexpr bool operator!=(dontcare_keyword_t, tribool x)
{ return tribool(dontcare) != x; }
} // end namespace orxonox
More information about the Orxonox-commit
mailing list