neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
setting_value.hpp
Go to the documentation of this file.
1// 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>
39#include <type_traits>
40#include <optional>
41#include <neolib/core/enum.hpp>
43
44namespace neolib
45{
46 template <typename T, typename = sfinae>
48 {
49 typedef std::optional<T> type;
50 };
51 template <typename T>
52 struct setting_container_type<T, std::enable_if_t<std::is_enum_v<T>, sfinae>>
53 {
54 typedef std::optional<enum_t<T>> type;
55 };
56
57 template <typename T>
59 {
60 typedef setting_value<T> self_type;
61 public:
64 public:
66 iValue{}
67 {
68 }
69 setting_value(T const& aDefaultValue) :
70 iValue{ aDefaultValue }
71 {
72 }
73 setting_value(self_type const& aOther) :
74 iValue{ aOther.iValue }
75 {
76 }
78 iValue{ aOther.is_set() ? aOther.get<T>() : container_type{} }
79 {
80 }
81 public:
82 setting_type type() const override
83 {
84 return setting_type_v<T>;
85 }
86 i_string const& type_name() const override
87 {
88 return setting_type_name_v<T>;
89 }
90 bool is_set() const override
91 {
92 return !!iValue;
93 }
94 void clear() override
95 {
96 iValue = std::nullopt;
97 }
98 public:
99 bool operator==(const i_setting_value& aRhs) const
100 {
101 if (type() != aRhs.type())
102 return false;
103 if (is_set() != aRhs.is_set())
104 return false;
105 if (!is_set())
106 return true;
107 return get<T>() == aRhs.get<T>();
108 }
109 bool operator<(const i_setting_value& aRhs) const
110 {
111 if (type() != aRhs.type())
112 return type() < aRhs.type();
113 if (is_set() != aRhs.is_set())
114 return is_set() < aRhs.is_set();
115 if (!is_set())
116 return false;
117 return get<T>() < aRhs.get<T>();
118 }
119 private:
120 void const* data() const override
121 {
122 if (is_set())
123 return &*iValue;
124 throw not_set();
125 }
126 void* data() override
127 {
128 if (!is_set())
129 iValue.emplace();
130 return &*iValue;
131 }
132 private:
133 container_type iValue;
134 };
135}
abstract_return_t< T const > get() const
virtual bool is_set() const =0
virtual setting_type type() const =0
i_setting_value abstract_type
i_string const & type_name() const override
bool operator==(const i_setting_value &aRhs) const
bool is_set() const override
setting_value(i_setting_value const &aOther)
void clear() override
setting_type type() const override
setting_value(T const &aDefaultValue)
bool operator<(const i_setting_value &aRhs) const
setting_value(self_type const &aOther)
setting_container_type< T >::type container_type
Definition plf_hive.h:79