PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Lib/ocaml/std_string.i

#
Swig | 126 lines | 90 code | 22 blank | 14 comment | 0 complexity | 03fc892ed0263f6ceed48e011c77cad5 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * std_string.i
  3. *
  4. * SWIG typemaps for std::string
  5. * ----------------------------------------------------------------------------- */
  6. // ------------------------------------------------------------------------
  7. // std::string is typemapped by value
  8. // This can prevent exporting methods which return a string
  9. // in order for the user to modify it.
  10. // However, I think I'll wait until someone asks for it...
  11. // ------------------------------------------------------------------------
  12. %{
  13. #include <string>
  14. #include <vector>
  15. %}
  16. %include <exception.i>
  17. %include <std_vector.i>
  18. namespace std {
  19. %naturalvar string;
  20. %naturalvar wstring;
  21. class string;
  22. class wstring;
  23. /* Overloading check */
  24. %typemap(in) string {
  25. /* %typemap(in) string */
  26. if (caml_ptr_check($input))
  27. $1.assign((char *)caml_ptr_val($input,0), caml_string_len($input));
  28. else
  29. SWIG_exception(SWIG_TypeError, "string expected");
  30. }
  31. %typemap(in) const string & (std::string temp) {
  32. /* %typemap(in) const string & */
  33. if (caml_ptr_check($input)) {
  34. temp.assign((char *)caml_ptr_val($input,0), caml_string_len($input));
  35. $1 = &temp;
  36. } else {
  37. SWIG_exception(SWIG_TypeError, "string expected");
  38. }
  39. }
  40. %typemap(in) string & (std::string temp) {
  41. /* %typemap(in) string & */
  42. if (caml_ptr_check($input)) {
  43. temp.assign((char *)caml_ptr_val($input,0), caml_string_len($input));
  44. $1 = &temp;
  45. } else {
  46. SWIG_exception(SWIG_TypeError, "string expected");
  47. }
  48. }
  49. %typemap(in) string * (std::string *temp) {
  50. /* %typemap(in) string * */
  51. if (caml_ptr_check($input)) {
  52. temp = new std::string((char *)caml_ptr_val($input,0), caml_string_len($input));
  53. $1 = temp;
  54. } else {
  55. SWIG_exception(SWIG_TypeError, "string expected");
  56. }
  57. }
  58. %typemap(free) string * (std::string *temp) {
  59. delete temp;
  60. }
  61. %typemap(argout) string & {
  62. /* %typemap(argout) string & */
  63. swig_result = caml_list_append(swig_result,caml_val_string_len((*$1).c_str(), (*$1).size()));
  64. }
  65. %typemap(directorout) string {
  66. /* %typemap(directorout) string */
  67. $result.assign((char *)caml_ptr_val($input,0), caml_string_len($input));
  68. }
  69. %typemap(out) string {
  70. /* %typemap(out) string */
  71. $result = caml_val_string_len($1.c_str(),$1.size());
  72. }
  73. %typemap(out) string * {
  74. /* %typemap(out) string * */
  75. $result = caml_val_string_len((*$1).c_str(),(*$1).size());
  76. }
  77. }
  78. #ifdef ENABLE_CHARPTR_ARRAY
  79. char **c_charptr_array( const std::vector <std::string > &str_v );
  80. %{
  81. SWIGEXT char **c_charptr_array( const std::vector <std::string > &str_v ) {
  82. char **out = new char *[str_v.size() + 1];
  83. out[str_v.size()] = 0;
  84. for( int i = 0; i < str_v.size(); i++ ) {
  85. out[i] = (char *)str_v[i].c_str();
  86. }
  87. return out;
  88. }
  89. %}
  90. #endif
  91. #ifdef ENABLE_STRING_VECTOR
  92. %template (StringVector) std::vector<std::string >;
  93. %insert(ml) %{
  94. (* Some STL convenience items *)
  95. let string_array_to_vector sa =
  96. let nv = _new_StringVector C_void in
  97. array_to_vector nv (fun x -> C_string x) sa ; nv
  98. let c_string_array ar =
  99. _c_charptr_array (string_array_to_vector ar)
  100. %}
  101. %insert(mli) %{
  102. val c_string_array: string array -> c_obj
  103. %}
  104. #endif