From 7ffc983edd4bf6530967914ddafdbe403d1e1ecd Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Sat, 30 Mar 2002 01:23:28 +0000 Subject: [PATCH] support for data members [SVN r13309] --- include/boost/python/class.hpp | 33 ++++++++++----- .../boost/python/copy_mutable_reference.hpp | 41 +++++++++++++++++++ test/Jamfile | 1 + test/data_members.py | 29 +++++++++++++ test/test_class.hpp | 7 ++-- 5 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 include/boost/python/copy_mutable_reference.hpp create mode 100644 test/data_members.py diff --git a/include/boost/python/class.hpp b/include/boost/python/class.hpp index f040a789..ad1561ba 100644 --- a/include/boost/python/class.hpp +++ b/include/boost/python/class.hpp @@ -22,6 +22,7 @@ # include # include # include +# include # include 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_ self; BOOST_STATIC_CONSTANT(bool, is_copyable = (!detail::has_noncopyable::value)); @@ -138,8 +139,28 @@ class class_ : private objects::class_base return *this; } + // + // Data member access + // + template + self& def_readonly(char const* name, D T::*pm) + { + ref fget(make_getter(pm)); + this->add_property(name, fget); + return *this; + } + + template + 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_::class_(char const* name) , this->object()); } -template -inline ref class_::object() const -{ - typedef objects::class_base base; - - return this->base::object(); -} - namespace detail { // This is an mpl BinaryMetaFunction object with a runtime behavior, diff --git a/include/boost/python/copy_mutable_reference.hpp b/include/boost/python/copy_mutable_reference.hpp new file mode 100644 index 00000000..f7594ccd --- /dev/null +++ b/include/boost/python/copy_mutable_reference.hpp @@ -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 +# include +# include + +namespace boost { namespace python { + +namespace detail +{ + template + struct copy_mutable_reference_expects_a_reference_to_non_const_return_type +# if defined(__GNUC__) && __GNUC__ >= 3 || defined(__EDG__) + {} +# endif + ; +} + +template struct to_python_value; + +struct copy_mutable_reference +{ + template + struct apply + { + typedef typename mpl::select_type< + detail::is_reference_to_non_const::value + , to_python_value + , detail::copy_mutable_reference_expects_a_reference_to_non_const_return_type + >::type type; + }; +}; + +}} // namespace boost::python + +#endif // COPY_MUTABLE_REFERENCE_DWA2002131_HPP diff --git a/test/Jamfile b/test/Jamfile index 52253f2a..39514d5d 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -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 diff --git a/test/data_members.py b/test/data_members.py new file mode 100644 index 00000000..33de59d0 --- /dev/null +++ b/test/data_members.py @@ -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]) diff --git a/test/test_class.hpp b/test/test_class.hpp index 414cd084..18164102 100644 --- a/test/test_class.hpp +++ b/test/test_class.hpp @@ -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