You.i Engine
YiPersistentStore.h
Go to the documentation of this file.
1 // © You i Labs Inc. All rights reserved.
2 #ifndef _YI_PERSISTENT_STORE_H_
3 #define _YI_PERSISTENT_STORE_H_
4 
5 #include "framework/YiPredef.h"
7 #include "signal/YiSignal.h"
8 #include "utility/YiString.h"
9 
10 #include <map>
11 
12 class IYIPersistentStorePriv;
13 
62 {
63 public:
64  enum class StoreError
65  {
66  OK = 0,
67  Corrupted,
70  Unknown
71  };
72 
73  CYIPersistentStore(uint64_t maxSizeInBytes = 0);
75  virtual ~CYIPersistentStore();
76 
78 
86  template<typename T>
87  void Add(const CYIString &Key, const T &value);
88 
99  template<typename T>
100  bool Find(const CYIString &Key, T *value) const;
101 
108  void Remove(const CYIString &key);
109 
113  std::vector<CYIString> GetKeys() const;
114 
118  void Clear();
119 
123  bool Save(const CYIString &file);
124 
129  bool Load(const CYIString &file);
130 
135  void RequestSave(const CYIString &file);
136 
142  void RequestLoad(const CYIString &file);
143 
147  bool Clear(const CYIString &file);
148 
150 
154  //
156 
160  //
162 
163 private:
164  std::map<CYIString, CYIString> m_stateData;
165  uint64_t m_persistentStoreMaxSize;
166  StoreError m_lastErrorCode;
167  mutable std::recursive_mutex m_stateDataMutex;
168 
169  std::unique_ptr<IYIPersistentStorePriv> m_pPriv;
171 };
172 
176 template<typename T>
177 inline void CYIPersistentStore::Add(const CYIString &Key, const T &value)
178 {
179  bool error = false;
180  CYIString StringValue = CYIString::FromValue(value, &error);
181  if (!error)
182  {
183  std::unique_lock<std::recursive_mutex> locker(m_stateDataMutex);
184 
185  m_stateData[Key] = StringValue;
186  }
187  else
188  {
189  youi_private::LogError("CYIPersistentStore", "Add failed for key [%s], conversion of value to string failed.", Key.GetData());
190  }
191 }
192 
193 template<typename T>
194 inline bool CYIPersistentStore::Find(const CYIString &Key, T *value) const
195 {
196  std::unique_lock<std::recursive_mutex> locker(m_stateDataMutex);
197  bool valueFound = false;
198  std::map<CYIString, CYIString>::const_iterator KeyValue = m_stateData.find(Key);
199  if (KeyValue != m_stateData.end())
200  {
201  CYIString stringValue = KeyValue->second;
202  bool error = false;
203  T temporary = stringValue.To<T>(&error);
204  if (!error)
205  {
206  *value = temporary;
207  valueFound = true;
208  }
209  else
210  {
211  youi_private::LogError("CYIPersistentStore", "Value was found for key [%s] but conversion from string failed.", Key.GetData());
212  }
213  }
214 
215  return valueFound;
216 }
217 
218 #endif // _YI_PERSISTENT_STORE_H_
The CYIPersistentStore class manages storing and retrieving application state data.
Definition: YiPersistentStore.h:61
void RequestSave(const CYIString &file)
Container class for Unicode strings. Conceptually, a CYIString object is a sequence of Unicode charac...
Definition: YiString.h:32
bool Load(const CYIString &file)
T To(bool *pError=nullptr) const
CYIPersistentStore(uint64_t maxSizeInBytes=0)
virtual ~CYIPersistentStore()
static CYIString FromValue(const T &arg, bool *pError=nullptr)
StoreError GetLastErrorCode() const
CYIPersistentStore & operator=(const CYIPersistentStore &rRHS)
void RequestLoad(const CYIString &file)
CYISignal< bool > LoadCompleted
Definition: YiPersistentStore.h:161
const char * GetData() const
bool Save(const CYIString &file)
void Remove(const CYIString &key)
std::vector< CYIString > GetKeys() const
StoreError
Definition: YiPersistentStore.h:64
bool Find(const CYIString &Key, T *value) const
Definition: YiPersistentStore.h:194
friend class IYIPersistentStorePriv
Definition: YiPersistentStore.h:170
CYISignal< bool > SaveCompleted
Definition: YiPersistentStore.h:155
void Add(const CYIString &Key, const T &value)
Definition: YiPersistentStore.h:177