Leviathan  0.8.0.0
Leviathan game engine
DataStore.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2019 Henri Hyyryläinen
3 #pragma once
4 #include "Define.h"
5 // ------------------------------------ //
8 
9 namespace Leviathan {
10 
11 #define DATAINDEX_TICKTIME 1
12 #define DATAINDEX_TICKCOUNT 2
13 #define DATAINDEX_FRAMETIME 3
14 #define DATAINDEX_FPS 4
15 #define DATAINDEX_WIDTH 5
16 #define DATAINDEX_HEIGHT 6
17 
18 #define DATAINDEX_FRAMETIME_MAX 7
19 #define DATAINDEX_FRAMETIME_MIN 8
20 #define DATAINDEX_FRAMETIME_AVERAGE 9
21 
22 #define DATAINDEX_FPS_MIN 10
23 #define DATAINDEX_FPS_MAX 11
24 #define DATAINDEX_FPS_AVERAGE 12
25 
26 #define DATAINDEX_FONT_SIZEMULTIPLIER 13
27 
28 
29 #define DATAINDEX_UND -1
30 
31 
32 struct DataListener {
34  DLLEXPORT DataListener(int index, bool onindex, const std::string& var = "");
35 
38  std::string VarName;
39 
40  // AutoUpdateableObject* Object;
41 };
42 
43 // holds all data listeners related to a object //
46  {
47  // free memory //
49  }
50 
51  std::vector<DataListener*> HandledListeners;
52 };
53 
54 
55 class DataStore {
56 public:
58  DLLEXPORT DataStore(bool main);
60 
61  DLLEXPORT void SetPersistance(unsigned int index, bool toset);
62  DLLEXPORT void SetPersistance(const std::string& name, bool toset);
63  DLLEXPORT int GetPersistance(unsigned int index) const;
64  DLLEXPORT int GetPersistance(const std::string& name) const;
65  // ------------------------------------ //
66  DLLEXPORT bool SetValue(const std::string& name, const VariableBlock& value1);
67  DLLEXPORT bool SetValue(const std::string& name, VariableBlock* value1);
68  DLLEXPORT bool SetValue(
69  const std::string& name, const std::vector<VariableBlock*>& values);
70 
71  DLLEXPORT bool SetValue(NamedVariableList& nameandvalues);
72 
73  DLLEXPORT size_t GetValueCount(const std::string& name) const;
74 
75  DLLEXPORT const VariableBlock* GetValue(const std::string& name) const;
76  DLLEXPORT bool GetValue(const std::string& name, VariableBlock& receiver) const;
77  DLLEXPORT bool GetValues(
78  const std::string& name, std::vector<const VariableBlock*>& receiver) const;
79 
80 
81  template<class T>
82  bool GetValueAndConvertTo(const std::string& name, T& receiver) const
83  {
84  // use try block to catch all exceptions (not found and conversion fail //
85  try {
86  if(!Values.GetValue(name)->ConvertAndAssingToVariable<T>(receiver)) {
87  // Couldn't convert or assign or find the value //
88  return false;
89  }
90  } catch(...) {
91  // variable not found / wrong type //
92  return false;
93  }
94  // correct variable has been set //
95  return true;
96  }
97 
98  // DLLEXPORT vector<VariableBlock*>* GetValues(const std::string &name) throw(...);
99 
100  DLLEXPORT void AddVar(std::shared_ptr<NamedVariableList> values);
101  DLLEXPORT void Remove(size_t index);
102  DLLEXPORT void Remove(const std::string& name);
103 
104  DLLEXPORT int GetVariableType(const std::string& name) const;
105  DLLEXPORT int GetVariableTypeOfAll(const std::string& name) const;
106  // ------------------------------------ //
107 
108  DLLEXPORT static std::string GetPersistStorageFile();
109 
110  DLLEXPORT static DataStore* Get();
111 
112  // variables //
113 public:
114  DLLEXPORT int GetTickTime() const;
115  DLLEXPORT int GetTickCount() const;
116  DLLEXPORT int GetTicksBehind() const;
117  DLLEXPORT int GetFrameTime() const;
118  DLLEXPORT int GetFPS() const;
119  DLLEXPORT int GetHeight() const;
120  DLLEXPORT int GetWidth() const;
121  DLLEXPORT int GetGUiActive() const;
122  DLLEXPORT int GetFrameTimeMin() const;
123  DLLEXPORT int GetFrameTimeMax() const;
124  DLLEXPORT int GetFrameTimeAverage() const;
125  DLLEXPORT int GetFPSMin() const;
126  DLLEXPORT int GetFPSMax() const;
127  DLLEXPORT int GetFPSAverage() const;
128  DLLEXPORT int GetFontSizeMultiplier() const;
129 
130  DLLEXPORT void SetTickTime(int newval);
131  DLLEXPORT void SetTickCount(int newval);
132  DLLEXPORT void SetTicksBehind(int value);
133  DLLEXPORT void SetFrameTime(int newval);
134  DLLEXPORT void SetFPS(int newval);
135  DLLEXPORT void SetGUiActive(int newval);
136  DLLEXPORT void SetFrameTimeMin(int newval);
137  DLLEXPORT void SetFrameTimeMax(int newval);
138  DLLEXPORT void SetFrameTimeAverage(int newval);
139  DLLEXPORT void SetFPSMin(int newval);
140  DLLEXPORT void SetFPSMax(int newval);
141  DLLEXPORT void SetFPSAverage(int newval);
142  DLLEXPORT void SetFontSizeMultiplier(int newval);
143 
144  DLLEXPORT void SetWidth(int newval);
145  DLLEXPORT void SetHeight(int newval);
146 
147  DLLEXPORT int GetValueFromValIndex(int valindex) const;
148 
149 public:
151  DLLEXPORT void RemoveListener(AutoUpdateableObject* object, int valueid,
152  const std::string& name = "", bool all = false);
153 
154 private:
155  // ------------------------------------ //
156  void Load();
157  void Save();
158 
159  // void _RemoveListener(int index);
160  void ValueUpdate(int index);
161  void ValueUpdate(const std::string& name);
162  // ------------------------------------ //
163  NamedVars Values;
164  // NamedVariableLists that should be saved to file on quit //
165  std::vector<bool> Persistencestates;
166 
167 
168  std::map<AutoUpdateableObject*, std::shared_ptr<DataListenHolder>> Listeners;
169 
170 
171  // vector<DataListener*> Listeners;
172 
173 
174 
175  // vars //
176  int TickTime;
177  int TickCount;
178  int TicksBehind = 0;
179  int FrameTime;
180  int FrameTimeMin;
181  int FrameTimeMax;
182  int FrameTimeAverage;
183 
184  int FPS;
185  int FPSMin;
186  int FPSMax;
187  int FPSAverage;
188  int Gui;
189 
190  int Width;
191  int Height;
192 
193  int FontSizeMultiplier;
194 
195  // static //
196  static DataStore* Staticaccess;
197 };
198 
199 } // namespace Leviathan
DLLEXPORT int GetFontSizeMultiplier() const
Definition: DataStore.cpp:543
DLLEXPORT int GetHeight() const
Definition: DataStore.cpp:426
DLLEXPORT void SetFPS(int newval)
Definition: DataStore.cpp:387
hosts one or more VariableBlocks keeping only one name for all of them
Definition: NamedVars.h:27
DLLEXPORT void SetFPSAverage(int newval)
Definition: DataStore.cpp:537
DLLEXPORT int GetFPSMax() const
Definition: DataStore.cpp:497
DLLEXPORT void SetFPSMax(int newval)
Definition: DataStore.cpp:531
DLLEXPORT int GetTicksBehind() const
Definition: DataStore.cpp:308
DLLEXPORT int GetFPS() const
Definition: DataStore.cpp:431
bool GetValueAndConvertTo(const std::string &name, T &receiver) const
Definition: DataStore.h:82
DLLEXPORT int GetFPSMin() const
Definition: DataStore.cpp:492
DLLEXPORT void SetFontSizeMultiplier(int newval)
Definition: DataStore.cpp:548
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
DLLEXPORT DataStore()
Definition: DataStore.cpp:11
DLLEXPORT int GetFrameTime() const
Definition: DataStore.cpp:313
DLLEXPORT const VariableBlock * GetValue(const std::string &name) const
Definition: NamedVars.cpp:974
DLLEXPORT int GetTickTime() const
Definition: DataStore.cpp:298
DLLEXPORT int GetFPSAverage() const
Definition: DataStore.cpp:502
DLLEXPORT int GetFrameTimeMin() const
Definition: DataStore.cpp:477
static DLLEXPORT std::string GetPersistStorageFile()
Definition: DataStore.cpp:553
DLLEXPORT int GetWidth() const
Definition: DataStore.cpp:421
DLLEXPORT void SetFrameTimeAverage(int newval)
Definition: DataStore.cpp:519
DLLEXPORT int GetPersistance(unsigned int index) const
Definition: DataStore.cpp:149
std::string VarName
Definition: DataStore.h:38
DLLEXPORT int GetFrameTimeMax() const
Definition: DataStore.cpp:482
DLLEXPORT void SetFPSMin(int newval)
Definition: DataStore.cpp:525
#define SAFE_DELETE_VECTOR(x)
Definition: Define.h:177
DLLEXPORT void SetFrameTimeMax(int newval)
Definition: DataStore.cpp:513
bool ConvertAndAssingToVariable(ConvertT &var) const
Definition: DataBlock.h:728
static DLLEXPORT DataStore * Get()
Definition: DataStore.cpp:72
DLLEXPORT void SetFrameTimeMin(int newval)
Definition: DataStore.cpp:507
DLLEXPORT ~DataStore()
Definition: DataStore.cpp:65
DLLEXPORT void SetHeight(int newval)
Definition: DataStore.cpp:369
DLLEXPORT bool SetValue(const std::string &name, const VariableBlock &value1)
Definition: DataStore.cpp:164
DLLEXPORT void SetTicksBehind(int value)
Definition: DataStore.cpp:411
DLLEXPORT bool GetValues(const std::string &name, std::vector< const VariableBlock * > &receiver) const
Definition: DataStore.cpp:129
DLLEXPORT void SetPersistance(unsigned int index, bool toset)
Definition: DataStore.cpp:135
DLLEXPORT void RegisterListener(AutoUpdateableObject *object, DataListener *listen)
Definition: DataStore.cpp:246
DLLEXPORT int GetGUiActive() const
Definition: DataStore.cpp:416
DLLEXPORT void RemoveListener(AutoUpdateableObject *object, int valueid, const std::string &name="", bool all=false)
Definition: DataStore.cpp:266
DLLEXPORT void Remove(size_t index)
Definition: DataStore.cpp:219
DLLEXPORT void SetGUiActive(int newval)
Definition: DataStore.cpp:381
DLLEXPORT int GetTickCount() const
Definition: DataStore.cpp:303
#define DLLEXPORT
Definition: Include.h:84
DLLEXPORT int GetValueFromValIndex(int valindex) const
Definition: DataStore.cpp:436
DLLEXPORT void SetFrameTime(int newval)
Definition: DataStore.cpp:393
DLLEXPORT int GetVariableType(const std::string &name) const
Definition: DataStore.cpp:236
DLLEXPORT const VariableBlock * GetValue(const std::string &name) const
DLLEXPORT void SetTickTime(int newval)
Definition: DataStore.cpp:405
DLLEXPORT size_t GetValueCount(const std::string &name) const
Definition: DataStore.cpp:124
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
DLLEXPORT int GetVariableTypeOfAll(const std::string &name) const
Definition: DataStore.cpp:241
DLLEXPORT DataListener()
Definition: DataStore.cpp:563
std::vector< DataListener * > HandledListeners
Definition: DataStore.h:51
DLLEXPORT void AddVar(std::shared_ptr< NamedVariableList > values)
Definition: DataStore.cpp:208
DLLEXPORT void SetTickCount(int newval)
Definition: DataStore.cpp:399
DLLEXPORT int GetFrameTimeAverage() const
Definition: DataStore.cpp:487
DLLEXPORT void SetWidth(int newval)
Definition: DataStore.cpp:375