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.

216 lines
5.8 KiB

  1. /*
  2. Sets
  3. */
  4. %fragment("StdSetTraits","header",fragment="StdSequenceTraits")
  5. %{
  6. namespace swig {
  7. template <class RubySeq, class T>
  8. inline void
  9. assign(const RubySeq& rubyseq, std::set<T>* seq) {
  10. #ifdef SWIG_STD_NOINSERT_TEMPLATE_STL
  11. typedef typename RubySeq::value_type value_type;
  12. typename RubySeq::const_iterator it = rubyseq.begin();
  13. for (;it != rubyseq.end(); ++it) {
  14. seq->insert(seq->end(),(value_type)(*it));
  15. }
  16. #else
  17. seq->insert(rubyseq.begin(), rubyseq.end());
  18. #endif
  19. }
  20. template <class T>
  21. struct traits_asptr<std::set<T> > {
  22. static int asptr(VALUE obj, std::set<T> **s) {
  23. return traits_asptr_stdseq<std::set<T> >::asptr(obj, s);
  24. }
  25. };
  26. template <class T>
  27. struct traits_from<std::set<T> > {
  28. static VALUE from(const std::set<T>& vec) {
  29. return traits_from_stdseq<std::set<T> >::from(vec);
  30. }
  31. };
  32. /**
  33. * Set Iterator class for an iterator with no end() boundaries.
  34. *
  35. */
  36. template<typename InOutIterator,
  37. typename ValueType = typename std::iterator_traits<InOutIterator>::value_type,
  38. typename FromOper = from_oper<ValueType>,
  39. typename AsvalOper = asval_oper<ValueType> >
  40. class SetIteratorOpen_T : public Iterator_T<InOutIterator>
  41. {
  42. public:
  43. FromOper from;
  44. AsvalOper asval;
  45. typedef InOutIterator nonconst_iter;
  46. typedef ValueType value_type;
  47. typedef Iterator_T<nonconst_iter> base;
  48. typedef SetIteratorOpen_T<InOutIterator, ValueType, FromOper, AsvalOper> self_type;
  49. public:
  50. SetIteratorOpen_T(nonconst_iter curr, VALUE seq = Qnil)
  51. : Iterator_T<InOutIterator>(curr, seq)
  52. {
  53. }
  54. virtual VALUE value() const {
  55. return from(static_cast<const value_type&>(*(base::current)));
  56. }
  57. // no setValue allowed
  58. Iterator *dup() const
  59. {
  60. return new self_type(*this);
  61. }
  62. };
  63. /**
  64. * Set Iterator class for a iterator where begin() and end() boundaries
  65. are known.
  66. *
  67. */
  68. template<typename InOutIterator,
  69. typename ValueType = typename std::iterator_traits<InOutIterator>::value_type,
  70. typename FromOper = from_oper<ValueType>,
  71. typename AsvalOper = asval_oper<ValueType> >
  72. class SetIteratorClosed_T : public Iterator_T<InOutIterator>
  73. {
  74. public:
  75. FromOper from;
  76. AsvalOper asval;
  77. typedef InOutIterator nonconst_iter;
  78. typedef ValueType value_type;
  79. typedef Iterator_T<nonconst_iter> base;
  80. typedef SetIteratorClosed_T<InOutIterator, ValueType, FromOper, AsvalOper> self_type;
  81. protected:
  82. virtual Iterator* advance(ptrdiff_t n)
  83. {
  84. std::advance( base::current, n );
  85. if ( base::current == end )
  86. throw stop_iteration();
  87. return this;
  88. }
  89. public:
  90. SetIteratorClosed_T(nonconst_iter curr, nonconst_iter first,
  91. nonconst_iter last, VALUE seq = Qnil)
  92. : Iterator_T<InOutIterator>(curr, seq), begin(first), end(last)
  93. {
  94. }
  95. virtual VALUE value() const {
  96. if (base::current == end) {
  97. throw stop_iteration();
  98. } else {
  99. return from(static_cast<const value_type&>(*(base::current)));
  100. }
  101. }
  102. // no setValue allowed
  103. Iterator *dup() const
  104. {
  105. return new self_type(*this);
  106. }
  107. private:
  108. nonconst_iter begin;
  109. nonconst_iter end;
  110. };
  111. // Template specialization to construct a closed iterator for sets
  112. // this turns a nonconst iterator into a const one for ruby to avoid
  113. // allowing the user to change the value
  114. template< typename InOutIter >
  115. inline Iterator*
  116. make_set_nonconst_iterator(const InOutIter& current,
  117. const InOutIter& begin,
  118. const InOutIter& end,
  119. VALUE seq = Qnil)
  120. {
  121. return new SetIteratorClosed_T< InOutIter >(current,
  122. begin, end, seq);
  123. }
  124. // Template specialization to construct an open iterator for sets
  125. // this turns a nonconst iterator into a const one for ruby to avoid
  126. // allowing the user to change the value
  127. template< typename InOutIter >
  128. inline Iterator*
  129. make_set_nonconst_iterator(const InOutIter& current,
  130. VALUE seq = Qnil)
  131. {
  132. return new SetIteratorOpen_T< InOutIter >(current, seq);
  133. }
  134. }
  135. %}
  136. %define %swig_set_methods(set...)
  137. %swig_sequence_methods_common(%arg(set));
  138. %fragment("RubyPairBoolOutputIterator","header",fragment=SWIG_From_frag(bool),fragment="RubySequence_Cont") {}
  139. // Redefine std::set iterator/reverse_iterator typemap
  140. %typemap(out,noblock=1) iterator, reverse_iterator {
  141. $result = SWIG_NewPointerObj(swig::make_set_nonconst_iterator(%static_cast($1,const $type &),
  142. self),
  143. swig::Iterator::descriptor(),SWIG_POINTER_OWN);
  144. }
  145. // Redefine std::set std::pair<iterator, bool> typemap
  146. %typemap(out,noblock=1,fragment="RubyPairBoolOutputIterator")
  147. std::pair<iterator, bool> {
  148. $result = rb_ary_new2(2);
  149. RARRAY_PTR($result)[0] = SWIG_NewPointerObj(swig::make_set_nonconst_iterator(%static_cast($1,$type &).first),
  150. swig::Iterator::descriptor(),SWIG_POINTER_OWN);
  151. RARRAY_PTR($result)[1] = SWIG_From(bool)(%static_cast($1,const $type &).second);
  152. RARRAY_LEN($result) = 2;
  153. }
  154. %extend {
  155. %alias push "<<";
  156. value_type push(const value_type& x) {
  157. self->insert(x);
  158. return x;
  159. }
  160. bool __contains__(const value_type& x) {
  161. return self->find(x) != self->end();
  162. }
  163. value_type __getitem__(difference_type i) const throw (std::out_of_range) {
  164. return *(swig::cgetpos(self, i));
  165. }
  166. };
  167. %enddef
  168. %mixin std::set "Enumerable";
  169. %rename("delete") std::set::__delete__;
  170. %rename("reject!") std::set::reject_bang;
  171. %rename("map!") std::set::map_bang;
  172. %rename("empty?") std::set::empty;
  173. %rename("include?" ) std::set::__contains__ const;
  174. %rename("has_key?" ) std::set::has_key const;
  175. %alias std::set::push "<<";
  176. %include <std/std_set.i>