From 33e3f4b6c98146c3e6bfbcd6cf89d6d4b155e3ee Mon Sep 17 00:00:00 2001 From: Raoul Gough Date: Thu, 11 Sep 2003 22:34:10 +0000 Subject: [PATCH] def_visitor to add indexing capabilities to a Boost Python class [SVN r1533] --- .../boost/python/suite/indexing/visitor.hpp | 160 ++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100755 include/boost/python/suite/indexing/visitor.hpp diff --git a/include/boost/python/suite/indexing/visitor.hpp b/include/boost/python/suite/indexing/visitor.hpp new file mode 100755 index 00000000..d759cecd --- /dev/null +++ b/include/boost/python/suite/indexing/visitor.hpp @@ -0,0 +1,160 @@ +// -*- mode:c++ -*- +// +// Header file visitor.hpp +// +// Copyright (c) 2003 Raoul M. Gough +// +// This material is provided "as is", with absolutely no warranty expressed +// or implied. Any use is at your own risk. +// +// Permission to use or copy this material for any purpose is hereby +// granted without fee, provided the above notices are retained on all +// copies. Permission to modify the material and to distribute modified +// versions is granted, provided the above notices are retained, and a +// notice that the material was modified is included with the above +// copyright notice. +// +// History +// ======= +// 2003/ 9/11 rmg File creation from container_suite.hpp +// +// $Id$ +// + +#ifndef visitor_rmg_20030823_included +#define visitor_rmg_20030823_included + +#include "slice_handler.hpp" + +#include +#include + +namespace indexing { + template + struct maybe_add_getitem { + template + static void apply (PythonClass &, Algorithms const &, Policy const &) { } + }; + + template<> + struct maybe_add_getitem { + template + static void apply (PythonClass &pyClass + , Algorithms const & + , Policy const &policy) + { + pyClass.def ("__getitem__", &Algorithms::get, policy); + } + }; + + template<> + struct maybe_add_getitem { + template + static void apply (PythonClass &pyClass + , Algorithms const & + , Policy const &policy) + { + pyClass.def ("__getitem__", &Algorithms::get, policy); + pyClass.def ("__getitem__" + , slice_handler::make_getitem (policy)); + } + }; + + template + struct maybe_add_setitem { + template + static void apply (PythonClass &, Algorithms const &, Policy const &) { } + }; + + template<> + struct maybe_add_setitem { + template + static void apply (PythonClass &pyClass + , Algorithms const & + , Policy const &policy) + { + pyClass.def ("__setitem__", &Algorithms::assign, policy); + } + }; + + template<> + struct maybe_add_setitem { + template + static void apply (PythonClass &pyClass + , Algorithms const & + , Policy const &policy) + { + pyClass.def ("__setitem__", &Algorithms::assign, policy); + pyClass.def ("__setitem__" + , slice_handler::make_setitem (policy)); + } + }; + + template + struct maybe_add_iter { + template + static void apply (PythonClass &, Algorithms const &, Policy const &) { } + }; + + template<> + struct maybe_add_iter { + template + static void apply (PythonClass &pyClass + , Algorithms const & + , Policy const &policy) + { + pyClass.def ("__iter__" + , boost::python::range (Algorithms::begin + , Algorithms::end)); + } + }; + + template + struct maybe_add_append { + template + static void apply (PythonClass &, Algorithms const &, Policy const &) { } + }; + + template<> + struct maybe_add_append { + template + static void apply (PythonClass &pyClass + , Algorithms const & + , Policy const &policy) + { + pyClass.def ("append", &Algorithms::push_back, policy); + } + }; + + template + class visitor + : public boost::python::def_visitor< visitor< Algorithms, Policy > > + { + Policy mPolicy; + + public: + typedef Algorithms algorithms; + typedef typename algorithms::container_traits traits; + + visitor (Policy const &policy) : mPolicy (policy) { } + + template + void visit (PythonClass &pyClass) const + { + maybe_add_getitem + ::apply (pyClass, algorithms(), mPolicy); + + maybe_add_setitem + ::apply (pyClass, algorithms(), mPolicy); + + maybe_add_iter<((traits::index_style != index_style_linear) + && traits::has_copyable_iter)> + ::apply (pyClass, algorithms(), mPolicy); + + maybe_add_append + ::apply (pyClass, algorithms(), mPolicy); + } + }; +} + +#endif // visitor_rmg_20030823_included