2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-20 04:42:28 +00:00

support for data members

[SVN r13309]
This commit is contained in:
Dave Abrahams
2002-03-30 01:23:28 +00:00
parent 4a81d366bb
commit 7ffc983edd
5 changed files with 98 additions and 13 deletions

View File

@@ -22,6 +22,7 @@
# include <boost/mpl/bool_t.hpp>
# include <boost/python/object/select_holder.hpp>
# include <boost/python/object/class_wrapper.hpp>
# include <boost/python/data_members.hpp>
# include <boost/utility.hpp>
namespace boost { namespace python {
@@ -65,7 +66,7 @@ template <
, class X2 // = detail::not_specified
, class X3 // = detail::not_specified
>
class class_ : private objects::class_base
class class_ : public objects::class_base
{
typedef class_<T,X1,X2,X3> self;
BOOST_STATIC_CONSTANT(bool, is_copyable = (!detail::has_noncopyable<X1,X2,X3>::value));
@@ -138,8 +139,28 @@ class class_ : private objects::class_base
return *this;
}
//
// Data member access
//
template <class D>
self& def_readonly(char const* name, D T::*pm)
{
ref fget(make_getter(pm));
this->add_property(name, fget);
return *this;
}
template <class D>
self& def_readwrite(char const* name, D T::*pm)
{
ref fget(make_getter(pm));
ref fset(make_setter(pm));
this->add_property(name, fget, fset);
return *this;
}
// return the underlying object
ref object() const;
// ref object() const;
private: // types
typedef objects::class_id class_id;
@@ -202,14 +223,6 @@ inline class_<T,X1,X2,X3>::class_(char const* name)
, this->object());
}
template <class T, class X1, class X2, class X3>
inline ref class_<T,X1,X2,X3>::object() const
{
typedef objects::class_base base;
return this->base::object();
}
namespace detail
{
// This is an mpl BinaryMetaFunction object with a runtime behavior,

View File

@@ -0,0 +1,41 @@
// Copyright David Abrahams 2002. Permission to copy, use,
// modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
#ifndef COPY_MUTABLE_REFERENCE_DWA2002131_HPP
# define COPY_MUTABLE_REFERENCE_DWA2002131_HPP
# include <boost/python/detail/indirect_traits.hpp>
# include <boost/mpl/select_type.hpp>
# include <boost/python/to_python_value.hpp>
namespace boost { namespace python {
namespace detail
{
template <class R>
struct copy_mutable_reference_expects_a_reference_to_non_const_return_type
# if defined(__GNUC__) && __GNUC__ >= 3 || defined(__EDG__)
{}
# endif
;
}
template <class T> struct to_python_value;
struct copy_mutable_reference
{
template <class T>
struct apply
{
typedef typename mpl::select_type<
detail::is_reference_to_non_const<T>::value
, to_python_value<T>
, detail::copy_mutable_reference_expects_a_reference_to_non_const_return_type<T>
>::type type;
};
};
}} // namespace boost::python
#endif // COPY_MUTABLE_REFERENCE_DWA2002131_HPP

View File

@@ -58,6 +58,7 @@ bpl-test callbacks ;
bpl-test virtual_functions ;
bpl-test back_reference ;
bpl-test implicit ;
bpl-test data_members ;
# --- unit tests of library components ---
unit-test indirect_traits_test

29
test/data_members.py Normal file
View File

@@ -0,0 +1,29 @@
'''
>>> from data_members_ext import *
>>> x = X(42)
>>> x.x
42
>>> try: x.x = 77
>>> except AttributeError: pass
>>> else: print 'no error'
>>> y = Y(69)
>>> y.x
69
>>> y.x = 77
>>> y.x
77
'''
def run(args = None):
import sys
import doctest
if args is not None:
sys.argv = args
return doctest.testmod(sys.modules.get(__name__))
if __name__ == '__main__':
print "running..."
import sys
sys.exit(run()[0])

View File

@@ -18,12 +18,13 @@ struct test_class
int value() const { assert(magic == 7654321 + n); return x; }
operator int() const { return x; }
static int count() { return counter; }
private:
void operator=(test_class const&);
private:
int x;
long magic;
static int counter;
private:
void operator=(test_class const&);
};
template <int n>