rubycontainer_extended.swg 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* -----------------------------------------------------------------------------
  2. * rubycontainer_extended.swg
  3. *
  4. * This file contains additional functions that make containers
  5. * behave closer to ruby primitive types.
  6. * However, some of these functions place some restrictions on
  7. * the underlying object inside of the container and the iterator
  8. * (that it has to have an == comparison function, that it has to have
  9. * an = assignment operator, etc).
  10. * ----------------------------------------------------------------------------- */
  11. /**
  12. * Macro used to add extend functions that require operator== in object.
  13. *
  14. * @param Container STL container
  15. * @param Type class inside container
  16. *
  17. */
  18. %define %swig_container_with_equal_operator( Container, Type )
  19. VALUE __delete__( const Type& val ) {
  20. VALUE r = Qnil;
  21. Container<Type >::iterator e = $self->end();
  22. Container<Type >::iterator i = std::remove( $self->begin(), e, val );
  23. // remove dangling elements now
  24. $self->erase( i, e );
  25. if ( i != e )
  26. r = swig::from< Type >( val );
  27. else if ( rb_block_given_p() )
  28. r = rb_yield(Qnil);
  29. return r;
  30. }
  31. %enddef // end of %swig_container_with_equal_operator
  32. /**
  33. * Macro used to add extend functions that require the assignment
  34. * operator (ie. = ) of contained class
  35. *
  36. * @param Container STL container
  37. * @param Type class inside container
  38. *
  39. */
  40. %define %swig_container_with_assignment( Container, Type )
  41. //
  42. // map! -- the equivalent of std::transform
  43. //
  44. Container< Type >* map_bang() {
  45. if ( !rb_block_given_p() )
  46. rb_raise( rb_eArgError, "No block given" );
  47. VALUE r = Qnil;
  48. Container< Type >::iterator i = $self->begin();
  49. Container< Type >::iterator e = $self->end();
  50. try {
  51. for ( ; i != e; ++i )
  52. {
  53. r = swig::from< Type >( *i );
  54. r = rb_yield( r );
  55. *i = swig::as< Type >( r );
  56. }
  57. }
  58. catch ( const std::invalid_argument& )
  59. {
  60. rb_raise(rb_eTypeError,
  61. "Yield block did not return a valid element for " "Container");
  62. }
  63. return $self;
  64. }
  65. %enddef // end of %swig_container_with_assignment
  66. /**
  67. * Macro used to add all extended functions to a container
  68. *
  69. * @param Container STL container
  70. * @param Type class inside container
  71. *
  72. */
  73. %define %swig_container_extend( Container, Type )
  74. %extend Container< Type > {
  75. %swig_container_with_assignment( %arg(Container), Type );
  76. %swig_container_with_equal_operator( %arg(Container), Type );
  77. }
  78. %enddef
  79. /**
  80. * Private macro used to add all extended functions to C/C++
  81. * primitive types
  82. *
  83. * @param Container an STL container, like std::vector (with no class template)
  84. *
  85. */
  86. %define %__swig_container_extend_primtypes( Container )
  87. %swig_container_extend( %arg( Container ), bool );
  88. %swig_container_extend( %arg( Container ), char );
  89. %swig_container_extend( %arg( Container ), short );
  90. %swig_container_extend( %arg( Container ), int );
  91. %swig_container_extend( %arg( Container ), unsigned short );
  92. %swig_container_extend( %arg( Container ), unsigned int );
  93. %swig_container_extend( %arg( Container ), float );
  94. %swig_container_extend( %arg( Container ), double );
  95. %swig_container_extend( %arg( Container ), std::complex );
  96. %swig_container_extend( %arg( Container ), std::string );
  97. %swig_container_extend( %arg( Container ), swig::GC_VALUE );
  98. %enddef
  99. %__swig_container_extend_primtypes( std::vector );
  100. %__swig_container_extend_primtypes( std::deque );
  101. %__swig_container_extend_primtypes( std::list );