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