2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-24 06:02:14 +00:00
add_property now uses member_function_cast


[SVN r16335]
This commit is contained in:
Dave Abrahams
2002-11-20 01:14:16 +00:00
parent 71ea2bec86
commit 39eab72293
5 changed files with 50 additions and 44 deletions

View File

@@ -273,19 +273,33 @@ class class_ : public objects::class_base
// Property creation
template <class Get>
self& add_property(char const* name, Get const& fget)
self& add_property(char const* name, Get fget)
{
base::add_property(name, object(fget));
base::add_property(
name
, object(
detail::member_function_cast<T,Get>::stage1(fget).stage2((T*)0).stage3(fget)
)
);
return *this;
}
template <class Get, class Set>
self& add_property(char const* name, Get const& fget, Set const& fset)
self& add_property(char const* name, Get fget, Set fset)
{
base::add_property(name, object(fget), object(fset));
base::add_property(
name
, object(
detail::member_function_cast<T,Get>::stage1(fget).stage2((T*)0).stage3(fget)
)
, object(
detail::member_function_cast<T,Set>::stage1(fset).stage2((T*)0).stage3(fset)
)
);
return *this;
}
template <class U>
self& setattr(char const* name, U const& x)
{

View File

@@ -65,7 +65,7 @@ namespace detail
struct tuple_extract_base_select
{
typedef typename Tuple::head_type head_type;
typedef typename mpl::apply1<Predicate, add_reference<head_type>::type>::type match_t;
typedef typename mpl::apply1<Predicate, typename add_reference<head_type>::type>::type match_t;
BOOST_STATIC_CONSTANT(bool, match = match_t::value);
typedef typename tuple_extract_impl<match>::template apply<Tuple,Predicate> type;
};

View File

@@ -47,47 +47,26 @@ struct is_reference_to_const<T const volatile&>
# endif
template <class T>
struct is_reference_to_function
struct is_reference_to_function : mpl::bool_c<false>
{
};
template <class T>
struct is_reference_to_function<T&> : is_function<T>
{
};
template <class T>
struct is_pointer_to_function : mpl::bool_c<false>
{
BOOST_STATIC_CONSTANT(bool, value = false);
};
// There's no such thing as a pointer-to-cv-function, so we don't need
// specializations for those
template <class T>
struct is_reference_to_function<T&>
struct is_pointer_to_function<T*> : is_function<T>
{
BOOST_STATIC_CONSTANT(bool, value = is_function<T>::value);
};
# if 0
template <class T>
struct is_reference_to_function<T const&>
{
BOOST_STATIC_CONSTANT(bool, value = is_function<T>::value);
};
template <class T>
struct is_reference_to_function<T volatile&>
{
BOOST_STATIC_CONSTANT(bool, value = is_function<T>::value);
};
template <class T>
struct is_reference_to_function<T const volatile&>
{
BOOST_STATIC_CONSTANT(bool, value = is_function<T>::value);
};
# endif
template <class T>
struct is_pointer_to_function
{
BOOST_STATIC_CONSTANT(bool, value = false);
};
template <class T>
struct is_pointer_to_function<T*>
{
// There's no such thing as a pointer-to-cv-function, so we don't need specializations for those
BOOST_STATIC_CONSTANT(bool, value = is_function<T>::value);
};
template <class T>

View File

@@ -22,11 +22,18 @@ typedef test_class<1> Y;
double get_fair_value(X const& x) { return x.value(); }
struct Var
struct VarBase
{
Var(std::string name_) : name(name_), value(), name2(name.c_str()), y(6) {}
VarBase(std::string name_) : name(name_) {}
std::string const name;
std::string get_name1() const { return name; }
};
struct Var : VarBase
{
Var(std::string name_) : VarBase(name_), value(), name2(name.c_str()), y(6) {}
std::string const& get_name2() const { return name; }
float value;
char const* name2;
@@ -39,7 +46,7 @@ BOOST_PYTHON_MODULE(data_members_ext)
.def("value", &X::value)
.def("set", &X::set)
.def_readonly("x", &X::x)
.add_property("fair_value", &get_fair_value)
.add_property("fair_value", get_fair_value)
;
class_<Y>("Y", init<int>())
@@ -60,6 +67,8 @@ BOOST_PYTHON_MODULE(data_members_ext)
// sense to add the test here.
.def("get_name1", &Var::get_name1, return_value_policy<return_by_value>())
.def("get_name2", &Var::get_name2, return_value_policy<return_by_value>())
.add_property("name3", &Var::get_name1)
;
}

View File

@@ -35,6 +35,10 @@
>>> v.y.x = -7
>>> v.y.x
-7
>>> v.name3
'pi'
'''
def run(args = None):