Cuberite
A lightweight, fast and extensible game server for Minecraft
MemorySettingsRepository.cpp
Go to the documentation of this file.
1 
2 #include "Globals.h"
3 
5 
6 
7 
8 
10 {
11  return m_Map.count(keyname) != 0;
12 }
13 
14 
15 
16 
17 
18 bool cMemorySettingsRepository::HasValue(const AString & a_KeyName, const AString & a_ValueName) const
19 {
20  auto outerIter = m_Map.find(a_KeyName);
21  if (outerIter == m_Map.end())
22  {
23  return false;
24  }
25  auto iter = outerIter->second.find(a_ValueName);
26  if (iter == outerIter->second.end())
27  {
28  return false;
29  }
30  return true;
31 }
32 
33 
34 
35 
36 
38 {
39  m_Map.emplace(a_keyname, std::unordered_multimap<AString, sValue>{});
40  return 0;
41 }
42 
43 
44 
45 
46 
47 bool cMemorySettingsRepository::AddKeyComment(const AString & keyname, const AString & comment)
48 {
49  return false;
50 }
51 
52 
53 
54 
55 
56 AString cMemorySettingsRepository::GetKeyComment(const AString & keyname, const int commentID) const
57 {
58  return "";
59 }
60 
61 
62 
63 
64 
65 bool cMemorySettingsRepository::DeleteKeyComment(const AString & keyname, const int commentID)
66 {
67  return false;
68 }
69 
70 
71 
72 
73 
74 void cMemorySettingsRepository::AddValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value)
75 {
76  if (m_Writable)
77  {
78  m_Map[a_KeyName].emplace(a_ValueName, sValue(a_Value));
79  }
80 }
81 
82 
83 
84 
85 
86 void cMemorySettingsRepository::AddValue (const AString & a_KeyName, const AString & a_ValueName, Int64 a_Value)
87 {
88  if (m_Writable)
89  {
90  m_Map[a_KeyName].emplace(a_ValueName, sValue(a_Value));
91  }
92 }
93 
94 
95 
96 
97 
98 void cMemorySettingsRepository::AddValue (const AString & a_KeyName, const AString & a_ValueName, bool a_Value)
99 {
100  if (m_Writable)
101  {
102  m_Map[a_KeyName].emplace(a_ValueName, sValue(a_Value));
103  }
104 }
105 
106 
107 
108 
109 
110 std::vector<std::pair<AString, AString>> cMemorySettingsRepository::GetValues(AString a_keyName)
111 {
112  std::vector<std::pair<AString, AString>> ret;
113  for (auto pair : m_Map[a_keyName])
114  {
115  ret.emplace_back(pair.first, pair.second.getStringValue());
116  }
117  return ret;
118 }
119 
120 
121 
122 
123 
124 AString cMemorySettingsRepository::GetValue (const AString & a_KeyName, const AString & a_ValueName, const AString & defValue) const
125 {
126  auto outerIter = m_Map.find(a_KeyName);
127  if (outerIter == m_Map.end())
128  {
129  return defValue;
130  }
131  auto iter = outerIter->second.find(a_ValueName);
132  if (iter == outerIter->second.end())
133  {
134  return defValue;
135  }
136  return iter->second.getStringValue();
137 }
138 
139 
140 
141 
142 
143 AString cMemorySettingsRepository::GetValueSet (const AString & a_KeyName, const AString & a_ValueName, const AString & defValue)
144 {
145  auto outerIter = m_Map.find(a_KeyName);
146  if (outerIter == m_Map.end())
147  {
148  AddValue(a_KeyName, a_ValueName, defValue);
149  return defValue;
150  }
151  auto iter = outerIter->second.find(a_ValueName);
152  if (iter == outerIter->second.end())
153  {
154  AddValue(a_KeyName, a_ValueName, defValue);
155  return defValue;
156  }
157  return iter->second.getStringValue();
158 }
159 
160 
161 
162 
163 
164 int cMemorySettingsRepository::GetValueSetI(const AString & a_KeyName, const AString & a_ValueName, const int defValue)
165 {
166  auto outerIter = m_Map.find(a_KeyName);
167  if (outerIter == m_Map.end())
168  {
169  AddValue(a_KeyName, a_ValueName, static_cast<Int64>(defValue));
170  return defValue;
171  }
172  auto iter = outerIter->second.find(a_ValueName);
173  if (iter == outerIter->second.end())
174  {
175  AddValue(a_KeyName, a_ValueName, static_cast<Int64>(defValue));
176  return defValue;
177  }
178  return static_cast<int>(iter->second.getIntValue());
179 }
180 
181 
182 
183 
184 
185 Int64 cMemorySettingsRepository::GetValueSetI(const AString & a_KeyName, const AString & a_ValueName, const Int64 defValue)
186 {
187  auto outerIter = m_Map.find(a_KeyName);
188  if (outerIter == m_Map.end())
189  {
190  AddValue(a_KeyName, a_ValueName, defValue);
191  return defValue;
192  }
193  auto iter = outerIter->second.find(a_ValueName);
194  if (iter == outerIter->second.end())
195  {
196  AddValue(a_KeyName, a_ValueName, defValue);
197  return defValue;
198  }
199  return iter->second.getIntValue();
200 }
201 
202 
203 
204 
205 
206 bool cMemorySettingsRepository::GetValueSetB(const AString & a_KeyName, const AString & a_ValueName, const bool defValue)
207 {
208  auto outerIter = m_Map.find(a_KeyName);
209  if (outerIter == m_Map.end())
210  {
211  AddValue(a_KeyName, a_ValueName, defValue);
212  return defValue;
213  }
214  auto iter = outerIter->second.find(a_ValueName);
215  if (iter == outerIter->second.end())
216  {
217  AddValue(a_KeyName, a_ValueName, defValue);
218  return defValue;
219  }
220  return iter->second.getBoolValue();
221 }
222 
223 
224 
225 
226 
227 bool cMemorySettingsRepository::SetValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value, const bool a_CreateIfNotExists)
228 {
229  if (!m_Writable)
230  {
231  return false;
232  }
233  auto outerIter = m_Map.find(a_KeyName);
234  if (outerIter == m_Map.end())
235  {
236  if (a_CreateIfNotExists)
237  {
238  AddValue(a_KeyName, a_ValueName, a_Value);
239  }
240  return a_CreateIfNotExists;
241  }
242  auto iter = outerIter->second.find(a_ValueName);
243  if (iter == outerIter->second.end())
244  {
245  if (a_CreateIfNotExists)
246  {
247  AddValue(a_KeyName, a_ValueName, a_Value);
248  }
249  return a_CreateIfNotExists;
250  }
251  iter->second = sValue(a_Value);
252  return true;
253 }
254 
255 
256 
257 
258 
259 bool cMemorySettingsRepository::SetValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value, const bool a_CreateIfNotExists)
260 {
261  if (!m_Writable)
262  {
263  return false;
264  }
265  auto outerIter = m_Map.find(a_KeyName);
266  if (outerIter == m_Map.end())
267  {
268  if (a_CreateIfNotExists)
269  {
270  AddValue(a_KeyName, a_ValueName, static_cast<Int64>(a_Value));
271  }
272  return a_CreateIfNotExists;
273  }
274  auto iter = outerIter->second.find(a_ValueName);
275  if (iter == outerIter->second.end())
276  {
277  if (a_CreateIfNotExists)
278  {
279  AddValue(a_KeyName, a_ValueName, static_cast<Int64>(a_Value));
280  }
281  return a_CreateIfNotExists;
282  }
283  iter->second = sValue(static_cast<Int64>(a_Value));
284  return true;
285 }
286 
287 
288 
289 
290 
291 bool cMemorySettingsRepository::DeleteValue(const AString & a_KeyName, const AString & a_ValueName)
292 {
293  if (!m_Writable)
294  {
295  return false;
296  }
297  auto outerIter = m_Map.find(a_KeyName);
298  if (outerIter == m_Map.end())
299  {
300  return false;
301  }
302  auto iter = outerIter->second.find(a_ValueName);
303  if (iter == outerIter->second.end())
304  {
305  return false;
306  }
307  outerIter->second.erase(iter);
308  return true;
309 }
310 
311 
312 
313 
314 
316 {
317  return true;
318 }
319 
virtual bool KeyExists(const AString keyname) const override
Returns true iff the specified key exists.
virtual bool SetValueI(const AString &a_KeyName, const AString &a_ValueName, const int a_Value, const bool a_CreateIfNotExists=true) override
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 GetValueSetB(const AString &keyname, const AString &valuename, const bool defValue=false) override
virtual int GetValueSetI(const AString &keyname, const AString &valuename, const int defValue=0) override
virtual bool Flush() override
Writes the changes to the backing store, if the repository has one.
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 void AddValue(const AString &a_KeyName, const AString &a_ValueName, const AString &a_Value) override
Adds a new value to the specified key.
virtual bool DeleteValue(const AString &keyname, const AString &valuename) override
Deletes the specified key, value pair.
virtual int AddKeyName(const AString &keyname) override
Add a key name.
std::unordered_map< AString, std::unordered_multimap< AString, sValue > > m_Map
std::string AString
Definition: StringUtils.h:13
virtual bool HasValue(const AString &a_KeyName, const AString &a_ValueName) const override
Returns true iff the specified value exists.
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 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 DeleteKeyComment(const AString &keyname, const int commentID) override
Delete a key comment, will always fail if the repository does not support comments.
virtual AString GetKeyComment(const AString &keyname, const int commentID) const override
Return a key comment, returns "" for repositories that do not return comments.
signed long long Int64
Definition: Globals.h:107
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.