1
// Header file pair.hpp
3
// Exposes std::pair< key, value > class
5
// Copyright (c) 2007 Roman Yakovenko
7
// Use, modification and distribution is subject to the Boost Software
8
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy
9
// at http://www.boost.org/LICENSE_1_0.txt)
13
// 2007/2/11 rmg File creation
14
// 2008/12/08 Roman Change indexing suite layout
17
#ifndef BOOST_PYTHON_STD_PAIR_KEY_VALUE_11_02_2007_HPP
18
#define BOOST_PYTHON_STD_PAIR_KEY_VALUE_11_02_2007_HPP
20
#include <boost/config.hpp>
21
#include <indexing_suite/container_traits.hpp>
22
#include <indexing_suite/container_suite.hpp>
23
#include <indexing_suite/algorithms.hpp>
24
#include <boost/detail/workaround.hpp>
26
namespace boost { namespace python { namespace indexing { namespace mapping{
30
template< typename TValueType, typename TValueCallPolicies >
31
struct pair_exposer_t{
33
typedef TValueType pair_type;
34
typedef BOOST_DEDUCED_TYPENAME pair_type::first_type key_type;
35
typedef BOOST_DEDUCED_TYPENAME pair_type::second_type mapped_type;
36
typedef pair_exposer_t< TValueType, TValueCallPolicies > exposer_type;
38
pair_exposer_t(const std::string& name){
39
class_< pair_type >( name.c_str() )
40
.def( "__len__", &exposer_type::len )
41
.def( "__getitem__", &exposer_type::get_item )
42
.add_property( "key", &exposer_type::get_key )
43
.add_property( "value", &exposer_type::get_mapped );
48
static size_t len( const pair_type& ){
52
static object get_item( pair_type& p, size_t index ){
58
return get_mapped( p );
61
objects::stop_iteration_error();
62
return object(); //will not reach this line
65
PyErr_SetString( PyExc_IndexError, "the only valid index numbers are: 0 and 1");
66
throw_error_already_set();
67
return object(); //will not reach this line
72
static object get_key( const pair_type& p ){
73
return object( p.first );
76
static object get_mapped( pair_type& p ){
77
typedef BOOST_DEDUCED_TYPENAME TValueCallPolicies::result_converter rc_type;
78
typedef BOOST_DEDUCED_TYPENAME rc_type:: template apply< mapped_type >::type converter_type;
79
converter_type converter;
80
return object( handle<>( converter( p.second ) ) );
86
template< typename TPythonClass, typename TValueType, typename TValueCallPolicies >
87
inline void register_value_type(TPythonClass &pyClass){
88
typedef details::pair_exposer_t< TValueType, TValueCallPolicies > exposer_type;
90
object class_name(pyClass.attr("__name__"));
91
extract<std::string> class_name_extractor(class_name);
92
std::string pair_name = class_name_extractor() + "_entry";
94
exposer_type expose( pair_name );
99
#endif // BOOST_PYTHON_STD_PAIR_KEY_VALUE_11_02_2007_HPP