openspades/Sources/Core/Settings.h

155 lines
4.2 KiB
C
Raw Normal View History

2013-08-29 11:45:22 +09:00
/*
Copyright (c) 2013 yvt
This file is part of OpenSpades.
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
*/
2013-08-18 16:18:06 +09:00
#pragma once
#include <string>
#include <map>
#include <vector>
2016-11-19 21:03:51 +09:00
#include <type_traits>
2013-08-18 16:18:06 +09:00
namespace spades {
2016-11-19 21:03:51 +09:00
enum class SettingItemFlags {
None = 0
};
inline SettingItemFlags operator | (SettingItemFlags lhs, SettingItemFlags rhs)
{
using T = std::underlying_type<SettingItemFlags>::type;
return (SettingItemFlags)(static_cast<T>(lhs) | static_cast<T>(rhs));
}
inline SettingItemFlags& operator |= (SettingItemFlags& lhs, SettingItemFlags rhs)
{
using T = std::underlying_type<SettingItemFlags>::type;
lhs = (SettingItemFlags)(static_cast<T>(lhs) | static_cast<T>(rhs));
return lhs;
}
struct SettingItemDescriptor {
const std::string defaultValue;
const SettingItemFlags flags;
SettingItemDescriptor() = default;
SettingItemDescriptor(const std::string &defaultValue = std::string(),
SettingItemFlags flags = SettingItemFlags::None) :
defaultValue(defaultValue),
flags(flags)
{}
bool operator == (const SettingItemDescriptor &o) const {
return defaultValue == o.defaultValue &&
flags == o.flags;
}
bool operator != (const SettingItemDescriptor &o) const {
return !(*this == o);
}
};
class ISettingItemListener {
protected:
ISettingItemListener() = default;
virtual ~ISettingItemListener() {}
public:
virtual void SettingChanged(const std::string &name) = 0;
};
2013-08-18 16:18:06 +09:00
class Settings {
struct Item {
std::string name;
std::string string;
float value;
int intValue;
2016-11-19 21:03:51 +09:00
const SettingItemDescriptor *descriptor;
bool defaults;
2016-11-19 21:03:51 +09:00
std::vector<ISettingItemListener *> listeners;
2013-08-18 16:18:06 +09:00
void Load();
2013-08-18 16:18:06 +09:00
void Set(const std::string&);
void Set(int);
void Set(float);
2016-11-19 21:03:51 +09:00
void NotifyChange();
2013-08-18 16:18:06 +09:00
};
std::map<std::string, Item *> items;
bool loaded;
2013-08-18 16:18:06 +09:00
Settings();
Item *GetItem(const std::string& name,
2016-11-19 21:03:51 +09:00
const SettingItemDescriptor *descriptor);
void Save();
2013-08-18 16:18:06 +09:00
public:
static Settings *GetInstance();
class ItemHandle {
Item *item;
public:
2016-11-19 21:03:51 +09:00
ItemHandle(const std::string& name,
const SettingItemDescriptor *descriptor);
2013-08-18 16:18:06 +09:00
void operator =(const std::string&);
void operator =(int);
void operator =(float);
operator std::string();
operator float();
operator int();
operator bool();
const char *CString();
2016-11-19 21:03:51 +09:00
const SettingItemDescriptor &GetDescriptor();
void AddListener(ISettingItemListener *);
void RemoveListener(ISettingItemListener *);
2013-08-18 16:18:06 +09:00
};
void Load();
2013-08-18 16:18:06 +09:00
void Flush();
std::vector<std::string> GetAllItemNames();
};
/*
template<const char *name, const char *def>
class Setting: public Settings::ItemHandle {
public:
Setting(): Settings::ItemHandle(name, def, desc){
}
};*/
2014-04-06 22:42:17 +09:00
static inline bool operator ==(const std::string& str, Settings::ItemHandle& handle) {
2013-08-18 16:18:06 +09:00
return str == (std::string)handle;
}
2016-11-19 21:03:51 +09:00
// Define SettingItemDescriptor with external linkage so duplicates are
// detected as linker errors.
#define DEFINE_SPADES_SETTING(name, ...) \
spades::SettingItemDescriptor name ## _desc { __VA_ARGS__ }; \
static spades::Settings::ItemHandle name ( # name , & name ## _desc )
#define SPADES_SETTING(name) \
extern spades::SettingItemDescriptor name ## _desc; \
static spades::Settings::ItemHandle name ( # name , & name ## _desc )
}