BARE2D
LuaContextManager.cpp
Go to the documentation of this file.
1 #include "LuaContextManager.hpp"
2 
3 #include "BAREErrors.hpp"
4 
5 #include "LuaScript.hpp"
6 
7 namespace BARE2D {
8 
9  LuaContextManager* LuaContextManager::m_instance = nullptr;
10 
12  {
13  // Not much to do here
14  }
15 
17  {
18  // Nor here.
19  }
20 
22  {
23  if (m_instance == nullptr) {
25  }
26  return m_instance;
27  }
28 
30  if(m_instance) delete m_instance;
31  }
32 
34  {
35  // We need to loop through all of the contexts and update them each. As well, we need to check if any are completed. If they are, remove and destroy them.
36  std::vector<unsigned int> completedScripts;
37 
38  for(auto it : m_contexts) {
39  // Update it
40  it.second->update();
41  if(it.second->isCompleted()) {
42  // If it is done, release resources and remove it from the list.
43  it.second->destroy();
44  completedScripts.push_back(it.first);
45  }
46  }
47 
48  for(unsigned int i : completedScripts) {
49  delete m_contexts[i];
50  m_contexts.erase(i);
51  }
52  }
53 
55  {
56  // Just add the pair to the map
57  m_contexts.insert(std::make_pair(id, context));
58  }
59 
60  void LuaContextManager::removeContext(unsigned int id)
61  {
62  // Find the context in the map, delete it, and then remove it from the map
63  // Find the context
64  auto it = m_contexts.find(id);
65  // Check to make sure it exists
66  if(it == m_contexts.end()) {
67  throwError(BAREError::LUA_FAILURE, "LuaContextManager attempted to remove nonexistent script with ID " + std::to_string(id));
68  return;
69  }
70 
71  // The element exists - we need to delete it and remove it from the map
72  // delete it
73  delete it->second;
74  // remove from the map
75  m_contexts.erase(it);
76 
77  // And we're good! Just return.
78  }
79 
81  // Find the context
82  auto it = m_contexts.find(id);
83  // Check to make sure it exists. If it doesn't, return nullptr
84  if(it == m_contexts.end()) {
85  return nullptr;
86  }
87  return it->second;
88  }
89 
90 }
BARE2D::LuaContextManager::m_contexts
std::map< unsigned int, LuaScriptContextWrapper * > m_contexts
Definition: LuaContextManager.hpp:52
BARE2D
Definition: App.cpp:13
LuaContextManager.hpp
NOTE: This is a singleton class. The LuaContextManager, well, manages the Lua Contexts which the Scri...
BARE2D::LuaContextManager
Definition: LuaContextManager.hpp:14
BARE2D::LuaScriptContextWrapper
Definition: LuaScript.hpp:31
BARE2D::LuaContextManager::getContext
LuaScriptContextWrapper * getContext(unsigned int id)
Gets a context wrapper from the map.
Definition: LuaContextManager.cpp:80
BARE2D::LuaContextManager::addContext
void addContext(unsigned int id, LuaScriptContextWrapper *context)
Adds a context wrapper to the map.
Definition: LuaContextManager.cpp:54
BARE2D::LuaContextManager::m_instance
static LuaContextManager * m_instance
Definition: LuaContextManager.hpp:43
BAREErrors.hpp
BARE2D::throwError
void throwError(BAREError err, std::string message)
Throws an error silently. Adds it to the pile.
Definition: BAREErrors.cpp:190
BARE2D::BAREError::LUA_FAILURE
@ LUA_FAILURE
BARE2D::LuaContextManager::removeContext
void removeContext(unsigned int id)
Removes a context wrapper from the map.
Definition: LuaContextManager.cpp:60
BARE2D::LuaContextManager::LuaContextManager
LuaContextManager()
Definition: LuaContextManager.cpp:11
BARE2D::LuaContextManager::update
void update()
Updates all of the context wrappers, pausing, removing (if finished), or resuming them as is correct.
Definition: LuaContextManager.cpp:33
BARE2D::LuaContextManager::getInstance
static LuaContextManager * getInstance()
Definition: LuaContextManager.cpp:21
BARE2D::LuaContextManager::~LuaContextManager
~LuaContextManager()
Definition: LuaContextManager.cpp:16
LuaScript.hpp
This is the basic Lua script wrapper - it is what the end-user will create and add to the queue....
BARE2D::LuaContextManager::destroyInstance
static void destroyInstance()
Definition: LuaContextManager.cpp:29