Update LuaBridge to e3f7a022

pull/527/head
Bernd Schoolmann 6 years ago
parent 023ead937f
commit 1f2349ef6e

@ -0,0 +1,55 @@
// https://github.com/vinniefalco/LuaBridge
//
// Copyright 2018, Dmitry Tarakanov
// SPDX-License-Identifier: MIT
#pragma once
#include <LuaBridge/detail/Stack.h>
#include <list>
namespace luabridge {
template <class T>
struct Stack <std::list <T> >
{
static void push(lua_State* L, std::list <T> const& list)
{
lua_createtable (L, static_cast <int> (list.size ()), 0);
typename std::list <T>::const_iterator item = list.begin();
for (std::size_t i = 1; i <= list.size (); ++i)
{
lua_pushinteger (L, static_cast <lua_Integer> (i));
Stack <T>::push (L, *item);
lua_settable (L, -3);
++item;
}
}
static std::list <T> get(lua_State* L, int index)
{
if (!lua_istable(L, index))
{
luaL_error(L, "#%d argments must be table", index);
}
std::list <T> list;
int const absindex = lua_absindex (L, index);
lua_pushnil (L);
while (lua_next (L, absindex) != 0)
{
list.push_back (Stack <T>::get (L, -1));
lua_pop (L, 1);
}
return list;
}
};
template <class T>
struct Stack <std::list <T> const&> : Stack <std::list <T> >
{
};
} // namespace luabridge

@ -27,116 +27,32 @@
*/
//==============================================================================
#ifndef LUABRIDGE_LUABRIDGE_HEADER
#define LUABRIDGE_LUABRIDGE_HEADER
#pragma once
// All #include dependencies are listed here
// instead of in the individual header files.
//
#include <cassert>
#include <sstream>
#include <stdexcept>
#include <string>
#include <typeinfo>
#define LUABRIDGE_MAJOR_VERSION 2
#define LUABRIDGE_MINOR_VERSION 0
#define LUABRIDGE_VERSION 200
namespace luabridge
{
// Forward declaration
//
template <class T>
struct Stack;
#include "detail/LuaHelpers.h"
#include "detail/TypeTraits.h"
#include "detail/TypeList.h"
#include "detail/FuncTraits.h"
#include "detail/Constructor.h"
#include "detail/Stack.h"
#include "detail/ClassInfo.h"
class LuaRef;
#include "detail/LuaException.h"
#include "detail/LuaRef.h"
#include "detail/Iterator.h"
//------------------------------------------------------------------------------
/**
security options.
*/
class Security
{
public:
static bool hideMetatables ()
{
return getSettings().hideMetatables;
}
static void setHideMetatables (bool shouldHide)
{
getSettings().hideMetatables = shouldHide;
}
private:
struct Settings
{
Settings () : hideMetatables (true)
{
}
bool hideMetatables;
};
static Settings& getSettings ()
{
static Settings settings;
return settings;
}
};
#include "detail/Userdata.h"
#include "detail/CFunctions.h"
#include "detail/Namespace.h"
//------------------------------------------------------------------------------
/**
Push an object onto the Lua stack.
*/
template <class T>
inline void push (lua_State* L, T t)
{
Stack <T>::push (L, t);
}
//------------------------------------------------------------------------------
/**
Set a global value in the lua_State.
@note This works on any type specialized by `Stack`, including `LuaRef` and
its table proxies.
*/
template <class T>
inline void setGlobal (lua_State* L, T t, char const* name)
{
push (L, t);
lua_setglobal (L, name);
}
//------------------------------------------------------------------------------
/**
Change whether or not metatables are hidden (on by default).
*/
inline void setHideMetatables (bool shouldHide)
{
Security::setHideMetatables (shouldHide);
}
#ifndef LUA_VERSION_NUM
#error "Lua headers must be included prior to LuaBridge ones"
#endif
}
#endif
#include <LuaBridge/detail/LuaHelpers.h>
#include <LuaBridge/detail/TypeTraits.h>
#include <LuaBridge/detail/TypeList.h>
#include <LuaBridge/detail/FuncTraits.h>
#include <LuaBridge/detail/Constructor.h>
#include <LuaBridge/detail/ClassInfo.h>
#include <LuaBridge/detail/LuaException.h>
#include <LuaBridge/detail/LuaRef.h>
#include <LuaBridge/detail/Iterator.h>
#include <LuaBridge/detail/Userdata.h>
#include <LuaBridge/detail/CFunctions.h>
#include <LuaBridge/detail/Security.h>
#include <LuaBridge/detail/Stack.h>
#include <LuaBridge/detail/Namespace.h>

File diff suppressed because it is too large Load Diff

