PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/octave/std_pair.i

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