Spinning Topp Logo BlackTopp Studios
inc
lua51scriptargument.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 _lua51scriptargument_cpp
41 #define _lua51scriptargument_cpp
42 
43 #include "datatypes.h"
44 #include "lua51scriptargument.h"
45 
46 #ifdef MEZZLUA51
47 
48 #include "lua51script.h"
49 #include "lua51scriptingengine.h"
50 #include "exception.h"
51 
52 #include <algorithm>
53 
54 /// @file
55 /// @brief This file has the implementation for the Lua script argument.
56 
57 extern "C"
58 {
59  #include "lua.h" // Lua Core
60  #include "lualib.h" // for opening the base state
61  #include "lauxlib.h" // Extra Lua Goodies like lua_open()
62 }
63 
64 namespace Mezzanine
65 {
66  namespace Scripting
67  {
68  namespace Lua
69  {
70 
71 
72  void Lua51IntegerArgument::Push(lua_State* TargetState) const
73  { lua_pushinteger(TargetState, this->Datum); }
74  void Lua51IntegerArgument::Pop(lua_State* TargetState)
75  {
76  int Top = lua_gettop(TargetState);
77  if(LUA_TNUMBER==lua_type(TargetState,Top))
78  {
79  Datum = lua_tointeger(TargetState,Top);
80  lua_pop(TargetState,1);
81  }
82  else
83  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION, "Expected a Lua number(Integer), but found something else.") }
84  }
85 
86  void Lua51RealArgument::Push(lua_State* TargetState) const
87  { lua_pushnumber(TargetState, this->Datum); }
88  void Lua51RealArgument::Pop(lua_State* TargetState)
89  {
90  int Top = lua_gettop(TargetState);
91  if(LUA_TNUMBER==lua_type(TargetState,Top))
92  {
93  Datum = lua_tonumber(TargetState,Top);
94  lua_pop(TargetState,1);
95  }
96  else
97  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION, "Expected a Lua number(Real), but found something else.") }
98  }
99 
100  void Lua51WholeArgument::Push(lua_State* TargetState) const
101  { lua_pushnumber(TargetState, this->Datum); }
102  void Lua51WholeArgument::Pop(lua_State* TargetState)
103  {
104  int Top = lua_gettop(TargetState);
105  if(LUA_TNUMBER==lua_type(TargetState,Top))
106  {
107  Datum = lua_tointeger(TargetState,Top);
108  lua_pop(TargetState,1);
109  }
110  else
111  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION, "Expected a Lua number(Whole), but found something else.") }
112  }
113 
114  void Lua51StringArgument::Push(lua_State *TargetState) const
115  { lua_pushlstring(TargetState, this->Datum.c_str(), this->Datum.size() ); }
116  void Lua51StringArgument::Pop(lua_State *TargetState)
117  {
118  int Top = lua_gettop(TargetState);
119  if(LUA_TSTRING==lua_type(TargetState,Top))
120  {
121  Datum = lua_tostring(TargetState,Top);
122  lua_pop(TargetState,1);
123  }
124  else
125  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION, "Expected a Lua string(String), but found something else.") }
126  }
127 
128  void Lua51BoolArgument::Push(lua_State *TargetState) const
129  { lua_pushboolean(TargetState, this->Datum); }
130  void Lua51BoolArgument::Pop(lua_State *TargetState)
131  {
132  int Top = lua_gettop(TargetState);
133  if(LUA_TBOOLEAN==lua_type(TargetState,Top))
134  {
135  Datum = lua_toboolean(TargetState,Top);
136  lua_pop(TargetState,1);
137  }
138  else
139  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION, "Expected a Lua string(String), but found something else.") }
140  }
141 
142  void Lua51NilArgument::Push(lua_State *TargetState) const
143  { lua_pushnil(TargetState); }
144  void Lua51NilArgument::Pop(lua_State *TargetState)
145  {
146  int Top = lua_gettop(TargetState);
147  if(LUA_TNIL==lua_type(TargetState,Top))
148  { lua_pop(TargetState,1); }
149  else
150  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION, "Expected a Lua string(String), but found something else.") }
151  // It seems Lua Nils never go on the stack when being returned sometimes.
152  }
153 
154 
155 
156 
157  } // Lua
158  } // Scripting
159 } // Mezzanine
160 
161 
162 
163 
164 #endif // MEZZLUA51
165 #endif
#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...
virtual void Push(lua_State *TargetState) const
Handle the details of putting this data onto Lua's Stack.
virtual void Push(lua_State *TargetState) const
Handle the details of putting this data onto Lua's Stack.
This implements the exception hiearchy for Mezzanine.
virtual void Pop(lua_State *TargetState)
Handle the details of pulling data from Lua's Stack into this.
This file has the interface for the Lua scripts.
virtual void Push(lua_State *TargetState) const
Handle the details of putting this data onto Lua's Stack.
This file has the definition of the Script Arguments for Lua 51.
virtual void Pop(lua_State *TargetState)
Handle the details of pulling data from Lua's Stack into this.
virtual void Push(lua_State *TargetState) const
Handle the details of putting this data onto Lua's Stack.
Thrown when parameters are checked at runtime and found invalid.
Definition: exception.h:108
virtual void Push(lua_State *TargetState) const
Handle the details of putting this data onto Lua's Stack.
virtual void Pop(lua_State *TargetState)
Handle the details of pulling data from Lua's Stack into this.
virtual void Push(lua_State *TargetState) const
Handle the details of putting this data onto Lua's Stack.
virtual void Pop(lua_State *TargetState)
Handle the details of pulling data from Lua's Stack into this.
virtual void Pop(lua_State *TargetState)
Handle the details of pulling data from Lua's Stack into this.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
This file has the interface for the Lua based implementation of the Scripting system.
virtual void Pop(lua_State *TargetState)
Handle the details of pulling data from Lua's Stack into this.