Home | History | Annotate | Download | only in octave
      1 // Maps
      2 
      3 %include <octcontainer.swg>
      4 
      5 %fragment("StdMapCommonTraits","header",fragment="StdSequenceTraits")
      6 {
      7   namespace swig {
      8     template <class ValueType>
      9     struct from_key_oper
     10     {
     11       typedef const ValueType& argument_type;
     12       typedef octave_value result_type;
     13       result_type operator()(argument_type v) const
     14       {
     15 	return swig::from(v.first);
     16       }
     17     };
     18 
     19     template <class ValueType>
     20     struct from_value_oper
     21     {
     22       typedef const ValueType& argument_type;
     23       typedef octave_value result_type;
     24       result_type operator()(argument_type v) const
     25       {
     26 	return swig::from(v.second);
     27       }
     28     };
     29 
     30     template<class OutIterator, class FromOper, class ValueType = typename OutIterator::value_type>
     31     struct OctMapIterator_T : OctSwigIteratorClosed_T<OutIterator, ValueType, FromOper>
     32     {
     33       OctMapIterator_T(OutIterator curr, OutIterator first, OutIterator last, octave_value seq)
     34 	: OctSwigIteratorClosed_T<OutIterator,ValueType,FromOper>(curr, first, last, seq)
     35       {
     36       }
     37     };
     38 
     39 
     40     template<class OutIterator,
     41 	     class FromOper = from_key_oper<typename OutIterator::value_type> >
     42     struct OctMapKeyIterator_T : OctMapIterator_T<OutIterator, FromOper>
     43     {
     44       OctMapKeyIterator_T(OutIterator curr, OutIterator first, OutIterator last, octave_value seq)
     45 	: OctMapIterator_T<OutIterator, FromOper>(curr, first, last, seq)
     46       {
     47       }
     48     };
     49 
     50     template<typename OutIter>
     51     inline OctSwigIterator*
     52     make_output_key_iterator(const OutIter& current, const OutIter& begin, const OutIter& end, octave_value seq = octave_value())
     53     {
     54       return new OctMapKeyIterator_T<OutIter>(current, begin, end, seq);
     55     }
     56 
     57     template<class OutIterator,
     58 	     class FromOper = from_value_oper<typename OutIterator::value_type> >
     59     struct OctMapValueIterator_T : OctMapIterator_T<OutIterator, FromOper>
     60     {
     61       OctMapValueIterator_T(OutIterator curr, OutIterator first, OutIterator last, octave_value seq)
     62 	: OctMapIterator_T<OutIterator, FromOper>(curr, first, last, seq)
     63       {
     64       }
     65     };
     66 
     67 
     68     template<typename OutIter>
     69     inline OctSwigIterator*
     70     make_output_value_iterator(const OutIter& current, const OutIter& begin, const OutIter& end, octave_value seq = 0)
     71     {
     72       return new OctMapValueIterator_T<OutIter>(current, begin, end, seq);
     73     }
     74   }
     75 }
     76 
     77 %fragment("StdMapTraits","header",fragment="StdMapCommonTraits")
     78 {
     79   namespace swig {
     80     template <class OctSeq, class K, class T >
     81     inline void
     82     assign(const OctSeq& octseq, std::map<K,T > *map) {
     83       typedef typename std::map<K,T>::value_type value_type;
     84       typename OctSeq::const_iterator it = octseq.begin();
     85       for (;it != octseq.end(); ++it) {
     86 	map->insert(value_type(it->first, it->second));
     87       }
     88     }
     89 
     90     template <class K, class T>
     91     struct traits_asptr<std::map<K,T> >  {
     92       typedef std::map<K,T> map_type;
     93       static int asptr(octave_value obj, map_type **val) {
     94 	/*
     95 	int res = SWIG_ERROR;
     96 	if (PyDict_Check(obj)) {
     97 	  SwigVar_PyObject items = PyObject_CallMethod(obj,(char *)"items",NULL);
     98 	  res = traits_asptr_stdseq<std::map<K,T>, std::pair<K, T> >::asptr(items, val);
     99 	} else {
    100 	  map_type *p;
    101 	  res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info<map_type>(),0);
    102 	  if (SWIG_IsOK(res) && val)  *val = p;
    103 	}
    104 	return res;
    105 	*/
    106 	return SWIG_ERROR;
    107       }
    108     };
    109 
    110     template <class K, class T >
    111     struct traits_from<std::map<K,T> >  {
    112       typedef std::map<K,T> map_type;
    113       typedef typename map_type::const_iterator const_iterator;
    114       typedef typename map_type::size_type size_type;
    115 
    116       static octave_value from(const map_type& map) {
    117 	/*
    118 	swig_type_info *desc = swig::type_info<map_type>();
    119 	if (desc && desc->clientdata) {
    120 	  return SWIG_NewPointerObj(new map_type(map), desc, SWIG_POINTER_OWN);
    121 	} else {
    122 	  size_type size = map.size();
    123 	  int pysize = (size <= (size_type) INT_MAX) ? (int) size : -1;
    124 	  if (pysize < 0) {
    125 	    SWIG_PYTHON_THREAD_BEGIN_BLOCK;
    126 	    PyErr_SetString(PyExc_OverflowError,
    127 			    "map size not valid in python");
    128 	    SWIG_PYTHON_THREAD_END_BLOCK;
    129 	    return NULL;
    130 	  }
    131 	  PyObject *obj = PyDict_New();
    132 	  for (const_iterator i= map.begin(); i!= map.end(); ++i) {
    133 	    swig::SwigVar_PyObject key = swig::from(i->first);
    134 	    swig::SwigVar_PyObject val = swig::from(i->second);
    135 	    PyDict_SetItem(obj, key, val);
    136 	  }
    137 	  return obj;
    138 	}
    139 	*/
    140 	return octave_value();
    141       }
    142     };
    143   }
    144 }
    145 
    146 %define %swig_map_common(Map...)
    147   %swig_sequence_iterator(Map);
    148   %swig_container_methods(Map);
    149 %enddef
    150 
    151 %define %swig_map_methods(Map...)
    152      %swig_map_common(Map)
    153 %enddef
    154 
    155 
    156 %include <std/std_map.i>
    157