Leviathan  0.8.0.0
Leviathan game engine
Leviathan::SimpleDatabase Class Reference

#include <SimpleDatabase.h>

+ Inheritance diagram for Leviathan::SimpleDatabase:

Public Member Functions

DLLEXPORT SimpleDatabase (const std::string &databasename)
 Creates a new database. Should be used as pointer. More...
 
virtual DLLEXPORT ~SimpleDatabase ()
 
virtual DLLEXPORT void GetRow (std::vector< std::string > &row, const std::string &table, int row_index, const std::vector< std::string > &columns)
 
virtual DLLEXPORT size_t GetNumRows (const std::string &table)
 
DLLEXPORT std::shared_ptr< VariableBlockGetValueOnRow (const std::string &table, const std::string &valuekeyname, const VariableBlock &wantedvalue, const std::string &wantedvaluekey)
 
DLLEXPORT bool AddValue (const std::string &database, std::shared_ptr< SimpleDatabaseRowObject > valuenamesandvalues)
 
DLLEXPORT bool RemoveValue (const std::string &database, int row)
 
DLLEXPORT bool LoadFromFile (const std::string &file)
 
DLLEXPORT void SaveToFile (const std::string &file)
 
DLLEXPORT bool WriteTableToJson (const std::string &tablename, std::string &receiver, bool humanreadable=false)
 Serializes a single table to JSON. More...
 
- Public Member Functions inherited from Leviathan::ThreadSafeGeneric< MutexType >
DLLEXPORT ThreadSafeGeneric ()
 
DLLEXPORT ~ThreadSafeGeneric ()
 
FORCE_INLINE void VerifyLock (RecursiveLock &guard) const
 
FORCE_INLINE void VerifyLock (Lock &lockit) const
 

Protected Member Functions

SimpleDatabaseObject::iterator _EnsureTable (const std::string &name)
 Makes sure that a table is fine. More...
 

Protected Attributes

SimpleDatabaseObject Database
 The main database structure. More...
 
- Protected Attributes inherited from Leviathan::ThreadSafeGeneric< MutexType >
MutexType ObjectsLock
 

Detailed Description

A class that can be used to pass databases to Rocket and generally keeping simple databases

Warning
Do NOT use this class as non-pointer objects (because linking will fail)
Todo:
This needs proper tests and fixing

Definition at line 19 of file SimpleDatabase.h.

Constructor & Destructor Documentation

◆ SimpleDatabase()

DLLEXPORT Leviathan::SimpleDatabase::SimpleDatabase ( const std::string &  databasename)

Creates a new database. Should be used as pointer.

Definition at line 11 of file SimpleDatabase.cpp.

11  {
12 
13 }

◆ ~SimpleDatabase()

DLLEXPORT Leviathan::SimpleDatabase::~SimpleDatabase ( )
virtual

Definition at line 15 of file SimpleDatabase.cpp.

15  {
16 
17 }

Member Function Documentation

◆ _EnsureTable()

SimpleDatabaseObject::iterator Leviathan::SimpleDatabase::_EnsureTable ( const std::string &  name)
protected

Makes sure that a table is fine.

Parameters
nameName of the table that is to be created if it doesn't exist

Definition at line 215 of file SimpleDatabase.cpp.

215  {
216  // Try to find it //
217  SimpleDatabaseObject::iterator iter = Database.find(name);
218 
219  if(iter != Database.end()){
220  // Valid database //
221  return iter;
222  }
223  // Ensure new database //
224  Database[name] = std::shared_ptr<std::vector<shared_ptr<std::map<std::string, std::shared_ptr<VariableBlock>>>>>(
225  new std::vector<shared_ptr<std::map<std::string, std::shared_ptr<VariableBlock>>>>());
226 
227  // Recurse, might want to avoid stack overflow //
228  return _EnsureTable(name);
229 }
SimpleDatabaseObject Database
The main database structure.
SimpleDatabaseObject::iterator _EnsureTable(const std::string &name)
Makes sure that a table is fine.

◆ AddValue()

DLLEXPORT bool Leviathan::SimpleDatabase::AddValue ( const std::string &  database,
std::shared_ptr< SimpleDatabaseRowObject valuenamesandvalues 
)

Definition at line 19 of file SimpleDatabase.cpp.

21 {
22  GUARD_LOCK();
23  // Using the database object add a new value to correct vector //
24  auto iter = _EnsureTable(database);
25 
26  // Return false if new table was not able to be added //
27  if(iter == Database.end())
28  return false;
29 
30  // Push back a new row //
31  iter->second->push_back(valuenamesandvalues);
32 
33  // Notify update //
34 
35 
36  return true;
37 }
SimpleDatabaseObject Database
The main database structure.
SimpleDatabaseObject::iterator _EnsureTable(const std::string &name)
Makes sure that a table is fine.
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ GetNumRows()

