summaryrefslogtreecommitdiff
path: root/Lib/python/std_pair.i
blob: fa1f35dc48ef3890b86164b070fa53dde9325259 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
  Pairs
*/
%include <pycontainer.swg>

%fragment("StdPairTraits","header",
	  fragment="StdTraits",fragment="PyObject_var") {
  namespace swig {
    template <class T, class U >
    struct traits_asptr<std::pair<T,U> >  {
      static int asptr(PyObject *obj, std::pair<T,U> **val) {
	typedef std::pair<T,U> value_type;
	if (PySequence_Check(obj) && (PySequence_Size(obj) == 2)) {
	  swig::PyObject_var first = PySequence_GetItem(obj,0);
	  swig::PyObject_var second = PySequence_GetItem(obj,1);
	  T *pfirst = 0;
	  U *psecond = 0;
	  if (val) {
	    *val = SWIG_new(std::pair<T,U>);
	    pfirst = &((*val)->first);
	    psecond = &((*val)->second);
	  }	  
	  if ((swig::asval(first,pfirst) == SWIG_OK) && (swig::asval(second,psecond) == SWIG_OK)) {
	    return SWIG_NEWOBJ;
	  } else {
	    SWIG_delete(*val);
	  }
	} else {
	  value_type *p;
	  if (SWIG_ConvertPtr(obj,(void**)&p,
			      swig::type_info<value_type>(),0) != -1) {
	    if (val) *val = p;
	    return SWIG_OLDOBJ;
	  }
	}
	if (val) {
	  PyErr_Format(PyExc_TypeError, "a %s is expected", 
		       swig::type_name<value_type>());
	}
	return 0;
      }
    };

    template <class T, class U >
    struct traits_from<std::pair<T,U> >   {
      static PyObject *from(const std::pair<T,U>& val) {
	PyObject* obj = PyTuple_New(2);
	PyTuple_SetItem(obj,0,swig::from(val.first));
	PyTuple_SetItem(obj,1,swig::from(val.second));
	return obj;
      }
    };
  }
}

%define %swig_pair_methods(pair...)
%extend {      
%pythoncode {
def __len__(self):
  return 2
def __getitem__(self, index):
  if not (index % 2): 
    return self.first
  else:
    return self.second
def __setitem__(self, index, val):
  if not (index % 2): 
    self.first = val
  else:
    self.second = val
def __repr__(self):
  return str((self.first, self.second))
}
}
%enddef


%include <std/std_pair.i>