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