|
1
|
1 #pragma once
|
|
|
2
|
|
|
3 #define FOOBAR2000_HAVE_CONFIGSTORE FOOBAR2020
|
|
|
4
|
|
|
5 #include <functional>
|
|
|
6 #include "commonObjects.h"
|
|
|
7
|
|
|
8 namespace fb2k {
|
|
|
9
|
|
|
10 class configStoreNotify {
|
|
|
11 public:
|
|
|
12 virtual void onVarChange() = 0;
|
|
|
13 };
|
|
|
14
|
|
|
15 typedef void * configStoreNotifyHandle_t;
|
|
|
16
|
|
|
17 //! \since 2.0
|
|
|
18 //! Interface to access foobar2000's configuration store, backed by SQLite database. \n
|
|
|
19 //! get* methods can be called at any time. set*/delete* \n
|
|
|
20 //! set*/delete* methods will trigger immediate commit when invoked without a transaction scope. \n
|
|
|
21 //! Use commitBlocking() to commit synchronously to be sure that the data has been flushed before continuing execution.
|
|
|
22 class configStore : public service_base {
|
|
|
23 FB2K_MAKE_SERVICE_COREAPI( configStore );
|
|
|
24 public:
|
|
|
25 //! Causes multiple writes to be chained together. \n
|
|
|
26 //! Use of this is no longer essential since late foobar2000 v2.0 betas, as delay-write cache is always used. \n
|
|
|
27 //! You can still use it to guarantee that multiple updates are written together, that is either all or none are saved, should the system or application crash.
|
|
|
28 virtual fb2k::objRef acquireTransactionScope() = 0;
|
|
|
29
|
|
|
30 //! Synchronously flushes changes to disk. Doesn't return until changes have actually been written. \n
|
|
|
31 //! Use of this is strongly recommended against.
|
|
|
32 virtual void commitBlocking() = 0;
|
|
|
33
|
|
|
34 virtual int64_t getConfigInt( const char * name, int64_t defVal = 0 ) = 0;
|
|
|
35 virtual void setConfigInt( const char * name, int64_t val ) = 0;
|
|
|
36 virtual void deleteConfigInt(const char * name) = 0;
|
|
|
37
|
|
|
38 virtual fb2k::stringRef getConfigString( const char * name, fb2k::stringRef defaVal = fb2k::string::stringWithString("")) = 0;
|
|
|
39 virtual void setConfigString( const char * name, const char * value ) = 0;
|
|
|
40 virtual void deleteConfigString(const char * name) = 0;
|
|
|
41
|
|
|
42 virtual fb2k::memBlockRef getConfigBlob( const char * name, fb2k::memBlockRef defVal = fb2k::memBlock::empty()) = 0;
|
|
|
43 virtual void setConfigBlob(const char* name, const void* ptr, size_t bytes) = 0;
|
|
|
44 virtual void setConfigBlob(const char* name, fb2k::memBlockRef val) = 0;
|
|
|
45 virtual void deleteConfigBlob(const char * name) = 0;
|
|
|
46
|
|
|
47 virtual double getConfigFloat( const char * name, double defVal = 0) = 0;
|
|
|
48 virtual void setConfigFloat( const char * name, double val ) = 0;
|
|
|
49 virtual void deleteConfigFloat( const char * name ) = 0;
|
|
|
50
|
|
|
51 virtual void addNotify(const char * name, configStoreNotify * notify) = 0;
|
|
|
52 virtual void removeNotify(const char * name, configStoreNotify * notify) = 0;
|
|
|
53
|
|
|
54 //! Lists values of any type in the specified domain.
|
|
|
55 //! For an example, calling with domain "foo" will return all foo.* keys, such as: foo.bar, foo.bar.2000, foo.asdf. Will not return "foobar".
|
|
|
56 virtual fb2k::arrayRef listDomainValues(const char* domain, bool withSubdomains) = 0;
|
|
|
57
|
|
|
58 objRef addNotify( const char * name, std::function<void () > f );
|
|
|
59 void addPermanentNotify( const char * name, std::function<void () > f );
|
|
|
60
|
|
|
61 //! Helper around getConfigInt.
|
|
|
62 bool getConfigBool( const char * name, bool defVal = false );
|
|
|
63 //! Helper around setConfigInt.
|
|
|
64 void setConfigBool( const char * name, bool val );
|
|
|
65 //! Helper around setConfigInt.
|
|
|
66 bool toggleConfigBool (const char * name, bool defVal = false );
|
|
|
67 //! Helper around deleteConfigInt.
|
|
|
68 void deleteConfigBool( const char * name );
|
|
|
69
|
|
|
70 //! Helper around getConfigString.
|
|
|
71 GUID getConfigGUID(const char* name, const GUID& defVal = pfc::guid_null);
|
|
|
72 //! Helper around setConfigString.
|
|
|
73 void setConfigGUID(const char* name, const GUID& val);
|
|
|
74 //! Helper around deleteConfigString.
|
|
|
75 void deleteConfigGUID(const char* name);
|
|
|
76
|
|
|
77 //! For internal core use, notifies everyone interested about off-process change to configuration data.
|
|
|
78 virtual void callNotify(const char * name) = 0;
|
|
|
79
|
|
|
80 fb2k::stringRef getConfigString( const char * name, const char * defVal ) { return getConfigString(name, defVal ? fb2k::makeString(defVal) : nullptr); }
|
|
|
81 fb2k::stringRef getConfigString( const char * name, std::nullptr_t ) { return getConfigString(name, fb2k::stringRef ( nullptr ) ); }
|
|
|
82 };
|
|
|
83
|
|
|
84 struct configEventHandle_;
|
|
|
85 typedef configEventHandle_ * configEventHandle_t;
|
|
|
86
|
|
|
87 class configEvent {
|
|
|
88 public:
|
|
|
89 configEvent(const char * name) : m_name(name) {}
|
|
|
90 configEventHandle_t operator+=(std::function< void() >) const;
|
|
|
91 void operator-=(configEventHandle_t) const;
|
|
|
92 private:
|
|
|
93 const char * const m_name;
|
|
|
94 };
|
|
|
95
|
|
|
96 class configEventRef {
|
|
|
97 public:
|
|
|
98 configEventRef() : m_name(), m_handle() {}
|
|
|
99 ~configEventRef();
|
|
|
100 configEventRef & operator<< ( const char * name );
|
|
|
101 configEventRef & operator<< ( std::function<void () > f );
|
|
|
102 void clear();
|
|
|
103 void set( const char * name, std::function<void () > f );
|
|
|
104 void setName( const char * name );
|
|
|
105 void setFunction( std::function<void()> f);
|
|
|
106 bool isActive() const { return m_handle != nullptr; }
|
|
|
107 private:
|
|
|
108 pfc::string8 m_name;
|
|
|
109 configEventHandle_t m_handle;
|
|
|
110
|
|
|
111 configEventRef( const configEventRef & ) = delete;
|
|
|
112 void operator=( const configEventRef & ) = delete;
|
|
|
113 };
|
|
|
114 /*
|
|
|
115 Usage example:
|
|
|
116 using namespace fb2k;
|
|
|
117 static_api_ptr_t<configStore> api;
|
|
|
118 stringRef val = api->getConfigString( "myComponent.foo", "defaultVal" );
|
|
|
119
|
|
|
120 {
|
|
|
121 auto scope = api->acquireTransactionScope();
|
|
|
122 int64_t v = api->getConfigInt("myComponent.somevar" );
|
|
|
123 v ++;
|
|
|
124 api->setConfigInt("myComponent.somevar", v);
|
|
|
125 api->setConfigString("myComponent.foo", "bar" );
|
|
|
126 // commit is triggered when leaving scope
|
|
|
127
|
|
|
128 // If you want to deterministically ensure that the data has been written before leaving scope,
|
|
|
129 // for an example when another process is about to read it,
|
|
|
130 // use api->commitBlocking()
|
|
|
131 }
|
|
|
132 */
|
|
|
133
|
|
|
134
|
|
|
135 //! \since 2.2
|
|
|
136 class configStore2 : public configStore {
|
|
|
137 FB2K_MAKE_SERVICE_COREAPI_EXTENSION(configStore2, configStore);
|
|
|
138 public:
|
|
|
139 // Use flagSuppressCache to prevent value from being cached. Prevents memory usage creep if querying lots of uniquely named variables.
|
|
|
140 static constexpr uint32_t flagSuppressCache = 1;
|
|
|
141
|
|
|
142 virtual int64_t getConfigInt2( const char * name, int64_t defVal = 0, uint32_t flags = 0 ) = 0;
|
|
|
143 virtual void setConfigInt2( const char * name, int64_t val, uint32_t flags = 0 ) = 0;
|
|
|
144 virtual void deleteConfigInt2(const char * name, uint32_t flags = 0) = 0;
|
|
|
145
|
|
|
146 virtual fb2k::stringRef getConfigString2( const char * name, fb2k::stringRef defaVal = fb2k::string::stringWithString(""), uint32_t flags = 0) = 0;
|
|
|
147 virtual void setConfigString2( const char * name, const char * value, uint32_t flags = 0 ) = 0;
|
|
|
148 virtual void deleteConfigString2(const char * name, uint32_t flags = 0) = 0;
|
|
|
149
|
|
|
150 virtual fb2k::memBlockRef getConfigBlob2( const char * name, fb2k::memBlockRef defVal = fb2k::memBlock::empty(), uint32_t flags = 0) = 0;
|
|
|
151 virtual void setConfigBlob2(const char* name, const void* ptr, size_t bytes, uint32_t flags = 0) = 0;
|
|
|
152 virtual void setConfigBlob2(const char* name, fb2k::memBlockRef val, uint32_t flags = 0) = 0;
|
|
|
153 virtual void deleteConfigBlob2(const char * name, uint32_t flags = 0) = 0;
|
|
|
154
|
|
|
155 virtual double getConfigFloat2( const char * name, double defVal = 0, uint32_t flags = 0) = 0;
|
|
|
156 virtual void setConfigFloat2( const char * name, double val, uint32_t flags = 0) = 0;
|
|
|
157 virtual void deleteConfigFloat2( const char * name, uint32_t flags = 0) = 0;
|
|
|
158
|
|
|
159
|
|
|
160 int64_t getConfigInt( const char * name, int64_t defVal ) override final { return getConfigInt2(name, defVal); }
|
|
|
161 void setConfigInt( const char * name, int64_t val ) override final { setConfigInt2(name, val); }
|
|
|
162 void deleteConfigInt(const char * name) override final { deleteConfigInt2(name); }
|
|
|
163
|
|
|
164 fb2k::stringRef getConfigString( const char * name, fb2k::stringRef defaVal) override final { return getConfigString2(name, defaVal); }
|
|
|
165 void setConfigString( const char * name, const char * value ) override final { setConfigString2(name, value); }
|
|
|
166 void deleteConfigString(const char * name) override final { deleteConfigString2(name); }
|
|
|
167
|
|
|
168 fb2k::memBlockRef getConfigBlob( const char * name, fb2k::memBlockRef defVal ) override final { return getConfigBlob2(name, defVal); }
|
|
|
169 void setConfigBlob(const char* name, const void* ptr, size_t bytes) override final { setConfigBlob2(name, ptr, bytes); }
|
|
|
170 void setConfigBlob(const char* name, fb2k::memBlockRef val) override final { setConfigBlob2(name, val); }
|
|
|
171 void deleteConfigBlob(const char * name) override final { deleteConfigBlob2(name); }
|
|
|
172
|
|
|
173 double getConfigFloat( const char * name, double defVal ) override final { return getConfigFloat2(name, defVal); }
|
|
|
174 void setConfigFloat( const char * name, double val ) override final { setConfigFloat2(name, val); }
|
|
|
175 void deleteConfigFloat( const char * name ) override final { deleteConfigFloat2(name); }
|
|
|
176 };
|
|
|
177 } // namespace fb2k
|