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.

179 lines
5.4 KiB

  1. /* -----------------------------------------------------------------------------
  2. * See the LICENSE file for information on copyright, usage and redistribution
  3. * of SWIG, and the README file for authors - http://www.swig.org/release.html.
  4. *
  5. * std_map.i
  6. *
  7. * SWIG typemaps for std::map
  8. * ----------------------------------------------------------------------------- */
  9. %include <std_common.i>
  10. // ------------------------------------------------------------------------
  11. // std::map
  12. // ------------------------------------------------------------------------
  13. %{
  14. #include <map>
  15. #include <algorithm>
  16. #include <stdexcept>
  17. %}
  18. // exported class
  19. namespace std {
  20. template<class K, class T> class map {
  21. // add typemaps here
  22. public:
  23. typedef size_t size_type;
  24. typedef ptrdiff_t difference_type;
  25. typedef K key_type;
  26. typedef T mapped_type;
  27. map();
  28. map(const map<K,T> &);
  29. unsigned int size() const;
  30. bool empty() const;
  31. void clear();
  32. %extend {
  33. const T& get(const K& key) throw (std::out_of_range) {
  34. std::map<K,T >::iterator i = self->find(key);
  35. if (i != self->end())
  36. return i->second;
  37. else
  38. throw std::out_of_range("key not found");
  39. }
  40. void set(const K& key, const T& x) {
  41. (*self)[key] = x;
  42. }
  43. void del(const K& key) throw (std::out_of_range) {
  44. std::map<K,T >::iterator i = self->find(key);
  45. if (i != self->end())
  46. self->erase(i);
  47. else
  48. throw std::out_of_range("key not found");
  49. }
  50. bool has_key(const K& key) {
  51. std::map<K,T >::iterator i = self->find(key);
  52. return i != self->end();
  53. }
  54. }
  55. };
  56. // specializations for built-ins
  57. %define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO)
  58. template<class T> class map<K,T> {
  59. // add typemaps here
  60. public:
  61. map();
  62. map(const map<K,T> &);
  63. unsigned int size() const;
  64. bool empty() const;
  65. void clear();
  66. %extend {
  67. T& get(K key) throw (std::out_of_range) {
  68. std::map<K,T >::iterator i = self->find(key);
  69. if (i != self->end())
  70. return i->second;
  71. else
  72. throw std::out_of_range("key not found");
  73. }
  74. void set(K key, const T& x) {
  75. (*self)[key] = x;
  76. }
  77. void del(K key) throw (std::out_of_range) {
  78. std::map<K,T >::iterator i = self->find(key);
  79. if (i != self->end())
  80. self->erase(i);
  81. else
  82. throw std::out_of_range("key not found");
  83. }
  84. bool has_key(K key) {
  85. std::map<K,T >::iterator i = self->find(key);
  86. return i != self->end();
  87. }
  88. }
  89. };
  90. %enddef
  91. %define specialize_std_map_on_value(T,CHECK,CONVERT_FROM,CONVERT_TO)
  92. template<class K> class map<K,T> {
  93. // add typemaps here
  94. public:
  95. map();
  96. map(const map<K,T> &);
  97. unsigned int size() const;
  98. bool empty() const;
  99. void clear();
  100. %extend {
  101. T get(const K& key) throw (std::out_of_range) {
  102. std::map<K,T >::iterator i = self->find(key);
  103. if (i != self->end())
  104. return i->second;
  105. else
  106. throw std::out_of_range("key not found");
  107. }
  108. void set(const K& key, T x) {
  109. (*self)[key] = x;
  110. }
  111. void del(const K& key) throw (std::out_of_range) {
  112. std::map<K,T >::iterator i = self->find(key);
  113. if (i != self->end())
  114. self->erase(i);
  115. else
  116. throw std::out_of_range("key not found");
  117. }
  118. bool has_key(const K& key) {
  119. std::map<K,T >::iterator i = self->find(key);
  120. return i != self->end();
  121. }
  122. }
  123. };
  124. %enddef
  125. %define specialize_std_map_on_both(K,CHECK_K,CONVERT_K_FROM,CONVERT_K_TO,
  126. T,CHECK_T,CONVERT_T_FROM,CONVERT_T_TO)
  127. template<> class map<K,T> {
  128. // add typemaps here
  129. public:
  130. map();
  131. map(const map<K,T> &);
  132. unsigned int size() const;
  133. bool empty() const;
  134. void clear();
  135. %extend {
  136. T get(K key) throw (std::out_of_range) {
  137. std::map<K,T >::iterator i = self->find(key);
  138. if (i != self->end())
  139. return i->second;
  140. else
  141. throw std::out_of_range("key not found");
  142. }
  143. void set(K key, T x) {
  144. (*self)[key] = x;
  145. }
  146. void del(K key) throw (std::out_of_range) {
  147. std::map<K,T >::iterator i = self->find(key);
  148. if (i != self->end())
  149. self->erase(i);
  150. else
  151. throw std::out_of_range("key not found");
  152. }
  153. bool has_key(K key) {
  154. std::map<K,T >::iterator i = self->find(key);
  155. return i != self->end();
  156. }
  157. }
  158. };
  159. %enddef
  160. // add specializations here
  161. }