@ -0,0 +1,55 @@
// https://github.com/vinniefalco/LuaBridge
//
// Copyright 2018, Dmitry Tarakanov
// SPDX-License-Identifier: MIT
#pragma once
#include <LuaBridge/detail/Stack.h>
#include <map>
namespace luabridge {
template <class K, class V>
struct Stack <std::map <K, V> >
{
typedef std::map <K, V> Map;
static void push(lua_State* L, const Map& map)
{
lua_createtable (L, 0, static_cast <int> (map.size ()));
typedef typename Map::const_iterator ConstIter;
for (ConstIter i = map.begin(); i != map.end(); ++i)
{
Stack <K>::push (L, i->first);
Stack <V>::push (L, i->second);
lua_settable (L, -3);
}
}
static Map get(lua_State* L, int index)
{
if (!lua_istable(L, index))
{
luaL_error(L, "#%d argments must be table", index);
}
Map map;
int const absindex = lua_absindex (L, index);
lua_pushnil (L);
while (lua_next (L, absindex) != 0)
{
map.emplace (Stack <K>::get (L, -2), Stack <V>::get (L, -1));
lua_pop (L, 1);
}
return map;
}
};
template <class K, class V>
struct Stack <std::map <K, V> const&> : Stack <std::map <K, V> >
{
};
} // namespace luabridge

@ -36,13 +36,16 @@
*/
//==============================================================================
#ifndef LUABRIDGE_REFCOUNTEDOBJECT_HEADER
#define LUABRIDGE_REFCOUNTEDOBJECT_HEADER
#pragma once
//#define LUABRIDGE_COMPILER_SUPPORTS_MOVE_SEMANTICS 1
#include <LuaBridge/detail/TypeTraits.h>
#include <cassert>
namespace luabridge {
//==============================================================================
/**
Adds reference-counting to an object.
@ -337,13 +340,6 @@ bool operator!= (ReferenceCountedObjectClass* object1, RefCountedObjectPtr<Refer
//==============================================================================
namespace luabridge
{
// forward declaration
template <class T>
struct ContainerTraits;
template <class T>
struct ContainerTraits <RefCountedObjectPtr <T> >
{
@ -355,9 +351,6 @@ struct ContainerTraits <RefCountedObjectPtr <T> >
}
};
}
//==============================================================================
#endif
} // namespace luabridge

@ -2,6 +2,7 @@
/*
https://github.com/vinniefalco/LuaBridge
Copyright 2019, Dmitry Tarakanov
Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
Copyright 2007, Nathan Reed
@ -27,15 +28,12 @@
*/
//==============================================================================
#ifndef LUABRIDGE_REFCOUNTEDPTR_HEADER
#define LUABRIDGE_REFCOUNTEDPTR_HEADER
#pragma once
#ifdef _MSC_VER
# include <hash_map>
#else
# include <stdint.h>
# include <ext/hash_map>
#endif
#include <unordered_map>
#include "RefCountedObject.h"
namespace luabridge {
//==============================================================================
/**
@ -44,22 +42,10 @@
struct RefCountedPtrBase
{
// Declaration of container for the refcounts
#ifdef _MSC_VER
typedef stdext::hash_map <const void *, int> RefCountsType;
#else
struct ptr_hash
{
size_t operator () (const void * const v) const
{
static __gnu_cxx::hash<unsigned int> H;
return H(uintptr_t(v));
}
};
typedef __gnu_cxx::hash_map<const void *, int, ptr_hash> RefCountsType;
#endif
typedef std::unordered_map <const void *, int> RefCountsType;
protected:
inline RefCountsType& getRefCounts ()
RefCountsType& getRefCounts () const
{
static RefCountsType refcounts;
return refcounts ;
@ -226,10 +212,19 @@ private:
T* m_p;
};
//==============================================================================
template <class T>
bool operator== (const RefCountedPtr <T>& lhs, const RefCountedPtr <T>& rhs)
{
return lhs.get () == rhs.get ();
}
namespace luabridge
template <class T>
bool operator!= (const RefCountedPtr <T>& lhs, const RefCountedPtr <T>& rhs)
{
return lhs.get() != rhs.get();
}
//==============================================================================
// forward declaration
template <class T>
@ -246,6 +241,4 @@ struct ContainerTraits <RefCountedPtr <T> >
}
};
}
#endif
} // namespace luabridge

@ -0,0 +1,54 @@
// https://github.com/vinniefalco/LuaBridge
//
// Copyright 2018, Dmitry Tarakanov
// SPDX-License-Identifier: MIT
#pragma once
#include <LuaBridge/detail/Stack.h>
#include <vector>
namespace luabridge {
template <class T>
struct Stack <std::vector <T> >
{
static void push(lua_State* L, std::vector <T> const& vector)
{
lua_createtable (L, static_cast <int> (vector.size ()), 0);
for (std::size_t i = 0; i < vector.size (); ++i)
{
lua_pushinteger (L, static_cast <lua_Integer> (i + 1));
Stack <T>::push (L, vector [i]);
lua_settable (L, -3);
}
}
static std::vector <T> get(lua_State* L, int index)
{
if (!lua_istable(L, index))
{
luaL_error(L, "#%d argments must be table", index);
}
std::vector <T> vector;
vector.reserve (static_cast <std::size_t> (get_length (L, index)));
int const absindex = lua_absindex (L, index);
lua_pushnil (L);
while (lua_next (L, absindex) != 0)
{
vector.push_back (Stack <T>::get (L, -1));
lua_pop (L, 1);
}
return vector;
}
};
template <class T>
struct Stack <std::vector <T> const&> : Stack <std::vector <T> >
{
};
} // namespace luabridge

@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
/*
https://github.com/vinniefalco/LuaBridge
Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
License: The MIT License (http://www.opensource.org/licenses/mit-license.php)
@ -26,6 +26,12 @@
*/
//==============================================================================
#pragma once
#include <string>
namespace luabridge {
// We use a structure so we can define everything in the header.
//
struct CFunc
@ -138,7 +144,7 @@ struct CFunc
{
assert (lua_isnil (L, -1));
lua_pop (L, 2);
result = luaL_error (L,"no writable variable '%s'", lua_tostring (L, 2));
result = luaL_error (L, "no writable variable '%s'", lua_tostring (L, 2));
}
}
@ -154,12 +160,12 @@ struct CFunc
static int readOnlyError (lua_State* L)
{
std::string s;
s = s + "'" + lua_tostring (L, lua_upvalueindex (1)) + "' is read-only";
return luaL_error (L, s.c_str ());
}
//----------------------------------------------------------------------------
/**
lua_CFunction to get a variable.
@ -206,8 +212,8 @@ struct CFunc
The function pointer is in the first upvalue.
*/
template <class FnPtr,
class ReturnType = typename FuncTraits <FnPtr>::ReturnType>
struct Call
class ReturnType = typename FuncTraits <FnPtr>::ReturnType>
struct Call
{
typedef typename FuncTraits <FnPtr>::Params Params;
static int f (lua_State* L)
@ -253,8 +259,8 @@ struct CFunc
The class userdata object is at the top of the Lua stack.
*/
template <class MemFnPtr,
class ReturnType = typename FuncTraits <MemFnPtr>::ReturnType>
struct CallMember
class ReturnType = typename FuncTraits <MemFnPtr>::ReturnType>
struct CallMember
{
typedef typename FuncTraits <MemFnPtr>::ClassType T;
typedef typename FuncTraits <MemFnPtr>::Params Params;
@ -272,8 +278,8 @@ struct CFunc
};
template <class MemFnPtr,
class ReturnType = typename FuncTraits <MemFnPtr>::ReturnType>
struct CallConstMember
class ReturnType = typename FuncTraits <MemFnPtr>::ReturnType>
struct CallConstMember
{
typedef typename FuncTraits <MemFnPtr>::ClassType T;
typedef typename FuncTraits <MemFnPtr>::Params Params;
@ -284,7 +290,7 @@ struct CFunc
T const* const t = Userdata::get <T> (L, 1, true);
MemFnPtr const& fnptr = *static_cast <MemFnPtr const*> (lua_touserdata (L, lua_upvalueindex (1)));
assert (fnptr != 0);
ArgList <Params, 2> args(L);
ArgList <Params, 2> args (L);
Stack <ReturnType>::push (L, FuncTraits <MemFnPtr>::call (t, fnptr, args));
return 1;
}
@ -440,3 +446,5 @@ struct CFunc
return 0;
}
};
} // namespace luabridge

@ -26,6 +26,10 @@
*/
//==============================================================================
#pragma once
namespace luabridge {
/** Unique Lua registry keys for a class.
Each registered class inserts three keys into the registry, whose
@ -71,3 +75,4 @@ public:
}
};
} // namespace luabridge

@ -27,8 +27,9 @@
*/
//==============================================================================
#ifndef LUABRIDGE_CONSTRUCTOR_HEADER
#define LUABRIDGE_CONSTRUCTOR_HEADER
#pragma once
namespace luabridge {
/*
* Constructor generators. These templates allow you to call operator new and
@ -201,4 +202,4 @@ struct Constructor <T, TypeList <P1, TypeList <P2, TypeList <P3,
}
};
#endif
} // namespace luabridge

@ -26,6 +26,10 @@
*/
//==============================================================================
#pragma once
namespace luabridge {
/**
Since the throw specification is part of a function signature, the FuncTraits
family of templates needs to be specialized for both types. The
@ -178,6 +182,129 @@ struct FuncTraits <R (*) (P1, P2, P3, P4, P5, P6, P7, P8), D>
}
};
/* Windows: WINAPI (a.k.a. __stdcall) function pointers. */
#ifdef _M_IX86 // Windows 32bit only
template <class R, class D>
struct FuncTraits <R (__stdcall *) (), D>
{
static bool const isMemberFunction = false;
typedef D DeclType;
typedef R ReturnType;
typedef None Params;
static R call (D fp, TypeListValues <Params>)
{
return fp ();
}
};
template <class R, class P1, class D>
struct FuncTraits <R (__stdcall *) (P1), D>
{
static bool const isMemberFunction = false;
typedef D DeclType;
typedef R ReturnType;
typedef TypeList <P1> Params;
static R call (D fp, TypeListValues <Params> tvl)
{
return fp (tvl.hd);
}
};
template <class R, class P1, class P2, class D>
struct FuncTraits <R (__stdcall *) (P1, P2), D>
{
static bool const isMemberFunction = false;
typedef D DeclType;
typedef R ReturnType;
typedef TypeList <P1, TypeList <P2> > Params;
static R call (D fp, TypeListValues <Params> tvl)
{
return fp (tvl.hd, tvl.tl.hd);
}
};
template <class R, class P1, class P2, class P3, class D>
struct FuncTraits <R (__stdcall *) (P1, P2, P3), D>
{
static bool const isMemberFunction = false;
typedef D DeclType;
typedef R ReturnType;
typedef TypeList <P1, TypeList <P2, TypeList <P3> > > Params;
static R call (D fp, TypeListValues <Params> tvl)
{
return fp (tvl.hd, tvl.tl.hd, tvl.tl.tl.hd);
}
};
template <class R, class P1, class P2, class P3, class P4, class D>
struct FuncTraits <R (__stdcall *) (P1, P2, P3, P4), D>
{
static bool const isMemberFunction = false;
typedef D DeclType;
typedef R ReturnType;
typedef TypeList <P1, TypeList <P2, TypeList <P3, TypeList <P4> > > > Params;
static R call (D fp, TypeListValues <Params> tvl)
{
return fp (tvl.hd, tvl.tl.hd, tvl.tl.tl.hd, tvl.tl.tl.tl.hd);
}
};
template <class R, class P1, class P2, class P3, class P4, class P5, class D>
struct FuncTraits <R (__stdcall *) (P1, P2, P3, P4, P5), D>
{
static bool const isMemberFunction = false;
typedef D DeclType;
typedef R ReturnType;
typedef TypeList <P1, TypeList <P2, TypeList <P3, TypeList <P4, TypeList <P5> > > > > Params;
static R call (D fp, TypeListValues <Params> tvl)
{
return fp (tvl.hd, tvl.tl.hd, tvl.tl.tl.hd, tvl.tl.tl.tl.hd, tvl.tl.tl.tl.tl.hd);
}
};
template <class R, class P1, class P2, class P3, class P4, class P5, class P6, class D>
struct FuncTraits <R (__stdcall *) (P1, P2, P3, P4, P5, P6), D>
{
static bool const isMemberFunction = false;
typedef D DeclType;
typedef R ReturnType;
typedef TypeList <P1, TypeList <P2, TypeList <P3, TypeList <P4, TypeList <P5, TypeList <P6> > > > > > Params;
static R call (D fp, TypeListValues <Params> tvl)
{
return fp (tvl.hd, tvl.tl.hd, tvl.tl.tl.hd, tvl.tl.tl.tl.hd, tvl.tl.tl.tl.tl.hd, tvl.tl.tl.tl.tl.tl.hd);
}
};
template <class R, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class D>
struct FuncTraits <R (__stdcall *) (P1, P2, P3, P4, P5, P6, P7), D>
{
static bool const isMemberFunction = false;
typedef D DeclType;
typedef R ReturnType;
typedef TypeList <P1, TypeList <P2, TypeList <P3, TypeList <P4, TypeList <P5, TypeList <P6, TypeList <P7> > > > > > > Params;
static R call (D fp, TypeListValues <Params> tvl)
{
return fp (tvl.hd, tvl.tl.hd, tvl.tl.tl.hd, tvl.tl.tl.tl.hd, tvl.tl.tl.tl.tl.hd, tvl.tl.tl.tl.tl.tl.hd, tvl.tl.tl.tl.tl.tl.tl.hd);
}
};
template <class R, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class D>
struct FuncTraits <R (__stdcall *) (P1, P2, P3, P4, P5, P6, P7, P8), D>
{
static bool const isMemberFunction = false;
typedef D DeclType;
typedef R ReturnType;
typedef TypeList <P1, TypeList <P2, TypeList <P3, TypeList <P4, TypeList <P5, TypeList <P6, TypeList <P7, TypeList <P8> > > > > > > > Params;
static R call (D fp, TypeListValues <Params> tvl)
{
return fp (tvl.hd, tvl.tl.hd, tvl.tl.tl.hd, tvl.tl.tl.tl.hd, tvl.tl.tl.tl.tl.hd, tvl.tl.tl.tl.tl.tl.hd, tvl.tl.tl.tl.tl.tl.tl.hd, tvl.tl.tl.tl.tl.tl.tl.tl.hd);
}
};
#endif // _M_IX86
/* Non-const member function pointers. */
template <class T, class R, class D>
@ -850,3 +977,5 @@ struct FuncTraits <R (T::*) (P1, P2, P3, P4, P5, P6, P7, P8) const LUABRIDGE_THR
};
#endif
} // namespace luabridge

@ -26,6 +26,14 @@
*/
//==============================================================================
#pragma once
#include <LuaBridge/detail/LuaRef.h>
#include <utility>
namespace luabridge {
/** Allows table iteration.
*/
class Iterator
@ -38,29 +46,32 @@ private:
void next ()
{
m_table.push(m_L);
m_key.push (m_L);
m_table.push ();
m_key.push ();
if (lua_next (m_L, -2))
{
m_value.pop (m_L);
m_key.pop (m_L);
m_value.pop ();
m_key.pop ();
}
else
{
m_key = Nil();
m_value = Nil();
m_key = Nil ();
m_value = Nil ();
}
lua_pop(m_L, 1);
lua_pop (m_L, 1);
}
public:
explicit Iterator (LuaRef table)
explicit Iterator (const LuaRef& table, bool isEnd = false)
: m_L (table.state ())
, m_table (table)
, m_key (table.state ()) // m_key is nil
, m_value (table.state ()) // m_value is nil
{
next (); // get the first (key, value) pair from table
if (!isEnd)
{
next (); // get the first (key, value) pair from table
}
}
lua_State* state () const
@ -68,9 +79,9 @@ public:
return m_L;
}
LuaRef operator* () const
std::pair<LuaRef, LuaRef> operator* () const
{
return m_value;
return std::make_pair (m_key, m_value);
}
LuaRef operator-> () const
@ -78,6 +89,12 @@ public:
return m_value;
}
bool operator!= (const Iterator& rhs) const
{
assert (m_L == rhs.m_L);
return !m_table.rawequal (rhs.m_table) || !m_key.rawequal (rhs.m_key);
}
Iterator& operator++ ()
{
if (isNil())
@ -92,17 +109,17 @@ public:
}
}
inline bool isNil () const
bool isNil () const
{
return m_key.isNil ();
}
inline LuaRef key () const
LuaRef key () const
{
return m_key;
}
inline LuaRef value () const
LuaRef value () const
{
return m_value;
}
@ -112,3 +129,25 @@ private:
Iterator operator++ (int);
};
class Range
{
Iterator m_begin;
Iterator m_end;
public:
Range (const Iterator& begin, const Iterator& end)
: m_begin (begin)
, m_end (end)
{
}
const Iterator& begin () const { return m_begin; }
const Iterator& end () const { return m_end; }
};
inline Range pairs(const LuaRef& table)
{
return Range (Iterator (table, false), Iterator (table, true));
}
} // namespace luabridge

