Home | History | Annotate | Download | only in octave
      1 // Pairs
      2 
      3 %include <octstdcommon.swg>
      4 
      5 //#define SWIG_STD_PAIR_ASVAL
      6 
      7 %fragment("StdPairTraits","header",fragment="StdTraits") {
      8   namespace swig {
      9 #ifdef SWIG_STD_PAIR_ASVAL
     10     template <class T, class U >
     11     struct traits_asval<std::pair<T,U> >  {
     12       typedef std::pair<T,U> value_type;
     13 
     14       static int get_pair(const octave_value& first, octave_value second,
     15 			  std::pair<T,U> *val)
     16       {
     17 	if (val) {
     18 	  T *pfirst = &(val->first);
     19 	  int res1 = swig::asval(first, pfirst);
     20 	  if (!SWIG_IsOK(res1))
     21 	    return res1;
     22 	  U *psecond = &(val->second);
     23 	  int res2 = swig::asval(second, psecond);
     24 	  if (!SWIG_IsOK(res2))
     25 	    return res2;
     26 	  return res1 > res2 ? res1 : res2;
     27 	} else {
     28 	  T *pfirst = 0;
     29 	  int res1 = swig::asval(first, pfirst);
     30 	  if (!SWIG_IsOK(res1))
     31 	    return res1;
     32 	  U *psecond = 0;
     33 	  int res2 = swig::asval((PyObject*)second, psecond);
     34 	  if (!SWIG_IsOK(res2))
     35 	    return res2;
     36 	  return res1 > res2 ? res1 : res2;
     37 	}
     38       }
     39 
     40       static int asval(const octave_value& obj, std::pair<T,U> *val) {
     41 	if (obj.is_cell()) {
     42 	  Cell c=obj.cell_value();
     43 	  if (c.numel()<2) {
     44 	    error("pair from Cell array requires at least two elements");
     45 	    return SWIG_ERROR;
     46 	  }
     47 	  return get_pair(c(0),c(1),val);
     48 	} else {
     49 	  value_type *p;
     50 	  int res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info<value_type>(),0);
     51 	  if (SWIG_IsOK(res) && val)
     52 	    *val = *p;
     53 	  return res;
     54 	}
     55 	return SWIG_ERROR;
     56       }
     57     };
     58 
     59 #else
     60     template <class T, class U >
     61     struct traits_asptr<std::pair<T,U> >  {
     62       typedef std::pair<T,U> value_type;
     63 
     64       static int get_pair(const octave_value& first, octave_value second,
     65 			  std::pair<T,U> **val)
     66       {
     67 	if (val) {
     68 	  value_type *vp = %new_instance(std::pair<T,U>);
     69 	  T *pfirst = &(vp->first);
     70 	  int res1 = swig::asval(first, pfirst);
     71 	  if (!SWIG_IsOK(res1))
     72 	    return res1;
     73 	  U *psecond = &(vp->second);
     74 	  int res2 = swig::asval(second, psecond);
     75 	  if (!SWIG_IsOK(res2))
     76 	    return res2;
     77 	  *val = vp;
     78 	  return SWIG_AddNewMask(res1 > res2 ? res1 : res2);
     79 	} else {
     80 	  T *pfirst = 0;
     81 	  int res1 = swig::asval(first, pfirst);
     82 	  if (!SWIG_IsOK(res1))
     83 	    return res1;
     84 	  U *psecond = 0;
     85 	  int res2 = swig::asval(second, psecond);
     86 	  if (!SWIG_IsOK(res2))
     87 	    return res2;
     88 	  return res1 > res2 ? res1 : res2;
     89 	}
     90 	return SWIG_ERROR;
     91       }
     92 
     93       static int asptr(const octave_value& obj, std::pair<T,U> **val) {
     94 	if (obj.is_cell()) {
     95 	  Cell c=obj.cell_value();
     96 	  if (c.numel()<2) {
     97 	    error("pair from Cell array requires at least two elements");
     98 	    return SWIG_ERROR;
     99 	  }
    100 	  return get_pair(c(0),c(1),val);
    101 	} else {
    102 	  value_type *p;
    103 	  int res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info<value_type>(),0);
    104 	  if (SWIG_IsOK(res) && val)
    105 	    *val = p;
    106 	  return res;
    107 	}
    108 	return SWIG_ERROR;
    109       }
    110     };
    111 
    112 #endif
    113     template <class T, class U >
    114     struct traits_from<std::pair<T,U> >   {
    115       static octave_value from(const std::pair<T,U>& val) {
    116 	Cell c(1,2);
    117 	c(0)=swig::from(val.first);
    118 	c(1)=swig::from(val.second);
    119 	return c;
    120       }
    121     };
    122   }
    123 }
    124 
    125 %define %swig_pair_methods(pair...)
    126 %enddef
    127 
    128 %include <std/std_pair.i>
    129 
    130