neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
i_setting_value.hpp
Go to the documentation of this file.
1// i_setting_value.hpp
2/*
3 * Copyright (c) 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
42namespace neolib
43{
44 enum class setting_type : uint32_t
45 {
46 Boolean,
47 Int8,
48 Int16,
49 Int32,
50 Int64,
51 Uint8,
52 Uint16,
53 Uint32,
54 Uint64,
55 Float32,
56 Float64,
57 String,
58 Enum,
59 Custom
60 };
61
62 template<setting_type ST> struct get_setting_type;
63 template<> struct get_setting_type<setting_type::Boolean> { typedef bool type; };
64 template<> struct get_setting_type<setting_type::Int8> { typedef int8_t type; };
65 template<> struct get_setting_type<setting_type::Int16> { typedef int16_t type; };
66 template<> struct get_setting_type<setting_type::Int32> { typedef int32_t type; };
67 template<> struct get_setting_type<setting_type::Int64> { typedef int64_t type; };
68 template<> struct get_setting_type<setting_type::Uint8> { typedef uint8_t type; };
69 template<> struct get_setting_type<setting_type::Uint16> { typedef uint16_t type; };
70 template<> struct get_setting_type<setting_type::Uint32> { typedef uint32_t type; };
71 template<> struct get_setting_type<setting_type::Uint64> { typedef uint64_t type; };
72 template<> struct get_setting_type<setting_type::Float32> { typedef float type; };
73 template<> struct get_setting_type<setting_type::Float64> { typedef double type; };
74 template<> struct get_setting_type<setting_type::String> { typedef string type; };
75 template <setting_type ST>
77
78 template <typename T, typename = sfinae> constexpr setting_type setting_type_v = setting_type::Custom;
91 template<typename T> constexpr setting_type setting_type_v<T, std::enable_if_t<std::is_enum_v<T>, sfinae>> = setting_type::Enum;
92
93 template <typename T, typename = sfinae> struct setting_type_name {};
94
95 #define define_setting_type(T) using neolib::setting_type_name; template<> struct setting_type_name<T> { static const neolib::string& name() { static neolib::string sTypeName = #T; return sTypeName; } };
96 #define define_setting_type_as(T, Name) using neolib::setting_type_name; template<> struct setting_type_name<T> { static const neolib::string& name() { static neolib::string sTypeName = #Name; return sTypeName; } };
97
100 define_setting_type(int16_t)
101 define_setting_type(int32_t)
102 define_setting_type(int64_t)
103 define_setting_type(uint8_t)
104 define_setting_type(uint16_t)
105 define_setting_type(uint32_t)
106 define_setting_type(uint64_t)
108 define_setting_type(double)
109 define_setting_type(string)
110 template<typename T> struct setting_type_name<T, std::enable_if_t<std::is_enum_v<T>, sfinae>> { static const neolib::string& name() { static neolib::string sTypeName = "enum"; return sTypeName; } };
111
112 template<typename T>
114
116 {
117 public:
119 public:
120 struct not_set : std::logic_error { not_set() : std::logic_error{ "neolib::i_setting_value::not_set" } {} };
121 public:
122 virtual ~i_setting_value() = default;
123 public:
124 virtual setting_type type() const = 0;
125 virtual i_string const& type_name() const = 0;
126 virtual bool is_set() const = 0;
127 virtual void clear() = 0;
128 public:
129 virtual bool operator==(const i_setting_value& aRhs) const = 0;
130 virtual bool operator<(const i_setting_value& aRhs) const = 0;
131 private:
132 virtual void const* data() const = 0;
133 virtual void* data() = 0;
134 public:
135 bool operator!=(const i_setting_value& aRhs) const
136 {
137 return !(*this == aRhs);
138 }
139 template <typename T>
141 {
142 if (type() != setting_type::Enum)
143 return *static_cast<abstract_t<T> const*>(data());
144 else
145 {
146 if constexpr (std::is_same_v<T, i_enum>)
147 return *static_cast<i_enum const*>(data());
148 else if constexpr (std::is_scalar_v<T>)
149 return static_cast<i_enum const*>(data())->value<T>();
150 else
151 return *static_cast<abstract_t<T> const*>(data());
152 }
153 }
154 template <typename T>
155 void set(T const& aNewValue)
156 {
157 if (type() != setting_type::Enum)
158 *static_cast<abstract_t<T>*>(data()) = aNewValue;
159 else
160 {
161 if constexpr (std::is_same_v<T, i_enum>)
162 *static_cast<i_enum*>(data()) = aNewValue;
163 else if constexpr (std::is_scalar_v<T>)
164 static_cast<i_enum*>(data())->set_value<T>(aNewValue);
165 else
166 *static_cast<abstract_t<T>*>(data()) = aNewValue;
167 }
168 }
169 };
170}
virtual bool operator==(const i_setting_value &aRhs) const =0
virtual bool operator<(const i_setting_value &aRhs) const =0
bool operator!=(const i_setting_value &aRhs) const
void set(T const &aNewValue)
virtual void clear()=0
virtual ~i_setting_value()=default
abstract_return_t< T const > get() const
virtual i_string const & type_name() const =0
virtual bool is_set() const =0
virtual setting_type type() const =0
#define define_setting_type(T)
typename detail::abstract_type< T >::type abstract_t
Definition neolib.hpp:178
typename detail::abstract_return_type< T >::type abstract_return_t
Definition neolib.hpp:213
constexpr setting_type setting_type_v< int16_t >
constexpr setting_type setting_type_v< bool >
constexpr setting_type setting_type_v< int8_t >
constexpr setting_type setting_type_v< int32_t >
constexpr setting_type setting_type_v< uint16_t >
i_enum_i32 i_enum
Definition i_enum.hpp:265
constexpr setting_type setting_type_v< string >
constexpr setting_type setting_type_v< float >
typename get_setting_type< ST >::type setting_type_t
constexpr setting_type setting_type_v
constexpr setting_type setting_type_v< int64_t >
constexpr setting_type setting_type_v< double >
constexpr setting_type setting_type_v< uint64_t >
constexpr setting_type setting_type_v< uint8_t >
constexpr setting_type setting_type_v< uint32_t >
const string setting_type_name_v
Definition plf_hive.h:79