Skip to content

Commit 556a19f

Browse files
committed
feat: FUUID and tweaks
1 parent 69d0662 commit 556a19f

14 files changed

Lines changed: 648 additions & 192 deletions

include/REX/FIniSettingStore.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
#ifdef COMMONLIB_OPTION_INI
44

5-
# include "REX/TSettingStore.h"
5+
# include "REX/FSettingStore.h"
6+
# include "REX/TSingleton.h"
67

78
namespace REX
89
{
910
class FIniSettingStore :
10-
public TSettingStore<FIniSettingStore>
11+
public FSettingStore,
12+
public TSingleton<FIniSettingStore>
1113
{
1214
public:
1315
virtual void Load() override;

include/REX/FJsonSettingStore.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
#ifdef COMMONLIB_OPTION_JSON
44

5-
# include "REX/TSettingStore.h"
5+
# include "REX/FSettingStore.h"
6+
# include "REX/TSingleton.h"
67

78
namespace REX
89
{
910
class FJsonSettingStore :
10-
public TSettingStore<FJsonSettingStore>
11+
public FSettingStore,
12+
public TSingleton<FJsonSettingStore>
1113
{
1214
public:
1315
virtual void Load() override;
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
#pragma once
22

33
#include "REX/ISettingStore.h"
4-
#include "REX/TSingleton.h"
54

65
namespace REX
76
{
8-
template <class T>
9-
class TSettingStore :
10-
public ISettingStore,
11-
public TSingleton<T>
7+
class FSettingStore :
8+
public ISettingStore
129
{
1310
public:
1411
virtual void Init(const char* a_fileBase, const char* a_fileUser) override
@@ -17,7 +14,7 @@ namespace REX
1714
m_fileUser = a_fileUser;
1815
}
1916

20-
virtual void Register(ISetting* a_setting) override
17+
virtual void Add(ISetting* a_setting) override
2118
{
2219
m_settings.emplace_back(a_setting);
2320
}

include/REX/FTomlSettingStore.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
#ifdef COMMONLIB_OPTION_TOML
44

5-
# include "REX/TSettingStore.h"
5+
# include "REX/FSettingStore.h"
6+
# include "REX/TSingleton.h"
67

78
namespace REX
89
{
910
class FTomlSettingStore :
10-
public TSettingStore<FTomlSettingStore>
11+
public FSettingStore,
12+
public TSingleton<FTomlSettingStore>
1113
{
1214
public:
1315
virtual void Load() override;

include/REX/FUUID.h

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#pragma once
2+
3+
namespace REX
4+
{
5+
class FUUID
6+
{
7+
public:
8+
inline constexpr FUUID() noexcept = default;
9+
inline constexpr FUUID(FUUID&&) noexcept = default;
10+
inline constexpr FUUID(const FUUID&) noexcept = default;
11+
12+
inline constexpr FUUID(std::uint8_t a_b1, std::uint8_t a_b2, std::uint8_t a_b3, std::uint8_t a_b4, std::uint8_t a_b5, std::uint8_t a_b6, std::uint8_t a_b7, std::uint8_t a_b8, std::uint8_t a_b9, std::uint8_t a_b10, std::uint8_t a_b11, std::uint8_t a_b12, std::uint8_t a_b13, std::uint8_t a_b14, std::uint8_t a_b15, std::uint8_t a_b16) noexcept :
13+
m_data{ a_b1, a_b2, a_b3, a_b4, a_b5, a_b6, a_b7, a_b8, a_b9, a_b10, a_b11, a_b12, a_b13, a_b14, a_b15, a_b16 }
14+
{}
15+
16+
template <std::size_t N>
17+
requires(N == 37)
18+
inline constexpr FUUID(const char (&a_string)[N]) noexcept
19+
{
20+
ParseString(a_string);
21+
}
22+
23+
inline constexpr FUUID& operator=(FUUID&&) noexcept = default;
24+
inline constexpr FUUID& operator=(const FUUID&) noexcept = default;
25+
26+
inline constexpr std::uint8_t& operator[](std::size_t a_pos) noexcept
27+
{
28+
return m_data[a_pos];
29+
}
30+
31+
inline constexpr const std::uint8_t operator[](std::size_t a_pos) const noexcept
32+
{
33+
return m_data[a_pos];
34+
}
35+
36+
inline constexpr bool operator==(const FUUID& a_rhs) const noexcept
37+
{
38+
for (auto i = 0u; i < sizeof(m_data); i++) {
39+
if (m_data[i] != a_rhs.m_data[i]) {
40+
return false;
41+
}
42+
}
43+
44+
return true;
45+
}
46+
47+
inline constexpr explicit operator bool() const noexcept
48+
{
49+
for (auto i = 0u; i < sizeof(m_data); i++) {
50+
if (m_data[i]) {
51+
return true;
52+
}
53+
}
54+
55+
return false;
56+
}
57+
58+
private:
59+
inline static constexpr std::uint8_t ParseStringHexChar(const char a_char)
60+
{
61+
const char c = a_char | 0x20;
62+
if (c >= '0' && c <= '9')
63+
return c - '0';
64+
else if (c >= 'a' && c <= 'f')
65+
return c - 'a' + 10;
66+
else
67+
throw "invalid hexadecimal character in FUUID";
68+
}
69+
70+
inline static constexpr std::uint8_t ParseStringHex(const char* a_str, const std::size_t a_pos)
71+
{
72+
return (ParseStringHexChar(a_str[a_pos]) << 4) + ParseStringHexChar(a_str[a_pos + 1]);
73+
}
74+
75+
inline constexpr void ParseString(const char* a_str)
76+
{
77+
//constexpr std::size_t parse_string_table_le[16]{
78+
// 6, 4, 2, 0, 11, 9, 16, 14, 19, 21, 24, 26, 28, 30, 32, 34
79+
//};
80+
81+
constexpr std::size_t parse_string_table_be[16]{
82+
0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34
83+
};
84+
85+
for (auto i = 0u; i < sizeof(m_data); i++) {
86+
m_data[i] = ParseStringHex(a_str, parse_string_table_be[i]);
87+
}
88+
}
89+
90+
private:
91+
std::uint8_t m_data[16];
92+
};
93+
static_assert(sizeof(FUUID) == 16);
94+
static_assert(FUUID{ "62470f2d-f92b-4189-8af6-157310dadc8b" } == FUUID{ 0x62, 0x47, 0x0f, 0x2d, 0xf9, 0x2b, 0x41, 0x89, 0x8a, 0xf6, 0x15, 0x73, 0x10, 0xda, 0xdc, 0x8b });
95+
}

include/REX/ISettingStore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ namespace REX
77
struct ISettingStore
88
{
99
virtual void Init(const char* a_file, const char* a_fileCustom) = 0;
10+
virtual void Add(ISetting* a_setting) = 0;
1011
virtual void Load() = 0;
1112
virtual void Save() = 0;
12-
virtual void Register(ISetting* a_setting) = 0;
1313
};
1414
}

include/REX/REX.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include "FMemoryMap.h"
88
#include "FModule.h"
99
#include "FModuleSection.h"
10+
#include "FSettingStore.h"
1011
#include "FTomlSettingStore.h"
12+
#include "FUUID.h"
1113
#include "HASH.h"
1214
#include "ISetting.h"
1315
#include "ISettingStore.h"
@@ -19,7 +21,6 @@
1921
#include "TJsonSetting.h"
2022
#include "TScopeExit.h"
2123
#include "TSetting.h"
22-
#include "TSettingStore.h"
2324
#include "TSingleton.h"
2425
#include "TStaticString.h"
2526
#include "TTomlSetting.h"

include/REX/TIniSetting.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,49 @@
88
namespace REX::Impl
99
{
1010
template <class T>
11-
void IniSettingLoad(void* a_file, std::string_view a_section, std::string_view a_key, T& a_value, T& a_valueDefault);
11+
void IniSettingLoad(void* a_data, std::string_view a_section, std::string_view a_key, T& a_value, T& a_valueDefault);
1212

1313
template <class T>
14-
void IniSettingSave(void* a_file, std::string_view a_section, std::string_view a_key, T& a_value);
14+
void IniSettingSave(void* a_data, std::string_view a_section, std::string_view a_key, T& a_value);
1515
}
1616

1717
namespace REX
1818
{
1919
template <class T, class S = FIniSettingStore>
2020
class TIniSetting :
21-
public TSetting<T, S>
21+
public TSetting<T>
2222
{
2323
public:
2424
TIniSetting(std::string_view a_section, std::string_view a_key, T a_default) :
25-
TSetting<T, S>(a_default),
25+
TSetting<T>(a_default),
2626
m_section(a_section),
2727
m_key(a_key)
28-
{}
28+
{
29+
S::GetSingleton()->Add(this);
30+
}
31+
32+
TIniSetting(std::string_view a_key, T a_default) :
33+
TSetting<T>(a_default),
34+
m_section(),
35+
m_key(a_key)
36+
{
37+
S::GetSingleton()->Add(this);
38+
}
2939

3040
public:
3141
virtual void Load(void* a_data, bool a_isBase) override
3242
{
3343
if (a_isBase) {
34-
Impl::IniSettingLoad(a_data, m_section, m_key, this->m_valueDefault, this->m_valueDefault);
44+
Impl::IniSettingLoad<T>(a_data, m_section, m_key, this->m_valueDefault, this->m_valueDefault);
3545
this->SetValue(this->m_valueDefault);
3646
} else {
37-
Impl::IniSettingLoad(a_data, m_section, m_key, this->m_value, this->m_valueDefault);
47+
Impl::IniSettingLoad<T>(a_data, m_section, m_key, this->m_value, this->m_valueDefault);
3848
}
3949
}
4050

4151
virtual void Save(void* a_data) override
4252
{
43-
Impl::IniSettingSave(a_data, m_section, m_key, this->m_value);
53+
Impl::IniSettingSave<T>(a_data, m_section, m_key, this->m_value);
4454
}
4555

4656
private:

include/REX/TJsonSetting.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,40 @@
88
namespace REX::Impl
99
{
1010
template <class T>
11-
void JsonSettingLoad(void* a_file, std::string_view a_path, T& a_value, T& a_valueDefault);
11+
void JsonSettingLoad(void* a_data, std::string_view a_path, T& a_value, T& a_valueDefault);
1212

1313
template <class T>
14-
void JsonSettingSave(void* a_file, std::string_view a_path, T& a_value);
14+
void JsonSettingSave(void* a_data, std::string_view a_path, T& a_value);
1515
}
1616

1717
namespace REX
1818
{
1919
template <class T, class S = FJsonSettingStore>
2020
class TJsonSetting :
21-
public TSetting<T, S>
21+
public TSetting<T>
2222
{
2323
public:
2424
TJsonSetting(std::string_view a_path, T a_default) :
25-
TSetting<T, S>(a_default),
25+
TSetting<T>(a_default),
2626
m_path(a_path)
27-
{}
27+
{
28+
S::GetSingleton()->Add(this);
29+
}
2830

2931
public:
3032
virtual void Load(void* a_data, bool a_isBase) override
3133
{
3234
if (a_isBase) {
33-
Impl::JsonSettingLoad(a_data, m_path, this->m_valueDefault, this->m_valueDefault);
35+
Impl::JsonSettingLoad<T>(a_data, m_path, this->m_valueDefault, this->m_valueDefault);
3436
this->SetValue(this->m_valueDefault);
3537
} else {
36-
Impl::JsonSettingLoad(a_data, m_path, this->m_value, this->m_valueDefault);
38+
Impl::JsonSettingLoad<T>(a_data, m_path, this->m_value, this->m_valueDefault);
3739
}
3840
}
3941

4042
virtual void Save(void* a_data) override
4143
{
42-
Impl::JsonSettingSave(a_data, m_path, this->m_value);
44+
Impl::JsonSettingSave<T>(a_data, m_path, this->m_value);
4345
}
4446

4547
private:

include/REX/TSetting.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#pragma once
22

3-
#include "REX/ISetting.h"
3+
#include "ISetting.h"
44

55
namespace REX
66
{
7-
template <class T, class Store>
7+
template <class T>
88
class TSetting :
99
public ISetting
1010
{
@@ -14,16 +14,23 @@ namespace REX
1414
TSetting(T a_default) :
1515
m_value(a_default),
1616
m_valueDefault(a_default)
17-
{
18-
Store::GetSingleton()->Register(this);
19-
}
17+
{}
2018

2119
public:
22-
T GetValue() const { return m_value; }
20+
T GetValue() const
21+
{
22+
return m_value;
23+
}
2324

24-
T GetValueDefault() const { return m_valueDefault; }
25+
T GetValueDefault() const
26+
{
27+
return m_valueDefault;
28+
}
2529

26-
void SetValue(T a_value) { m_value = a_value; }
30+
void SetValue(T a_value)
31+
{
32+
m_value = a_value;
33+
}
2734

2835
public:
2936
operator T&() { return m_value; }

0 commit comments

Comments
 (0)