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.

134 lines
3.2 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_vector.i
  6. *
  7. * std::vector typemaps for LUA
  8. * ----------------------------------------------------------------------------- */
  9. %{
  10. #include <vector>
  11. %}
  12. %include <std_except.i> // the general exepctions
  13. /*
  14. A really cut down version of the vector class.
  15. Note: this does not match the true std::vector class
  16. but instead is an approximate, so that SWIG knows how to wrapper it.
  17. (Eg, all access is by value, not ref, as SWIG turns refs to pointers)
  18. And no support for iterators & insert/erase
  19. It would be useful to have a vector<->Lua table conversion routine
  20. */
  21. namespace std {
  22. template<class T>
  23. class vector {
  24. public:
  25. vector();
  26. vector(unsigned int);
  27. vector(const vector&);
  28. vector(unsigned int,T);
  29. unsigned int size() const;
  30. unsigned int max_size() const;
  31. bool empty() const;
  32. void clear();
  33. void push_back(T val);
  34. void pop_back();
  35. T front()const; // only read front & back
  36. T back()const; // not write to them
  37. // operator [] given later:
  38. %extend // this is a extra bit of SWIG code
  39. {
  40. // [] is replaced by __getitem__ & __setitem__
  41. // simply throws a string, which causes a lua error
  42. T __getitem__(unsigned int idx) throw (std::out_of_range)
  43. {
  44. if (idx>=self->size())
  45. throw std::out_of_range("in vector::__getitem__()");
  46. return (*self)[idx];
  47. }
  48. void __setitem__(unsigned int idx,T val) throw (std::out_of_range)
  49. {
  50. if (idx>=self->size())
  51. throw std::out_of_range("in vector::__setitem__()");
  52. (*self)[idx]=val;
  53. }
  54. };
  55. };
  56. }
  57. /*
  58. Vector<->LuaTable fns
  59. These look a bit like the array<->LuaTable fns
  60. but are templated, not %defined
  61. (you must have template support for STL)
  62. */
  63. /*
  64. %{
  65. // reads a table into a vector of numbers
  66. // lua numbers will be cast into the type required (rounding may occur)
  67. // return 0 if non numbers found in the table
  68. // returns new'ed ptr if ok
  69. template<class T>
  70. std::vector<T>* SWIG_read_number_vector(lua_State* L,int index)
  71. {
  72. int i=0;
  73. std::vector<T>* vec=new std::vector<T>();
  74. while(1)
  75. {
  76. lua_rawgeti(L,index,i+1);
  77. if (!lua_isnil(L,-1))
  78. {
  79. lua_pop(L,1);
  80. break; // finished
  81. }
  82. if (!lua_isnumber(L,-1))
  83. {
  84. lua_pop(L,1);
  85. delete vec;
  86. return 0; // error
  87. }
  88. vec->push_back((T)lua_tonumber(L,-1));
  89. lua_pop(L,1);
  90. ++i;
  91. }
  92. return vec; // ok
  93. }
  94. // writes a vector of numbers out as a lua table
  95. template<class T>
  96. int SWIG_write_number_vector(lua_State* L,std::vector<T> *vec)
  97. {
  98. lua_newtable(L);
  99. for(int i=0;i<vec->size();++i)
  100. {
  101. lua_pushnumber(L,(double)((*vec)[i]));
  102. lua_rawseti(L,-2,i+1);// -1 is the number, -2 is the table
  103. }
  104. }
  105. %}
  106. // then the typemaps
  107. %define SWIG_TYPEMAP_NUM_VECTOR(T)
  108. // in
  109. %typemap(in) std::vector<T> *INPUT
  110. %{ $1 = SWIG_read_number_vector<T>(L,$input);
  111. if (!$1) SWIG_fail;%}
  112. %typemap(freearg) std::vector<T> *INPUT
  113. %{ delete $1;%}
  114. // out
  115. %typemap(argout) std::vector<T> *OUTPUT
  116. %{ SWIG_write_number_vector(L,$1); SWIG_arg++; %}
  117. %enddef
  118. */