comparison foosdk/sdk/foobar2000/SDK/configStore.cpp @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #include "foobar2000-sdk-pch.h"
2 #include "configStore.h"
3 #ifdef FOOBAR2000_MOBILE
4 #include "appearance.h"
5 #endif
6
7 using namespace fb2k;
8
9 namespace {
10 class configStoreNotifyImpl : public configStoreNotify {
11 public:
12 void onVarChange() { f(); }
13 std::function< void() > f;
14 };
15 }
16
17 objRef configStore::addNotify( const char * name, std::function<void () > f ) {
18 auto nameRef = makeString(name);
19 ptr self = this;
20 auto obj = new configStoreNotifyImpl;
21 obj->f = f;
22 this->addNotify(name, obj );
23 return callOnRelease([self, nameRef, obj] {
24 self->removeNotify( nameRef->c_str(), obj );
25 delete obj;
26 });
27 }
28 void configStore::addPermanentNotify( const char * name, std::function<void () > f ) {
29 auto obj = new configStoreNotifyImpl;
30 obj->f = f;
31 this->addNotify(name, obj );
32 }
33
34 bool configStore::getConfigBool( const char * name, bool defVal ) {
35 return this->getConfigInt( name, defVal ? 1 : 0) != 0;
36 }
37
38 bool configStore::toggleConfigBool (const char * name, bool defVal ) {
39 bool newVal = ! getConfigBool(name, defVal);
40 setConfigBool( name, newVal );
41 return newVal;
42 }
43
44 void configStore::setConfigBool( const char * name, bool val ) {
45 this->setConfigInt( name, val ? 1 : 0 );
46 }
47 void configStore::deleteConfigBool( const char * name ) {
48 this->deleteConfigInt( name );
49 }
50
51 GUID configStore::getConfigGUID(const char* name, const GUID& defVal) {
52 auto str = this->getConfigString(name, nullptr);
53 if (str.is_empty()) return defVal;
54 try {
55 return pfc::GUID_from_text(str->c_str());
56 } catch (...) {
57 return defVal;
58 }
59 }
60
61 void configStore::setConfigGUID(const char* name, const GUID& val) {
62 this->setConfigString(name, pfc::print_guid(val));
63 }
64
65 void configStore::deleteConfigGUID(const char* name) {
66 this->deleteConfigString(name);
67 }
68
69
70 configEventHandle_t configEvent::operator+=(std::function< void() > f) const {
71 auto obj = new configStoreNotifyImpl;
72 obj->f = f;
73 try {
74 static_api_ptr_t<configStore>()->addNotify(m_name, obj);
75 } catch (...) {
76 delete obj; throw;
77 }
78 return reinterpret_cast<configEventHandle_t>(obj);
79 }
80
81 void configEvent::operator-=(configEventHandle_t h) const {
82 auto obj = reinterpret_cast<configStoreNotifyImpl*>(h);
83 if ( core_api::are_services_available() ) {
84 static_api_ptr_t<configStore>()->removeNotify(m_name, obj);
85 }
86 delete obj;
87 }
88
89 configEventRef & configEventRef::operator<< ( const char * name ) { m_name = name; return *this; }
90
91 configEventRef & configEventRef::operator<< ( std::function<void () > f ) {
92 setFunction(f);
93 return *this;
94 }
95
96 configEventRef::~configEventRef() {
97 try {
98 clear();
99 } catch(...) {
100 // corner case: called from worker while app is shutting down, don't crash
101 }
102 }
103
104 void configEventRef::clear() {
105 if (m_handle != nullptr) {
106 configEvent( m_name ) -= m_handle;
107 m_handle = nullptr;
108 }
109 }
110 void configEventRef::set( const char * name, std::function<void () > f ) {
111 clear();
112 m_name = name;
113 m_handle = configEvent( m_name ) += f;
114 }
115
116 void configEventRef::setName( const char * name ) {
117 clear();
118 m_name = name;
119 }
120
121 void configEventRef::setFunction(std::function<void ()> f) {
122 clear();
123 PFC_ASSERT( m_name.length() > 0 );
124 m_handle = configEvent( m_name ) += f;
125 }
126
127 #ifdef FOOBAR2000_MOBILE
128 namespace {
129 class appearanceChangeNoitfyImpl : public appearanceChangeNoitfy {
130 public:
131
132 void onAppearanceChange() {
133 f();
134 }
135
136 std::function<void () > f;
137 };
138 }
139
140 appearance::notifyHandle_t appearance::addNotify2( std::function<void () > f) {
141 auto ret = new appearanceChangeNoitfyImpl();
142 ret->f = f;
143 addNotify(ret);
144 return reinterpret_cast<notifyHandle_t>(ret);
145 }
146 void appearance::removeNotify2( appearance::notifyHandle_t h ) {
147 auto obj = reinterpret_cast<appearanceChangeNoitfyImpl*>(h);
148 removeNotify(obj);
149 delete obj;
150 }
151
152 void appearanceChangeNotifyRef::set( std::function<void()> f ) {
153 clear();
154 handle = static_api_ptr_t<appearance>()->addNotify2(f);
155 }
156 void appearanceChangeNotifyRef::clear() {
157 if (isSet()) {
158 if ( core_api::are_services_available() ) {
159 static_api_ptr_t<appearance>()->removeNotify2(handle);
160 }
161 handle = nullptr;
162 }
163 }
164 #endif