add "sameinstance()" lua binding for all shared/weak ptrs
[ardour.git] / libs / lua / LuaBridge / detail / LuaException.h
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:
33   lua_State* m_L;
34   std::string m_what;
35
36 public:
37   //----------------------------------------------------------------------------
38   /**
39       Construct a LuaException after a lua_pcall().
40   */
41   LuaException (lua_State* L, int /*code*/)
42     : m_L (L)
43   {
44     whatFromStack ();
45   }
46
47   //----------------------------------------------------------------------------
48
49   LuaException (lua_State *L,
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   //============================================================================
72   /**
73       Throw an exception.
74
75       This centralizes all the exceptions thrown, so that we can set
76       breakpoints before the stack is unwound, or otherwise customize the
77       behavior.
78   */
79   template <class Exception>
80   static void Throw (Exception e)
81   {
82     throw e;
83   }
84
85   //----------------------------------------------------------------------------
86   /**
87       Wrapper for lua_pcall that throws.
88   */
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:
100   void whatFromStack ()
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 };