CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/ConfigSettings.h
Views: 1401
#pragma once12#include <cstdint>3#include <unordered_map>4#include "Core/ConfigValues.h"56class Path;7class Section; // ini file section8struct UrlEncoder;910enum class CfgFlag : u8 {11DEFAULT = 0,12DONT_SAVE = 1, // normally don't like negative flags, but these are really not many.13PER_GAME = 2,14REPORT = 4,15};16ENUM_CLASS_BITOPS(CfgFlag);1718struct ConfigSetting {19enum Type {20TYPE_TERMINATOR,21TYPE_BOOL,22TYPE_INT,23TYPE_UINT32,24TYPE_UINT64,25TYPE_FLOAT,26TYPE_STRING,27TYPE_TOUCH_POS,28TYPE_PATH,29TYPE_CUSTOM_BUTTON30};31union DefaultValue {32bool b;33int i;34uint32_t u;35uint64_t lu;36float f;37const char *s;38const char *p; // not sure how much point..39ConfigTouchPos touchPos;40ConfigCustomButton customButton;41};42union SettingPtr {43bool *b;44int *i;45uint32_t *u;46uint64_t *lu;47float *f;48std::string *s;49Path *p;50ConfigTouchPos *touchPos;51ConfigCustomButton *customButton;52};5354typedef bool (*BoolDefaultCallback)();55typedef int (*IntDefaultCallback)();56typedef uint32_t(*Uint32DefaultCallback)();57typedef uint64_t(*Uint64DefaultCallback)();58typedef float (*FloatDefaultCallback)();59typedef const char *(*StringDefaultCallback)();60typedef ConfigTouchPos(*TouchPosDefaultCallback)();61typedef const char *(*PathDefaultCallback)();62typedef ConfigCustomButton(*CustomButtonDefaultCallback)();6364union DefaultCallback {65BoolDefaultCallback b;66IntDefaultCallback i;67Uint32DefaultCallback u;68Uint64DefaultCallback lu;69FloatDefaultCallback f;70StringDefaultCallback s;71PathDefaultCallback p;72TouchPosDefaultCallback touchPos;73CustomButtonDefaultCallback customButton;74};7576ConfigSetting(const char *ini, bool *v, bool def, CfgFlag flags = CfgFlag::DEFAULT)77: iniKey_(ini), type_(TYPE_BOOL), flags_(flags) {78ptr_.b = v;79cb_.b = nullptr;80default_.b = def;81getPtrLUT()[v] = this;82}8384ConfigSetting(const char *ini, int *v, int def, CfgFlag flags = CfgFlag::DEFAULT)85: iniKey_(ini), type_(TYPE_INT), flags_(flags) {86ptr_.i = v;87cb_.i = nullptr;88default_.i = def;89getPtrLUT()[v] = this;90}9192ConfigSetting(const char *ini, int *v, int def, std::string(*transTo)(int), int (*transFrom)(const std::string &), CfgFlag flags = CfgFlag::DEFAULT)93: iniKey_(ini), type_(TYPE_INT), flags_(flags), translateTo_(transTo), translateFrom_(transFrom) {94ptr_.i = v;95cb_.i = nullptr;96default_.i = def;97getPtrLUT()[v] = this;98}99100ConfigSetting(const char *ini, uint32_t *v, uint32_t def, CfgFlag flags = CfgFlag::DEFAULT)101: iniKey_(ini), type_(TYPE_UINT32), flags_(flags) {102ptr_.u = v;103cb_.u = nullptr;104default_.u = def;105getPtrLUT()[v] = this;106}107108ConfigSetting(const char *ini, uint64_t *v, uint64_t def, CfgFlag flags = CfgFlag::DEFAULT)109: iniKey_(ini), type_(TYPE_UINT64), flags_(flags) {110ptr_.lu = v;111cb_.lu = nullptr;112default_.lu = def;113getPtrLUT()[v] = this;114}115116ConfigSetting(const char *ini, float *v, float def, CfgFlag flags = CfgFlag::DEFAULT)117: iniKey_(ini), type_(TYPE_FLOAT), flags_(flags) {118ptr_.f = v;119cb_.f = nullptr;120default_.f = def;121getPtrLUT()[v] = this;122}123124ConfigSetting(const char *ini, std::string *v, const char *def, CfgFlag flags = CfgFlag::DEFAULT)125: iniKey_(ini), type_(TYPE_STRING), flags_(flags) {126ptr_.s = v;127cb_.s = nullptr;128default_.s = def;129getPtrLUT()[v] = this;130}131132ConfigSetting(const char *ini, Path *v, const char *def, CfgFlag flags = CfgFlag::DEFAULT)133: iniKey_(ini), type_(TYPE_PATH), flags_(flags) {134ptr_.p = v;135cb_.p = nullptr;136default_.p = def;137getPtrLUT()[v] = this;138}139140ConfigSetting(const char *iniX, const char *iniY, const char *iniScale, const char *iniShow, ConfigTouchPos *v, ConfigTouchPos def, CfgFlag flags = CfgFlag::DEFAULT)141: iniKey_(iniX), ini2_(iniY), ini3_(iniScale), ini4_(iniShow), type_(TYPE_TOUCH_POS), flags_(flags) {142ptr_.touchPos = v;143cb_.touchPos = nullptr;144default_.touchPos = def;145getPtrLUT()[v] = this;146}147148ConfigSetting(const char *iniKey, const char *iniImage, const char *iniShape, const char *iniToggle, const char *iniRepeat, ConfigCustomButton *v, ConfigCustomButton def, CfgFlag flags = CfgFlag::DEFAULT)149: iniKey_(iniKey), ini2_(iniImage), ini3_(iniShape), ini4_(iniToggle), ini5_(iniRepeat), type_(TYPE_CUSTOM_BUTTON), flags_(flags) {150ptr_.customButton = v;151cb_.customButton = nullptr;152default_.customButton = def;153getPtrLUT()[v] = this;154}155156ConfigSetting(const char *ini, bool *v, BoolDefaultCallback def, CfgFlag flags = CfgFlag::DEFAULT)157: iniKey_(ini), type_(TYPE_BOOL), flags_(flags) {158ptr_.b = v;159cb_.b = def;160getPtrLUT()[v] = this;161}162163ConfigSetting(const char *ini, int *v, IntDefaultCallback def, CfgFlag flags = CfgFlag::DEFAULT)164: iniKey_(ini), type_(TYPE_INT), flags_(flags) {165ptr_.i = v;166cb_.i = def;167getPtrLUT()[v] = this;168}169170ConfigSetting(const char *ini, int *v, IntDefaultCallback def, std::string(*transTo)(int), int(*transFrom)(const std::string &), CfgFlag flags = CfgFlag::DEFAULT)171: iniKey_(ini), type_(TYPE_INT), flags_(flags), translateTo_(transTo), translateFrom_(transFrom) {172ptr_.i = v;173cb_.i = def;174getPtrLUT()[v] = this;175}176177ConfigSetting(const char *ini, uint32_t *v, Uint32DefaultCallback def, CfgFlag flags = CfgFlag::DEFAULT)178: iniKey_(ini), type_(TYPE_UINT32), flags_(flags) {179ptr_.u = v;180cb_.u = def;181getPtrLUT()[v] = this;182}183184ConfigSetting(const char *ini, float *v, FloatDefaultCallback def, CfgFlag flags = CfgFlag::DEFAULT)185: iniKey_(ini), type_(TYPE_FLOAT), flags_(flags) {186ptr_.f = v;187cb_.f = def;188getPtrLUT()[v] = this;189}190191ConfigSetting(const char *ini, std::string *v, StringDefaultCallback def, CfgFlag flags = CfgFlag::DEFAULT)192: iniKey_(ini), type_(TYPE_STRING), flags_(flags) {193ptr_.s = v;194cb_.s = def;195getPtrLUT()[v] = this;196}197198ConfigSetting(const char *iniX, const char *iniY, const char *iniScale, const char *iniShow, ConfigTouchPos *v, TouchPosDefaultCallback def, CfgFlag flags = CfgFlag::DEFAULT)199: iniKey_(iniX), ini2_(iniY), ini3_(iniScale), ini4_(iniShow), type_(TYPE_TOUCH_POS), flags_(flags) {200ptr_.touchPos = v;201cb_.touchPos = def;202getPtrLUT()[v] = this;203}204205// Should actually be called ReadFromIni or something.206bool Get(const Section *section) const;207208// Yes, this can be const because what's modified is not the ConfigSetting struct, but the value which is stored elsewhere.209// Should actually be called WriteToIni or something.210void Set(Section *section) const;211212void RestoreToDefault() const;213214void ReportSetting(UrlEncoder &data, const std::string &prefix) const;215216// Easy flag accessors.217bool PerGame() const { return flags_ & CfgFlag::PER_GAME; }218bool SaveSetting() const { return !(flags_ & CfgFlag::DONT_SAVE); }219bool Report() const { return flags_ & CfgFlag::REPORT; }220221const char *iniKey_ = nullptr;222const char *ini2_ = nullptr;223const char *ini3_ = nullptr;224const char *ini4_ = nullptr;225const char *ini5_ = nullptr;226227const Type type_;228229// Returns false if per-game settings are not currently used230static bool perGame(void *ptr);231232private:233CfgFlag flags_;234SettingPtr ptr_;235DefaultValue default_{};236DefaultCallback cb_;237238// We only support transform for ints.239std::string (*translateTo_)(int) = nullptr;240int (*translateFrom_)(const std::string &) = nullptr;241242static std::unordered_map<void*, ConfigSetting*>& getPtrLUT();243};244245struct ConfigSectionSettings {246const char *section;247const ConfigSetting *settings;248size_t settingsCount;249};250251252