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.

155 lines
4.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_string.i
  6. *
  7. * std::string typemaps for LUA
  8. * ----------------------------------------------------------------------------- */
  9. %{
  10. #include <string>
  11. %}
  12. /*
  13. Only std::string and const std::string& are typemaped
  14. they are converted to the Lua strings automatically
  15. std::string& and std::string* are not
  16. they must be explicitly managed (see below)
  17. eg.
  18. std::string test_value(std::string x) {
  19. return x;
  20. }
  21. can be used as
  22. s="hello world"
  23. s2=test_value(s)
  24. assert(s==s2)
  25. */
  26. %naturalvar std::string;
  27. /*
  28. Bug report #1526022 by neomantra
  29. Lua strings and std::string can contain embeded zero's
  30. Therefore a standard out typemap should not be:
  31. lua_pushstring(L,$1.c_str());
  32. but
  33. lua_pushlstring(L,$1.data(),$1.size());
  34. Similarly for getting the string
  35. $1 = (char*)lua_tostring(L, $input);
  36. becomes
  37. $1.assign(lua_tostring(L,$input),lua_strlen(L,$input));
  38. Not using: lua_tolstring() as this is only found in Lua 5.1 & not 5.0.2
  39. */
  40. %typemap(in,checkfn="lua_isstring") std::string
  41. %{$1.assign(lua_tostring(L,$input),lua_strlen(L,$input));%}
  42. %typemap(out) std::string
  43. %{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_arg++;%}
  44. %typemap(in,checkfn="lua_isstring") const std::string& (std::string temp)
  45. %{temp.assign(lua_tostring(L,$input),lua_strlen(L,$input)); $1=&temp;%}
  46. %typemap(out) const std::string&
  47. %{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_arg++;%}
  48. // for throwing of any kind of string, string ref's and string pointers
  49. // we convert all to lua strings
  50. %typemap(throws) std::string,const std::string&
  51. %{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_fail;%}
  52. %typemap(throws) std::string*,const std::string*
  53. %{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_fail;%}
  54. // and the typechecks
  55. %typecheck(SWIG_TYPECHECK_STRING) std::string,const std::string& {
  56. $1 = lua_isstring(L,$input);
  57. }
  58. /*
  59. std::string& can be wrappered, but you must inform SWIG if it is in or out
  60. eg:
  61. void fn(std::string& str);
  62. Is this an in/out/inout value?
  63. Therefore you need the usual
  64. %apply (std::string& INOUT) {(std::string& str)};
  65. or
  66. %apply std::string& INOUT {std::string& str};
  67. typemaps to tell SWIG what to do.
  68. */
  69. %typemap(in) std::string &INPUT=const std::string &;
  70. %typemap(in, numinputs=0) std::string &OUTPUT (std::string temp)
  71. %{ $1 = &temp; %}
  72. %typemap(argout) std::string &OUTPUT
  73. %{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_arg++;%}
  74. %typemap(in) std::string &INOUT =const std::string &;
  75. %typemap(argout) std::string &INOUT = std::string &OUTPUT;
  76. /*
  77. For const std::string* and std::string* is not clear
  78. is this a pointer or an array?
  79. Therefore just leaving it as is
  80. (there is some rough code below which could be used if needed
  81. // SWIG wraps const ref's as pointer
  82. // typemaps to deal with this and const ptrs
  83. %typemap(in,checkfn="lua_isstring")
  84. const std::string& INPUT(std::string temp),
  85. const std::string* INPUT(std::string temp)
  86. %{temp=(char*)lua_tostring(L, $input); $1=&temp;%}
  87. %typemap(out) const std::string&, const std::string*
  88. %{ lua_pushstring(L,$1->c_str()); SWIG_arg++;%}
  89. // the non-const pointer version
  90. %typemap(in) std::string *INPUT=const std::string *INPUT;
  91. %typemap(in, numinputs=0) std::string *OUTPUT (std::string temp)
  92. %{ $1 = &temp; %}
  93. %typemap(argout) std::string *OUTPUT
  94. %{ lua_pushstring(L,$1->c_str()); SWIG_arg++;%}
  95. %typemap(in) std::string *INOUT = std::string *INPUT;
  96. %typemap(argout) std::string *INOUT = std::string *OUTPUT;
  97. */
  98. /*
  99. A really cut down version of the string class
  100. This provides basic mapping of lua strings <-> std::string
  101. and little else
  102. (the std::string has a lot of unneeded functions anyway)
  103. note: no fn's taking the const string&
  104. as this is overloaded by the const char* version
  105. */
  106. namespace std {
  107. class string {
  108. public:
  109. string();
  110. string(const char*);
  111. //string(const string&);
  112. unsigned int size() const;
  113. unsigned int length() const;
  114. bool empty() const;
  115. // no support for operator[]
  116. const char* c_str()const;
  117. const char* data()const;
  118. // assign does not return a copy of this object
  119. // (no point in a scripting language)
  120. void assign(const char*);
  121. //void assign(const string&);
  122. // no support for all the other features
  123. // its probably better to do it in lua
  124. };
  125. }