C++/OCaml bindings and the C++ std namespace

View: New views
2 Messages — Rating Filter:   Alert me  

C++/OCaml bindings and the C++ std namespace

by Guillaume Yziquel-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello.

I managed to bind an C++ library with Objective Caml. cf. below for the
toploop.

However, I needed to add a function converting a C char * to a C++
std::string:

> yziquel@seldon:~/svn/main/libmorfo-ocaml$ cat freeling.i
> %module freeling
> %{
>
> #include "freeling.h"
>
> std::string string_of_chars (char * c)
> {
>   std:string s (c);
>   return s;
> }
>
> %}
>
> class tokenizer {
>    public:
>        /// Constructor
>        tokenizer(const std::string &);
>
>        /// tokenize string with default options
>        std::list<word> tokenize(const std::string &);
>        /// tokenize string with default options, tracking offset in given int param.
>        std::list<word> tokenize(const std::string &, unsigned long &);
> };
>
> std::string string_of_chars (char *);

There doesn't seem to be a standard way to convert an OCaml string to a
std::string. So I was wondering if there was a way to do something like

> class std::string {
>   public
>     string(char *)
> };

Moreover, it seems to me that the OCaml support in Swig isn't very well
suited for the needs of OCamlers. The Camlp4 syntax extension is
something that I really do not like, and the binded code is pretty much
typed in a way I dislike.

What documentation should I start reading to modify or rewrite the OCaml
plugin? Or, at least, to see what I could do?

All the best,

Guillaume Yziquel.


> yziquel@seldon:~/svn/main/libmorfo-ocaml$ ocaml freeling.cma
>         Objective Caml version 3.11.0
>
> # module X = Freeling;;
> module X :
>   sig
>     type c_enum_type = [ `unknown ]
>     type c_enum_value = [ `Int of int ]
>     type c_obj = c_enum_value Swig.c_obj_t
>     val module_name : string
>     exception BadArgs of string
>     exception BadMethodName of c_obj * string * string
>     exception NotObject of c_obj
>     exception NotEnumType of c_obj
>     exception LabelNotFromThisEnum of c_obj
>     exception InvalidDirectorCall of c_obj
>     val new_tokenizer : c_obj -> c_obj
>     val _new_tokenizer : c_obj -> c_obj
>     val _delete_tokenizer : c_obj -> c_obj
>     val create_tokenizer_from_ptr : c_obj -> c_obj
>     val _string_of_chars : c_obj -> c_obj
>     val enum_to_int : c_enum_type -> c_obj -> Swig.c_obj
>     val int_to_enum : c_enum_type -> int -> c_obj
>     val swig_val : c_enum_type -> c_obj -> Swig.c_obj
>   end
> # open Freeling;;
> # let s = "/usr/share/freeling/en/tokenizer.dat";;
> val s : string = "/usr/share/freeling/en/tokenizer.dat"
> # let ss = C_string s;;
> Error: Unbound constructor C_string
> # open Swig;;
> # let ss = C_string s;;
> val ss : 'a Swig.c_obj_t = C_string "/usr/share/freeling/en/tokenizer.dat"
> # let sss = string_of_chars ss;;
> Error: Unbound value string_of_chars
> # let sss = _string_of_chars ss;;
> val sss : Freeling.c_obj = C_ptr (6710128L, 47161782545120L)
> # let tk = new_tokenizer sss;;
> val tk : Freeling.c_obj = C_obj <fun>



------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: C++/OCaml bindings and the C++ std namespace

by wsfulton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Guillaume Yziquel wrote:

> Hello.
>
> I managed to bind an C++ library with Objective Caml. cf. below for the
> toploop.
>
> However, I needed to add a function converting a C char * to a C++
> std::string:
>
>> yziquel@seldon:~/svn/main/libmorfo-ocaml$ cat freeling.i
>> %module freeling
>> %{
>>
>> #include "freeling.h"
>>
>> std::string string_of_chars (char * c)
>> {
>>   std:string s (c);
>>   return s;
>> }
>>
>> %}
>>
>> class tokenizer {
>>    public:
>>        /// Constructor
>>        tokenizer(const std::string &);
>>
>>        /// tokenize string with default options
>>        std::list<word> tokenize(const std::string &);
>>        /// tokenize string with default options, tracking offset in given int param.
>>        std::list<word> tokenize(const std::string &, unsigned long &);
>> };
>>
>> std::string string_of_chars (char *);
>
> There doesn't seem to be a standard way to convert an OCaml string to a
> std::string. So I was wondering if there was a way to do something like
>
>> class std::string {
>>   public
>>     string(char *)
>> };
>
> Moreover, it seems to me that the OCaml support in Swig isn't very well
> suited for the needs of OCamlers. The Camlp4 syntax extension is
> something that I really do not like, and the binded code is pretty much
> typed in a way I dislike.
>
> What documentation should I start reading to modify or rewrite the OCaml
> plugin? Or, at least, to see what I could do?
>
Guillaume, the OCaml module has been neglected for a few years and it
has very few users. The generated code used to compile quite cleanly
with older versions of Ocaml, but not so now. Feel free to improve the
wrappers, I suggest you discuss the development of it on the swig-devel
mailing list. You might want to contact the original Ocaml developer Art
Yerkes, see the README file. The developer documentation is in the
Doc/Devel directory. Also see Doc/Manual/Extending.html.

William


------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user