neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
enum.hpp
Go to the documentation of this file.
1// i_enum.hpp
2/*
3 * Copyright (c) 2019, 2020 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36#pragma once
37
38#include <neolib/neolib.hpp>
41#include <neolib/core/map.hpp>
43
44namespace neolib
45{
46 template <typename Enum>
47 class basic_enum : public reference_counted<i_basic_enum<std::underlying_type_t<Enum>>>
48 {
49 typedef basic_enum<Enum> self_type;
51 // exceptions
52 public:
53 using typename base_type::bad_enum_string;
54 // types
55 public:
57 typedef Enum enum_type;
58 typedef std::underlying_type_t<Enum> underlying_type;
59 // construction/assignment
60 public:
62 iValue{}
63 {
64 }
66 iValue{ static_cast<underlying_type>(aValue) }
67 {
68 }
69 basic_enum(const abstract_type& aOther) :
70 iValue{ aOther.value() }
71 {
72 }
73 // state
74 public:
75 underlying_type value() const final
76 {
77 return iValue;
78 }
79 void set_value(underlying_type aValue) final
80 {
81 iValue = aValue;
82 }
83 underlying_type set_value(const i_string& aValue) final
84 {
85 for (auto const& e : enumerators())
86 if (e.second() == aValue)
87 {
88 iValue = e.first();
89 return value();
90 }
91 throw bad_enum_string();
92 }
93 underlying_type const* data() const final
94 {
95 return &iValue;
96 }
98 {
99 return &iValue;
100 }
101 // meta
102 public:
103 void to_string(i_string& aString) const final
104 {
105 aString = enumerators().find(value())->second();
106 }
107 const typename base_type::enumerators_t& enumerators() const final
108 {
109 return enum_enumerators<enum_type>();
110 }
111 // implementation
112 private:
113 abstract_type* do_clone() const final
114 {
115 return new self_type{ *this };
116 }
117 abstract_type& do_assign(const abstract_type& aRhs) final
118 {
119 iValue = aRhs.value();
120 return *this;
121 }
122 // state
123 private:
124 underlying_type iValue;
125 };
126
127 template <typename Enum>
129}
130
131
void set_value(underlying_type aValue) final
Definition enum.hpp:79
underlying_type set_value(const i_string &aValue) final
Definition enum.hpp:83
void to_string(i_string &aString) const final
Definition enum.hpp:103
i_basic_enum< std::underlying_type_t< Enum > > abstract_type
Definition enum.hpp:56
underlying_type const * data() const final
Definition enum.hpp:93
underlying_type * data() final
Definition enum.hpp:97
std::underlying_type_t< Enum > underlying_type
Definition enum.hpp:58
underlying_type value() const final
Definition enum.hpp:75
basic_enum(enum_type aValue)
Definition enum.hpp:65
const base_type::enumerators_t & enumerators() const final
Definition enum.hpp:107
basic_enum(const abstract_type &aOther)
Definition enum.hpp:69
i_multimap< underlying_type, i_string > enumerators_t
Definition i_enum.hpp:153
basic_enum< Enum > enum_t
Definition enum.hpp:128