DLLEXPORT size_t Leviathan::SimpleDatabase::GetNumRows ( const std::string &  table)
virtual

Definition at line 95 of file SimpleDatabase.cpp.

95  {
96  GUARD_LOCK();
97  // If we are missing the database we shouldn't add it //
98  SimpleDatabaseObject::iterator iter = Database.find(table);
99 
100  if(iter == Database.end()){
101  // No such database //
102  return 0;
103  }
104 
105  return iter->second->size();
106 }
SimpleDatabaseObject Database
The main database structure.
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ GetRow()

DLLEXPORT void Leviathan::SimpleDatabase::GetRow ( std::vector< std::string > &  row,
const std::string &  table,
int  row_index,
const std::vector< std::string > &  columns 
)
virtual

Definition at line 61 of file SimpleDatabase.cpp.

63 {
64  GUARD_LOCK();
65  // If we are missing the database we shouldn't add it //
66  SimpleDatabaseObject::iterator iter = Database.find(table);
67 
68  if(iter == Database.end()){
69  // No such database //
70  return;
71  }
72 
73  // Validate index //
74  if(iter->second->size() <= (size_t)row_index || row_index < 0){
75  return;
76  }
77 
78  // Valid value //
79  const std::map<std::string, std::shared_ptr<VariableBlock>>& datbaseentry = *iter->second->at(row_index).get();
80 
81  // Copy data //
82  for(size_t i = 0; i < columns.size(); i++){
83 
84 
85  auto iter2 = datbaseentry.find(columns[i]);
86  if(iter2 != datbaseentry.end()){
87  // Add to result //
88  row.push_back(iter2->second->operator std::string());
89  }
90  }
91 
92 
93 }
SimpleDatabaseObject Database
The main database structure.
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ GetValueOnRow()

DLLEXPORT std::shared_ptr< VariableBlock > Leviathan::SimpleDatabase::GetValueOnRow ( const std::string &  table,
const std::string &  valuekeyname,
const VariableBlock wantedvalue,
const std::string &  wantedvaluekey 
)

Definition at line 108 of file SimpleDatabase.cpp.

111 {
112  GUARD_LOCK();
113  // Search the database for matching row and return another value from that row //
114  // If we are missing the database we shouldn't add it //
115  SimpleDatabaseObject::iterator iter = Database.find(table);
116 
117  if(iter == Database.end()){
118  // No such database //
119  return NULL;
120  }
121 
122  // Search the database //
123  for(size_t i = 0; i < iter->second->size(); i++){
124 
125  auto finditer = iter->second->at(i)->find(valuekeyname);
126 
127  if(finditer == iter->second->at(i)->end())
128  continue;
129 
130  // Check does the value match //
131  if(*finditer->second == wantedvalue){
132  // Found match //
133  auto wantedfinder = iter->second->at(i)->find(wantedvaluekey);
134 
135  if(wantedfinder != iter->second->at(i)->end()){
136 
137  return wantedfinder->second;
138  }
139  }
140  }
141 
142  return NULL;
143 }
SimpleDatabaseObject Database
The main database structure.
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ LoadFromFile()

DLLEXPORT bool Leviathan::SimpleDatabase::LoadFromFile ( const std::string &  file)

Definition at line 231 of file SimpleDatabase.cpp.

