PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/lua/std_string.i

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