Gorgon Game Engine
Runtime.h
Go to the documentation of this file.
1 
3 #pragma once
4 
5 #include <ostream>
6 #include <string>
7 
8 
9 #include "Reflection.h"
10 #include "Data.h"
11 #include "Input.h"
12 #include "Instruction.h"
13 
14 namespace Gorgon {
15 
16  namespace Scripting {
17 
20  public:
21 
24  void Register(const Data &data) {
25  ASSERT(data.GetData().IsPointer(), "Reference keeping can only be performed for reference types, "
26  "offender: "+data.GetType().GetName(), 1, 4);
27 
28  void *ptr=data.GetData().Pointer();
29 
30  //ignore register requests to nullptr
31  if(ptr==nullptr) return;
32 
33  if(references[ptr]==0)
34  references[ptr]=1;
35  }
36 
39  void Register(void *ptr) {
40  //ignore register requests to nullptr
41  if(ptr==nullptr) return;
42 
43  references[ptr];
44  }
45 
47  void Increase(const Data &data) {
48  ASSERT(data.GetData().IsPointer(), "Reference keeping can only be performed for reference types, "
49  "offender: "+data.GetType().GetName(), 1, 4);
50 
51  void *ptr=data.GetData().Pointer();
52  auto f=references.find(ptr);
53  if(f==references.end()) return;
54 
55  f->second++;
56  }
57 
59  void Increase(void *ptr) {
60  auto f=references.find(ptr);
61  if(f==references.end()) return;
62 
63  f->second++;
64  }
65 
68  void Decrease(const Data &data) {
69  ASSERT(data.GetData().IsPointer(), "Reference keeping can only be performed for reference types, "
70  "offender: "+data.GetType().GetName(), 1, 4);
71 
72  void *ptr=data.GetData().Pointer();
73  auto f=references.find(ptr);
74  if(f==references.end()) return;
75 
76  int &v=f->second;
77  v--;
78  if(v<=0) {
79  data.Delete();
80  references.erase(f);
81  }
82  }
83 
85  void Reset(const Data &data) {
86  ASSERT(data.GetData().IsPointer(), "Reference keeping can only be performed for reference types, "
87  "offender: "+data.GetType().GetName(), 1, 4);
88 
89  void *ptr=data.GetData().Pointer();
90  auto f=references.find(ptr);
91  if(f==references.end()) return;
92 
93  int &v=f->second;
94  v=0;
95  }
96 
97  bool IsRegistered(const Data &data) const {
98  if(!data.IsValid()) return false;
99 
100  ASSERT(data.GetData().IsPointer(), "Reference keeping can only be performed for reference types, "
101  "offender: "+data.GetType().GetName(), 1, 4);
102 
103  void *ptr=data.GetData().Pointer();
104  auto f=references.find(ptr);
105  if(f==references.end()) return false;
106 
107  return true;
108  }
109 
111  void Unregister(const Data &data) {
112  ASSERT(data.GetData().IsPointer(), "Reference keeping can only be performed for reference types, "
113  "offender: "+data.GetType().GetName(), 1, 4);
114 
115  void *ptr=data.GetData().Pointer();
116  references.erase(ptr);
117  }
118 
121  void GetRidOf(Data &data) {
122  ASSERT(data.GetData().IsPointer(), "Reference keeping can only be performed for reference types, "
123  "offender: "+data.GetType().GetName(), 1, 4);
124 
125  void *ptr=data.GetData().Pointer();
126  auto f=references.find(ptr);
127  if(f==references.end()) return;
128 
129  int &v=f->second;
130  v++;
131  data=Data::Invalid();
132  v--;
133  }
134 
135  void list() {
136  for(auto it : references) {
137  std::cout<<it.first<<": "<<it.second<<std::endl;
138  }
139  }
140 
141  private:
142  std::map<void*, int> references;
143  };
144 
146  class Variable : public Data {
147  friend class VirtualMachine;
148  public:
149 
151  Variable(const std::string &name="", const Data &data=Data::Invalid()) : name(name), Data(data) { }
152 
155  Variable(const std::string &name, const Type &type, const Any &value) :
156  name(name), Data(type, value) {
157  }
158 
159 
163  Variable(const std::string &name, const Type &type) :
164  name(name), Data(type) {
165  }
166 
167 
169  void Set(Any value) {
170  isreference=false;
171  isconstant=false;
172  delete parent;
173  parent=nullptr;
174 
175  data.Swap(value);
176  }
177 
179  void Set(const Data &value) {
180  Data::operator=(value);
181  }
182 
185  void Set(const Type &type, Any value) {
186  isreference=false;
187  isconstant=false;
188  delete parent;
189  parent=nullptr;
190 
191  data.Swap(value);
192  this->type=&type;
193  }
194 
197  void SetReferenceable(const Data &value);
198 
200  std::string GetName() const {
201  return name;
202  }
203 
204  bool IsReferenceAssigned() const { return ref; }
205 
206  private:
207  bool ref=false;
208  std::string name;
209  };
210 
213  std::string name, help;
214  std::vector<Value> options;
217  bool optional=false, reference=false, constant=false, variable=false;
218 
219  void *defvaldata=nullptr;
220  void *optdata=nullptr;
221 
222  bool operator ==(const ParameterTemplate &) const {
223  Utils::ASSERT_FALSE("This cannot occur");
224 
225  return false;
226  }
227  };
228 
229  }
230 }
Gorgon::Scripting::Data::isreference
bool isreference
Is a reference, data is a ptr to the original type.
Definition: Data.h:191
Gorgon::Scripting::ParameterTemplate::operator==
bool operator==(const ParameterTemplate &) const
Definition: Runtime.h:222
Gorgon::Scripting::ReferenceCounter::Unregister
void Unregister(const Data &data)
Unregisters an object from reference counter.
Definition: Runtime.h:111
Gorgon::Scripting::Data::data
Any data
Stored data.
Definition: Data.h:185
Gorgon::Any::Swap
void Swap(Any &other)
Swaps the contents of the current any with another.
Definition: Any.h:141
Gorgon::Scripting::ParameterTemplate::optdata
void * optdata
Definition: Runtime.h:220
Gorgon::Scripting::Variable::Set
void Set(const Type &type, Any value)
Sets the data contained in this variable by modifying its type.
Definition: Runtime.h:185
Gorgon::Scripting::ParameterTemplate::help
std::string help
Definition: Runtime.h:213
Gorgon::Scripting::ReferenceCounter::Register
void Register(const Data &data)
Registers a new object of reference counting, this will set reference count to one.
Definition: Runtime.h:24
Gorgon::Scripting::ParameterTemplate::optional
bool optional
Definition: Runtime.h:217
Gorgon::Scripting::ReferenceCounter
This class allows references to be counted and destroyed properly.
Definition: Runtime.h:19
Gorgon::Scripting::ReferenceCounter::Reset
void Reset(const Data &data)
Resets the reference count to 0.
Definition: Runtime.h:85
Gorgon::Any
This class can hold any other information providing type erasure.
Definition: Any.h:32
Gorgon::Scripting::VirtualMachine::References
ReferenceCounter References
This system allows objects of automatic lifetime.
Definition: VirtualMachine.h:222
Gorgon::Scripting::ParameterTemplate::reference
bool reference
Definition: Runtime.h:217
Reflection.h
Gorgon::Scripting::Data
Data describes a piece of data.
Definition: Data.h:22
Embedding.h
Gorgon::Scripting::Variable::Set
void Set(const Data &value)
Sets the data contained in this variable.
Definition: Runtime.h:179
Gorgon::Scripting::Variable::GetName
std::string GetName() const
Returns the name of the variable.
Definition: Runtime.h:200
Gorgon::Scripting::Variable::SetReferenceable
void SetReferenceable(const Data &value)
Sets the data contained in this variable.
Definition: Runtime.cpp:15
Gorgon::Scripting::VirtualMachine::Get
static VirtualMachine & Get()
Returns the current VM for this thread.
Definition: VirtualMachine.h:109
Gorgon::Utils::ASSERT_FALSE
void ASSERT_FALSE(const std::string &message, int skip=1, int depth=4)
Definition: Assert.h:192
Gorgon::Scripting::Data::GetType
const Type & GetType() const
Returns the type of the data.
Definition: Data.h:173
Gorgon::Scripting::ParameterTemplate::name
std::string name
Definition: Runtime.h:213
Gorgon::Scripting::Variable::Set
void Set(Any value)
Sets the data contained in this variable without changing its type.
Definition: Runtime.h:169
Gorgon::Scripting::Type
This class stores information about types.
Definition: Reflection.h:1165
Gorgon::Scripting::ParameterTemplateType
Type * ParameterTemplateType()
Definition: Runtime.cpp:8
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Scripting::ParameterTemplate::variable
bool variable
Definition: Runtime.h:217
Gorgon::Scripting::ParameterTemplate::options
std::vector< Value > options
Definition: Runtime.h:214
Gorgon::Scripting::Type::Assign
virtual void Assign(Data &l, const Data &r) const =0
Assigns the value of the second parameter to the first, reference types can ignore this function.
Gorgon::Scripting::Variable::Variable
Variable(const std::string &name, const Type &type)
Constructor that sets the name, and type of the variable.
Definition: Runtime.h:163
Gorgon::Scripting::Data::IsReference
bool IsReference() const
Returns if this data contains a reference.
Definition: Data.cpp:212
ASSERT
#define ASSERT(expression, message,...)
Replaces regular assert to allow messages and backtrace.
Definition: Assert.h:161
Gorgon::Scripting::Data::Invalid
static Data Invalid()
Constructs an invalid data object.
Definition: Data.h:27
Gorgon::Scripting::ReferenceCounter::list
void list()
Definition: Runtime.h:135
Gorgon::Scripting::Variable::Variable
Variable(const std::string &name, const Type &type, const Any &value)
Constructor that sets the name, type and value of the variable.
Definition: Runtime.h:155
Gorgon::Any::IsPointer
bool IsPointer() const
Checks if any contains a pointer.
Definition: Any.h:327
Gorgon::Scripting::ReferenceCounter::IsRegistered
bool IsRegistered(const Data &data) const
Definition: Runtime.h:97
Gorgon::Scripting::Value
This class contains a parsed value.
Definition: Instruction.h:82
Gorgon::Scripting::ParameterTemplate::type
Value type
Definition: Runtime.h:216
Gorgon::Scripting::ReferenceCounter::GetRidOf
void GetRidOf(Data &data)
Never use without a proper reason.
Definition: Runtime.h:121
Gorgon::Scripting::MappedValueType
This class allows embedded types to become scripting types that are passed around as values.
Definition: Embedding.h:1300
Input.h
Gorgon::Scripting::Data::Delete
void Delete() const
Attempts to delete the data contained in this data.
Definition: Data.cpp:136
Gorgon::Scripting::Data::GetData
Any GetData() const
Returns the data contained in this data element.
Definition: Data.h:133
Gorgon::Scripting::Data::operator=
Data & operator=(Data)
Assignment operator.
Definition: Data.cpp:96
Gorgon::Scripting::Variable::IsReferenceAssigned
bool IsReferenceAssigned() const
Definition: Runtime.h:204
Runtime.h
This file contains classes that stores runtime and end programmer defined objects.
Gorgon::Scripting::Data::parent
Data * parent
Definition: Data.h:196
Gorgon::Scripting::ParameterTemplate::defvaldata
void * defvaldata
Definition: Runtime.h:219
Gorgon::Any::Pointer
void * Pointer() const
Returns the pointer without type information.
Definition: Any.h:332
Data.h
Gorgon::Scripting::Data::type
const Type * type
Type of the data.
Definition: Data.h:188
Instruction.h
Gorgon::Scripting::ReferenceCounter::Increase
void Increase(void *ptr)
Increases the reference count of the given object. If it is not registered, this request is ignored.
Definition: Runtime.h:59
Gorgon::Scripting::ReferenceCounter::Register
void Register(void *ptr)
Registers a new object of reference counting, this will set reference count to 0.
Definition: Runtime.h:39
Gorgon::Scripting::ParameterTemplate::constant
bool constant
Definition: Runtime.h:217
Gorgon::Scripting::ReferenceCounter::Decrease
void Decrease(const Data &data)
Decreases the reference count of the given object.
Definition: Runtime.h:68
Gorgon::Scripting::ParameterTemplate
This class holds information about a parameter without resolving constructs.
Definition: Runtime.h:212
Gorgon::Scripting::Variable::Variable
Variable(const std::string &name="", const Data &data=Data::Invalid())
Creates an invalid variable.
Definition: Runtime.h:151
Gorgon::Scripting::Data::IsValid
bool IsValid() const
Returns if the data is in a valid state.
Definition: Data.h:152
Gorgon::Scripting::ParameterTemplate::defaultvalue
Value defaultvalue
Definition: Runtime.h:215
Gorgon::Scripting::ReferenceCounter::Increase
void Increase(const Data &data)
Increases the reference count of the given object. If it is not registered, this request is ignored.
Definition: Runtime.h:47
Gorgon::Scripting::VirtualMachine
This class defines a virtual environment for scripts to run.
Definition: VirtualMachine.h:45
Gorgon::Scripting::Data::isconstant
bool isconstant
This data is a constant and should not be changed.
Definition: Data.h:194
Gorgon::Scripting::Variable
This class represents a variable. It contains the data and the name of the variable.
Definition: Runtime.h:146
Gorgon::Scripting::Member::GetName
std::string GetName() const
Returns the name of this member.
Definition: Reflection.h:325