18 #ifndef ION_BASE_SETTING_H_
19 #define ION_BASE_SETTING_H_
26 #include "base/macros.h"
46 typedef std::function<void(SettingBase* setting)>
Listener;
50 : listener(listener_in), enabled(enabled_in) {}
56 const std::string&
GetName()
const {
return name_; }
71 void RegisterListener(
const std::string& key,
const Listener& listener);
73 void EnableListener(
const std::string& key,
bool enable);
75 void UnregisterListener(
const std::string& key);
78 void NotifyListeners();
82 virtual const std::string ToString()
const = 0;
86 virtual bool FromString(
const std::string&
str) = 0;
95 typedef std::map<std::string, ListenerInfo> ListenerMap;
98 std::string doc_string_;
99 std::string type_descriptor_;
100 ListenerMap listeners_;
139 template <
typename SettingType>
154 template <
typename T>
160 const std::string& doc_string)
167 const std::string& doc_string)
168 :
SettingBase(group->GetGroupName() +
'/' + name, doc_string),
175 :
SettingBase(name, std::string()), value_(value) {
181 :
SettingBase(group->GetGroupName() +
'/' + name, std::string()),
192 std::istringstream in(str);
207 operator T()
const {
return value_; }
216 return setting.value_ ==
value;
226 template <
typename T>
232 const std::string& doc_string)
239 const std::string& doc_string)
240 :
SettingBase(group->GetGroupName() +
'/' + name, doc_string),
247 :
SettingBase(name, std::string()), value_(value) {
253 :
SettingBase(group->GetGroupName() +
'/' + name, std::string()),
266 std::istringstream in(str);
276 const std::atomic<T>&
GetValue()
const {
return value_; }
281 operator T()
const {
return value_.load(); }
290 const Setting<std::atomic<T> >& setting) {
291 return setting.value_ ==
value;
295 std::atomic<T> value_;
297 DISALLOW_IMPLICIT_CONSTRUCTORS(
Setting<std::atomic<T> >);
304 template <
typename T>
312 const std::string& env_var_name,
313 const T& default_value,
314 const std::string& doc_string)
315 :
Setting<
T>(setting_name, default_value, doc_string) {
316 const std::string env_value =
326 return setting ==
value;
336 template <
typename T>
341 : setting_(setting) {
343 original_value_ = setting_->GetValue();
344 setting_->SetValue(value);
351 setting_->SetValue(original_value_);
367 template <
typename SettingType>
379 setting->SetTypeDescriptor(
"bool");
385 #endif // ION_BASE_SETTING_H_
const std::string & GetDocString() const
Returns the documentation string associated with this.
Setting(const std::string &name, const T &value, const std::string &doc_string)
Creates a new setting with the passed name, initial value, and documentation string.
void NotifyListeners()
Notify listeners that this setting has changed.
const std::string GetEnvironmentVariableValue(const std::string &name)
Returns the value of the named environment variable.
bool operator==(const T &value) const
Equality testers.
bool StartsWith(const std::string &target, const std::string &start)
Returns whether target begins with start.
bool FromString(const std::string &str) override
Parses the passed string to set the value of this and returns whether the parsing was successful...
Setting(const std::string &name, const T &value)
Convenience constructor that does not require a documentation string.
Setting(const SettingGroup *group, const std::string &name, const T &value, const std::string &doc_string)
Same as above, but places the setting in the passed group.
friend bool operator==(const T &value, const EnvironmentSetting< T > &setting)
EnvironmentSetting(const std::string &setting_name, const std::string &env_var_name, const T &default_value, const std::string &doc_string)
Creates a new setting with the passed setting_name and attempts to set its initial value from the env...
SettingGroup(const SettingGroup &parent_group, std::string name)
Base class for Setting, which encapsulates the name of the setting and any functors that should be ca...
Setting(const std::string &name, const T &value)
Convenience constructor that does not require a documentation string.
Setting(const std::string &name, const T &value, const std::string &doc_string)
Creates a new setting with the passed name, initial value, and documentation string.
T * GetMutableValue()
Direct value mutators.
SettingGroup(const std::string &name)
bool operator==(const T &value) const
Equality testers.
const std::atomic< T > & GetValue() const
const std::string ToString() const override
Returns a string version of this setting.
friend bool operator==(const T &value, const Setting< std::atomic< T > > &setting)
bool RemovePrefix(const std::string &prefix, std::string *target)
Removes prefix from the beginning of target if target starts with it.
Setting(const SettingGroup *group, const std::string &name, const T &value)
Same as above, but places the setting in the passed group.
void SetValue(const T &value)
bool FromString(const std::string &str) override
Parses the passed string to set the value of this and returns whether the parsing was successful...
void SetTypeDescriptorForType(SettingType *setting)
Sets the type descriptor string of a setting based on its type.
const std::string GetGroupName() const
Returns the name of the group that this wraps.
void SetValue(const T &value)
void SetTypeDescriptor(const std::string &desc)
Sets/returns a string containing information about the Setting's type.
bool StringToValue(std::istringstream &in, T *val)
This file defines two public functions: StringToValue() and ValueToString().
bool RemoveSuffix(const std::string &suffix, std::string *target)
Removes suffix from the end of target if target ends with it.
std::string ValueToString(const T &val)
ValueToString.
bool operator==(const T &value) const
Equality testers.
const std::string & GetName() const
Returns the name associated with this.
const std::string & GetTypeDescriptor() const
const std::string ToString() const override
Returns a string version of this setting.
std::function< void(SettingBase *setting)> Listener
A function that is called when the value changes.
const T & GetValue() const
Setting(const SettingGroup *group, const std::string &name, const T &value)
Same as above, but places the setting in the passed group.
Setting(const SettingGroup *group, const std::string &name, const T &value, const std::string &doc_string)
Same as above, but places the setting in the passed group.
~ScopedSettingValue()
Restore the original value to the Setting<T>.
friend bool operator==(const T &value, const Setting< T > &setting)
bool EndsWith(const std::string &target, const std::string &end)
Returns whether target ends with end.
SettingManager tracks all existing SettingBase instances, and allows callers to obtain a map of all s...
A SettingGroup is a convenience class to hold Settings that are in the same hierarchical group...
void operator=(const T &value)
ListenerInfo(const Listener &listener_in, bool enabled_in)
Sets a Setting<T> to a new value.
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
void operator=(const T &value)
An EnvironmentSetting is a Setting can take its initial value from the named system environment varia...
ScopedSettingValue(Setting< T > *setting, const T &value)
Save the original setting value, and change to the new value.
std::atomic< T > * GetMutableValue()
Direct value mutators.