Cuberite
A lightweight, fast and extensible game server for Minecraft
OverridesSettingsRepository.cpp
Go to the documentation of this file.
1 
2 #include "Globals.h"
4 
5 cOverridesSettingsRepository::cOverridesSettingsRepository(std::unique_ptr<cSettingsRepositoryInterface> a_Main, cSettingsRepositoryInterface & a_Overrides) :
6  m_Main(std::move(a_Main)),
7  m_Overrides(&a_Overrides)
8 {
9 }
10 
11 
12 
13 
14 
16 {
17  return m_Overrides->KeyExists(a_keyName) || m_Main->KeyExists(a_keyName);
18 }
19 
20 
21 
22 
23 
24 bool cOverridesSettingsRepository::HasValue(const AString & a_KeyName, const AString & a_ValueName) const
25 {
26  return m_Overrides->HasValue(a_KeyName, a_ValueName) || m_Main->HasValue(a_KeyName, a_ValueName);
27 }
28 
29 
30 
31 
32 
34 {
35 
36  if (m_Overrides->KeyExists(a_keyname))
37  {
38  m_Overrides->AddKeyName(a_keyname);
39  return 0;
40  }
41 
42  return m_Main->AddKeyName(a_keyname);
43 }
44 
45 
46 
47 
48 
49 bool cOverridesSettingsRepository::AddKeyComment(const AString & a_keyname, const AString & a_comment)
50 {
51  if (m_Overrides->KeyExists(a_keyname))
52  {
53  return m_Overrides->AddKeyComment(a_keyname, a_comment);
54  }
55 
56  return m_Main->AddKeyComment(a_keyname, a_comment);
57 }
58 
59 
60 
61 
62 
63 AString cOverridesSettingsRepository::GetKeyComment(const AString & a_keyname, const int a_commentID) const
64 {
65 
66  if (m_Overrides->KeyExists(a_keyname))
67  {
68  return m_Overrides->GetKeyComment(a_keyname, a_commentID);
69  }
70 
71  return m_Main->GetKeyComment(a_keyname, a_commentID);
72 }
73 
74 
75 
76 
77 
78 bool cOverridesSettingsRepository::DeleteKeyComment(const AString & a_keyname, const int a_commentID)
79 {
80  if (m_Overrides->KeyExists(a_keyname))
81  {
82  return m_Overrides->DeleteKeyComment(a_keyname, a_commentID);
83  }
84 
85  return m_Main->DeleteKeyComment(a_keyname, a_commentID);
86 }
87 
88 
89 
90 
91 
92 void cOverridesSettingsRepository::AddValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value)
93 {
94  if (m_Overrides->HasValue(a_KeyName, a_ValueName))
95  {
96  m_Overrides->AddValue(a_KeyName, a_ValueName, a_Value);
97  }
98  else
99  {
100  m_Main->AddValue(a_KeyName, a_ValueName, a_Value);
101  }
102 }
103 
104 
105 
106 
107 
108 std::vector<std::pair<AString, AString>> cOverridesSettingsRepository::GetValues(AString a_keyName)
109 {
110  auto overrides = m_Overrides->GetValues(a_keyName);
111  auto main = m_Main->GetValues(a_keyName);
112 
113  auto ret = overrides;
114 
115  for (const auto & mainpair : main)
116  {
117  bool found = false;
118  for (const auto & overridepair : overrides)
119  {
120  if (overridepair.first == mainpair.first)
121  {
122  found = true;
123  break;
124  }
125  }
126  if (!found)
127  {
128  ret.push_back(mainpair);
129  }
130  }
131  return ret;
132 }
133 
134 
135 
136 
137 
138 AString cOverridesSettingsRepository::GetValue(const AString & a_KeyName, const AString & a_ValueName, const AString & defValue) const
139 {
140  if (m_Overrides->HasValue(a_KeyName, a_ValueName))
141  {
142  return m_Overrides->GetValue(a_KeyName, a_ValueName, defValue);
143  }
144  else
145  {
146  return m_Main->GetValue(a_KeyName, a_ValueName, defValue);
147  }
148 }
149 
150 
151 
152 
153 
154 AString cOverridesSettingsRepository::GetValueSet (const AString & a_KeyName, const AString & a_ValueName, const AString & defValue)
155 {
156  if (m_Overrides->HasValue(a_KeyName, a_ValueName))
157  {
158  return m_Overrides->GetValueSet(a_KeyName, a_ValueName, defValue);
159  }
160  else
161  {
162  return m_Main->GetValueSet(a_KeyName, a_ValueName, defValue);
163  }
164 }
165 
166 
167 
168 
169 
170 int cOverridesSettingsRepository::GetValueSetI(const AString & a_KeyName, const AString & a_ValueName, const int defValue)
171 {
172  if (m_Overrides->HasValue(a_KeyName, a_ValueName))
173  {
174  return m_Overrides->GetValueSetI(a_KeyName, a_ValueName, defValue);
175  }
176  else
177  {
178  return m_Main->GetValueSetI(a_KeyName, a_ValueName, defValue);
179  }
180 }
181 
182 
183 
184 
185 
186 Int64 cOverridesSettingsRepository::GetValueSetI(const AString & a_KeyName, const AString & a_ValueName, const Int64 defValue)
187 {
188  if (m_Overrides->HasValue(a_KeyName, a_ValueName))
189  {
190  return m_Overrides->GetValueSetI(a_KeyName, a_ValueName, defValue);
191  }
192  else
193  {
194  return m_Main->GetValueSetI(a_KeyName, a_ValueName, defValue);
195  }
196 }
197 
198 
199 
200 
201 
202 bool cOverridesSettingsRepository::GetValueSetB(const AString & a_KeyName, const AString & a_ValueName, const bool defValue)
203 {
204  if (m_Overrides->HasValue(a_KeyName, a_ValueName))
205  {
206  return m_Overrides->GetValueSetB(a_KeyName, a_ValueName, defValue);
207  }
208  else
209  {
210  return m_Main->GetValueSetB(a_KeyName, a_ValueName, defValue);
211  }
212 }
213 
214 
215 
216 
217 
218 bool cOverridesSettingsRepository::SetValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value, const bool a_CreateIfNotExists)
219 {
220  if (m_Overrides->HasValue(a_KeyName, a_ValueName))
221  {
222  return m_Overrides->SetValue(a_KeyName, a_ValueName, a_Value, a_CreateIfNotExists);
223  }
224  else
225  {
226  return m_Main->SetValue(a_KeyName, a_ValueName, a_Value, a_CreateIfNotExists);
227  }
228 }
229 
230 
231 
232 
233 
234 bool cOverridesSettingsRepository::SetValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value, const bool a_CreateIfNotExists)
235 {
236  if (m_Overrides->HasValue(a_KeyName, a_ValueName))
237  {
238  return m_Overrides->SetValueI(a_KeyName, a_ValueName, a_Value, a_CreateIfNotExists);
239  }
240  else
241  {
242  return m_Main->SetValueI(a_KeyName, a_ValueName, a_Value, a_CreateIfNotExists);
243  }
244 }
245 
246 
247 
248 
249 
250 bool cOverridesSettingsRepository::DeleteValue(const AString & a_KeyName, const AString & a_ValueName)
251 {
252  if (m_Overrides->HasValue(a_KeyName, a_ValueName))
253  {
254  return m_Overrides->DeleteValue(a_KeyName, a_ValueName);
255  }
256  else
257  {
258  return m_Main->DeleteValue(a_KeyName, a_ValueName);
259  }
260 }
261 
262 
263 
264 
265 
267 {
268  return m_Overrides->Flush() && m_Main->Flush();
269 }
270 
signed long long Int64
Definition: Globals.h:151
int main(int argc, char **argv)
Definition: main.cpp:211
std::string AString
Definition: StringUtils.h:11
Definition: FastNBT.h:132
virtual std::vector< std::pair< AString, AString > > GetValues(AString a_keyName) override
returns a vector containing a name, value pair for each value under the key
virtual int GetValueSetI(const AString &keyname, const AString &valuename, const int defValue=0) override
virtual AString GetKeyComment(const AString &keyname, const int commentID) const override
Return a key comment, returns "" for repositories that do not return comments.
cSettingsRepositoryInterface * m_Overrides
virtual AString GetValue(const AString &keyname, const AString &valuename, const AString &defValue="") const override
Get the value at the specified key and value, returns defValue on failure.
virtual bool AddKeyComment(const AString &keyname, const AString &comment) override
Add a key comment, will always fail if the repository does not support comments.
virtual bool HasValue(const AString &a_KeyName, const AString &a_ValueName) const override
Returns true iff the specified value exists.
cOverridesSettingsRepository(std::unique_ptr< cSettingsRepositoryInterface > a_Main, cSettingsRepositoryInterface &a_Overrides)
virtual bool GetValueSetB(const AString &keyname, const AString &valuename, const bool defValue=false) override
virtual bool SetValue(const AString &a_KeyName, const AString &a_ValueName, const AString &a_Value, const bool a_CreateIfNotExists=true) override
Overwrites the value of the key, value pair Specify the optional parameter as false if you do not wan...
virtual int AddKeyName(const AString &keyname) override
Add a key name.
virtual bool DeleteKeyComment(const AString &keyname, const int commentID) override
Delete a key comment, will always fail if the repository does not support comments.
std::unique_ptr< cSettingsRepositoryInterface > m_Main
virtual bool DeleteValue(const AString &keyname, const AString &valuename) override
Deletes the specified key, value pair.
virtual bool KeyExists(const AString keyname) const override
Returns true iff the specified key exists.
virtual bool Flush() override
Writes the changes to the backing store, if the repository has one.
virtual bool SetValueI(const AString &a_KeyName, const AString &a_ValueName, const int a_Value, const bool a_CreateIfNotExists=true) override
virtual void AddValue(const AString &a_KeyName, const AString &a_ValueName, const AString &a_Value) override
Adds a new value to the specified key.
virtual AString GetValueSet(const AString &keyname, const AString &valuename, const AString &defValue="") override
Gets the value; if not found, write the default to the repository.
virtual bool DeleteValue(const AString &keyname, const AString &valuename)=0
Deletes the specified key, value pair.
virtual bool GetValueSetB(const AString &keyname, const AString &valuename, const bool defValue=false)=0
virtual int AddKeyName(const AString &keyname)=0
Add a key name.
virtual AString GetValueSet(const AString &keyname, const AString &valuename, const AString &defValue="")=0
Gets the value; if not found, write the default to the repository.
virtual bool AddKeyComment(const AString &keyname, const AString &comment)=0
Add a key comment, will always fail if the repository does not support comments.
virtual bool DeleteKeyComment(const AString &keyname, const int commentID)=0
Delete a key comment, will always fail if the repository does not support comments.
virtual std::vector< std::pair< AString, AString > > GetValues(AString a_keyName)=0
returns a vector containing a name, value pair for each value under the key
virtual bool KeyExists(const AString keyname) const =0
Returns true iff the specified key exists.
virtual AString GetValue(const AString &keyname, const AString &valuename, const AString &defValue="") const =0
Get the value at the specified key and value, returns defValue on failure.
virtual void AddValue(const AString &a_KeyName, const AString &a_ValueName, const AString &a_Value)=0
Adds a new value to the specified key.
virtual int GetValueSetI(const AString &keyname, const AString &valuename, const int defValue=0)=0
virtual AString GetKeyComment(const AString &keyname, const int commentID) const =0
Return a key comment, returns "" for repositories that do not return comments.
virtual bool SetValue(const AString &a_KeyName, const AString &a_ValueName, const AString &a_Value, const bool a_CreateIfNotExists=true)=0
Overwrites the value of the key, value pair Specify the optional parameter as false if you do not wan...
virtual bool Flush()=0
Writes the changes to the backing store, if the repository has one.
virtual bool HasValue(const AString &a_KeyName, const AString &a_ValueName) const =0
Returns true iff the specified value exists.
virtual bool SetValueI(const AString &a_KeyName, const AString &a_ValueName, const int a_Value, const bool a_CreateIfNotExists=true)=0