Cuberite
A lightweight, fast and extensible game server for Minecraft
MemorySettingsRepository.h
Go to the documentation of this file.
1 
2 #pragma once
3 
5 
6 #include <unordered_map>
7 
9 {
10 public:
11 
12  virtual bool KeyExists(const AString keyname) const override;
13 
14  virtual bool HasValue(const AString & a_KeyName, const AString & a_ValueName) const override;
15 
16  virtual int AddKeyName(const AString & keyname) override;
17 
18  virtual bool AddKeyComment(const AString & keyname, const AString & comment) override;
19 
20  virtual AString GetKeyComment(const AString & keyname, const int commentID) const override;
21 
22  virtual bool DeleteKeyComment(const AString & keyname, const int commentID) override;
23 
24  virtual void AddValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value) override;
25  void AddValue (const AString & a_KeyName, const AString & a_ValueName, const Int64 a_Value);
26  void AddValue (const AString & a_KeyName, const AString & a_ValueName, const bool a_Value);
27 
28  virtual std::vector<std::pair<AString, AString>> GetValues(AString a_keyName) override;
29 
30  virtual AString GetValue (const AString & keyname, const AString & valuename, const AString & defValue = "") const override;
31 
32 
33  virtual AString GetValueSet (const AString & keyname, const AString & valuename, const AString & defValue = "") override;
34  virtual int GetValueSetI(const AString & keyname, const AString & valuename, const int defValue = 0) override;
35  virtual Int64 GetValueSetI(const AString & keyname, const AString & valuename, const Int64 defValue = 0) override;
36  virtual bool GetValueSetB(const AString & keyname, const AString & valuename, const bool defValue = false) override;
37 
38  virtual bool SetValue (const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value, const bool a_CreateIfNotExists = true) override;
39  virtual bool SetValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value, const bool a_CreateIfNotExists = true) override;
40 
41  virtual bool DeleteValue(const AString & keyname, const AString & valuename) override;
42 
43  virtual bool Flush() override;
44 
45  void SetReadOnly()
46  {
47  m_Writable = false;
48  }
49 
50 private:
51 
52  bool m_Writable = true;
53 
54  struct sValue
55  {
56  sValue(AString value):
57  #ifdef _DEBUG
58  m_Type(eType::String),
59  #endif
60  m_stringValue (value)
61  {
62  }
63 
64  sValue(Int64 value):
65  #ifdef _DEBUG
66  m_Type(eType::Int64),
67  #endif
68  m_intValue(value)
69  {
70  }
71 
72  sValue(bool value):
73  #ifdef _DEBUG
74  m_Type(eType::Bool),
75  #endif
76  m_boolValue(value)
77  {
78  }
79 
80  AString getStringValue() const { ASSERT(m_Type == eType::String); return m_stringValue; }
82  bool getBoolValue() const { ASSERT(m_Type == eType::Bool); return m_boolValue; }
83 
84  private:
85 
86  #ifdef _DEBUG
87  enum class eType
88  {
89  String,
90  Int64,
91  Bool
92  } m_Type;
93  #endif
94 
96  union
97  {
100  };
101  };
102 
103  std::unordered_map<AString, std::unordered_multimap<AString, sValue>> m_Map{};
104 };
105 
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.
#define ASSERT(x)
Definition: Globals.h:335
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.
eMonsterType m_Type
Definition: Monster.cpp:33
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.