PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/rel-1-3-29/SWIG/Lib/perl5/std_map.i

#
Swig | 175 lines | 149 code | 18 blank | 8 comment | 0 complexity | bf3d67a7b52cf9413be17123f6d2c652 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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. map();
  24. map(const map<K,T> &);
  25. unsigned int size() const;
  26. bool empty() const;
  27. void clear();
  28. %extend {
  29. T& get(const K& key) throw (std::out_of_range) {
  30. std::map<K,T >::iterator i = self->find(key);
  31. if (i != self->end())
  32. return i->second;
  33. else
  34. throw std::out_of_range("key not found");
  35. }
  36. void set(const K& key, const T& x) {
  37. (*self)[key] = x;
  38. }
  39. void del(const K& key) throw (std::out_of_range) {
  40. std::map<K,T >::iterator i = self->find(key);
  41. if (i != self->end())
  42. self->erase(i);
  43. else
  44. throw std::out_of_range("key not found");
  45. }
  46. bool has_key(const K& key) {
  47. std::map<K,T >::iterator i = self->find(key);
  48. return i != self->end();
  49. }
  50. }
  51. };
  52. // specializations for built-ins
  53. %define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO)
  54. template<class T> class map<K,T> {
  55. // add typemaps here
  56. public:
  57. map();
  58. map(const map<K,T> &);
  59. unsigned int size() const;
  60. bool empty() const;
  61. void clear();
  62. %extend {
  63. T& get(K key) throw (std::out_of_range) {
  64. std::map<K,T >::iterator i = self->find(key);
  65. if (i != self->end())
  66. return i->second;
  67. else
  68. throw std::out_of_range("key not found");
  69. }
  70. void set(K key, const T& x) {
  71. (*self)[key] = x;
  72. }
  73. void del(K key) throw (std::out_of_range) {
  74. std::map<K,T >::iterator i = self->find(key);
  75. if (i != self->end())
  76. self->erase(i);
  77. else
  78. throw std::out_of_range("key not found");
  79. }
  80. bool has_key(K key) {
  81. std::map<K,T >::iterator i = self->find(key);
  82. return i != self->end();
  83. }
  84. }
  85. };
  86. %enddef
  87. %define specialize_std_map_on_value(T,CHECK,CONVERT_FROM,CONVERT_TO)
  88. template<class K> class map<K,T> {
  89. // add typemaps here
  90. public:
  91. map();
  92. map(const map<K,T> &);
  93. unsigned int size() const;
  94. bool empty() const;
  95. void clear();
  96. %extend {
  97. T get(const K& key) throw (std::out_of_range) {
  98. std::map<K,T >::iterator i = self->find(key);
  99. if (i != self->end())
  100. return i->second;
  101. else
  102. throw std::out_of_range("key not found");
  103. }
  104. void set(const K& key, T x) {
  105. (*self)[key] = x;
  106. }
  107. void del(const K& key) throw (std::out_of_range) {
  108. std::map<K,T >::iterator i = self->find(key);
  109. if (i != self->end())
  110. self->erase(i);
  111. else
  112. throw std::out_of_range("key not found");
  113. }
  114. bool has_key(const K& key) {
  115. std::map<K,T >::iterator i = self->find(key);
  116. return i != self->end();
  117. }
  118. }
  119. };
  120. %enddef
  121. %define specialize_std_map_on_both(K,CHECK_K,CONVERT_K_FROM,CONVERT_K_TO,
  122. T,CHECK_T,CONVERT_T_FROM,CONVERT_T_TO)
  123. template<> class map<K,T> {
  124. // add typemaps here
  125. public:
  126. map();
  127. map(const map<K,T> &);
  128. unsigned int size() const;
  129. bool empty() const;
  130. void clear();
  131. %extend {
  132. T get(K key) throw (std::out_of_range) {
  133. std::map<K,T >::iterator i = self->find(key);
  134. if (i != self->end())
  135. return i->second;
  136. else
  137. throw std::out_of_range("key not found");
  138. }
  139. void set(K key, T x) {
  140. (*self)[key] = x;
  141. }
  142. void del(K key) throw (std::out_of_range) {
  143. std::map<K,T >::iterator i = self->find(key);
  144. if (i != self->end())
  145. self->erase(i);
  146. else
  147. throw std::out_of_range("key not found");
  148. }
  149. bool has_key(K key) {
  150. std::map<K,T >::iterator i = self->find(key);
  151. return i != self->end();
  152. }
  153. }
  154. };
  155. %enddef
  156. // add specializations here
  157. }