// Copyright 2011 Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728/// \file state.hpp29/// Provides the state wrapper class for the Lua C state.3031#if !defined(LUTOK_STATE_HPP)32#define LUTOK_STATE_HPP3334#include <string>3536#if defined(_LIBCPP_VERSION) || __cplusplus >= 201103L37#include <memory>38#else39#include <tr1/memory>40#endif4142namespace lutok {434445class debug;46class state;474849/// The type of a C++ function that can be bound into Lua.50///51/// Functions of this type are free to raise exceptions. These will not52/// propagate into the Lua C API. However, any such exceptions will be reported53/// as a Lua error and their type will be lost.54typedef int (*cxx_function)(state&);555657/// Stack index constant pointing to the registry table.58extern const int registry_index;596061/// A RAII model for the Lua state.62///63/// This class holds the state of the Lua interpreter during its existence and64/// provides wrappers around several Lua library functions that operate on such65/// state.66///67/// These wrapper functions differ from the C versions in that they use the68/// implicit state hold by the class, they use C++ types where appropriate and69/// they use exceptions to report errors.70///71/// The wrappers intend to be as lightweight as possible but, in some72/// situations, they are pretty complex because they need to do extra work to73/// capture the errors reported by the Lua C API. We prefer having fine-grained74/// error control rather than efficiency, so this is OK.75class state {76struct impl;7778/// Pointer to the shared internal implementation.79#if defined(_LIBCPP_VERSION) || __cplusplus >= 201103L80std::shared_ptr< impl > _pimpl;81#else82std::tr1::shared_ptr< impl > _pimpl;83#endif8485void* new_userdata_voidp(const size_t);86void* to_userdata_voidp(const int);8788friend class state_c_gate;89explicit state(void*);90void* raw_state(void);9192public:93state(void);94~state(void);9596void close(void);97void get_global(const std::string&);98void get_global_table(void);99bool get_metafield(const int, const std::string&);100bool get_metatable(const int);101void get_table(const int);102int get_top(void);103void insert(const int);104bool is_boolean(const int);105bool is_function(const int);106bool is_nil(const int);107bool is_number(const int);108bool is_string(const int);109bool is_table(const int);110bool is_userdata(const int);111void load_file(const std::string&);112void load_string(const std::string&);113void new_table(void);114template< typename Type > Type* new_userdata(void);115bool next(const int);116void open_all(void);117void open_base(void);118void open_string(void);119void open_table(void);120void pcall(const int, const int, const int);121void pop(const int);122void push_boolean(const bool);123void push_cxx_closure(cxx_function, const int);124void push_cxx_function(cxx_function);125void push_integer(const int);126void push_nil(void);127void push_string(const std::string&);128void push_value(const int);129void raw_get(const int);130void raw_set(const int);131void set_global(const std::string&);132void set_metatable(const int);133void set_table(const int);134bool to_boolean(const int);135long to_integer(const int);136template< typename Type > Type* to_userdata(const int);137std::string to_string(const int);138int upvalue_index(const int);139};140141142} // namespace lutok143144#endif // !defined(LUTOK_STATE_HPP)145146147