neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
i_setting.hpp
Go to the documentation of this file.
1// i_setting.hpp
2/*
3 * Copyright (c) 2007, 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>
44
45namespace neolib
46{
47 class i_settings;
48
50 {
51 friend class settings;
52 public:
54 public:
55 struct setting_not_modified : std::logic_error { setting_not_modified() : std::logic_error{ "neolib::i_setting::setting_not_modified" } {} };
56 struct setting_not_optional : std::logic_error { setting_not_optional() : std::logic_error{ "neolib::i_setting::setting_not_optional" } {} };
57 public:
58 declare_event(changing)
59 declare_event(changed)
60 public:
61 virtual i_settings& manager() const = 0;
62 virtual i_string const& key() const = 0;
63 virtual i_setting_constraints const& constraints() const = 0;
64 virtual i_string const& format() const = 0;
65 virtual bool hidden() const = 0;
66 virtual bool is_enabled() const = 0;
67 virtual void set_enabled(bool aEnabled) = 0;
68 virtual bool is_default(bool aUnappliedNew = false) const = 0;
69 virtual bool modified() const = 0;
70 virtual i_setting_value const& default_value() const = 0;
71 virtual i_setting_value const& value(bool aUnappliedNew = false) const = 0;
72 virtual i_setting_value const& modified_value() const = 0;
73 virtual void value_as_string(i_string& aValue, bool aUnappliedNew = false) const = 0;
74 virtual void set_default_value(i_setting_value const& aDefaultValue) = 0;
75 virtual void set_value(i_setting_value const& aNewValue) = 0;
76 virtual void set_value_from_string(i_string const& aNewValue) = 0;
77 virtual void clear() = 0;
78 protected:
80 private:
81 virtual bool apply_change() = 0;
82 virtual bool discard_change() = 0;
83 private:
84 virtual void clone(i_ref_ptr<i_setting>& aResult) const = 0;
85 public:
86 bool enabled() const
87 {
88 return is_enabled();
89 }
90 bool disabled() const
91 {
92 return !enabled();
93 }
94 void enable()
95 {
96 set_enabled(true);
97 }
98 void disabled()
99 {
100 set_enabled(false);
101 }
102 template <typename T>
103 abstract_return_t<const T> value(bool aUnappliedNew = false) const
104 {
105 return value(aUnappliedNew).get<T>();
106 }
107 template <typename T>
109 {
110 return modified_value().get<T>();
111 }
112 std::string value_as_string(bool aUnappliedNew = false) const
113 {
114 thread_local string result;
115 value_as_string(result, aUnappliedNew);
116 return result.to_std_string();
117 }
118 template <typename T>
119 void set_default_value(T const& aDefaultValue, std::enable_if_t<!std::is_convertible_v<T&, i_setting_value&>, sfinae> = {})
120 {
121 auto& temp = temp_setting_value();
122 temp.set<T>(aDefaultValue);
123 set_default_value(static_cast<i_setting_value const&>(temp));
124 }
125 template <typename T>
126 void set_value(T const& aNewValue, std::enable_if_t<!std::is_convertible_v<T&, i_setting_value&>, sfinae> = {})
127 {
128 auto& temp = temp_setting_value();
129 temp.set<T>(aNewValue);
130 set_value(static_cast<i_setting_value const&>(temp));
131 }
132 template <typename T>
133 void apply_value(T const& aNewValue, std::enable_if_t<!std::is_convertible_v<T&, i_setting_value&>, sfinae> = {})
134 {
135 auto& temp = temp_setting_value();
136 temp.set<T>(aNewValue);
137 set_value(static_cast<i_setting_value const&>(temp));
138 apply_change();
139 }
140 public:
142 {
143 set_value(aRhs.value());
144 return *this;
145 }
147 {
148 set_value(aRhs);
149 return *this;
150 }
151 template <typename T>
152 i_setting& operator=(T const& aNewValue)
153 {
154 set_value(aNewValue);
155 return *this;
156 }
157 };
158}
abstract_return_t< T const > get() const
virtual bool modified() const =0
virtual i_setting_value const & modified_value() const =0
i_setting & operator=(T const &aNewValue)
void apply_value(T const &aNewValue, std::enable_if_t<!std::is_convertible_v< T &, i_setting_value & >, sfinae >={})
virtual bool is_default(bool aUnappliedNew=false) const =0
virtual i_setting_constraints const & constraints() const =0
virtual void value_as_string(i_string &aValue, bool aUnappliedNew=false) const =0
bool disabled() const
Definition i_setting.hpp:90
std::string value_as_string(bool aUnappliedNew=false) const
virtual void set_enabled(bool aEnabled)=0
abstract_return_t< const T > value(bool aUnappliedNew=false) const
void set_default_value(T const &aDefaultValue, std::enable_if_t<!std::is_convertible_v< T &, i_setting_value & >, sfinae >={})
virtual i_setting_value & temp_setting_value()=0
virtual bool is_enabled() const =0
i_setting abstract_type
Definition i_setting.hpp:53
virtual void set_value(i_setting_value const &aNewValue)=0
bool enabled() const
Definition i_setting.hpp:86
virtual i_setting_value const & value(bool aUnappliedNew=false) const =0
i_setting & operator=(i_setting const &aRhs)
virtual void set_default_value(i_setting_value const &aDefaultValue)=0
virtual void set_value_from_string(i_string const &aNewValue)=0
virtual void clear()=0
virtual i_string const & format() const =0
i_setting & operator=(i_setting_value const &aRhs)
void set_value(T const &aNewValue, std::enable_if_t<!std::is_convertible_v< T &, i_setting_value & >, sfinae >={})
abstract_return_t< const T > modified_value() const
virtual declare_event(changing) declare_event(changed) public i_string const & key() const =0
virtual i_setting_value const & default_value() const =0
virtual bool hidden() const =0
std::string to_std_string() const
Definition string.hpp:85
typename detail::abstract_return_type< T >::type abstract_return_t
Definition neolib.hpp:213
#define declare_event(declName,...)
Definition i_event.hpp:305