📄 policy.hpp
字号:
// Copyright (c) 2003 Daniel Wallin and Arvid Norberg
// 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.
#ifndef LUABIND_POLICY_HPP_INCLUDED
#define LUABIND_POLICY_HPP_INCLUDED
#include <luabind/config.hpp>
#include <typeinfo>
#include <boost/type_traits/is_enum.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/mpl/integral_c.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/type_traits/add_reference.hpp>
#include <boost/limits.hpp>
#include <luabind/detail/class_registry.hpp>
#include <luabind/detail/primitives.hpp>
#include <luabind/detail/object_rep.hpp>
#include <luabind/detail/typetraits.hpp>
#include <boost/type_traits/add_reference.hpp>
#include <luabind/detail/decorate_type.hpp>
#include <luabind/object.hpp>
namespace luabind
{
namespace detail
{
struct conversion_policy_base {};
}
template<int N, bool HasArg = true>
struct conversion_policy : detail::conversion_policy_base
{
BOOST_STATIC_CONSTANT(int, index = N);
BOOST_STATIC_CONSTANT(bool, has_arg = HasArg);
};
class index_map
{
public:
index_map(const int* m): m_map(m) {}
int operator[](int index) const
{
return m_map[index + 1];
}
private:
const int* m_map;
};
namespace converters
{
using luabind::detail::yes_t;
using luabind::detail::no_t;
using luabind::detail::by_value;
using luabind::detail::by_reference;
using luabind::detail::by_const_reference;
using luabind::detail::by_pointer;
using luabind::detail::by_const_pointer;
no_t is_user_defined(...);
template<bool B = true> struct yes_no : yes_t { typedef yes_t type; };
template<> struct yes_no<false> : no_t { typedef no_t type; };
/*
template<int N, class T>
struct TO
{
BOOST_STATIC_CONSTANT(bool, is_specialized = false);
std::pair<int,int> match(lua_State*, detail::type<T>, boost::mpl::int_<N>, int)
{
return std::make_pair(-1,-1);
}
template<int I>
void convert(lua_State*, detail::type<T>, boost::mpl::int_<N>, int) {}
};
no_t is_implicit_conversion(...);
template<class T>
yes_no<TO<0,T>::is_specialized> is_implicit_conversion(by_value<T>);
template<class T>
yes_no<TO<0,T>::is_specialized> is_implicit_conversion(by_const_reference<T>);
template<class T>
yes_no<TO<0,T*>::is_specialized> is_implicit_conversion(by_pointer<T>);
template<class T>
yes_no<TO<0,const T*>::is_specialized> is_implicit_conversion(by_const_pointer<T>);
#define LUABIND_IMPLICIT(index, to, from) template<> struct TO<index,to >:FROM<from > {}*/
}
namespace detail
{
template<class T>
struct is_user_defined
{
BOOST_STATIC_CONSTANT(bool, value =
sizeof(luabind::converters::is_user_defined(LUABIND_DECORATE_TYPE(T))) == sizeof(yes_t));
};
/* template<class T>
struct is_implicit_conversion
{
BOOST_STATIC_CONSTANT(bool, value =
sizeof(luabind::converters::is_implicit_conversion(LUABIND_DECORATE_TYPE(T))) == sizeof(yes_t));
};
*/
int implicit_cast(const class_rep* crep, LUABIND_TYPE_INFO const&, int& pointer_offset);
}
template<class T> class functor;
class object;
}
namespace luabind { namespace detail
{
template<class>
struct is_primitive;
template<class T>
yes_t is_lua_functor_test(const functor<T>&);
#if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
no_t is_lua_functor_test(...);
#else
template<class T>
no_t is_lua_functor_test(const T&);
#endif
template<class T>
struct is_lua_functor
{
static T t;
BOOST_STATIC_CONSTANT(bool, value = sizeof(is_lua_functor_test(t)) == sizeof(yes_t));
};
namespace
{
static char msvc_fix[64];
}
template<class T>
struct indirect_type
{
typedef typename
boost::mpl::if_<is_primitive<T>
, const type<T>&
, typename boost::mpl::apply_if<boost::mpl::or_<boost::is_reference<T>, boost::is_pointer<T> >
, identity<T>
, boost::add_reference<T>
>::type
>::type result_type;
static inline result_type get()
{
return reinterpret_cast<result_type>(msvc_fix);
}
};
template<class H, class T>
struct policy_cons
{
typedef H head;
typedef T tail;
template<class U>
policy_cons<U, policy_cons<H,T> > operator,(policy_cons<U,detail::null_type>)
{
return policy_cons<U, policy_cons<H,T> >();
}
template<class U>
policy_cons<U, policy_cons<H,T> > operator+(policy_cons<U,detail::null_type>)
{
return policy_cons<U, policy_cons<H,T> >();
}
template<class U>
policy_cons<U, policy_cons<H,T> > operator|(policy_cons<U,detail::null_type>)
{
return policy_cons<U, policy_cons<H,T> >();
}
};
struct indirection_layer
{
template<class T>
indirection_layer(const T&);
};
template<class H, class T>
yes_t is_policy_cons_test(const policy_cons<H,T>&);
no_t is_policy_cons_test(...);
template<class T>
struct is_policy_cons
{
static const T& t;
BOOST_STATIC_CONSTANT(bool, value =
sizeof(is_policy_cons_test(t)) == sizeof(yes_t));
};
no_t is_string_literal(indirection_layer);
yes_t is_string_literal(const char*);
template<class T>
struct is_primitive/*: boost::mpl::bool_c<false>*/
{
static T t;
BOOST_STATIC_CONSTANT(bool, value = sizeof(is_string_literal(t)) == sizeof(yes_t));
};
template<> struct is_primitive<luabind::object>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const luabind::object>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const luabind::object&>: boost::mpl::bool_<true> {};
template<> struct is_primitive<int>: boost::mpl::bool_<true> {};
template<> struct is_primitive<char>: boost::mpl::bool_<true> {};
template<> struct is_primitive<short>: boost::mpl::bool_<true> {};
template<> struct is_primitive<long>: boost::mpl::bool_<true> {};
template<> struct is_primitive<unsigned char>: boost::mpl::bool_<true> {};
template<> struct is_primitive<unsigned short>: boost::mpl::bool_<true> {};
template<> struct is_primitive<unsigned long>: boost::mpl::bool_<true> {};
template<> struct is_primitive<unsigned int>: boost::mpl::bool_<true> {};
template<> struct is_primitive<float>: boost::mpl::bool_<true> {};
template<> struct is_primitive<double>: boost::mpl::bool_<true> {};
template<> struct is_primitive<long double>: boost::mpl::bool_<true> {};
template<> struct is_primitive<char*>: boost::mpl::bool_<true> {};
template<> struct is_primitive<bool>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const int>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const char>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const short>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const long>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const unsigned int>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const unsigned char>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const unsigned short>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const unsigned long>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const float>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const double>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const long double>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const char*>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const char* const>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const bool>: boost::mpl::bool_<true> {};
// TODO: add more
template<> struct is_primitive<const int&>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const char&>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const short&>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const long&>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const unsigned int&>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const unsigned char&>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const unsigned short&>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const unsigned long&>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const float&>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const double&>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const long double&>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const bool&>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const std::string&>: boost::mpl::bool_<true> {};
template<> struct is_primitive<std::string>: boost::mpl::bool_<true> {};
template<> struct is_primitive<const std::string>: boost::mpl::bool_<true> {};
template<class Direction> struct primitive_converter;
template<>
struct primitive_converter<cpp_to_lua>
{
void apply(lua_State* L, const luabind::object& v)
{
// if you hit this assert you are trying to return a value from one state into another lua state
assert((v.lua_state() == L) && "you cannot return a value from one lua state into another");
v.pushvalue();
}
void apply(lua_State* L, int v) { lua_pushnumber(L, v); }
void apply(lua_State* L, short v) { lua_pushnumber(L, v); }
void apply(lua_State* L, char v) { lua_pushnumber(L, v); }
void apply(lua_State* L, long v) { lua_pushnumber(L, v); }
void apply(lua_State* L, unsigned int v) { lua_pushnumber(L, v); }
void apply(lua_State* L, unsigned short v) { lua_pushnumber(L, v); }
void apply(lua_State* L, unsigned char v) { lua_pushnumber(L, v); }
void apply(lua_State* L, unsigned long v) { lua_pushnumber(L, v); }
void apply(lua_State* L, float v) { lua_pushnumber(L, v); }
void apply(lua_State* L, double v) { lua_pushnumber(L, v); }
void apply(lua_State* L, long double v) { lua_pushnumber(L, v); }
void apply(lua_State* L, const char* v) { lua_pushstring(L, v); }
void apply(lua_State* L, const std::string& v) { lua_pushstring(L, v.c_str()); }
void apply(lua_State* L, bool b) { lua_pushboolean(L, b); }
};
template<>
struct primitive_converter<lua_to_cpp>
{
// TODO: add more
bool apply(lua_State* L, detail::by_value<bool>, int index) { return lua_toboolean(L, index) == 1; }
float apply(lua_State* L, detail::by_value<float>, int index) { return static_cast<float>(lua_tonumber(L, index)); }
double apply(lua_State* L, detail::by_value<double>, int index) { return static_cast<double>(lua_tonumber(L, index)); }
long double apply(lua_State* L, detail::by_value<long double>, int index) { return static_cast<long double>(lua_tonumber(L, index)); }
int apply(lua_State* L, detail::by_value<int>, int index) { return static_cast<int>(lua_tonumber(L, index)); }
short apply(lua_State* L, detail::by_value<short>, int index) { return static_cast<short>(lua_tonumber(L, index)); }
char apply(lua_State* L, detail::by_value<char>, int index) { return static_cast<char>(lua_tonumber(L, index)); }
long apply(lua_State* L, detail::by_value<long>, int index) { return static_cast<long>(lua_tonumber(L, index)); }
unsigned int apply(lua_State* L, detail::by_value<unsigned int>, int index) { return static_cast<unsigned int>(lua_tonumber(L, index)); }
unsigned short apply(lua_State* L, detail::by_value<unsigned short>, int index) { return static_cast<short>(lua_tonumber(L, index)); }
unsigned char apply(lua_State* L, detail::by_value<unsigned char>, int index) { return static_cast<char>(lua_tonumber(L, index)); }
unsigned long apply(lua_State* L, detail::by_value<unsigned long>, int index) { return static_cast<long>(lua_tonumber(L, index)); }
float apply(lua_State* L, detail::by_value<const float>, int index) { return static_cast<float>(lua_tonumber(L, index)); }
double apply(lua_State* L, detail::by_value<const double>, int index) { return static_cast<double>(lua_tonumber(L, index)); }
long double apply(lua_State* L, detail::by_value<const long double>, int index) {return static_cast<long double>(lua_tonumber(L, index)); }
int apply(lua_State* L, detail::by_value<const int>, int index) { return static_cast<int>(lua_tonumber(L, index)); }
short apply(lua_State* L, detail::by_value<const short>, int index) { return static_cast<short>(lua_tonumber(L, index)); }
char apply(lua_State* L, detail::by_value<const char>, int index) { return static_cast<char>(lua_tonumber(L, index)); }
long apply(lua_State* L, detail::by_value<const long>, int index) { return static_cast<long>(lua_tonumber(L, index)); }
unsigned int apply(lua_State* L, detail::by_value<const unsigned int>, int index) { return static_cast<int>(lua_tonumber(L, index)); }
unsigned short apply(lua_State* L, detail::by_value<const unsigned short>, int index) { return static_cast<short>(lua_tonumber(L, index)); }
unsigned char apply(lua_State* L, detail::by_value<const unsigned char>, int index) { return static_cast<char>(lua_tonumber(L, index)); }
unsigned long apply(lua_State* L, detail::by_value<const unsigned long>, int index) { return static_cast<long>(lua_tonumber(L, index)); }
std::string apply(lua_State* L, detail::by_value<std::string>, int index) { return static_cast<const char*>(lua_tostring(L, index)); }
const std::string apply(lua_State* L, detail::by_value<const std::string>, int index) { return static_cast<const char*>(lua_tostring(L, index)); }
luabind::object apply(lua_State* L, detail::by_value<luabind::object>, int index)
{
lua_pushvalue(L, index);
return luabind::object(L, detail::ref(L), true/*luabind::object::reference()*/);
}
const luabind::object apply(lua_State* L, detail::by_value<const luabind::object>, int index)
{
lua_pushvalue(L, index);
return luabind::object(L, detail::ref(L), true/*luabind::object::reference()*/);
}
// TODO: add more
template<class T>
T apply(lua_State* L, detail::by_const_reference<T>, int index) { return apply(L, detail::by_value<T>(), index); }
const char* apply(lua_State* L, detail::by_const_pointer<char>, int index) { return static_cast<const char*>(lua_tostring(L, index)); }
// matchers
static int match(lua_State* L, detail::by_value<bool>, int index) { if (lua_type(L, index) == LUA_TBOOLEAN) return 0; else return -1;}
static int match(lua_State* L, detail::by_value<float>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
static int match(lua_State* L, detail::by_value<double>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
static int match(lua_State* L, detail::by_value<long double>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
static int match(lua_State* L, detail::by_value<int>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
static int match(lua_State* L, detail::by_value<short>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
static int match(lua_State* L, detail::by_value<char>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
static int match(lua_State* L, detail::by_value<long>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
static int match(lua_State* L, detail::by_value<unsigned int>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
static int match(lua_State* L, detail::by_value<unsigned short>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
static int match(lua_State* L, detail::by_value<unsigned char>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
static int match(lua_State* L, detail::by_value<unsigned long>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
static int match(lua_State* L, detail::by_value<std::string>, int index) { if (lua_type(L, index) == LUA_TSTRING) return 0; else return -1;}
static int match(lua_State* L, detail::by_value<const std::string>, int index) { if (lua_type(L, index) == LUA_TSTRING) return 0; else return -1;}
static int match(lua_State*, detail::by_value<luabind::object>, int) { return std::numeric_limits<int>::max() - 1; }
static int match(lua_State*, detail::by_value<const luabind::object>, int) { return std::numeric_limits<int>::max() - 1; }
static int match(lua_State* L, by_const_pointer<char>, int index) { if (lua_type(L, index) == LUA_TSTRING) return 0; else return -1;}
static int match(lua_State* L, by_const_pointer<const char>, int index) { if (lua_type(L, index) == LUA_TSTRING) return 0; else return -1;}
template<class T>
static int match(lua_State* L, detail::by_const_reference<T>, int index) { return match(L, detail::by_value<T>(), index); }
template<class T>
void converter_postcall(lua_State*, T, int) {}
};
// *********** default converters ***************
/* template<class> struct implicit_converter;
template<>
struct implicit_converter<lua_to_cpp>
{
int converter_index;
template<class T>
T apply(lua_State* L, detail::by_value<T>, int index)
{
return converters::TO<T>::convert(L, detail::type<T>(), index);
}
template<class T>
static int match(lua_State* L, detail::by_value<T>, int index)
{
return converters::TO<T>::match(L, detail::type<T>(), index);
}
template<class T>
T apply(lua_State* L, detail::by_const_reference<T>, int index)
{
return converters::TO<T>::convert(L, detail::type<T>(), index);
}
template<class T>
static int match(lua_State* L, detail::by_const_reference<T>, int index)
{
return converters::TO<T>::match(L, detail::type<T>(), index);
}
template<class T>
T* apply(lua_State* L, detail::by_pointer<T>, int index)
{
return converters::TO<T*>::convert(L, detail::type<T*>(), index);
}
template<class T>
static int match(lua_State* L, detail::by_pointer<T>, int index)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -