Leviathan  0.8.0.0
Leviathan game engine
AutoUpdateable.cpp
Go to the documentation of this file.
1 // ------------------------------------ //
2 #include "AutoUpdateable.h"
3 
5 using namespace Leviathan;
6 using namespace std;
7 // ------------------------------------ //
9  ValuesUpdated = false;
10 }
12  if(MonitoredValues.size() != 0){
13  StopMonitoring(MonitoredValues, true);
14  }
15 }
16 // ------------------------------------ //
18  const std::vector<VariableBlock*> &IndexesAndNamesToListen)
19 {
20 
21  // loop through wanted listen indexes and names //
22  for(size_t i = 0; i < IndexesAndNamesToListen.size(); i++){
23  // check for wstring //
24  if(IndexesAndNamesToListen[i]->GetBlockConst()->Type == DATABLOCK_TYPE_WSTRING){
25  // start listening on named index //
26 
27  // register new listener //
28  DataStore::Get()->RegisterListener(this, new DataListener(-1, false,
29  IndexesAndNamesToListen[i]->operator string()));
30 
31  // add to listened things //
32  MonitoredValues.push_back(shared_ptr<VariableBlock>(
33  new VariableBlock(
34  IndexesAndNamesToListen[i]->GetBlockConst()->AllocateNewFromThis())));
35 
36  continue;
37  }
38 
39  // force to int //
40  if(!IndexesAndNamesToListen[i]->IsConversionAllowedNonPtr<int>()){
41  // cannot be listened to //
42  Logger::Get()->Warning("AutoUpdateableObject: StartMonitoring: cannot listen to "
43  "index (not int/string)"+IndexesAndNamesToListen[i]->operator string());
44  continue;
45  }
46 
47  // should be good now //
48  int tmpindex = (int)*IndexesAndNamesToListen[i];
49 
50  // register new listener //
51  DataStore::Get()->RegisterListener(this, new DataListener(tmpindex, true, ""));
52 
53  // add to listened things //
54  MonitoredValues.push_back(shared_ptr<VariableBlock>(new VariableBlock(tmpindex)));
55  }
56 }
57 void Leviathan::AutoUpdateableObject::StopMonitoring(vector<shared_ptr<VariableBlock>> &unregisterindexandnames,
58  bool all /*= false*/)
59 {
60  // check are all going to be deleted anyways //
61  if(all){
62  // clear all //
63  MonitoredValues.clear();
64  // unregister all //
65  DataStore::Get()->RemoveListener(this, -1, "", true);
66 
67  return;
68 
69  }
70  // remove specific ones //
71  for(size_t i = 0; i < unregisterindexandnames.size(); i++){
72  // find matching already listened index //
73  for(size_t a = 0; a < MonitoredValues.size(); i++){
74  // check match //
75  if(*unregisterindexandnames[i] == *MonitoredValues[a]){
76  // unregister it //
77  string possiblename = "";
78  int possibleindex = -1;
79 
80  if(unregisterindexandnames[i]->GetBlockConst()->Type == DATABLOCK_TYPE_WSTRING){
81  // assign //
82  unregisterindexandnames[i]->ConvertAndAssingToVariable<string>(possiblename);
83 
84  } else {
85  // force to int //
86  unregisterindexandnames[i]->ConvertAndAssingToVariable<int>(possibleindex);
87  }
88 
89  // unregister from listening //
90  DataStore::Get()->RemoveListener(this, possibleindex, possiblename, false);
91 
92  // erase //
93  MonitoredValues.erase(MonitoredValues.begin()+a);
94  break;
95  }
96  }
97  }
98 }
99 
101  const std::shared_ptr<NamedVariableList> &updated)
102 {
103  ValuesUpdated = true;
104 
105  // push to update vector //
106  UpdatedValues.push_back(updated);
107 
108  return true;
109 }
110 
111 
112 // ------------------------------------ //
114  // just clear the vector, should automatically delete //
115  UpdatedValues.clear();
116 
117  ValuesUpdated = false;
118 }
119 // ------------------------------------ //
120 
121 
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
virtual DLLEXPORT ~AutoUpdateableObject()
virtual DLLEXPORT bool OnUpdate(const std::shared_ptr< NamedVariableList > &updated)
virtual DLLEXPORT void StopMonitoring(std::vector< std::shared_ptr< VariableBlock >> &unregisterindexandnames, bool all=false)
#define DATABLOCK_TYPE_WSTRING
Definition: DataBlock.h:25
DLLEXPORT void Warning(const std::string &data) override
Definition: Logger.cpp:190
static DLLEXPORT DataStore * Get()
Definition: DataStore.cpp:72
DLLEXPORT void RegisterListener(AutoUpdateableObject *object, DataListener *listen)
Definition: DataStore.cpp:246
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
DLLEXPORT void RemoveListener(AutoUpdateableObject *object, int valueid, const std::string &name="", bool all=false)
Definition: DataStore.cpp:266
virtual DLLEXPORT void StartMonitoring(const std::vector< VariableBlock * > &IndexesAndNamesToListen)
#define DLLEXPORT
Definition: Include.h:84
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12