openspades/Sources/Core/Settings.h

141 lines
3.8 KiB
C
Raw Normal View History

2013-08-29 11:45:22 +09:00
/*
Copyright (c) 2013 yvt
2013-08-29 11:45:22 +09:00
This file is part of OpenSpades.
2013-08-29 11:45:22 +09:00
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.
2013-08-29 11:45:22 +09:00
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.
2013-08-29 11:45:22 +09:00
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-29 11:45:22 +09:00
*/
2013-08-18 16:18:06 +09:00
#pragma once
#include <map>
#include <string>
2016-11-19 21:03:51 +09:00
#include <type_traits>
#include <vector>
2013-08-18 16:18:06 +09:00
namespace spades {
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(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;
const SettingItemDescriptor *descriptor;
bool defaults;
std::vector<ISettingItemListener *> listeners;
void Load();
void Set(const std::string &);
2013-08-18 16:18:06 +09:00
void Set(int);
void Set(float);
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, const SettingItemDescriptor *descriptor);
void Save();
2013-08-18 16:18:06 +09:00
public:
static Settings *GetInstance();
2013-08-18 16:18:06 +09:00
class ItemHandle {
Item *item;
2013-08-18 16:18:06 +09:00
public:
ItemHandle(const std::string &name, const SettingItemDescriptor *descriptor);
void operator=(const std::string &);
void operator=(int);
void operator=(float);
2013-08-18 16:18:06 +09:00
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){
}
2013-08-18 16:18:06 +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) static spades::Settings::ItemHandle name(#name, nullptr)
2016-11-19 21:03:51 +09:00
}