TorqueScript  0.2
consoleobject.hpp
1 
15 #pragma once
16 
17 #include <string>
18 #include <memory>
19 #include <vector>
20 #include <unordered_map>
21 
22 #include <torquescript/storedvalue.hpp>
23 
24 namespace TorqueScript
25 {
26  class Interpreter;
27  class ExecutionScope;
28  class Interpreter;
29  class ConsoleObjectDescriptor;
30  struct ObjectInstantiationDescriptor;
31 
32  typedef ConsoleObject* (*InitializeConsoleObjectFromDescriptorPointer)(Interpreter* interpreter, struct ObjectInstantiationDescriptor& descriptor);
33 
34  extern std::unordered_map<std::string, ConsoleObjectDescriptor*>* sConsoleObjectDescriptors;
35  static std::unordered_map<std::string, ConsoleObjectDescriptor*>* getConsoleObjectDescriptors()
36  {
37  if (sConsoleObjectDescriptors)
38  {
39  return sConsoleObjectDescriptors;
40  }
41  return sConsoleObjectDescriptors = new std::unordered_map<std::string, ConsoleObjectDescriptor*>();
42  }
43 
45  {
46  public:
47  ConsoleObjectDescriptor(const std::string& name, const std::string& parentName, InitializeConsoleObjectFromDescriptorPointer initializePointer) : mName(name), mParentName(parentName), mInitializePointer(initializePointer)
48  {
49  std::unordered_map<std::string, ConsoleObjectDescriptor*>* descriptors = getConsoleObjectDescriptors();
50 
51  assert(descriptors->find(name) == descriptors->end());
52  descriptors->insert(std::make_pair(name, this));
53  }
54 
55  std::string mName;
56  std::string mParentName;
57  std::vector<std::string> mHierarchy;
58  InitializeConsoleObjectFromDescriptorPointer mInitializePointer;
59  };
60 
61  static std::vector<std::string> relinkNamespace(const std::string& space)
62  {
63  std::unordered_map<std::string, ConsoleObjectDescriptor*>* descriptors = getConsoleObjectDescriptors();
64 
65  auto search = descriptors->find(space);
66  if (search == descriptors->end())
67  {
68  throw std::runtime_error("Fatal error in relink namespaces!");
69  }
70 
71  ConsoleObjectDescriptor* currentDescriptor = search->second;
72 
73  std::vector<std::string> result;
74  result.push_back(currentDescriptor->mParentName);
75 
76  // ConsoleObject won't have an entry
77  if (currentDescriptor->mParentName == "ConsoleObject")
78  {
79  return result;
80  }
81 
82  std::vector<std::string> children = relinkNamespace(currentDescriptor->mParentName);
83  result.insert(result.end(), children.begin(), children.end());
84  return result;
85  }
86 
87  static void relinkNamespaces()
88  {
89  std::unordered_map<std::string, ConsoleObjectDescriptor*>* descriptors = getConsoleObjectDescriptors();
90 
91  for (auto&& entry : *descriptors)
92  {
93  // Reset the hierarchy of this namespace
94  entry.second->mHierarchy = relinkNamespace(entry.second->mName);
95  entry.second->mHierarchy.insert(entry.second->mHierarchy.begin(), entry.second->mName);
96  }
97  }
98 
99  template <typename classType>
101  {
102 
103  };
104 
105  #define DECLARE_CONSOLE_OBJECT(type, super) \
106  template<> \
107  struct TypeInformation<type> \
108  { \
109  typedef TypeInformation<super> ParentInfo; \
110  static std::string getName() \
111  { \
112  return #type; \
113  } \
114  static std::vector<std::string> getHierarchy() \
115  { \
116  const std::string current = #type; \
117  std::vector<std::string> result; \
118  result.push_back(current); \
119  const std::vector<std::string> upper = ParentInfo::getHierarchy(); \
120  result.insert(result.end(), upper.begin(), upper.end()); \
121  return result; \
122  } \
123  static ConsoleObjectDescriptor* Descriptor; \
124  };
125 
126  #define DECLARE_CONSOLE_OBJECT_BODY() \
127  public: \
128  virtual std::string getClassName() override; \
129 
130  #define IMPLEMENT_CONSOLE_OBJECT(type, super) \
131  ConsoleObjectDescriptor* TypeInformation<type>::Descriptor = new ConsoleObjectDescriptor(#type, #super, type::instantiateFromDescriptor); \
132  std::string type::getClassName() \
133  { \
134  return #type; \
135  }
136 
143  {
144  public:
145  ConsoleObject(Interpreter* interpreter);
146  virtual ~ConsoleObject();
147 
154  StoredValue* getTaggedField(const std::string& name);
155 
161  void setTaggedField(const std::string& name, StoredValue value);
162 
167  virtual std::string getClassName() = 0;
168 
169  virtual bool addChild(std::shared_ptr<ConsoleObject> child);
170 
171  protected:
172  Interpreter* mInterpreter;
173 
175  std::unordered_map<std::string, StoredValue> mTaggedFields;
176  };
177 
178  template<>
180  {
181  static std::string getName()
182  {
183  return "ConsoleObject";
184  }
185 
186  static std::vector<std::string> getHierarchy()
187  {
188  std::vector<std::string> result;
189  result.push_back("ConsoleObject");
190  return result;
191  }
192  };
193 }
Definition: consoleobject.hpp:45
Base class for object instances recognized by the interpreter. These object instances may contain mem...
Definition: consoleobject.hpp:143
virtual std::string getClassName()=0
Retrieves the class name of this ConsoleObject instance.
void setTaggedField(const std::string &name, StoredValue value)
Sets a tagged field by name on the object.
std::unordered_map< std::string, StoredValue > mTaggedFields
A mapping of tagged field names to their stored values.
Definition: consoleobject.hpp:175
StoredValue * getTaggedField(const std::string &name)
Retrieves a tagged field by name from the object.
The interpreter class represents a high level instance of the TorqueScript interpreter....
Definition: interpreter.hpp:46
Storage class used to keep variable values in-memory of arbitrary data types. The data types supporte...
Definition: storedvalue.hpp:80
Definition: ast.hpp:28
Definition: consoleobject.hpp:101