Revision: 900
http://svn.sourceforge.net/pygccxml/?rev=900&view=rev
Author: roman_yakovenko
Date: 2007-02-11 05:28:38 -0800 (Sun, 11 Feb 2007)
Log Message:
-----------
expose mapping containers value type
Modified Paths:
--------------
pyplusplus_dev/indexing_suite_v2/indexing/map.hpp
pyplusplus_dev/indexing_suite_v2/indexing/multimap.hpp
Added Paths:
-----------
pyplusplus_dev/indexing_suite_v2/indexing/pair.hpp
Modified: pyplusplus_dev/indexing_suite_v2/indexing/map.hpp
===================================================================
--- pyplusplus_dev/indexing_suite_v2/indexing/map.hpp 2007-02-10 21:40:21 UTC (rev 899)
+++ pyplusplus_dev/indexing_suite_v2/indexing/map.hpp 2007-02-11 13:28:38 UTC (rev 900)
@@ -23,6 +23,7 @@
#include <boost/python/suite/indexing/algorithms.hpp>
#include <boost/detail/workaround.hpp>
#include <map>
+#include <boost/python/suite/indexing/pair.hpp>
namespace boost { namespace python { namespace indexing {
/////////////////////////////////////////////////////////////////////////
@@ -105,7 +106,12 @@
static void visit_container_class( PythonClass &pyClass, Policy const &policy)
{
ContainerTraits::visit_container_class (pyClass, policy);
- pyClass.def( "keys", &self_type::keys );
+ pyClass.def( "keys", &self_type::keys );
+
+ typedef BOOST_DEDUCED_TYPENAME most_derived::container::value_type value_type;
+ mapping::register_value_type< PythonClass, value_type, Policy >( pyClass );
+ //now we can expose iterators functionality
+ pyClass.def( "__iter__", python::iterator< most_derived::container >() );
}
};
Modified: pyplusplus_dev/indexing_suite_v2/indexing/multimap.hpp
===================================================================
--- pyplusplus_dev/indexing_suite_v2/indexing/multimap.hpp 2007-02-10 21:40:21 UTC (rev 899)
+++ pyplusplus_dev/indexing_suite_v2/indexing/multimap.hpp 2007-02-11 13:28:38 UTC (rev 900)
@@ -20,6 +20,7 @@
#include <boost/detail/workaround.hpp>
#include <functional>
#include <map>
+#include <boost/python/suite/indexing/pair.hpp>
namespace boost { namespace python { namespace indexing {
/////////////////////////////////////////////////////////////////////////
@@ -103,6 +104,12 @@
{
ContainerTraits::visit_container_class (pyClass, policy);
pyClass.def( "keys", &self_type::keys );
+
+ typedef BOOST_DEDUCED_TYPENAME most_derived::container::value_type value_type;
+ mapping::register_value_type< PythonClass, value_type, Policy >( pyClass );
+ //now we can expose iterators functionality
+ pyClass.def( "__iter__", python::iterator< most_derived::container >() );
+
}
};
@@ -141,8 +148,7 @@
boost::python::list _keys;
//For some reason code with set could not be compiled
//std::set< key_param > unique_keys;
- typedef BOOST_DEDUCED_TYPENAME container::iterator iter_type;
- for( iter_type index = most_derived::begin(c); index != most_derived::end(c); ++index ){
+ for( iterator index = most_derived::begin(c); index != most_derived::end(c); ++index ){
//if( unique_keys.end() == unique_keys.find( index->first ) ){
// unique_keys.insert( index->first );
if( !_keys.count( index->first ) ){
Added: pyplusplus_dev/indexing_suite_v2/indexing/pair.hpp
===================================================================
--- pyplusplus_dev/indexing_suite_v2/indexing/pair.hpp (rev 0)
+++ pyplusplus_dev/indexing_suite_v2/indexing/pair.hpp 2007-02-11 13:28:38 UTC (rev 900)
@@ -0,0 +1,98 @@
+// Header file pair.hpp
+//
+// Exposes std::pair< key, value > class
+//
+// Copyright (c) 2007 Roman Yakovenko
+//
+// Use, modification and distribution is subject to the Boost Software
+// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy
+// at http://www.boost.org/LICENSE_1_0.txt)
+//
+// History
+// =======
+// 2007/2/11 rmg File creation
+//
+
+#ifndef BOOST_PYTHON_STD_PAIR_KEY_VALUE_11_02_2007_HPP
+#define BOOST_PYTHON_STD_PAIR_KEY_VALUE_11_02_2007_HPP
+
+#include <boost/config.hpp>
+#include <boost/python/suite/indexing/container_traits.hpp>
+#include <boost/python/suite/indexing/container_suite.hpp>
+#include <boost/python/suite/indexing/algorithms.hpp>
+#include <boost/detail/workaround.hpp>
+
+namespace boost { namespace python { namespace indexing { namespace mapping{
+
+namespace details{
+
+template< typename TValueType, typename TValueCallPolicies >
+struct pair_exposer_t{
+
+ typedef TValueType pair_type;
+ typedef BOOST_DEDUCED_TYPENAME pair_type::first_type key_type;
+ typedef BOOST_DEDUCED_TYPENAME pair_type::second_type mapped_type;
+ typedef pair_exposer_t< TValueType, TValueCallPolicies > exposer_type;
+
+ pair_exposer_t(const std::string& name){
+ class_< pair_type >( name.c_str() )
+ .def( "__len__", &exposer_type::len )
+ .def( "__getitem__", &exposer_type::get_item )
+ .add_property( "key", &exposer_type::get_key )
+ .add_property( "value", &exposer_type::get_mapped );
+ }
+
+private:
+
+ static size_t len( const pair_type& ){
+ return 2;
+ }
+
+ static object get_item( pair_type& p, size_t index ){
+ switch( index ){
+ case 0:{
+ return get_key( p );
+ }
+ case 1:{
+ return get_mapped( p );
+ }
+ case 2:{
+ objects::stop_iteration_error();
+ return object(); //will not reach this line
+ }
+ default:{
+ PyErr_SetString( PyExc_IndexError, "the only valid index numbers are: 0 and 1");
+ throw_error_already_set();
+ return object(); //will not reach this line
+ }
+ }
+ }
+
+ static object get_key( const pair_type& p ){
+ return object( p.first );
+ }
+
+ static object get_mapped( pair_type& p ){
+ typedef BOOST_DEDUCED_TYPENAME TValueCallPolicies::result_converter rc_type;
+ typedef BOOST_DEDUCED_TYPENAME rc_type:: template apply< mapped_type >::type converter_type;
+ converter_type converter;
+ return object( handle<>( converter( p.second ) ) );
+ }
+
+};
+} //details
+
+template< typename TPythonClass, typename TValueType, typename TValueCallPolicies >
+inline void register_value_type(TPythonClass &pyClass){
+ typedef details::pair_exposer_t< TValueType, TValueCallPolicies > exposer_type;
+
+ object class_name(pyClass.attr("__name__"));
+ extract<std::string> class_name_extractor(class_name);
+ std::string pair_name = class_name_extractor() + "_entry";
+
+ exposer_type expose( pair_name );
+}
+
+} } } }
+
+#endif // BOOST_PYTHON_STD_PAIR_KEY_VALUE_11_02_2007_HPP
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|