class.hpp
来自「这是整套横扫千军3D版游戏的源码」· HPP 代码 · 共 1,342 行 · 第 1/3 页
HPP
1,342 行
// 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_CLASS_HPP_INCLUDED
#define LUABIND_CLASS_HPP_INCLUDED
/*
ISSUES:
------------------------------------------------------
* solved for member functions, not application operator *
if we have a base class that defines a function a derived class must be able to
override that function (not just overload). Right now we just add the other overload
to the overloads list and will probably get an ambiguity. If we want to support this
each method_rep must include a vector of type_info pointers for each parameter.
Operators do not have this problem, since operators always have to have
it's own type as one of the arguments, no ambiguity can occur. Application
operator, on the other hand, would have this problem.
Properties cannot be overloaded, so they should always be overridden.
If this is to work for application operator, we really need to specify if an application
operator is const or not.
If one class registers two functions with the same name and the same
signature, there's currently no error. The last registered function will
be the one that's used.
How do we know which class registered the function? If the function was
defined by the base class, it is a legal operation, to override it.
we cannot look at the pointer offset, since it always will be zero for one of the bases.
TODO:
------------------------------------------------------
finish smart pointer support
* the adopt policy should not be able to adopt pointers to held_types. This
must be prohibited.
* name_of_type must recognize holder_types and not return "custom"
document custom policies, custom converters
store the instance object for policies.
support the __concat metamethod. This is a bit tricky, since it cannot be
treated as a normal operator. It is a binary operator but we want to use the
__tostring implementation for both arguments.
*/
#include <luabind/prefix.hpp>
#include <luabind/config.hpp>
#include <string>
#include <map>
#include <vector>
#include <cassert>
#include <boost/static_assert.hpp>
#include <boost/type_traits.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>
#include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/list.hpp>
#include <boost/mpl/apply.hpp>
#include <boost/mpl/lambda.hpp>
#include <boost/mpl/logical.hpp>
#include <boost/mpl/find_if.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/logical.hpp>
#include <luabind/config.hpp>
#include <luabind/scope.hpp>
#include <luabind/raw_policy.hpp>
#include <luabind/back_reference.hpp>
#include <luabind/detail/constructor.hpp>
#include <luabind/detail/call.hpp>
#include <luabind/detail/signature_match.hpp>
#include <luabind/detail/primitives.hpp>
#include <luabind/detail/property.hpp>
#include <luabind/detail/typetraits.hpp>
#include <luabind/detail/class_rep.hpp>
#include <luabind/detail/method_rep.hpp>
#include <luabind/detail/construct_rep.hpp>
#include <luabind/detail/object_rep.hpp>
#include <luabind/detail/calc_arity.hpp>
#include <luabind/detail/call_member.hpp>
#include <luabind/detail/enum_maker.hpp>
#include <luabind/detail/get_signature.hpp>
#include <luabind/detail/implicit_cast.hpp>
#include <luabind/detail/operator_id.hpp>
#include <luabind/detail/pointee_typeid.hpp>
#include <luabind/detail/link_compatibility.hpp>
// to remove the 'this' used in initialization list-warning
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4355)
#endif
namespace luabind
{
namespace detail
{
struct unspecified {};
template<class Derived> struct operator_;
struct you_need_to_define_a_get_const_holder_function_for_your_smart_ptr {};
}
template<class T, class X1 = detail::unspecified, class X2 = detail::unspecified, class X3 = detail::unspecified>
struct class_;
// TODO: this function will only be invoked if the user hasn't defined a correct overload
// maybe we should have a static assert in here?
inline detail::you_need_to_define_a_get_const_holder_function_for_your_smart_ptr*
get_const_holder(...)
{
return 0;
}
namespace detail
{
template<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_BASES, class A)>
double is_bases_helper(const bases<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_BASES, A)>&);
#ifndef BOOST_MSVC
template<class T>
char is_bases_helper(const T&);
#else
char is_bases_helper(...);
#endif
template<class T>
struct is_bases
{
static const T& t;
BOOST_STATIC_CONSTANT(bool, value = sizeof(is_bases_helper(t)) == sizeof(double));
typedef boost::mpl::bool_<value> type;
BOOST_MPL_AUX_LAMBDA_SUPPORT(1,is_bases,(T))
};
double is_not_unspecified_helper(const unspecified*);
char is_not_unspecified_helper(...);
template<class T>
struct is_not_unspecified
{
BOOST_STATIC_CONSTANT(bool, value = sizeof(is_not_unspecified_helper(static_cast<T*>(0))) == sizeof(char));
typedef boost::mpl::bool_<value> type;
BOOST_MPL_AUX_LAMBDA_SUPPORT(1,is_not_unspecified,(T))
};
template<class Predicate>
struct get_predicate
{
typedef typename boost::mpl::and_<
is_not_unspecified<boost::mpl::_1>
, Predicate
> type;
};
template<class Parameters, class Predicate, class DefaultValue>
struct extract_parameter
{
typedef typename get_predicate<Predicate>::type pred;
typedef typename boost::mpl::find_if<Parameters, pred>::type iterator;
typedef typename boost::mpl::eval_if<
boost::is_same<
iterator
, typename boost::mpl::end<Parameters>::type
>
, boost::mpl::identity<DefaultValue>
, boost::mpl::deref<iterator>
>::type type;
};
template<class Fn, class Class, class Policies>
struct mem_fn_callback
{
typedef int result_type;
int operator()(lua_State* L) const
{
return call(fn, (Class*)0, L, (Policies*)0);
}
mem_fn_callback(Fn fn_)
: fn(fn_)
{
}
Fn fn;
};
template<class Fn, class Class, class Policies>
struct mem_fn_matcher
{
typedef int result_type;
int operator()(lua_State* L) const
{
return match(fn, L, (Class*)0, (Policies*)0);
}
mem_fn_matcher(Fn fn_)
: fn(fn_)
{
}
Fn fn;
};
struct pure_virtual_tag
{
static void precall(lua_State*, index_map const&) {}
static void postcall(lua_State*, index_map const&) {}
};
template<class Policies>
struct has_pure_virtual
{
typedef typename boost::mpl::eval_if<
boost::is_same<pure_virtual_tag, typename Policies::head>
, boost::mpl::true_
, has_pure_virtual<typename Policies::tail>
>::type type;
BOOST_STATIC_CONSTANT(bool, value = type::value);
};
template<>
struct has_pure_virtual<null_type>
{
BOOST_STATIC_CONSTANT(bool, value = false);
typedef boost::mpl::bool_<value> type;
};
// prints the types of the values on the stack, in the
// range [start_index, lua_gettop()]
LUABIND_API std::string stack_content_by_name(lua_State* L, int start_index);
struct LUABIND_API create_class
{
static int stage1(lua_State* L);
static int stage2(lua_State* L);
};
// if the class is held by a smart pointer, we need to be able to
// implicitly dereference the pointer when needed.
template<class UnderlyingT, class HeldT>
struct extract_underlying_type
{
static void* extract(void* ptr)
{
HeldT& held_obj = *reinterpret_cast<HeldT*>(ptr);
UnderlyingT* underlying_ptr = static_cast<UnderlyingT*>(get_pointer(held_obj));
return underlying_ptr;
}
};
template<class UnderlyingT, class HeldT>
struct extract_underlying_const_type
{
static const void* extract(void* ptr)
{
HeldT& held_obj = *reinterpret_cast<HeldT*>(ptr);
const UnderlyingT* underlying_ptr = static_cast<const UnderlyingT*>(get_pointer(held_obj));
return underlying_ptr;
}
};
template<class HeldType>
struct internal_holder_extractor
{
typedef void*(*extractor_fun)(void*);
template<class T>
static extractor_fun apply(detail::type_<T>)
{
return &detail::extract_underlying_type<T, HeldType>::extract;
}
};
template<>
struct internal_holder_extractor<detail::null_type>
{
typedef void*(*extractor_fun)(void*);
template<class T>
static extractor_fun apply(detail::type_<T>)
{
return 0;
}
};
template<class HeldType, class ConstHolderType>
struct convert_holder
{
static void apply(void* holder, void* target)
{
new(target) ConstHolderType(*reinterpret_cast<HeldType*>(holder));
};
};
template<class HeldType>
struct const_converter
{
typedef void(*converter_fun)(void*, void*);
template<class ConstHolderType>
static converter_fun apply(ConstHolderType*)
{
return &detail::convert_holder<HeldType, ConstHolderType>::apply;
}
};
template<>
struct const_converter<detail::null_type>
{
typedef void(*converter_fun)(void*, void*);
template<class T>
static converter_fun apply(T*)
{
return 0;
}
};
template<class HeldType>
struct internal_const_holder_extractor
{
typedef const void*(*extractor_fun)(void*);
template<class T>
static extractor_fun apply(detail::type_<T>)
{
return get_extractor(detail::type_<T>(), get_const_holder(static_cast<HeldType*>(0)));
}
private:
template<class T, class ConstHolderType>
static extractor_fun get_extractor(detail::type_<T>, ConstHolderType*)
{
return &detail::extract_underlying_const_type<T, ConstHolderType>::extract;
}
};
template<>
struct internal_const_holder_extractor<detail::null_type>
{
typedef const void*(*extractor_fun)(void*);
template<class T>
static extractor_fun apply(detail::type_<T>)
{
return 0;
}
};
// this is simply a selector that returns the type_info
// of the held type, or invalid_type_info if we don't have
// a held_type
template<class HeldType>
struct internal_holder_type
{
static LUABIND_TYPE_INFO apply()
{
return LUABIND_TYPEID(HeldType);
}
};
template<>
struct internal_holder_type<detail::null_type>
{
static LUABIND_TYPE_INFO apply()
{
return LUABIND_INVALID_TYPE_INFO;
}
};
// this is the actual held_type constructor
template<class HeldType, class T>
struct internal_construct_holder
{
static void apply(void* target, void* raw_pointer)
{
new(target) HeldType(static_cast<T*>(raw_pointer));
}
};
// this is the actual held_type default constructor
template<class HeldType, class T>
struct internal_default_construct_holder
{
static void apply(void* target)
{
new(target) HeldType();
}
};
// the following two functions are the ones that returns
// a pointer to a held_type_constructor, or 0 if there
// is no held_type
template<class HeldType>
struct holder_constructor
{
typedef void(*constructor)(void*,void*);
template<class T>
static constructor apply(detail::type_<T>)
{
return &internal_construct_holder<HeldType, T>::apply;
}
};
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?