Home | History | Annotate | Download | only in ocaml
      1 /* -----------------------------------------------------------------------------
      2  * std_string.i
      3  *
      4  * SWIG typemaps for std::string
      5  * ----------------------------------------------------------------------------- */
      6 
      7 // ------------------------------------------------------------------------
      8 // std::string is typemapped by value
      9 // This can prevent exporting methods which return a string
     10 // in order for the user to modify it.
     11 // However, I think I'll wait until someone asks for it...
     12 // ------------------------------------------------------------------------
     13 
     14 %{
     15 #include <string>
     16 #include <vector>
     17 %}
     18 
     19 %include <exception.i>
     20 %include <std_vector.i>
     21 
     22 namespace std {
     23 
     24 %naturalvar string;
     25 %naturalvar wstring;
     26 
     27 class string;
     28 class wstring;
     29 
     30 /* Overloading check */
     31 %typemap(in) string {
     32   /* %typemap(in) string */
     33   if (caml_ptr_check($input))
     34     $1.assign((char *)caml_ptr_val($input,0), caml_string_len($input));
     35   else
     36     SWIG_exception(SWIG_TypeError, "string expected");
     37 }
     38 
     39 %typemap(in) const string & ($*1_ltype temp) {
     40   /* %typemap(in) const string & */
     41   if (caml_ptr_check($input)) {
     42     temp.assign((char *)caml_ptr_val($input,0), caml_string_len($input));
     43     $1 = &temp;
     44   } else {
     45     SWIG_exception(SWIG_TypeError, "string expected");
     46   }
     47 }
     48 
     49 %typemap(in) string & ($*1_ltype temp) {
     50   /* %typemap(in) string & */
     51   if (caml_ptr_check($input)) {
     52     temp.assign((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 
     59 %typemap(in) string * ($*1_ltype *temp) {
     60   /* %typemap(in) string * */
     61   if (caml_ptr_check($input)) {
     62     temp = new $*1_ltype((char *)caml_ptr_val($input,0), caml_string_len($input));
     63     $1 = temp;
     64   } else {
     65     SWIG_exception(SWIG_TypeError, "string expected");
     66   }
     67 }
     68 
     69 %typemap(free) string * ($*1_ltype *temp) {
     70   delete temp;
     71 }
     72 
     73 %typemap(argout) string & {
     74   /* %typemap(argout) string & */
     75   swig_result =	caml_list_append(swig_result,caml_val_string_len((*$1).c_str(), (*$1).size()));
     76 }
     77 
     78 %typemap(directorout) string {
     79   /* %typemap(directorout) string */
     80 	$result.assign((char *)caml_ptr_val($input,0), caml_string_len($input));
     81 }
     82 
     83 %typemap(out) string {
     84   /* %typemap(out) string */
     85   $result = caml_val_string_len($1.c_str(),$1.size());
     86 }
     87 
     88 %typemap(out) string * {
     89   /* %typemap(out) string * */
     90 	$result = caml_val_string_len((*$1).c_str(),(*$1).size());
     91 }
     92 }
     93 
     94 #ifdef ENABLE_CHARPTR_ARRAY
     95 char **c_charptr_array( const std::vector <std::string > &str_v );
     96 
     97 %{
     98   SWIGEXT char **c_charptr_array( const std::vector <std::string > &str_v ) {
     99     char **out = new char *[str_v.size() + 1];
    100     out[str_v.size()] = 0;
    101     for( int i = 0; i < str_v.size(); i++ ) {
    102       out[i] = (char *)str_v[i].c_str();
    103     }
    104     return out;
    105   }
    106 %}
    107 #endif
    108 
    109 #ifdef ENABLE_STRING_VECTOR
    110 %template (StringVector) std::vector<std::string >;
    111 
    112 %insert(ml) %{
    113   (* Some STL convenience items *)
    114 
    115   let string_array_to_vector sa =
    116     let nv = _new_StringVector C_void in
    117       array_to_vector nv (fun x -> C_string x) sa ; nv
    118 
    119   let c_string_array ar =
    120     _c_charptr_array (string_array_to_vector ar)
    121 %}
    122 
    123 %insert(mli) %{
    124   val c_string_array: string array -> c_obj
    125 %}
    126 #endif
    127