Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.5 KiB

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