Spinning Topp Logo BlackTopp Studios
inc
lua51workunit.h
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_h
41 #define _lua51workunit_h
42 
43 #include "datatypes.h"
44 
45 #ifdef MEZZLUA51
46 
49 
50 #include "Threading/workunit.h"
51 
52 #include <vector>
53 
54 /// @file
55 /// @brief This file has the declaration for a workunit that can execute lua script every frame
56 
57 
58 namespace Mezzanine
59 {
60  namespace Scripting
61  {
62  namespace Lua
63  {
64  // foraward declarations
65  class Lua51ScriptingEngine;
66 
67  /// @brief This is a simple Container of script that will execute every script it is
68  /// given in order each frame.
69  /// @details This WorkUnit starts with no dependencies, the developer using this must
70  /// set those, (see @ref Threading::DefaultWorkUnit::AddDependency for details). This
71  /// is not automatically added to the WorkScheduler on the Entresol, and must be added
72  /// manually.
73  /// @n @n
74  /// Internally this uses an std::vector to store scripts and each frame it will iterate
75  /// over them and execute them one at a time. This exposes iterators and a few convience
76  /// function to make the script manageable.
77  /// @n @n
78  /// All scripts are stored in CountedPtr to allow for shared ownership.
80  {
81  private:
82  /// @internal
83  /// @brief Stores the pointers to each script to run.
84  std::vector<CountedPtr<Lua51Script> > ScriptsToRun;
85  /// @brief A basic iterator.
86  typedef std::vector<CountedPtr<Lua51Script> >::iterator iterator;
87  /// @brief A read only iterator .
88  typedef std::vector<CountedPtr<Lua51Script> >::const_iterator const_iterator;
89 
90  /// @brief Used to track where to run the scripts
91  Lua51ScriptingEngine* LuaRuntime;
92  public:
93 
94  /// @brief Create a Lua51WorkUnit
95  /// @param TargetRuntime The Lua runtime to execute Scripts against.
96  Lua51WorkUnit(Lua51ScriptingEngine* TargetRuntime);
97  /// @brief Virtual deconstructor
98  virtual ~Lua51WorkUnit();
99 
100  /// @brief Adds a script to be run once each frame.
101  /// @param ScriptToAdd A CountedPtr to a script that should be run each Frame.
102  /// @note Consider this as Invalidating all iterators to this container.
103  void push_back(CountedPtr<Lua51Script> ScriptToAdd);
104  /// @brief Adds a script similar to push_back.
105  /// @param ScriptToAdd A iScript to add.
106  virtual void AddScript(CountedPtr<iScript> ScriptToAdd);
107  /// @brief Adds a script similar to push_back (Actually calls it).
108  /// @param ScriptToAdd A Lua51Script to add.
109  virtual void AddScript(CountedPtr<Lua51Script> ScriptToAdd);
110  /// @brief Add a script by providing it source code.
111  /// @param Source The Lua source code to add.
112  virtual void AddScript(String Source);
113 
114 
115  /// @brief Get an Iterator to a script from the counted pointer
116  /// @param Target a CountedPtr to convert into an iterator
117  /// @return If the Script has been added this returns its iterator, otherwise it returns and end() iterator.
118  /// @note Searches in Linear time, likely useless for external use, it is used internally.
119  iterator find(CountedPtr<Lua51Script> Target);
120  /// @brief Get an const_iterator to a script from the counted pointer
121  /// @param Target a CountedPtr to convert into an const_iterator
122  /// @return If the Script has been added this returns its const_iterator, otherwise it returns and end() const_iterator.
123  /// @note Searches in Linear time, likely useless for external use, it is used internally.
124  const_iterator find(CountedPtr<Lua51Script> Target) const;
125 
126  /// @brief Erase The first found Script use that pointer
127  /// @param Target A CountedPtr to a script that has already been added. If the script has not been added this fails silently.
128  /// @note Consider this as Invalidating all iterators to this container. This takes linear time to find the Pointer in the
129  /// Container, then linear time to erase the entry for each Script after the target script.
130  void erase(CountedPtr<Lua51Script> Target);
131  /// @brief Remove the target script from the container
132  /// @param Target The script to remvoe.
133  /// @note This takes linear time to erase the entry for each Script after the target script or constant time if it is the last Script
134  void erase(iterator Target);
135  /// @brief Similar to calling erase
136  /// @param ScriptToRemove A CountedPtr to the script to remove
137  virtual void RemoveScript(CountedPtr<iScript> ScriptToRemove);
138  /// @brief The Same as calling erase and passing a CountedPtr to a script
139  /// @param ScriptToRemove A CountedPtr to the script to remove
140  virtual void RemoveScript(CountedPtr<Lua51Script> ScriptToRemove);
141  /// @brief Remove a script by index.
142  /// @param Index The index of the Script to Remove.
143  virtual void RemoveScript(Whole Index);
144 
145  /// @brief Get an iterator to the first script.
146  /// @return An iterator.
147  iterator begin();
148  /// @brief Get a const_iterator to the first script.
149  /// @return A const_iterator.
150  const_iterator begin() const;
151 
152  /// @brief Get an iterator one past the last script.
153  /// @return An iterator
154  iterator end();
155  /// @brief Get a const_iterator one past the last script.
156  /// @return A const_iterator
157  const_iterator end() const;
158 
159  /// @brief How many Scripts have been added to this workunit
160  /// @return A Whole containing the amount of arguments passed in so far.
161  virtual Whole GetScriptCount() const;
162 
163  /// @brief Remove all the Scripts from this workunit
164  /// @details This should run in constant time. It still might be slower than removing and reading just one a few Scripts.
165  virtual void ClearScripts();
166 
167  /// @brief Retrieve a Script previously passed in.
168  /// @param Index The index of the passed parameter to retrun.
169  /// @return A reference counted pointer to a iScript.
170  virtual CountedPtr<iScript> GetScript(Whole Index);
171  /// @brief Retrieve a Script previously passed in.
172  /// @param Index The index of the passed parameter to retrun.
173  /// @return A reference counted pointer to a Lua51Script.
174  virtual CountedPtr<Lua51Script> GetLua51Script(Whole Index) const;
175 
176  /// @brief Runs all scripts that have been added to this work unit
177  virtual void DoWork(Threading::DefaultThreadSpecificStorage::Type& CurrentThreadStorage);
178  };
179  } // Lua
180  } // Scripting
181 
182 } // Mezzanine
183 
184 
185 #endif // MEZZLUA51
186 #endif // \ Include gaurd
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 file has the interfaces for ScriptsWorkUnit.
A thread specific collection of double-buffered and algorithm specific resources. ...
This file has the interface for the Lua scripts.
This is a simple Container of script that will execute every script it is given in order each frame...
Definition: lua51workunit.h:79
The workhorse of the Lua scripting system. All scripts come here to be executed.
The interface for a container of script that can be executed each frame.
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
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
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
This file has the definition of the workunit.