Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/util/OrxEnum.h @ 12106

Last change on this file since 12106 was 11096, checked in by muemart, 9 years ago

Constexpr for some util things

  • Property svn:eol-style set to native
File size: 2.8 KB
RevLine 
[2541]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 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
[7401]30    @file
31    @ingroup Util
[2541]32*/
33
34#ifndef _OrxEnum_H__
35#define _OrxEnum_H__
36
37#include "UtilPrereqs.h"
38
39namespace orxonox
40{
[7401]41    /** Lightweight enumeration class that can be extended at run time.
42    @details
43        The class accepts type int and also defines operator int(). Therefore
44        int and OrxEnum can be used interchangeably so you can extend the content
45        of the enumeration at run time by adding ints.
46    @par Declaring an OrxEnum
47        Write a struct that inherits OrxEnum and use some macros:
48        @code
49        struct MyEnum : OrxEnum<MyEnum>
50        {
51            OrxEnumConstructors(MyEnum);
52
53            static const int Value1 = -1;
54            static const int Value2 = 0;
55            static const int Value3 = Value2 + 10;
56        };
57        @endcode
58    */
[2541]59    template <class T>
60    struct OrxEnum
61    {
62        public:
[11096]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;
[2541]67
[11096]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_); }
[2541]72
73        private:
74            int type_;
75    };
76}
77
[7401]78/// See orxonox::OrxEnum for more info
[11096]79#define OrxEnumConstructors(enumName)                                  \
80constexpr enumName() { }                                               \
81constexpr enumName(int type) : OrxEnum<enumName>(type)             { } \
82constexpr enumName(const enumName& inst) : OrxEnum<enumName>(inst) { } \
[2541]83void dummyFunction()
84
85#endif /* _OrxEnum_H__ */
Note: See TracBrowser for help on using the repository browser.