Ardour  8.7-14-g57a6773833
LuaException.h
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 /*
3  https://github.com/vinniefalco/LuaBridge
4 
5  Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
6  Copyright 2008, Nigel Atkinson <suprapilot+LuaCode@gmail.com>
7 
8  License: The MIT License (http://www.opensource.org/licenses/mit-license.php)
9 
10  Permission is hereby granted, free of charge, to any person obtaining a copy
11  of this software and associated documentation files (the "Software"), to deal
12  in the Software without restriction, including without limitation the rights
13  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  copies of the Software, and to permit persons to whom the Software is
15  furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included in all
18  copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26  SOFTWARE.
27 */
28 //==============================================================================
29 
30 class LuaException : public std::exception
31 {
32 private:
34  std::string m_what;
35 
36 public:
37  //----------------------------------------------------------------------------
41  LuaException (lua_State* L, int /*code*/)
42  : m_L (L)
43  {
44  whatFromStack ();
45  }
46 
47  //----------------------------------------------------------------------------
48 
50  char const*,
51  char const*,
52  long)
53  : m_L (L)
54  {
55  whatFromStack ();
56  }
57 
58  //----------------------------------------------------------------------------
59 
60  ~LuaException() throw ()
61  {
62  }
63 
64  //----------------------------------------------------------------------------
65 
66  char const* what() const throw ()
67  {
68  return m_what.c_str();
69  }
70 
71  //============================================================================
79  template <class Exception>
80  static void Throw (Exception e)
81  {
82  throw e;
83  }
84 
85  //----------------------------------------------------------------------------
89  static void pcall (lua_State* L, int nargs = 0, int nresults = 0, int msgh = 0)
90  {
91  int code = lua_pcall (L, nargs, nresults, msgh);
92 
93  if (code != LUABRIDGE_LUA_OK)
94  Throw (LuaException (L, code));
95  }
96 
97  //----------------------------------------------------------------------------
98 
99 protected:
101  {
102  if (lua_gettop (m_L) > 0)
103  {
104  char const* s = lua_tostring (m_L, -1);
105  m_what = s ? s : "";
106  }
107  else
108  {
109  // stack is empty
110  m_what = "missing error";
111  }
112  }
113 };
#define LUABRIDGE_LUA_OK
Definition: LuaHelpers.h:99
std::string m_what
Definition: LuaException.h:34
static void pcall(lua_State *L, int nargs=0, int nresults=0, int msgh=0)
Definition: LuaException.h:89
LuaException(lua_State *L, int)
Definition: LuaException.h:41
void whatFromStack()
Definition: LuaException.h:100
char const * what() const
Definition: LuaException.h:66
lua_State * m_L
Definition: LuaException.h:33
static void Throw(Exception e)
Definition: LuaException.h:80
LuaException(lua_State *L, char const *, char const *, long)
Definition: LuaException.h:49
#define lua_pcall(L, n, r, f)
int() lua_gettop(lua_State *L)
#define lua_tostring(L, i)