Spinning Topp Logo BlackTopp Studios
inc
lua51workunit.cpp
Go to the documentation of this file.
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _lua51workunit_cpp
41 #define _lua51workunit_cpp
42 
43 #include "datatypes.h"
44 
45 #ifdef MEZZLUA51
46 
47 #include "lua51workunit.h"
48 #include "lua51scriptingengine.h"
49 
50 /// @file
51 /// @brief This file has the implementation a workunit that can execute lua script every frame
52 
53 namespace Mezzanine
54 {
55  namespace Scripting
56  {
57  namespace Lua
58  {
60  : LuaRuntime(TargetRuntime)
61  {}
62 
64  {}
65 
67  { ScriptsToRun.push_back(ScriptToAdd); }
68 
70  {
71  CountedPtr<Lua51Script> ScriptToStow = CountedPtrCast<Lua51Script>(ScriptToAdd);
72  if(ScriptToStow)
73  {
74  AddScript(ScriptToStow);
75  }else{
76  MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_CAST_EXCEPTION, "Lua51WorkUnit attempted to store a script, but it did not appear to bea Lua51 script.")
77  }
78  }
79 
81  { push_back(ScriptToAdd); }
82 
83 
85  { AddScript(CountedPtr<Lua51Script>(new Lua51Script(Source, LuaRuntime))); }
86 
87  Lua51WorkUnit::iterator Lua51WorkUnit::find(CountedPtr<Lua51Script> Target)
88  {
89  for(Lua51WorkUnit::iterator Iter=this->begin(); Iter!=this->end() ; Iter++)
90  {
91  if(*Iter == Target)
92  { return Iter; }
93  }
94  return this->end();
95  }
96 
97  Lua51WorkUnit::const_iterator Lua51WorkUnit::find(CountedPtr<Lua51Script> Target) const
98  {
99  for(Lua51WorkUnit::const_iterator Iter=this->begin(); Iter!=this->end(); Iter++)
100  {
101  if(*Iter == Target)
102  { return Iter; }
103  }
104  return this->end();
105  }
106 
108  {
109  Lua51WorkUnit::iterator Iter = this->find(Target);
110  if(Iter!=this->end())
111  { ScriptsToRun.erase(Iter); }
112  }
113 
114  void Lua51WorkUnit::erase(Lua51WorkUnit::iterator Target)
115  { ScriptsToRun.erase(Target); }
116 
118  {
119  CountedPtr<Lua51Script> ScriptToBtow = CountedPtrCast<Lua51Script>(ScriptToRemove);
120  if(ScriptToBtow)
121  {
122  RemoveScript(ScriptToBtow);
123  }else{
124  MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_CAST_EXCEPTION, "Lua51WorkUnit attempted to removed a script, but it did not appear to bea Lua51 script.")
125  }
126  }
127 
129  { erase(ScriptToRemove); }
130 
132  { erase(this->begin()+Index); }
133 
134  Lua51WorkUnit::iterator Lua51WorkUnit::begin()
135  { return ScriptsToRun.begin(); }
136  Lua51WorkUnit::const_iterator Lua51WorkUnit::begin() const
137  { return ScriptsToRun.begin(); }
138 
139  Lua51WorkUnit::iterator Lua51WorkUnit::end()
140  { return ScriptsToRun.end(); }
141  Lua51WorkUnit::const_iterator Lua51WorkUnit::end() const
142  { return ScriptsToRun.end(); }
143 
145  { return ScriptsToRun.size(); }
146 
148  { ScriptsToRun.clear(); }
149 
151  { return CountedPtrCast<iScript>(ScriptsToRun.at(Index)); }
152 
154  { return ScriptsToRun.at(Index); }
155 
157  {
158  Logger& Log = CurrentThreadStorage.GetUsableLogger();
159  Log << "Running Per Frame LuaScripts." << std::endl;
160  for(iterator Iter = this->begin(); Iter!=this->end(); Iter++)
161  {
162  #ifdef MEZZ_DEBUG
163  Log << "Executing \"" << (*Iter)->GetName() << "\"" << std::endl;
164  #endif
165  LuaRuntime->Execute(*Iter);
166  }
167  }
168 
169 
170  } // Lua
171  } // Scripting
172 } // Mezzanine
173 
174 
175 
176 
177 #endif // MEZZLUA51
178 #endif
virtual Whole GetScriptCount() const
How many Scripts have been added to this workunit.
std::stringstream Logger
In case we ever replace the stringstream with another class, this will allow us to swap it out...
Definition: datatypes.h:180
iterator find(CountedPtr< Lua51Script > Target)
Get an Iterator to a script from the counted pointer.
virtual CountedPtr< iScript > Execute(const String &ScriptSource)
Compile and execute a passed string.
virtual void AddScript(CountedPtr< iScript > ScriptToAdd)
Adds a script similar to push_back.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
All the definitions for datatypes as well as some basic conversion functions are defined here...
A simple reference counting pointer.
Definition: countedptr.h:70
This class is used to store a Lua script and in its source and compiled state.
Definition: lua51script.h:94
void push_back(CountedPtr< Lua51Script > ScriptToAdd)
Adds a script to be run once each frame.
virtual CountedPtr< Lua51Script > GetLua51Script(Whole Index) const
Retrieve a Script previously passed in.
A thread specific collection of double-buffered and algorithm specific resources. ...
This file has the declaration for a workunit that can execute lua script every frame.
The workhorse of the Lua scripting system. All scripts come here to be executed.
virtual CountedPtr< iScript > GetScript(Whole Index)
Retrieve a Script previously passed in.
Lua51WorkUnit(Lua51ScriptingEngine *TargetRuntime)
Create a Lua51WorkUnit.
Logger & GetUsableLogger()
Get the usable logger for this thread specific resource.
void erase(CountedPtr< Lua51Script > Target)
Erase The first found Script use that pointer.
virtual void RemoveScript(CountedPtr< iScript > ScriptToRemove)
Similar to calling erase.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
virtual ~Lua51WorkUnit()
Virtual deconstructor.
Thrown when a pointer parameter is checked at runtime and cannot be cast as expected.
Definition: exception.h:109
virtual void DoWork(Threading::DefaultThreadSpecificStorage::Type &CurrentThreadStorage)
Runs all scripts that have been added to this work unit.
iterator begin()
Get an iterator to the first script.
This file has the interface for the Lua based implementation of the Scripting system.
virtual void ClearScripts()
Remove all the Scripts from this workunit.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
iterator end()
Get an iterator one past the last script.