Spinning Topp Logo BlackTopp Studios
inc
lua51script.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 _lua51script_h
41 #define _lua51script_h
42 
43 #include "datatypes.h"
44 
45 #ifdef MEZZLUA51
46 
48 #include "Scripting/script.h"
50 
51 /// @file
52 /// @brief This file has the interface for the Lua scripts.
53 
54 
55 namespace Mezzanine
56 {
57  namespace Scripting
58  {
59  namespace Lua
60  {
61  class Lua51ScriptingEngine;
62  class Lua51IntegerArgument;
63  class Lua51RealArgument;
64  class Lua51WholeArgument;
65  class Lua51StringArgument;
66  class Lua51BoolArgument;
67  class Lua51NilArgument;
68 
69  /// @brief An expanded version of the BinaryTools::BinaryBuffer to carry one tiny piece of metadata around with it.
71  {
72  public:
73  /// @brief just a passthru to BinaryBuffer::operator=
74  /// @param RH The assiging binary buffer
75  /// @return This returns a reference to this for operator chaing and such.
77  {
78  BinaryBuffer::operator=(RH);
79  return *this;
80  }
81 
82  /// @brief Used to indicate that this data has been loaded into lua.
83  bool Loaded;
84 
85  /// @brief Constructor just set the extra flag to false.
86  FlaggedBuffer() : Loaded(false)
87  {}
88  };
89 
90  ////////////////////////////////////////////////////////////////////////////////////////
91  /// @brief This class is used to store a Lua script and in its source and compiled
92  /// state.
93  /// @details To execute this script use @ref LuaScriptingEngine::Compile
97  {
98  private:
99  /// @brief Makes passing internal data much easier and all Lua51 are logically
100  /// encapsulated as a single system still.
101  friend class Lua51ScriptingEngine;
102 
103  /// @brief A set of the arguments being passed into the Lua script
104  ArgumentGroup Args;
105 
106  /// @brief A set of all the values the Lua script returned the last time it was
107  /// executed.
108  ArgumentGroup Returns;
109 
110  /// @brief This will contain the source of the script
111  String SourceCode;
112 
113  /// @brief Stores the Name of the Lua Script
114  String Name;
115 
116  /// @brief If the script is compiled this will be used to store it.
117  FlaggedBuffer CompiledByteCode;
118 
119  /// @brief If set this script just calls an existing lua function.
120  /// @todo Add a function to set/get this
121  Boole FunctionCall;
122 
123  public:
124  ///////////////////////////////////////////////////////////////////////////////////////
125  // LuaScript basics
126  /// @brief Simple constructor, creates a script that executes a no-op.
127  Lua51Script();
128 
129  /// @brief Compiling Constructor
130  /// @param InitialSourceCode The source of the script to be used in this.
131  /// @param Compiler Defaults to a null pointer. If passed a null pointer this
132  /// does nothing. If passed a valid LuaScriptingEngine then that engine is used
133  /// to compile (but not run) this script.
134  /// @param JustAFunctionCall Defaults to false, when true this optimizes the
135  /// calling process and simply calls a function.
136  /// @param ScriptName For tracking and recalling scripts by name, defaults to
137  /// "?"
138  Lua51Script(const String& InitialSourceCode,
139  Lua51ScriptingEngine* Compiler=0,
140  Boole JustAFunctionCall = false,
141  String ScriptName = "?");
142 
143  /// @brief Compiling Cosntructor without pointer
144  /// @param InitialSourceCode The source of the script to be used in this.
145  /// @param Compiler A reference to a valid LuaScriptingEngine then that engine
146  /// is used to compile (but not run) this script.
147  /// @param JustAFunctionCall Defaults to false, when true this optimizes the
148  /// calling process and simply calls a function.
149  /// @param ScriptName For tracking and recalling scripts by name, defaults to
150  /// "?"
151  Lua51Script(const String& InitialSourceCode,
152  Lua51ScriptingEngine& Compiler,
153  Boole JustAFunctionCall = false,
154  String ScriptName = "?");
155 
156  /// @brief Virtual destructor
157  virtual ~Lua51Script();
158 
159  ///////////////////////////////////////////////////////////////////////////////////////
160  // Arguments
161  /// @copydoc Mezzanine::Scripting::iScript::AddArgument
162  virtual void AddArgument(CountedPtr<iScriptArgument> Arg);
163 
164  /// @brief Another overload to make adding arguments easier
165  /// @param Arg A Lua51IntegerArgument to pass into the script
166  virtual void AddArgument(Lua51IntegerArgument Arg);
167 
168  /// @brief Another overload to make adding arguments easier
169  /// @param Arg A Lua51RealArgument to pass into the script
170  virtual void AddArgument(Lua51RealArgument Arg);
171 
172  /// @brief Another overload to make adding arguments easier
173  /// @param Arg A Lua51WholeArgument to pass into the script
174  virtual void AddArgument(Lua51WholeArgument Arg);
175 
176  /// @brief Another overload to make adding arguments easier
177  /// @param Arg A Lua51StringArgument to pass into the script
178  virtual void AddArgument(Lua51StringArgument Arg);
179 
180  /// @brief Another overload to make adding arguments easier
181  /// @param Arg A Lua51BoolArgument to pass into the script
182  virtual void AddArgument(Lua51BoolArgument Arg);
183 
184  /// @brief Another overload to make adding arguments easier
185  /// @param Arg A Lua51NilArgument to pass into the script
186  virtual void AddArgument(Lua51NilArgument Arg);
187 
188  /// @brief Another overload to make adding arguments easier, This one adds a Lua51IntegerArgument
189  /// @param Arg An Integer to pass into the script
190  virtual void AddArgument(Integer Arg);
191 
192  /// @brief Another overload to make adding arguments easier, This one adds a Lua51RealArgument
193  /// @param Arg An Real to pass into the script
194  virtual void AddArgument(Real Arg);
195 
196  /// @brief Another overload to make adding arguments easier, This one adds a Lua51WholeArgument
197  /// @param Arg An Whole to pass into the script
198  virtual void AddArgument(Whole Arg);
199 
200  /// @brief Another overload to make adding arguments easier, This one adds a Lua51StringArgument
201  /// @param Arg An String to pass into the script
202  virtual void AddArgument(String Arg);
203 
204  /// @brief Another overload to make adding arguments easier, This one adds a Lua51StringArgument
205  /// @param Arg An Char8* to pass into the script
206  virtual void AddArgument(Char8* Arg);
207 
208  /// @brief Another overload to make adding arguments easier, This one adds a Lua51BoolArgument
209  /// @param Arg An Bool to pass into the script
210  virtual void AddArgument(Boole Arg);
211 
212  /// @brief When called with no arguments this inserts a Lua51nil
213  virtual void AddArgument();
214 
215  /// @copydoc Mezzanine::Scripting::iScript::RemoveArgument
216  virtual void RemoveArgument(CountedPtr<iScriptArgument> Arg);
217 
218  /// @copydoc Mezzanine::Scripting::iScript::RemoveArgument
219  virtual void RemoveArgument(Whole ArgNumber);
220 
221  /// @copydoc Mezzanine::Scripting::iScript::GetArgumentCount
222  virtual Whole GetArgumentCount() const;
223 
224  /// @copydoc Mezzanine::Scripting::iScript::ClearArguments
225  virtual void ClearArguments();
226 
227  /// @copydoc Mezzanine::Scripting::iScript::GetArgument
228  virtual CountedPtr<iScriptArgument> GetArgument(Whole ArgNumber) const;
229 
230  ///////////////////////////////////////////////////////////////////////////////////////
231  // Source code
232  /// @copydoc Mezzanine::Scripting::iScript::SetSourceCode
233  virtual void SetSourceCode(const String& Code);
234 
235  /// @copydoc SetSourceCode(const String&)
236  /// @param Return1 Something for containing the return from the script
237  virtual void SetSourceCode(const String& Code, CountedPtr<iScriptArgument> Return1);
238 
239  /// @copydoc Mezzanine::Scripting::iScript::GetSourceCode
240  virtual String GetSourceCode() const;
241 
242  /// @copydoc Mezzanine::Scripting::iScriptCompilable::SetByteCode
243  virtual void SetByteCode(BinaryTools::BinaryBuffer Code);
244 
245  /// @copydoc Mezzanine::Scripting::iScriptCompilable::GetByteCode
246  virtual BinaryTools::BinaryBuffer GetByteCode() const;
247 
248  /// @brief Get a reference to bytecode instead of a copy.
249  /// @return A BinaryTools::BinaryBuffer& containing either nothing or the Bytecode that would executed.
250  virtual FlaggedBuffer& GetByteCodeReference();
251 
252  /// @copydoc Mezzanine::Scripting::iScriptCompilable::IsCompiled
253  virtual bool IsCompiled() const;
254 
255  /// @brief This will compile the Lua script
256  /// @param Compiler This will be used to compile the script, no safety checks are performed.
257  void Compile(Lua51ScriptingEngine* Compiler);
258 
259  /// @brief This will compile the Lua script
260  /// @param Compiler This will be used to compile the script, no safety checks are performed.
261  void Compile(Lua51ScriptingEngine& Compiler);
262 
263  ///////////////////////////////////////////////////////////////////////////////////////
264  // Name
265 
266  /// @brief Get the name of the current Script
267  /// @return A String containing the name
268  String GetName() const;
269 
270  /// @brief change the name of the Script
271  /// @param NewName The new value for the scripts name
272  void SetName(const String &NewName);
273 
274  ///////////////////////////////////////////////////////////////////////////////////////
275  // Returns
276  /// @copydoc Mezzanine::Scripting::iScriptMultipleReturn::GetReturnCount
277  virtual Whole GetReturnCount() const;
278 
279  /// @copydoc Mezzanine::Scripting::iScriptMultipleReturn::AddReturn
280  virtual void AddReturn(CountedPtr<iScriptArgument> ReturnArg);
281 
282  /// @copydoc Mezzanine::Scripting::iScriptMultipleReturn::GetAllReturns
283  virtual ArgumentGroup GetAllReturns() const;
284 
285  /// @copydoc Mezzanine::Scripting::iScriptMultipleReturn::GetReturn
286  virtual CountedPtr<iScriptArgument> GetReturn(Whole ReturnNumber) const;
287 
288 
289  ///////////////////////////////////////////////////////////////////////////////////////
290  // CountedPointer Inheritance
291  public:
292  /// @brief Get a pointer to the most Derived type of this class
293  /// @return A pointer of the most derived pointing to this.
295  { return this; }
296  };
297  } // Lua
298  } // Scripting
299 
300  /// @brief Marks Lua51Script for internal reference counting if a CountedPtr checks
301  template <>
302  class ReferenceCountTraits <Scripting::Lua::Lua51Script>
303  {
304  public:
305  /// @brief The type that maintains the Reference count for Lua51Script with be Lua51Script itself
307 
308  /// @brief Given a pointer to the raw object this will return a pointer to an initialized reference count
309  /// @param Target A pointer to a Scripting::Lua::Lua51Script that will simply be returned
310  /// @return This returns whatever was passed into target because it already is a valid Reference Counter
311  static RefCountType* ConstructionPointer(RefCountType* Target)
312  { return Target; }
313 
314  /// @brief This uses dynamic casting when resolving casts inside the CountedPtr
315  enum { IsCastable = CastDynamic };
316  };
317 } // Mezzanine
318 
319 
320 #endif // MEZZLUA51
321 #endif // \_scriptinglua_h
FlaggedBuffer & operator=(const BinaryBuffer &RH)
just a passthru to BinaryBuffer::operator=
Definition: lua51script.h:76
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
std::vector< CountedPtr< iScriptArgument > > ArgumentGroup
A group of arguments that can be returned from some scripts.
Definition: script.h:295
Scripting::Lua::Lua51Script RefCountType
The type that maintains the Reference count for Lua51Script with be Lua51Script itself.
Definition: lua51script.h:306
All the definitions for datatypes as well as some basic conversion functions are defined here...
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
This class is used to store a Lua script and in its source and compiled state.
Definition: lua51script.h:94
This file has the interfaces for Scripts and tag derived classes.
The interface for a script that can be compiled to bytecode.
Definition: script.h:238
No special care is required for Whole number Lua Arguments, so a simple typedef is used...
This script can return simple group of values.
Definition: script.h:301
A dynamic cast from the pointer as provided with no attempt to calls functions on the pointer target...
Definition: countedptr.h:64
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
char Char8
A datatype to represent one character.
Definition: datatypes.h:169
static RefCountType * ConstructionPointer(RefCountType *Target)
Given a pointer to the raw object this will return a pointer to an initialized reference count...
Definition: lua51script.h:311
No special care is required for String Lua Arguments, so a simple typedef is used.
This is used to deduce at compile if a specific class has built-in reference counting or needs an ext...
Definition: countedptr.h:87
An expanded version of the BinaryTools::BinaryBuffer to carry one tiny piece of metadata around with ...
Definition: lua51script.h:70
The implementations in the ScriptArgumentGeneric will cover most of what this needs...
FlaggedBuffer()
Constructor just set the extra flag to false.
Definition: lua51script.h:86
The workhorse of the Lua scripting system. All scripts come here to be executed.
bool Loaded
Used to indicate that this data has been loaded into lua.
Definition: lua51script.h:83
virtual Lua51Script * GetMostDerived()
Get a pointer to the most Derived type of this class.
Definition: lua51script.h:294
BinaryBuffer()
Default constructor, set everything to zero. Doesn't allocate anything.
This file has the interfaces for Script Arguments and the associated dependency chain.
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
No special care is required for Bool Lua Arguments, so a simple typedef is used.
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
A way to store and pass binary buffers, for example compiled bytecode.
Definition: binarybuffer.h:68
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
A Real that can readily be passed into lua scripts.
Represents not much of anything but will insert and retrieves Nils from Lua51.
This file has the interfaces for Scripting managers and the associated dependency chain...