~yade-dev/yade/0.80

« back to all changes in this revision

Viewing changes to py/3rd-party/boost-python-indexing-suite-v2-noSymlinkHeaders/pair.hpp

  • Committer: Anton Gladky
  • Date: 2012-05-02 21:50:42 UTC
  • Revision ID: gladky.anton@gmail.com-20120502215042-v1fa9r65usqe7kfk
0.80.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Header file pair.hpp
 
2
//
 
3
// Exposes std::pair< key, value > class
 
4
//
 
5
// Copyright (c) 2007 Roman Yakovenko
 
6
//
 
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)
 
10
//
 
11
// History
 
12
// =======
 
13
// 2007/2/11   rmg     File creation
 
14
// 2008/12/08   Roman   Change indexing suite layout
 
15
//
 
16
 
 
17
#ifndef BOOST_PYTHON_STD_PAIR_KEY_VALUE_11_02_2007_HPP
 
18
#define BOOST_PYTHON_STD_PAIR_KEY_VALUE_11_02_2007_HPP
 
19
 
 
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>
 
25
 
 
26
namespace boost { namespace python { namespace indexing { namespace mapping{
 
27
 
 
28
namespace details{
 
29
 
 
30
template< typename TValueType, typename TValueCallPolicies >
 
31
struct pair_exposer_t{
 
32
 
 
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;
 
37
 
 
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 );
 
44
    }
 
45
 
 
46
private:
 
47
 
 
48
    static size_t len( const pair_type& ){
 
49
        return 2;
 
50
    }
 
51
 
 
52
    static object get_item( pair_type& p, size_t index ){
 
53
        switch( index ){
 
54
            case 0:{
 
55
                return get_key( p );
 
56
            }
 
57
            case 1:{
 
58
                return get_mapped( p );
 
59
            }
 
60
            case 2:{
 
61
                objects::stop_iteration_error();
 
62
                return object(); //will not reach this line
 
63
            }
 
64
            default:{
 
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
 
68
            }
 
69
        }
 
70
    }
 
71
 
 
72
    static object get_key( const pair_type& p ){
 
73
        return object( p.first );
 
74
    }
 
75
 
 
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 ) ) );
 
81
    }
 
82
 
 
83
};
 
84
} //details
 
85
 
 
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;
 
89
 
 
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";
 
93
 
 
94
    exposer_type expose( pair_name );
 
95
}
 
96
 
 
97
} } } }
 
98
 
 
99
#endif // BOOST_PYTHON_STD_PAIR_KEY_VALUE_11_02_2007_HPP