Home | History | Annotate | Download | only in std
      1 %include <std/std_except.i>
      2 
      3 //
      4 // Use the following macro with modern STL implementations
      5 //
      6 //#define SWIG_STD_MODERN_STL
      7 //
      8 // Use this to deactive the previous definition, when using gcc-2.95
      9 // or similar old compilers.
     10 //
     11 //#define SWIG_STD_NOMODERN_STL
     12 
     13 // Here, we identify compilers we know have problems with STL.
     14 %{
     15 #if defined(__GNUC__)
     16 #  if __GNUC__ == 2 && __GNUC_MINOR <= 96
     17 #     define SWIG_STD_NOMODERN_STL
     18 #  endif
     19 #endif
     20 %}
     21 
     22 //
     23 // Common code for supporting the C++ std namespace
     24 //
     25 
     26 %{
     27 #include <string>
     28 #include <stdexcept>
     29 #include <stddef.h>
     30 %}
     31 
     32 
     33 %fragment("StdIteratorTraits","header",fragment="<stddef.h>") %{
     34 #if defined(__SUNPRO_CC) && defined(_RWSTD_VER)
     35 #  if !defined(SWIG_NO_STD_NOITERATOR_TRAITS_STL)
     36 #    define SWIG_STD_NOITERATOR_TRAITS_STL
     37 #  endif
     38 #endif
     39 
     40 #if !defined(SWIG_STD_NOITERATOR_TRAITS_STL)
     41 #include <iterator>
     42 #else
     43 namespace std {
     44   template <class Iterator>
     45   struct iterator_traits {
     46     typedef ptrdiff_t difference_type;
     47     typedef typename Iterator::value_type value_type;
     48   };
     49 
     50   template <class Iterator, class Category,class T, class Reference, class Pointer, class Distance>
     51   struct iterator_traits<__reverse_bi_iterator<Iterator,Category,T,Reference,Pointer,Distance> > {
     52     typedef Distance difference_type;
     53     typedef T value_type;
     54   };
     55 
     56   template <class T>
     57   struct iterator_traits<T*> {
     58     typedef T value_type;
     59     typedef ptrdiff_t difference_type;
     60   };
     61 
     62   template<typename _InputIterator>
     63   inline typename iterator_traits<_InputIterator>::difference_type
     64   distance(_InputIterator __first, _InputIterator __last)
     65   {
     66     typename iterator_traits<_InputIterator>::difference_type __n = 0;
     67     while (__first != __last) {
     68       ++__first; ++__n;
     69     }
     70     return __n;
     71   }
     72 }
     73 #endif
     74 %}
     75 
     76 %fragment("StdTraitsCommon","header") %{
     77 namespace swig {
     78   template <class Type>
     79   struct noconst_traits {
     80     typedef Type noconst_type;
     81   };
     82 
     83   template <class Type>
     84   struct noconst_traits<const Type> {
     85     typedef Type noconst_type;
     86   };
     87 
     88   /*
     89     type categories
     90   */
     91   struct pointer_category { };
     92   struct value_category { };
     93 
     94   /*
     95     General traits that provides type_name and type_info
     96   */
     97   template <class Type> struct traits { };
     98 
     99   template <class Type>
    100   inline const char* type_name() {
    101     return traits<typename noconst_traits<Type >::noconst_type >::type_name();
    102   }
    103 
    104   template <class Type>
    105   struct traits_info {
    106     static swig_type_info *type_query(std::string name) {
    107       name += " *";
    108       return SWIG_TypeQuery(name.c_str());
    109     }
    110     static swig_type_info *type_info() {
    111       static swig_type_info *info = type_query(type_name<Type>());
    112       return info;
    113     }
    114   };
    115 
    116   template <class Type>
    117   inline swig_type_info *type_info() {
    118     return traits_info<Type>::type_info();
    119   }
    120 
    121   /*
    122     Partial specialization for pointers
    123   */
    124   template <class Type> struct traits <Type *> {
    125     typedef pointer_category category;
    126     static std::string make_ptr_name(const char* name) {
    127       std::string ptrname = name;
    128       ptrname += " *";
    129       return ptrname;
    130     }
    131     static const char* type_name() {
    132       static std::string name = make_ptr_name(swig::type_name<Type>());
    133       return name.c_str();
    134     }
    135   };
    136 
    137   template <class Type, class Category>
    138   struct traits_as { };
    139 
    140   template <class Type, class Category>
    141   struct traits_check { };
    142 
    143 }
    144 %}
    145 
    146 /*
    147   Generate the traits for a swigtype
    148 */
    149 
    150 %define %traits_swigtype(Type...)
    151 %fragment(SWIG_Traits_frag(Type),"header",fragment="StdTraits") {
    152   namespace swig {
    153     template <>  struct traits<Type > {
    154       typedef pointer_category category;
    155       static const char* type_name() { return  #Type; }
    156     };
    157   }
    158 }
    159 %enddef
    160 
    161 
    162 
    163 /*
    164   Generate the typemaps for a class that has 'value' traits
    165 */
    166 
    167 %define %typemap_traits(Code,Type...)
    168   %typemaps_asvalfrom(%arg(Code),
    169 		     %arg(swig::asval<Type >),
    170 		     %arg(swig::from),
    171 		     %arg(SWIG_Traits_frag(Type)),
    172 		     %arg(SWIG_Traits_frag(Type)),
    173 		     Type);
    174 %enddef
    175 
    176 /*
    177   Generate the typemaps for a class that behaves more like a 'pointer' or
    178   plain wrapped Swigtype.
    179 */
    180 
    181 %define %typemap_traits_ptr(Code,Type...)
    182   %typemaps_asptrfrom(%arg(Code),
    183 		     %arg(swig::asptr),
    184 		     %arg(swig::from),
    185 		     %arg(SWIG_Traits_frag(Type)),
    186 		     %arg(SWIG_Traits_frag(Type)),
    187 		     Type);
    188 %enddef
    189 
    190 
    191 /*
    192   Equality methods
    193 */
    194 %define %std_equal_methods(Type...)
    195 %extend Type {
    196   bool operator == (const Type& v) {
    197     return *self == v;
    198   }
    199 
    200   bool operator != (const Type& v) {
    201     return *self != v;
    202   }
    203 }
    204 
    205 %enddef
    206 
    207 /*
    208   Order methods
    209 */
    210 
    211 %define %std_order_methods(Type...)
    212 %extend Type {
    213   bool operator > (const Type& v) {
    214     return *self > v;
    215   }
    216 
    217   bool operator < (const Type& v) {
    218     return *self < v;
    219   }
    220 
    221   bool operator >= (const Type& v) {
    222     return *self >= v;
    223   }
    224 
    225   bool operator <= (const Type& v) {
    226     return *self <= v;
    227   }
    228 }
    229 %enddef
    230 
    231 /*
    232   Comparison methods
    233 */
    234 
    235 %define %std_comp_methods(Type...)
    236 %std_equal_methods(Type )
    237 %std_order_methods(Type )
    238 %enddef
    239 
    240