1 // Copyright 2011 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 /// Provides the state wrapper class for the Lua C state.
32 #if !defined(LUTOK_STATE_HPP)
33 #define LUTOK_STATE_HPP
37 #if defined(_LIBCPP_VERSION) || __cplusplus >= 201103L
50 /// The type of a C++ function that can be bound into Lua.
52 /// Functions of this type are free to raise exceptions. These will not
53 /// propagate into the Lua C API. However, any such exceptions will be reported
54 /// as a Lua error and their type will be lost.
55 typedef int (*cxx_function)(state&);
58 /// Stack index constant pointing to the registry table.
59 extern const int registry_index;
62 /// A RAII model for the Lua state.
64 /// This class holds the state of the Lua interpreter during its existence and
65 /// provides wrappers around several Lua library functions that operate on such
68 /// These wrapper functions differ from the C versions in that they use the
69 /// implicit state hold by the class, they use C++ types where appropriate and
70 /// they use exceptions to report errors.
72 /// The wrappers intend to be as lightweight as possible but, in some
73 /// situations, they are pretty complex because they need to do extra work to
74 /// capture the errors reported by the Lua C API. We prefer having fine-grained
75 /// error control rather than efficiency, so this is OK.
79 /// Pointer to the shared internal implementation.
80 #if defined(_LIBCPP_VERSION) || __cplusplus >= 201103L
81 std::shared_ptr< impl > _pimpl;
83 std::tr1::shared_ptr< impl > _pimpl;
86 void* new_userdata_voidp(const size_t);
87 void* to_userdata_voidp(const int);
89 friend class state_c_gate;
90 explicit state(void*);
91 void* raw_state(void);
98 void get_global(const std::string&);
99 void get_global_table(void);
100 bool get_metafield(const int, const std::string&);
101 bool get_metatable(const int);
102 void get_table(const int);
104 void insert(const int);
105 bool is_boolean(const int);
106 bool is_function(const int);
107 bool is_nil(const int);
108 bool is_number(const int);
109 bool is_string(const int);
110 bool is_table(const int);
111 bool is_userdata(const int);
112 void load_file(const std::string&);
113 void load_string(const std::string&);
114 void new_table(void);
115 template< typename Type > Type* new_userdata(void);
116 bool next(const int);
118 void open_base(void);
119 void open_string(void);
120 void open_table(void);
121 void pcall(const int, const int, const int);
123 void push_boolean(const bool);
124 void push_cxx_closure(cxx_function, const int);
125 void push_cxx_function(cxx_function);
126 void push_integer(const int);
128 void push_string(const std::string&);
129 void push_value(const int);
130 void raw_get(const int);
131 void raw_set(const int);
132 void set_global(const std::string&);
133 void set_metatable(const int);
134 void set_table(const int);
135 bool to_boolean(const int);
136 long to_integer(const int);
137 template< typename Type > Type* to_userdata(const int);
138 std::string to_string(const int);
139 int upvalue_index(const int);
145 #endif // !defined(LUTOK_STATE_HPP)