231  {
232  // The file should be able to be processed as named variable lists //
233  // read the file entirely //
234  std::string filecontents;
235 
236  try{
237  FileSystem::ReadFileEntirely(file, filecontents);
238  }
239  catch(const InvalidArgument &e){
240 
241  Logger::Get()->Error("SimpleDatabase: LoadFromFile: file could not be read, exception:");
242  e.PrintToLog();
243  return false;
244  }
245 
246 
247  // file needs to be split to lines //
248  vector<std::string> Lines;
249 
250  if(!StringOperations::CutString(filecontents, std::string("\n"), Lines)){
251 
252  Lines.push_back(filecontents);
253  Logger::Get()->Warning("ObjectFileProcessor: file seems to be only a single line: "+filecontents);
254  }
255 
256  GUARD_LOCK();
257 
258  SimpleDatabaseObject::iterator insertiter;
259 
260  for(size_t i = 0; i < Lines.size(); i++){
261  // skip empty lines //
262  if(Lines[i].size() == 0){
263  continue;
264  }
265 
266  // Check is this new table //
267  if(StringOperations::StringStartsWith<std::string>(Lines[i], "TABLE")){
268  // Move to a new table //
269 
270  StringIterator itr(&Lines[i]);
271 
272  auto tablename = itr.GetStringInQuotes<std::string>(QUOTETYPE_BOTH);
273 
274  Database[*tablename] = std::shared_ptr<std::vector<shared_ptr<SimpleDatabaseRowObject>>>(
275  new std::vector<shared_ptr<SimpleDatabaseRowObject>>());
276 
277  // Change the iter //
278  insertiter = Database.find(*tablename);
279  continue;
280  }
281 
282  // try to create a named var from this line //
283  try{
284  shared_ptr<NamedVariableList> namevar(new NamedVariableList(Lines[i]));
285  // didn't cause an exception, is valid add //
286 
287  insertiter->second->push_back(shared_ptr<SimpleDatabaseRowObject>(new SimpleDatabaseRowObject()));
288 
289  auto toinsert = insertiter->second->back();
290 
291  if(namevar->GetVariableCount() % 2 != 0){
292 
293  Logger::Get()->Warning("SimpleDatabase: LoadFromFile: file: "+file+", line: "+
294  Convert::ToString(i)+" has invalid number of elements");
295  continue;
296  }
297 
298  for(size_t namei = 0; namei < namevar->GetVariableCount(); namei += 2){
299 
300  std::string name;
301 
302  namevar->GetValueDirect(namei)->ConvertAndAssingToVariable<std::string>(name);
303 
304  string blockdata;
305 
306  if(!namevar->GetValueDirect(namei+1)->ConvertAndAssingToVariable<string>(blockdata)){
307  Logger::Get()->Warning("SimpleDatabase: LoadFromFile: file: "+file+", line: "+
308  Convert::ToString(i)+" couldn't convert value to string");
309  }
310 
311  (*toinsert)[name] = std::shared_ptr<VariableBlock>(new VariableBlock(blockdata));
312  }
313  }
314  catch(...){
315  continue;
316  }
317  }
318  // Is done //
319  return true;
320 }
hosts one or more VariableBlocks keeping only one name for all of them
Definition: NamedVars.h:27
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
std::map< std::string, std::shared_ptr< VariableBlock > > SimpleDatabaseRowObject
DLLEXPORT void Warning(const std::string &data) override
Definition: Logger.cpp:190
Iterator class for getting parts of a string.
static std::string ToString(const T &val)
Definition: Convert.h:72
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
SimpleDatabaseObject Database
The main database structure.
static DLLEXPORT bool ReadFileEntirely(const std::string &file, std::string &resultreceiver)
Definition: FileSystem.cpp:544
static bool CutString(const StringTypeN &strtocut, const StringTypeN &separator, std::vector< StringTypeN > &vec)
DLLEXPORT void Error(const std::string &data) override
Definition: Logger.cpp:177
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ RemoveValue()

DLLEXPORT bool Leviathan::SimpleDatabase::RemoveValue ( const std::string &  database,
int  row 
)

Definition at line 39 of file SimpleDatabase.cpp.

39  {
40  GUARD_LOCK();
41  // If we are missing the database we shouldn't add it //
42  SimpleDatabaseObject::iterator iter = Database.find(database);
43 
44  if(iter == Database.end()){
45  // No such database //
46  return false;
47  }
48 
49  // Remove at the specified index if possible //
50  if(iter->second->size() <= (size_t)row || row < 0)
51  return false;
52 
53  // Remove //
54  iter->second->erase(iter->second->begin()+(size_t)row);
55  // Notify update //
56 
57 
58  return true;
59 }
SimpleDatabaseObject Database
The main database structure.
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ SaveToFile()

DLLEXPORT void Leviathan::SimpleDatabase::SaveToFile ( const std::string &  file)

Definition at line 322 of file SimpleDatabase.cpp.

322  {
323 
324  std::string datastr;
325  std::string tmpdata;
326  {
327  GUARD_LOCK();
328  // Just iterate over everything and write them to file //
329  for(auto iter = Database.begin(); iter != Database.end(); ++iter){
330 
331  datastr += "TABLE = \""+iter->first+"\";\n";
332 
333  for(auto iter2 = iter->second->begin(); iter2 != iter->second->end(); ++iter2){
334 
335  datastr += "n= [";
336 
337  for(auto iter3 = (*iter2)->begin(); iter3 != (*iter2)->end(); ++iter3){
338 
339  datastr += "[\""+iter3->first+"\"]";
340 
341  if(!iter3->second->ConvertAndAssingToVariable<std::string>(tmpdata)){
342 
343  assert(0 && "database has a value that cannot be stored as a string");
344  }
345  datastr += "[\""+tmpdata+"\"]";
346  }
347 
348  datastr += "];\n";
349  }
350  }
351  }
352 
353  FileSystem::WriteToFile(datastr, file);
354 }
SimpleDatabaseObject Database
The main database structure.
static DLLEXPORT bool WriteToFile(const std::string &data, const std::string &filename)
Definition: FileSystem.cpp:462
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ WriteTableToJson()

