1*a1563752Sjmmv // Copyright 2011 Google Inc.
2*a1563752Sjmmv // All rights reserved.
3*a1563752Sjmmv //
4*a1563752Sjmmv // Redistribution and use in source and binary forms, with or without
5*a1563752Sjmmv // modification, are permitted provided that the following conditions are
6*a1563752Sjmmv // met:
7*a1563752Sjmmv //
8*a1563752Sjmmv // * Redistributions of source code must retain the above copyright
9*a1563752Sjmmv // notice, this list of conditions and the following disclaimer.
10*a1563752Sjmmv // * Redistributions in binary form must reproduce the above copyright
11*a1563752Sjmmv // notice, this list of conditions and the following disclaimer in the
12*a1563752Sjmmv // documentation and/or other materials provided with the distribution.
13*a1563752Sjmmv // * Neither the name of Google Inc. nor the names of its contributors
14*a1563752Sjmmv // may be used to endorse or promote products derived from this software
15*a1563752Sjmmv // without specific prior written permission.
16*a1563752Sjmmv //
17*a1563752Sjmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*a1563752Sjmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*a1563752Sjmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*a1563752Sjmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*a1563752Sjmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*a1563752Sjmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*a1563752Sjmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*a1563752Sjmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*a1563752Sjmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*a1563752Sjmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*a1563752Sjmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*a1563752Sjmmv
29*a1563752Sjmmv #include "c_gate.hpp"
30*a1563752Sjmmv #include "state.ipp"
31*a1563752Sjmmv
32*a1563752Sjmmv
33*a1563752Sjmmv /// Creates a new gateway to an existing C++ Lua state.
34*a1563752Sjmmv ///
35*a1563752Sjmmv /// \param state_ The state to connect to. This object must remain alive while
36*a1563752Sjmmv /// the newly-constructed state_c_gate is alive.
state_c_gate(state & state_)37*a1563752Sjmmv lutok::state_c_gate::state_c_gate(state& state_) :
38*a1563752Sjmmv _state(state_)
39*a1563752Sjmmv {
40*a1563752Sjmmv }
41*a1563752Sjmmv
42*a1563752Sjmmv
43*a1563752Sjmmv /// Destructor.
44*a1563752Sjmmv ///
45*a1563752Sjmmv /// Destroying this object has no implications on the life cycle of the Lua
46*a1563752Sjmmv /// state. Only the corresponding state object controls when the Lua state is
47*a1563752Sjmmv /// closed.
~state_c_gate(void)48*a1563752Sjmmv lutok::state_c_gate::~state_c_gate(void)
49*a1563752Sjmmv {
50*a1563752Sjmmv }
51*a1563752Sjmmv
52*a1563752Sjmmv
53*a1563752Sjmmv /// Creates a C++ state for a C Lua state.
54*a1563752Sjmmv ///
55*a1563752Sjmmv /// \warning The created state object does NOT own the C state. You must take
56*a1563752Sjmmv /// care to properly destroy the input lua_State when you are done with it to
57*a1563752Sjmmv /// not leak resources.
58*a1563752Sjmmv ///
59*a1563752Sjmmv /// \param raw_state The raw state to wrap temporarily.
60*a1563752Sjmmv ///
61*a1563752Sjmmv /// \return The wrapped state without strong ownership on the input state.
62*a1563752Sjmmv lutok::state
connect(lua_State * raw_state)63*a1563752Sjmmv lutok::state_c_gate::connect(lua_State* raw_state)
64*a1563752Sjmmv {
65*a1563752Sjmmv return state(static_cast< void* >(raw_state));
66*a1563752Sjmmv }
67*a1563752Sjmmv
68*a1563752Sjmmv
69*a1563752Sjmmv /// Returns the C native Lua state.
70*a1563752Sjmmv ///
71*a1563752Sjmmv /// \return A native lua_State object holding the Lua C API state.
72*a1563752Sjmmv lua_State*
c_state(void)73*a1563752Sjmmv lutok::state_c_gate::c_state(void)
74*a1563752Sjmmv {
75*a1563752Sjmmv return static_cast< lua_State* >(_state.raw_state());
76*a1563752Sjmmv }
77