PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Lib/std/std_common.i

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