DLLEXPORT bool Leviathan::SimpleDatabase::WriteTableToJson ( const std::string &  tablename,
std::string &  receiver,
bool  humanreadable = false 
)

Serializes a single table to JSON.

The JSON structure creates an object that has a single array member named the same as the table

Returns
true if the table was found, false otherwise

Definition at line 146 of file SimpleDatabase.cpp.

148 {
149  // Holds the data //
150  Json::Value root;
151 
152  // Add all the values to this //
153  Json::Value tablearray;
154 
155  {
156  GUARD_LOCK();
157  // If we are missing the database we shouldn't add it //
158  SimpleDatabaseObject::iterator iter = Database.find(tablename);
159 
160  if(iter == Database.end()){
161  // No such database //
162  return false;
163  }
164 
165  // Loop all the values on this table //
166  for(size_t i = 0; i < iter->second->size(); i++){
167  // Create a value from this row //
168  Json::Value rowdata;
169 
170  // Loop all values //
171  for(auto iter2 = iter->second->at(i)->begin(); iter2 != iter->second->at(i)->end(); ++iter2){
172  // We want to set it as the native data //
173  switch(iter2->second->GetBlockConst()->Type){
174  case DATABLOCK_TYPE_INT: rowdata[(iter2->first)] =
175  iter2->second->operator int(); break;
176  case DATABLOCK_TYPE_FLOAT: rowdata[(iter2->first)] =
177  iter2->second->operator float(); break;
178  case DATABLOCK_TYPE_BOOL: rowdata[(iter2->first)] =
179  iter2->second->operator bool(); break;
180  case DATABLOCK_TYPE_WSTRING: rowdata[(iter2->first)] =
181  iter2->second->operator string(); break;
182  case DATABLOCK_TYPE_STRING: rowdata[(iter2->first)] =
183  iter2->second->operator string(); break;
184  case DATABLOCK_TYPE_CHAR: rowdata[(iter2->first)] =
185  iter2->second->operator char(); break;
186  case DATABLOCK_TYPE_DOUBLE: rowdata[(iter2->first)] =
187  iter2->second->operator double(); break;
188  default: assert(0 && "unallowed datablock type in SimpleDatabase");
189  }
190  }
191 
192  // Set the value to the array //
193  tablearray.append(rowdata);
194  }
195 
196  }
197  // The lock ends here since it is no longer needed //
198 
199  // Add it and write //
200  root[(tablename)] = tablearray;
201 
202  // It succeeded //
203  if(!humanreadable){
204  Json::FastWriter writer;
205  receiver = writer.write(root);
206  } else {
207  Json::StyledWriter writer;
208  receiver = writer.write(root);
209  }
210 
211  return true;
212 }
Outputs a Value in JSON format without formatting (not human friendly).
Definition: json.h:1775
Writes a Value in JSON format in a human friendly way.
Definition: json.h:1819
virtual std::string write(const Value &root)
Definition: jsoncpp.cpp:3713
#define DATABLOCK_TYPE_INT
Definition: DataBlock.h:22
#define DATABLOCK_TYPE_WSTRING
Definition: DataBlock.h:25
#define DATABLOCK_TYPE_FLOAT
Definition: DataBlock.h:23
#define DATABLOCK_TYPE_STRING
Definition: DataBlock.h:26
virtual std::string write(const Value &root)
Serialize a Value in JSON format.
Definition: jsoncpp.cpp:3793
#define DATABLOCK_TYPE_DOUBLE
Definition: DataBlock.h:28
Value & append(const Value &value)
Append value to array at the end.
Definition: jsoncpp.cpp:2818
Represents a JSON value.
Definition: json.h:480
SimpleDatabaseObject Database
The main database structure.
#define DATABLOCK_TYPE_BOOL
Definition: DataBlock.h:24
#define DATABLOCK_TYPE_CHAR
Definition: DataBlock.h:27
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

Member Data Documentation

◆ Database

SimpleDatabaseObject Leviathan::SimpleDatabase::Database
protected

The main database structure.

Definition at line 59 of file SimpleDatabase.h.


The documentation for this class was generated from the following files: