4 %include <rubystdcommon.swg>
6 //#define SWIG_STD_PAIR_ASVAL
8 %fragment("StdPairTraits","header",fragment="StdTraits") {
11 template <class T, class U >
12 struct traits_asval<std::pair<T,U> > {
13 typedef std::pair<T,U> value_type;
15 static int get_pair(VALUE first, VALUE second,
19 T *pfirst = &(val->first);
20 int res1 = swig::asval((VALUE)first, pfirst);
21 if (!SWIG_IsOK(res1)) return res1;
22 U *psecond = &(val->second);
23 int res2 = swig::asval((VALUE)second, psecond);
24 if (!SWIG_IsOK(res2)) return res2;
25 return res1 > res2 ? res1 : res2;
28 int res1 = swig::asval((VALUE)first, pfirst);
29 if (!SWIG_IsOK(res1)) return res1;
31 int res2 = swig::asval((VALUE)second, psecond);
32 if (!SWIG_IsOK(res2)) return res2;
33 return res1 > res2 ? res1 : res2;
37 static int asval(VALUE obj, std::pair<T,U> *val) {
39 if ( TYPE(obj) == T_ARRAY ) {
40 if (RARRAY_LEN(obj) == 2) {
41 VALUE first = rb_ary_entry(obj,0);
42 VALUE second = rb_ary_entry(obj,1);
43 res = get_pair(first, second, val);
47 res = SWIG_ConvertPtr(obj,(void**)&p,
48 swig::type_info<value_type>(),0);
49 if (SWIG_IsOK(res) && val) *val = *p;
55 template <class T, class U >
56 struct traits_asptr<std::pair<T,U> > {
57 typedef std::pair<T,U> value_type;
59 static int get_pair(VALUE first, VALUE second,
63 value_type *vp = %new_instance(std::pair<T,U>);
64 T *pfirst = &(vp->first);
65 int res1 = swig::asval((VALUE)first, pfirst);
66 if (!SWIG_IsOK(res1)) return res1;
67 U *psecond = &(vp->second);
68 int res2 = swig::asval((VALUE)second, psecond);
69 if (!SWIG_IsOK(res2)) return res2;
71 return SWIG_AddNewMask(res1 > res2 ? res1 : res2);
74 int res1 = swig::asval((VALUE)first, pfirst);
75 if (!SWIG_IsOK(res1)) return res1;
77 int res2 = swig::asval((VALUE)second, psecond);
78 if (!SWIG_IsOK(res2)) return res2;
79 return res1 > res2 ? res1 : res2;
83 static int asptr(VALUE obj, std::pair<T,U> **val) {
85 if ( TYPE(obj) == T_ARRAY ) {
86 if ( RARRAY_LEN(obj) == 2) {
87 VALUE first = rb_ary_entry(obj,0);
88 VALUE second = rb_ary_entry(obj,1);
89 res = get_pair(first, second, val);
93 res = SWIG_ConvertPtr(obj,(void**)&p,
94 swig::type_info<value_type>(),0);
95 if (SWIG_IsOK(res) && val) *val = p;
103 template <class T, class U >
104 struct traits_from<std::pair<T,U> > {
105 static VALUE _wrap_pair_second( VALUE self )
107 std::pair< typename swig::noconst_traits<T >::noconst_type,U>* p = NULL;
108 swig::asptr( self, &p );
109 return swig::from( p->second );
112 static VALUE _wrap_pair_second_eq( VALUE self, VALUE arg )
114 std::pair< typename swig::noconst_traits<T >::noconst_type,U>* p = NULL;
115 swig::asptr( self, &p );
116 return swig::from( p->second );
119 static VALUE from(const std::pair<T,U>& val) {
120 VALUE obj = rb_ary_new2(2);
121 RARRAY_PTR(obj)[0] = swig::from<
122 typename swig::noconst_traits<T >::noconst_type>(val.first);
123 RARRAY_PTR(obj)[1] = swig::from(val.second);
125 rb_define_singleton_method(obj, "second",
126 VALUEFUNC(_wrap_pair_second), 0 );
127 rb_define_singleton_method(obj, "second=",
128 VALUEFUNC(_wrap_pair_second_eq), 1 );
129 rb_obj_freeze(obj); // treat as immutable tuple
138 %typemap(in) std::pair* (int res) {
139 res = swig::asptr( $input, &$1 );
141 %argument_fail(res, "$1_type", $symname, $argnum);
145 %define %swig_pair_methods(pair...)
148 VALUE inspect() const
151 VALUE str = rb_str_new2( swig::type_name< pair >() );
152 str = rb_str_cat2( str, " (" );
153 tmp = swig::from( $self->first );
154 tmp = rb_obj_as_string( tmp );
155 str = rb_str_buf_append( str, tmp );
156 str = rb_str_cat2( str, "," );
157 tmp = swig::from( $self->second );
158 tmp = rb_obj_as_string( tmp );
159 str = rb_str_buf_append( str, tmp );
160 str = rb_str_cat2( str, ")" );
167 VALUE str = rb_str_new2( "(" );
168 tmp = swig::from( $self->first );
169 tmp = rb_obj_as_string( tmp );
170 str = rb_str_buf_append( str, tmp );
171 str = rb_str_cat2( str, "," );
172 tmp = swig::from( $self->second );
173 tmp = rb_obj_as_string( tmp );
174 str = rb_str_buf_append( str, tmp );
175 str = rb_str_cat2( str, ")" );
179 VALUE __getitem__( int index )
181 if (( index % 2 ) == 0 )
182 return swig::from( $self->first );
184 return swig::from( $self->second );
187 VALUE __setitem__( int index, VALUE obj )
190 if (( index % 2 ) == 0 )
192 res = swig::asval( obj, &($self->first) );
196 res = swig::asval(obj, &($self->second) );
199 rb_raise( rb_eArgError, "invalid item for " #pair );
207 %include <std/std_pair.i>