CommonLibSSE (powerof3)
TOML.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "REX/REX/Setting.h"
4 
5 #ifdef REX_OPTION_TOML
6 namespace REX::TOML
7 {
8  using sec_t = std::vector<std::string>;
9  using key_t = std::string_view;
10 
11  namespace Impl
12  {
13  template <class T>
14  void SettingLoad(void* a_file, sec_t a_section, key_t a_key, T& a_value, T& a_valueDefault);
15 
16  template <class T>
17  void SettingSave(void* a_file, sec_t a_section, key_t a_key, T& a_value);
18  }
19 
20  class SettingStore :
21  public TSettingStore<SettingStore>
22  {
23  public:
24  virtual void Load() override;
25  virtual void Save() override;
26  };
27 
28  template <class T, class Store = SettingStore>
29  class Setting :
30  public TSetting<T, Store>
31  {
32  public:
33  Setting(key_t a_key, T a_default) :
34  TSetting<T, Store>(a_default),
35  m_section(),
36  m_key(a_key)
37  {}
38 
39  Setting(std::string_view a_section, key_t a_key, T a_default) :
40  TSetting<T, Store>(a_default),
41  m_section(),
42  m_key(a_key)
43  {
44  for (const auto token : std::ranges::split_view{ a_section, std::string_view{ "." } }) {
45  m_section.emplace_back(token.data(), token.size());
46  }
47  }
48 
49  Setting(std::initializer_list<std::string> a_section, key_t a_key, T a_default) :
50  TSetting<T, Store>(a_default),
51  m_section(a_section),
52  m_key(a_key)
53  {}
54 
55  public:
56  virtual void Load(void* a_data, bool a_isBase) override
57  {
58  if (a_isBase) {
59  Impl::SettingLoad(a_data, m_section, m_key, this->m_valueDefault, this->m_valueDefault);
60  this->SetValue(this->m_valueDefault);
61  } else {
62  Impl::SettingLoad(a_data, m_section, m_key, this->m_value, this->m_valueDefault);
63  }
64  }
65 
66  virtual void Save(void* a_data) override
67  {
68  Impl::SettingSave(a_data, m_section, m_key, this->m_value);
69  }
70 
71  private:
72  sec_t m_section;
73  key_t m_key;
74  };
75 
76  template <class Store = SettingStore>
77  using Bool = Setting<bool, Store>;
78 
79  template <class Store = SettingStore>
80  using F32 = Setting<float, Store>;
81 
82  template <class Store = SettingStore>
83  using F64 = Setting<double, Store>;
84 
85  template <class Store = SettingStore>
86  using I8 = Setting<std::int8_t, Store>;
87 
88  template <class Store = SettingStore>
89  using I16 = Setting<std::int16_t, Store>;
90 
91  template <class Store = SettingStore>
92  using I32 = Setting<std::int32_t, Store>;
93 
94  template <class Store = SettingStore>
95  using U8 = Setting<std::uint8_t, Store>;
96 
97  template <class Store = SettingStore>
98  using U16 = Setting<std::uint16_t, Store>;
99 
100  template <class Store = SettingStore>
101  using U32 = Setting<std::uint32_t, Store>;
102 
103  template <class Store = SettingStore>
104  using Str = Setting<std::string, Store>;
105 }
106 
107 template <class T, class S, class CharT>
108 struct std::formatter<REX::TOML::Setting<T, S>, CharT> : std::formatter<T, CharT>
109 {
110  template <class FormatContext>
111  auto format(const REX::TOML::Setting<T, S>& a_setting, FormatContext& a_ctx) const
112  {
113  return std::formatter<T, CharT>::format(a_setting.GetValue(), a_ctx);
114  }
115 };
116 #endif
Definition: BSDirectInputManager.h:8
constexpr REL::ID Save(static_cast< std::uint64_t >(34818))
constexpr REL::ID Load(static_cast< std::uint64_t >(34819))