@ -27,6 +27,13 @@
*/
//==============================================================================
#pragma once
#include <exception>
#include <string>
namespace luabridge {
class LuaException : public std::exception
{
private:
@ -111,3 +118,5 @@ protected:
}
}
};
} // namespace luabridge

@ -27,6 +27,12 @@
*/
//==============================================================================
#pragma once
#include <cassert>
namespace luabridge {
// These are for Lua versions prior to 5.2.0.
//
#if LUA_VERSION_NUM < 502
@ -141,3 +147,5 @@ inline bool equalstates (lua_State* L1, lua_State* L2)
return lua_topointer (L1, LUA_REGISTRYINDEX) ==
lua_topointer (L2, LUA_REGISTRYINDEX);
}
} // namespace luabridge

File diff suppressed because it is too large Load Diff

@ -27,6 +27,16 @@
*/
//==============================================================================
#pragma once
#include <LuaBridge/detail/Security.h>
#include <LuaBridge/detail/TypeTraits.h>
#include <stdexcept>
#include <string>
namespace luabridge {
/** Provides C++ to Lua registration capabilities.
This class is not instantiated directly, call `getGlobalNamespace` to start
@ -482,8 +492,9 @@ private:
}
else
{
rawgetfield (L, -1, "__class");
rawgetfield (L, -1, "__const");
// Map T back from its stored tables
lua_rawgetp(L, LUA_REGISTRYINDEX, ClassInfo <T>::getClassKey());
lua_rawgetp(L, LUA_REGISTRYINDEX, ClassInfo <T>::getConstKey());
// Reverse the top 3 stack elements
lua_insert (L, -3);
@ -737,8 +748,8 @@ private:
Both the get and the set functions require a T const* and T* in the first
argument respectively.
*/
template <class TG, class TS>
Class <T>& addProperty (char const* name, TG (*get) (T const*), void (*set) (T*, TS))
template <class TG, class TS = TG>
Class <T>& addProperty (char const* name, TG (*get) (T const*), void (*set) (T*, TS) = 0)
{
// Add to __propget in class and const tables.
{
@ -768,24 +779,6 @@ private:
return *this;
}
// read-only
template <class TG, class TS>
Class <T>& addProperty (char const* name, TG (*get) (T const*))
{
// Add to __propget in class and const tables.
rawgetfield (L, -2, "__propget");
rawgetfield (L, -4, "__propget");
typedef TG (*get_t) (T const*);
new (lua_newuserdata (L, sizeof (get_t))) get_t (get);
lua_pushcclosure (L, &CFunc::Call <get_t>::f, 1);
lua_pushvalue (L, -1);
rawsetfield (L, -4, name);
rawsetfield (L, -2, name);
lua_pop (L, 2);
return *this;
}
//--------------------------------------------------------------------------
/**
Add or replace a member function.
@ -793,6 +786,11 @@ private:
template <class MemFn>
Class <T>& addFunction (char const* name, MemFn mf)
{
static const std::string GC = "__gc";
if (name == GC)
{
throw std::logic_error (GC + " metamethod registration is forbidden");
}
CFunc::CallMemberFunctionHelper <MemFn, FuncTraits <MemFn>::isConstMemberFunction>::add (L, name, mf);
return *this;
}
@ -868,10 +866,9 @@ private:
*/
explicit Namespace (lua_State* L_)
: L (L_)
, m_stackSize (0)
, m_stackSize (1)
{
lua_getglobal (L, "_G");
++m_stackSize;
}
//----------------------------------------------------------------------------
@ -1005,6 +1002,11 @@ public:
template <class T>
Namespace& addVariable (char const* name, T* pt, bool isWritable = true)
{
if (m_stackSize == 1)
{
throw std::logic_error ("Unsupported addVariable on global namespace");
}
assert (lua_istable (L, -1));
rawgetfield (L, -1, "__propget");
@ -1038,9 +1040,14 @@ public:
If the set function is omitted or null, the property is read-only.
*/
template <class TG, class TS>
template <class TG, class TS = TG>
Namespace& addProperty (char const* name, TG (*get) (), void (*set)(TS) = 0)
{
if (m_stackSize == 1)
{
throw std::logic_error ("Unsupported addProperty on global namespace");
}
assert (lua_istable (L, -1));
rawgetfield (L, -1, "__propget");
@ -1134,3 +1141,5 @@ inline Namespace getGlobalNamespace (lua_State* L)
{
return Namespace::getGlobalNamespace (L);
}
} // namespace luabridge

@ -0,0 +1,72 @@
#pragma once
namespace luabridge {
//------------------------------------------------------------------------------
/**
security options.
*/
class Security
{
public:
static bool hideMetatables()
{
return getSettings().hideMetatables;
}
static void setHideMetatables(bool shouldHide)
{
getSettings().hideMetatables = shouldHide;
}
private:
struct Settings
{
Settings() : hideMetatables(true)
{
}
bool hideMetatables;
};
static Settings& getSettings()
{
static Settings settings;
return settings;
}
};
//------------------------------------------------------------------------------
/**
Push an object onto the Lua stack.
*/
template <class T>
inline void push(lua_State* L, T t)
{
Stack <T>::push(L, t);
}
//------------------------------------------------------------------------------
/**
Set a global value in the lua_State.
@note This works on any type specialized by `Stack`, including `LuaRef` and
its table proxies.
*/
template <class T>
inline void setGlobal(lua_State* L, T t, char const* name)
{
push(L, t);
lua_setglobal(L, name);
}
//------------------------------------------------------------------------------
/**
Change whether or not metatables are hidden (on by default).
*/
inline void setHideMetatables(bool shouldHide)
{
Security::setHideMetatables(shouldHide);
}
} // namespace luabridge

@ -27,6 +27,17 @@
*/
//==============================================================================
#pragma once
#include <LuaBridge/detail/LuaHelpers.h>
#include <string>
namespace luabridge {
template <class T>
struct Stack;
//------------------------------------------------------------------------------
/**
Receive the lua_State* as an argument.
@ -77,18 +88,10 @@ struct Stack <int>
};
template <>
struct Stack <int const&>
struct Stack <int const&> : public Stack <int>
{
static inline void push (lua_State* L, int value)
{
lua_pushnumber (L, static_cast <lua_Number> (value));
}
static inline int get (lua_State* L, int index)
{
return static_cast <int > (luaL_checknumber (L, index));
}
};
//------------------------------------------------------------------------------
/**
Stack specialization for `unsigned int`.
@ -108,17 +111,8 @@ struct Stack <unsigned int>
};
template <>
struct Stack <unsigned int const&>
struct Stack <unsigned int const&> : Stack <unsigned int>
{
static inline void push (lua_State* L, unsigned int value)
{
lua_pushnumber (L, static_cast <lua_Number> (value));
}
static inline unsigned int get (lua_State* L, int index)
{
return static_cast <unsigned int > (luaL_checknumber (L, index));
}
};
//------------------------------------------------------------------------------
@ -140,17 +134,8 @@ struct Stack <unsigned char>
};
template <>
struct Stack <unsigned char const&>
struct Stack <unsigned char const&> : Stack <unsigned char>
{
static inline void push (lua_State* L, unsigned char value)
{
lua_pushnumber (L, static_cast <lua_Number> (value));
}
static inline unsigned char get (lua_State* L, int index)
{
return static_cast <unsigned char> (luaL_checknumber (L, index));
}
};
//------------------------------------------------------------------------------
@ -172,17 +157,8 @@ struct Stack <short>
};
template <>
struct Stack <short const&>
struct Stack <short const&> : Stack <short>
{
static inline void push (lua_State* L, short value)
{
lua_pushnumber (L, static_cast <lua_Number> (value));
}
static inline short get (lua_State* L, int index)
{
return static_cast <short> (luaL_checknumber (L, index));
}
};
//------------------------------------------------------------------------------
@ -204,17 +180,8 @@ struct Stack <unsigned short>
};
template <>
struct Stack <unsigned short const&>
struct Stack <unsigned short const&> : Stack <unsigned short>
{
static inline void push (lua_State* L, unsigned short value)
{
lua_pushnumber (L, static_cast <lua_Number> (value));
}
static inline unsigned short get (lua_State* L, int index)
{
return static_cast <unsigned short> (luaL_checknumber (L, index));
}
};
//------------------------------------------------------------------------------
@ -236,17 +203,8 @@ struct Stack <long>
};
template <>
struct Stack <long const&>
struct Stack <long const&> : public Stack <long>
{
static inline void push (lua_State* L, long value)
{
lua_pushnumber (L, static_cast <lua_Number> (value));
}
static inline long get (lua_State* L, int index)
{
return static_cast <long> (luaL_checknumber (L, index));
}
};
//------------------------------------------------------------------------------
@ -268,17 +226,8 @@ struct Stack <unsigned long>
};
template <>
struct Stack <unsigned long const&>
struct Stack <unsigned long const&> : public Stack <unsigned long>
{
static inline void push (lua_State* L, unsigned long value)
{
lua_pushnumber (L, static_cast <lua_Number> (value));
}
static inline unsigned long get (lua_State* L, int index)
{
return static_cast <unsigned long> (luaL_checknumber (L, index));
}
};
//------------------------------------------------------------------------------
@ -300,17 +249,8 @@ struct Stack <float>
};
template <>
struct Stack <float const&>
struct Stack <float const&> : Stack <float>
{
static inline void push (lua_State* L, float value)
{
lua_pushnumber (L, static_cast <lua_Number> (value));
}
static inline float get (lua_State* L, int index)
{
return static_cast <float> (luaL_checknumber (L, index));
}
};
//------------------------------------------------------------------------------
@ -330,17 +270,9 @@ template <> struct Stack <double>
}
};
template <> struct Stack <double const&>
template <>
struct Stack <double const&> : Stack <double>
{
static inline void push (lua_State* L, double value)
{
lua_pushnumber (L, static_cast <lua_Number> (value));
}
static inline double get (lua_State* L, int index)
{
return static_cast <double> (luaL_checknumber (L, index));
}
};
//------------------------------------------------------------------------------
@ -348,7 +280,8 @@ template <> struct Stack <double const&>
Stack specialization for `bool`.
*/
template <>
struct Stack <bool> {
struct Stack <bool>
{
static inline void push (lua_State* L, bool value)
{
lua_pushboolean (L, value ? 1 : 0);
@ -361,16 +294,8 @@ struct Stack <bool> {
};
template <>
struct Stack <bool const&> {
static inline void push (lua_State* L, bool value)
{
lua_pushboolean (L, value ? 1 : 0);
}
static inline bool get (lua_State* L, int index)
{
return lua_toboolean (L, index) ? true : false;
}
struct Stack <bool const&> : Stack <bool>
{
};
//------------------------------------------------------------------------------
@ -382,8 +307,7 @@ struct Stack <char>
{
static inline void push (lua_State* L, char value)
{
char str [2] = { value, 0 };
lua_pushstring (L, str);
lua_pushlstring (L, &value, 1);
}
static inline char get (lua_State* L, int index)
@ -393,23 +317,13 @@ struct Stack <char>
};
template <>
struct Stack <char const&>
struct Stack <char const&> : Stack <char>
{
static inline void push (lua_State* L, char value)
{
char str [2] = { value, 0 };
lua_pushstring (L, str);
}
static inline char get (lua_State* L, int index)
{
return luaL_checkstring (L, index) [0];
}
};
//------------------------------------------------------------------------------
/**
Stack specialization for `float`.
Stack specialization for `const char*`.
*/
template <>
struct Stack <char const*>
@ -437,33 +351,64 @@ struct Stack <std::string>
{
static inline void push (lua_State* L, std::string const& str)
{
lua_pushlstring (L, str.c_str (), str.size());
lua_pushlstring (L, str.data (), str.size());
}
static inline std::string get (lua_State* L, int index)
{
size_t len;
const char *str = luaL_checklstring(L, index, &len);
const char *str = luaL_checklstring (L, index, &len);
return std::string (str, len);
}
};
template <>
struct Stack <std::string const&> : Stack <std::string>
{
};
//------------------------------------------------------------------------------
/**
Stack specialization for `std::string const&`.
Stack specialization for `long long`.
*/
template <>
struct Stack <std::string const&>
struct Stack <long long>
{
static inline void push (lua_State* L, std::string const& str)
static inline void push (lua_State* L, long long value)
{
lua_pushlstring (L, str.c_str(), str.size());
lua_pushinteger (L, static_cast <lua_Integer> (value));
}
static inline long long get (lua_State* L, int index)
{
return static_cast <long long> (luaL_checkinteger (L, index));
}
};
static inline std::string get (lua_State* L, int index)
template <>
struct Stack <long long const&> : public Stack <long long>
{
};
//------------------------------------------------------------------------------
/**
Stack specialization for `unsigned long long`.
*/
template <>
struct Stack <unsigned long long>
{
static inline void push (lua_State* L, unsigned long long value)
{
size_t len;
const char *str = luaL_checklstring(L, index, &len);
return std::string (str, len);
lua_pushinteger (L, static_cast <lua_Integer> (value));
}
static inline unsigned long long get (lua_State* L, int index)
{
return static_cast <unsigned long long> (luaL_checkinteger (L, index));
}
};
template <>
struct Stack <unsigned long long const&> : Stack <unsigned long long>
{
};
} // namespace luabridge

@ -43,6 +43,15 @@
*/
//==============================================================================
#pragma once
#include <LuaBridge/detail/Stack.h>
#include <string>
#include <typeinfo>
namespace luabridge {
/**
None type means void parameters or return value.
*/
@ -172,3 +181,5 @@ struct ArgList <TypeList <Head, Tail>, Start>
{
}
};
} // namespace luabridge

@ -26,8 +26,9 @@
*/
//==============================================================================
#ifndef LUABRIDGE_TYPEINFO_HEADER
#define LUABRIDGE_TYPEINFO_HEADER
#pragma once
namespace luabridge {
//------------------------------------------------------------------------------
/**
@ -57,6 +58,7 @@ template <class T>
struct ContainerTraits
{
typedef bool isNotContainer;
typedef T Type;
};
//------------------------------------------------------------------------------
@ -122,4 +124,4 @@ struct TypeTraits
/**@}*/
};
#endif
} // namespace luabridge

@ -26,6 +26,14 @@
*/
//==============================================================================
#pragma once
#include <LuaBridge/detail/TypeList.h>
#include <cassert>
namespace luabridge {
//==============================================================================
/**
Return the identity pointer for our lightuserdata tokens.
@ -179,14 +187,18 @@ private:
void const* baseClassKey,
bool canBeConst)
{
assert (index > 0);
index = index > 0 ? index : lua_absindex (L, index);
Userdata* ud = 0;
bool mismatch = false;
char const* got = 0;
lua_rawgetp (L, LUA_REGISTRYINDEX, baseClassKey);
assert (lua_istable (L, -1));
if (!lua_istable (L, -1))
{
throw std::logic_error ("The class is not registered in LuaBridge");
}
// Make sure we have a userdata.
if (lua_isuserdata (L, index))
@ -371,8 +383,10 @@ public:
UserdataValue <T>* const ud = new (
lua_newuserdata (L, sizeof (UserdataValue <T>))) UserdataValue <T> ();
lua_rawgetp (L, LUA_REGISTRYINDEX, ClassInfo <T>::getClassKey ());
// If this goes off it means you forgot to register the class!
assert (lua_istable (L, -1));
if (!lua_istable (L, -1))
{
throw std::logic_error ("The class is not registered in LuaBridge");
}
lua_setmetatable (L, -2);
return ud->getPointer ();
}
@ -408,8 +422,10 @@ private:
{
new (lua_newuserdata (L, sizeof (UserdataPtr))) UserdataPtr (p);
lua_rawgetp (L, LUA_REGISTRYINDEX, key);
// If this goes off it means you forgot to register the class!
assert (lua_istable (L, -1));
if (!lua_istable (L, -1))
{
throw std::logic_error ("The class is not registered in LuaBridge");
}
lua_setmetatable (L, -2);
}
else
@ -427,8 +443,10 @@ private:
new (lua_newuserdata (L, sizeof (UserdataPtr)))
UserdataPtr (const_cast <void*> (p));
lua_rawgetp (L, LUA_REGISTRYINDEX, key);
// If this goes off it means you forgot to register the class!
assert (lua_istable (L, -1));
if (!lua_istable (L, -1))
{
throw std::logic_error ("The class is not registered in LuaBridge");
}
lua_setmetatable (L, -2);
}
else
@ -815,3 +833,5 @@ struct Stack <T const&>
return helper_t::get (L, index);
}
};
} // namespace luabridge

@ -1,6 +1,39 @@
//==============================================================================
/*
https://github.com/vinniefalco/LuaBridge
Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
Copyright 2007, Nathan Reed
License: The MIT License (http://www.opensource.org/licenses/mit-license.php)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//==============================================================================
#pragma once
#include <sstream>
#include <string>
namespace luabridge {
std::string dumpLuaState(lua_State *L) {
std::stringstream ostr;
int i;
@ -26,3 +59,5 @@ std::string dumpLuaState(lua_State *L) {
}
return ostr.str();
}
} // namespace luabridge

Loading…
Cancel
Save