MinitScript  0.9.31 PRE-BETA
Context.cpp
Go to the documentation of this file.
2 
3 #include <algorithm>
4 #include <string>
5 #include <unordered_map>
6 #include <vector>
7 
11 
12 using std::find;
13 using std::string;
14 using std::unordered_map;
15 using std::vector;
16 
18 
21 
22 Context::Context() {
23 }
24 
25 Context::~Context() {
26  for (const auto& [scriptId, script]: scriptsById) delete script;
27  scriptsById.clear();
28 }
29 
30 void Context::addScript(const string& id, MinitScript* script) {
31  // do we already have a script registered with this id?
32  if (scriptsById.find(id) != scriptsById.end()) {
33  // yep
34  Console::printLine("An error occurred: a script with id " + id + " is already registered");
35  return;
36  }
37  // no, add it
38  scriptsById[id] = script;
39 }
40 
41 void Context::removeScript(const string& id) {
42  // get script
43  auto scriptsIt = scriptsById.find(id);
44  if (scriptsIt == scriptsById.end()) {
45  Console::printLine("An error occurred: no script with id " + id + " is registered");
46  return;
47  }
48  auto script = scriptsIt->second;
49  // do we have this script in use currently
50  if (find(scriptCallStack.begin(), scriptCallStack.end(), script) != scriptCallStack.end()) {
51  // yes
52  Console::printLine("An error occurred: script with id " + id + " is currently in use");
53  return;
54  }
55  // its safe to remove it
56  delete script;
57  scriptsById.erase(scriptsIt);
58 }