Changeset 25326
- Timestamp:
- Apr 27, 2021, 9:45:40 AM (3 years ago)
- Location:
- ps/trunk/source
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
ps/trunk/source/graphics/ObjectManager.cpp
r25308 r25326 52 52 RegisterFileReloadFunc(ReloadChangedFileCB, this); 53 53 54 m_QualityHook = std::make_unique<CConfigDBHook>(g_ConfigDB.RegisterHookAndCall("max_actor_quality", [this]() { this->ActorQualityChanged(); }));54 m_QualityHook = std::make_unique<CConfigDBHook>(g_ConfigDB.RegisterHookAndCall("max_actor_quality", [this]() { ActorQualityChanged(); })); 55 55 56 56 if (!CXeromyces::AddValidator(g_VFS, "actor", "art/actors/actor.rng")) … … 61 61 { 62 62 UnloadObjects(); 63 64 g_ConfigDB.UnregisterHook(std::move(m_QualityHook));65 63 66 64 UnregisterFileReloadFunc(ReloadChangedFileCB, this); -
ps/trunk/source/gui/tests/test_GuiManager.h
r25169 r25326 27 27 #include "ps/XML/Xeromyces.h" 28 28 29 30 29 31 class TestGuiManager : public CxxTest::TestSuite 30 32 { 31 CConfigDB*configDB;33 configDB; 32 34 public: 33 35 … … 38 40 TS_ASSERT_OK(g_VFS->Mount(L"cache", DataDir() / "_testcache" / "", 0, VFS_MAX_PRIORITY)); 39 41 40 configDB = new CConfigDB;42 configDB = ; 41 43 42 44 CXeromyces::Startup(); … … 49 51 delete g_GUI; 50 52 CXeromyces::Terminate(); 51 delete configDB;53 ; 52 54 g_VFS.reset(); 53 55 DeleteDirectory(DataDir()/"_testcache"); … … 190 192 TS_ASSERT_EQUALS(hotkey_pressed_value, false); 191 193 192 configDB->RemoveValue(CFG_SYSTEM, test_hotkey_name);193 194 UnloadHotkeys(); 194 195 } -
ps/trunk/source/ps/ConfigDB.cpp
r25279 r25326 38 38 } 39 39 40 std::recursive_mutex g_ConfigDBMutex;41 42 40 // These entries will not be printed to logfiles, so that logfiles can be shared without leaking personal or sensitive data 43 41 const std::unordered_set<std::string> g_UnloggedEntries = { … … 90 88 91 89 typedef std::map<CStr, CConfigValueSet> TConfigMap; 92 TConfigMap CConfigDB::m_Map[CFG_LAST];93 VfsPath CConfigDB::m_ConfigFile[CFG_LAST];94 bool CConfigDB::m_HasChanges[CFG_LAST];95 96 std::multimap<CStr, std::function<void()>> CConfigDB::m_Hooks;97 90 98 91 #define GETVAL(type)\ … … 100 93 {\ 101 94 CHECK_NS(;);\ 102 std::lock_guard<std::recursive_mutex> s( g_ConfigDBMutex);\95 std::lock_guard<std::recursive_mutex> s(Mutex);\ 103 96 TConfigMap::iterator it = m_Map[CFG_COMMAND].find(name);\ 104 97 if (it != m_Map[CFG_COMMAND].end())\ … … 127 120 #undef GETVAL 128 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 129 152 bool CConfigDB::HasChanges(EConfigNamespace ns) const 130 153 { 131 154 CHECK_NS(false); 132 155 133 std::lock_guard<std::recursive_mutex> s( g_ConfigDBMutex);156 std::lock_guard<std::recursive_mutex> s(Mutex); 134 157 return m_HasChanges[ns]; 135 158 } … … 139 162 CHECK_NS(;); 140 163 141 std::lock_guard<std::recursive_mutex> s( g_ConfigDBMutex);164 std::lock_guard<std::recursive_mutex> s(Mutex); 142 165 m_HasChanges[ns] = value; 143 166 } … … 147 170 CHECK_NS(;); 148 171 149 std::lock_guard<std::recursive_mutex> s( g_ConfigDBMutex);150 TConfigMap:: iterator it = m_Map[CFG_COMMAND].find(name);172 std::lock_guard<std::recursive_mutex> s(Mutex); 173 TConfigMap::iterator it = m_Map[CFG_COMMAND].find(name); 151 174 if (it != m_Map[CFG_COMMAND].end()) 152 175 { … … 170 193 CHECK_NS(CFG_LAST); 171 194 172 std::lock_guard<std::recursive_mutex> s( g_ConfigDBMutex);173 TConfigMap:: iterator it = m_Map[CFG_COMMAND].find(name);195 std::lock_guard<std::recursive_mutex> s(Mutex); 196 TConfigMap::iterator it = m_Map[CFG_COMMAND].find(name); 174 197 if (it != m_Map[CFG_COMMAND].end()) 175 198 return CFG_COMMAND; … … 187 210 std::map<CStr, CConfigValueSet> CConfigDB::GetValuesWithPrefix(EConfigNamespace ns, const CStr& prefix) const 188 211 { 189 std::lock_guard<std::recursive_mutex> s( g_ConfigDBMutex);212 std::lock_guard<std::recursive_mutex> s(Mutex); 190 213 std::map<CStr, CConfigValueSet> ret; 191 214 … … 210 233 CHECK_NS(;); 211 234 212 std::lock_guard<std::recursive_mutex> s( g_ConfigDBMutex);235 std::lock_guard<std::recursive_mutex> s(Mutex); 213 236 TConfigMap::iterator it = m_Map[ns].find(name); 214 237 if (it == m_Map[ns].end()) … … 233 256 CHECK_NS(;); 234 257 235 std::lock_guard<std::recursive_mutex> s( g_ConfigDBMutex);258 std::lock_guard<std::recursive_mutex> s(Mutex); 236 259 TConfigMap::iterator it = m_Map[ns].find(name); 237 260 if (it == m_Map[ns].end()) … … 245 268 CHECK_NS(;); 246 269 247 std::lock_guard<std::recursive_mutex> s( g_ConfigDBMutex);270 std::lock_guard<std::recursive_mutex> s(Mutex); 248 271 TConfigMap::iterator it = m_Map[ns].find(name); 249 272 if (it == m_Map[ns].end()) … … 258 281 CHECK_NS(;); 259 282 260 std::lock_guard<std::recursive_mutex> s( g_ConfigDBMutex);283 std::lock_guard<std::recursive_mutex> s(Mutex); 261 284 m_ConfigFile[ns] = path; 262 285 } … … 266 289 CHECK_NS(false); 267 290 268 std::lock_guard<std::recursive_mutex> s( g_ConfigDBMutex);291 std::lock_guard<std::recursive_mutex> s(Mutex); 269 292 270 293 shared_ptr<u8> buffer; … … 429 452 CHECK_NS(false); 430 453 431 std::lock_guard<std::recursive_mutex> s( g_ConfigDBMutex);454 std::lock_guard<std::recursive_mutex> s(Mutex); 432 455 return WriteFile(ns, m_ConfigFile[ns]); 433 456 } … … 437 460 CHECK_NS(false); 438 461 439 std::lock_guard<std::recursive_mutex> s( g_ConfigDBMutex);462 std::lock_guard<std::recursive_mutex> s(Mutex); 440 463 shared_ptr<u8> buf; 441 464 AllocateAligned(buf, 1*MiB, maxSectorSize); … … 469 492 CHECK_NS(false); 470 493 471 std::lock_guard<std::recursive_mutex> s( g_ConfigDBMutex);494 std::lock_guard<std::recursive_mutex> s(Mutex); 472 495 return WriteValueToFile(ns, name, value, m_ConfigFile[ns]); 473 496 } … … 477 500 CHECK_NS(false); 478 501 479 std::lock_guard<std::recursive_mutex> s( g_ConfigDBMutex);502 std::lock_guard<std::recursive_mutex> s(Mutex); 480 503 481 504 TConfigMap newMap; … … 492 515 { 493 516 hook(); 517 494 518 return CConfigDBHook(*this, m_Hooks.emplace(name, hook)); 495 519 } … … 498 522 { 499 523 if (hook.m_Ptr != m_Hooks.end()) 524 525 500 526 m_Hooks.erase(hook.m_Ptr); 527 528 501 529 } 502 530 -
ps/trunk/source/ps/ConfigDB.h
r25279 r25326 30 30 #include "lib/file/vfs/vfs_path.h" 31 31 #include "ps/CStr.h" 32 #include "ps/Singleton.h" 33 32 33 #include <array> 34 34 #include <map> 35 36 35 37 #include <vector> 36 38 … … 60 62 class CConfigDBHook; 61 63 62 #define g_ConfigDB CConfigDB::GetSingleton()63 64 class CConfigDB : public Singleton<CConfigDB>64 #define g_ConfigDB ) 65 66 class CConfigDB 65 67 { 66 68 friend CConfigDBHook; 67 69 public: 70 71 72 73 74 75 76 77 78 79 68 80 /** 69 81 * Attempt to retrieve the value of a config variable with the given name; … … 179 191 * This is simple on purpose, the hook is responsible for checking if it should do something. 180 192 * When RegisterHookAndCall is called, the hook is immediately triggered. 181 */ 182 CConfigDBHook RegisterHookAndCall(const CStr& name, std::function<void()> hook); 193 * NB: CConfigDBHook will auto-unregister the hook when destroyed, 194 * so you can use it to tie the lifetime of the hook to your object. 195 * The hook will be deleted alongside ConfigDB anyways. 196 */ 197 [[nodiscard]] CConfigDBHook RegisterHookAndCall(const CStr& name, std::function<void()> hook); 183 198 184 199 void UnregisterHook(CConfigDBHook&& hook); … … 186 201 187 202 private: 188 static std::map<CStr, CConfigValueSet> m_Map[]; 189 static std::multimap<CStr, std::function<void()>> m_Hooks; 190 static VfsPath m_ConfigFile[]; 191 static bool m_HasChanges[]; 203 std::array<std::map<CStr, CConfigValueSet>, CFG_LAST> m_Map; 204 std::multimap<CStr, std::function<void()>> m_Hooks; 205 std::array<VfsPath, CFG_LAST> m_ConfigFile; 206 std::array<bool, CFG_LAST> m_HasChanges; 207 208 mutable std::recursive_mutex m_Mutex; 192 209 }; 193 210 … … 197 214 public: 198 215 CConfigDBHook() = delete; 216 199 217 // Point the moved-from hook to end, which is checked for in UnregisterHook, 200 218 // to avoid a double-erase error. 201 CConfigDBHook(CConfigDBHook&& h) : m_ConfigDB(h.m_ConfigDB) { m_Ptr = std::move(h.m_Ptr); h.m_Ptr = m_ConfigDB.m_Hooks.end(); } 202 CConfigDBHook(const CConfigDBHook&) = delete; 219 CConfigDBHook(CConfigDBHook&& h) : m_ConfigDB(h.m_ConfigDB) 220 { 221 m_Ptr = std::move(h.m_Ptr); 222 h.m_Ptr = m_ConfigDB.m_Hooks.end(); 223 } 224 // Unregisters the hook. Must be called before the original ConfigDB gets deleted. 225 ~CConfigDBHook() 226 { 227 m_ConfigDB.UnregisterHook(std::move(*this)); 228 } 203 229 private: 204 CConfigDBHook(CConfigDB& cdb, std::multimap<CStr, std::function<void()>>::iterator p) : m_ConfigDB(cdb), m_Ptr(p) {}; 230 CConfigDBHook(CConfigDB& cdb, std::multimap<CStr, std::function<void()>>::iterator p) 231 : m_ConfigDB(cdb), m_Ptr(p) 232 {}; 205 233 206 234 std::multimap<CStr, std::function<void()>>::iterator m_Ptr; -
ps/trunk/source/ps/GameSetup/Config.cpp
r23747 r25326 1 /* Copyright (C) 202 0Wildfire Games.1 /* Copyright (C) 202 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * … … 127 127 TIMER(L"CONFIG_Init"); 128 128 129 new CConfigDB;129 ; 130 130 131 131 // Load the global, default config file -
ps/trunk/source/ps/GameSetup/GameSetup.cpp
r25231 r25326 720 720 from_config: 721 721 TIMER_BEGIN(L"shutdown ConfigDB"); 722 delete &g_ConfigDB;722 ; 723 723 TIMER_END(L"shutdown ConfigDB"); 724 724 -
ps/trunk/source/ps/Joystick.cpp
r17470 r25326 1 /* Copyright (C) 20 14Wildfire Games.1 /* Copyright (C) 20 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * … … 34 34 { 35 35 bool joystickEnable = false; 36 37 36 38 CFG_GET_VAL("joystick.enable", joystickEnable); 37 39 if (!joystickEnable) -
ps/trunk/source/ps/tests/test_ConfigDB.h
r25107 r25326 21 21 #include "ps/ConfigDB.h" 22 22 23 24 23 25 extern PIVFS g_VFS; 24 26 25 27 class TestConfigDB : public CxxTest::TestSuite 26 28 { 27 CConfigDB*configDB;29 configDB; 28 30 public: 29 31 … … 33 35 TS_ASSERT_OK(g_VFS->Mount(L"config", DataDir() / "_testconfig" / "")); 34 36 35 configDB = new CConfigDB;37 configDB = ; 36 38 } 37 39 … … 40 42 DeleteDirectory(DataDir()/"_testconfig"); 41 43 g_VFS.reset(); 42 43 delete configDB; 44 configDB.reset(); 44 45 } 45 46 -
ps/trunk/source/ps/tests/test_Hotkeys.h
r25180 r25326 32 32 class TestHotkey : public CxxTest::TestSuite 33 33 { 34 CConfigDB*configDB;34 configDB; 35 35 // Stores whether one of these was sent in the last fakeInput call. 36 36 bool hotkeyPress = false; … … 65 65 TS_ASSERT_OK(g_VFS->Mount(L"cache", DataDir() / "_testcache" / "")); 66 66 67 configDB = new CConfigDB;67 configDB = ; 68 68 69 69 g_scancodes = {}; … … 72 72 void tearDown() 73 73 { 74 delete configDB;74 ; 75 75 g_VFS.reset(); 76 76 DeleteDirectory(DataDir()/"_testcache"); -
ps/trunk/source/renderer/RenderingOptions.cpp
r25210 r25326 186 186 void CRenderingOptions::ClearHooks() 187 187 { 188 if (CConfigDB::IsInitialised())189 for (CConfigDBHook& hook : *m_ConfigHooks)190 g_ConfigDB.UnregisterHook(std::move(hook));191 188 m_ConfigHooks->clear(); 192 189 } -
ps/trunk/source/renderer/RenderingOptions.h
r24651 r25326 28 28 #define INCLUDED_RENDERINGOPTIONS 29 29 30 30 31 class CStr8; 31 32 class CRenderer;
Note:
See TracChangeset
for help on using the changeset viewer.