Skip to content

Commit

Permalink
[Ocaml] Apply patch #3151788 from Joel Reymont. Brings Ocaml support …
Browse files Browse the repository at this point in the history
…a up to date (ver 3.11 and 3.12), including std::string.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12460 626c5289-ae23-0410-ae9c-e8d60b6d4f22
  • Loading branch information
wsfulton committed Feb 14, 2011
1 parent 7d700cc commit 1fab53b
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 246 deletions.
14 changes: 7 additions & 7 deletions Examples/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ ocaml_static_cpp: $(SRCS)
$(OCAMLCORE)
$(SWIG) -ocaml -c++ $(SWIGOPT) $(INTERFACEPATH)
cp $(ICXXSRCS) $(ICXXSRCS:%.cxx=%.c)
$(OCC) -cc '$(CXX)' -g -c -ccopt -g -ccopt "-xc++ $(INCLUDES)" \
$(OCC) -cc '$(CXX) -Wno-write-strings' -g -c -ccopt -g -ccopt "-xc++ $(INCLUDES)" \
$(ICXXSRCS:%.cxx=%.c) $(SRCS) $(CXXSRCS)
$(OCC) -g -c $(INTERFACE:%.i=%.mli)
$(OCC) -g -c $(INTERFACE:%.i=%.ml)
Expand All @@ -643,31 +643,31 @@ ocaml_static_cpp: $(SRCS)
$(INTERFACE:%.i=%.cmo) \
$(PROGFILE:%.ml=%.cmo) \
$(INTERFACE:%.i=%_wrap.@OBJEXT@) $(OBJS) \
-cclib "$(LIBS)" -cc '$(CXX)'
-cclib "$(LIBS)" -cc '$(CXX) -Wno-write-strings'

ocaml_static_cpp_toplevel: $(SRCS)
$(OCAMLCORE)
$(SWIG) -ocaml -c++ $(SWIGOPT) $(INTERFACEPATH)
cp $(ICXXSRCS) $(ICXXSRCS:%.cxx=%.c)
$(OCC) -cc '$(CXX)' -g -c -ccopt -g -ccopt "-xc++ $(INCLUDES)" \
$(OCC) -cc '$(CXX) -Wno-write-strings' -g -c -ccopt -g -ccopt "-xc++ $(INCLUDES)" \
$(ICXXSRCS:%.cxx=%.c) $(SRCS) $(CXXSRCS)
$(OCC) -g -c $(INTERFACE:%.i=%.mli)
$(OCC) -g -c $(INTERFACE:%.i=%.ml)
test -z "$(PROGFILE)" || test -f "$(PROGFILE)" && \
$(OCC) $(OCAMLPP) -c $(PROGFILE)
$(NOLINK) || $(OCAMLMKTOP) \
swig.cmo \
-I $(OCAMLP4WHERE) camlp4o.cma swigp4.cmo \
-I $(OCAMLP4WHERE) dynlink.cma camlp4o.cma swigp4.cmo \
-g -ccopt -g -cclib -g -custom -o $(TARGET)_top \
$(INTERFACE:%.i=%.cmo) \
$(INTERFACE:%.i=%_wrap.@OBJEXT@) $(OBJS) \
-cclib "$(LIBS)" -cc '$(CXX)'
-cclib "$(LIBS)" -cc '$(CXX) -Wno-write-strings'

ocaml_dynamic_cpp: $(SRCS)
$(OCAMLCORE)
$(SWIG) -ocaml -c++ $(SWIGOPT) $(INTERFACEPATH)
cp $(ICXXSRCS) $(ICXXSRCS:%.cxx=%.c)
$(OCC) -cc '$(CXX)' -g -c -ccopt -g -ccopt "-xc++ $(INCLUDES)" \
$(OCC) -cc '$(CXX) -Wno-write-strings' -g -c -ccopt -g -ccopt "-xc++ $(INCLUDES)" \
$(ICXXSRCS:%.cxx=%.c) $(SRCS) $(CXXSRCS) -ccopt -fPIC
$(CXXSHARED) $(CFLAGS) -o $(INTERFACE:%.i=%@SO@) \
$(INTERFACE:%.i=%_wrap.@OBJEXT@) $(OBJS) \
Expand All @@ -684,7 +684,7 @@ ocaml_dynamic_cpp: $(SRCS)
$(OCC) -cclib -export-dynamic -g -ccopt -g -cclib -g -custom \
-o $(TARGET) \
-package dl -linkpkg \
$(INTERFACE:%.i=%.cmo) $(PROGFILE:%.ml=%.cmo) -cc '$(CXX)'
$(INTERFACE:%.i=%.cmo) $(PROGFILE:%.ml=%.cmo) -cc '$(CXX) -Wno-write-strings'

ocaml_clean:
rm -f *_wrap* *~ .~* *.cmo *.cmi $(MLFILE) $(MLFILE)i swig.mli swig.cmi swig.ml swig.cmo swigp4.ml swigp4.cmo
Expand Down
4 changes: 4 additions & 0 deletions Examples/ocaml/argout_ref/example.i
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/* File : example.i */
%module example

%{
extern "C" void factor(int &x, int &y);
%}

extern "C" void factor(int &x, int &y);
2 changes: 2 additions & 0 deletions Examples/ocaml/shapes/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ void draw_depth_map( volume *v, int div_x, int div_y ) {
}

double volume::depth( double x, double y ) { return 0.0; }

volume::~volume() { }
3 changes: 2 additions & 1 deletion Examples/ocaml/shapes/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class shape {

class volume {
public:
virtual double depth( double x, double y );
virtual double depth( double x, double y );
virtual ~volume();
};

extern void draw_shape_coverage( shape *s, int div_x, int div_y );
Expand Down
6 changes: 6 additions & 0 deletions Examples/ocaml/std_string/README
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
--- New ---

It's not possible to access std::(w)string at the moment as it breaks other string examples.

--- Old ---

This example shows how to use both std::string and std::wstring in Ocaml,
and also demonstrates that one might use this to make a locale-obedient
Ocaml program.
Expand Down
18 changes: 11 additions & 7 deletions Examples/ocaml/strings_test/example.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
/* -*- mode: c++ -*- */
/* File : example.h -- Tests all string typemaps */

#include <sys/time.h>
#include <time.h>

void takes_std_string( std::string in ) {
cout << "takes_std_string( \"" << in << "\" );" << endl;
}

std::string gives_std_string() {
time_t t;

return std::string( asctime( localtime( &t ) ) );
struct timeval tv;

gettimeofday(&tv, NULL);
return std::string( asctime( localtime( &tv.tv_sec ) ) );
}

void takes_char_ptr( char *p ) {
Expand All @@ -24,10 +28,10 @@ void takes_and_gives_std_string( std::string &inout ) {
inout.insert( inout.end(), ']' );
}

void takes_and_gives_char_ptr( char *&ptr ) {
char *pout = strchr( ptr, '.' );
if( pout ) ptr = pout + 1;
else ptr = "foo";
void takes_and_gives_char_ptr( char *&inout ) {
char *pout = strchr( inout, '.' );
if( pout ) inout = pout + 1;
else inout = "foo";
}

/*
Expand Down
180 changes: 65 additions & 115 deletions Lib/ocaml/std_string.i
Original file line number Diff line number Diff line change
Expand Up @@ -11,141 +11,91 @@
// However, I think I'll wait until someone asks for it...
// ------------------------------------------------------------------------

%include <exception.i>

%{
#include <string>
#include <vector>
using std::string;
using std::vector;
%}


%include <exception.i>
%include <std_vector.i>

%naturalvar std::string;
%naturalvar std::wstring;

namespace std {
template <class charT> class basic_string {
public:
typedef charT *pointer;
typedef charT &reference;
typedef const charT &const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
basic_string();
basic_string( charT *str );
size_t size();
charT operator []( int pos ) const;
charT *c_str() const;
basic_string<charT> &operator = ( const basic_string &ws );
basic_string<charT> &operator = ( const charT *str );
basic_string<charT> &append( const basic_string<charT> &other );
basic_string<charT> &append( const charT *str );
void push_back( charT c );
void clear();
void reserve( size_type t );
void resize( size_type n, charT c = charT() );
int compare( const basic_string<charT> &other ) const;
int compare( const charT *str ) const;
basic_string<charT> &insert( size_type pos,
const basic_string<charT> &str );
size_type find( const basic_string<charT> &other, int pos = 0 ) const;
size_type find( charT c, int pos = 0 ) const;
%extend {
bool operator == ( const basic_string<charT> &other ) const {
return self->compare( other ) == 0;
}
bool operator != ( const basic_string<charT> &other ) const {
return self->compare( other ) != 0;
}
bool operator < ( const basic_string<charT> &other ) const {
return self->compare( other ) == -1;
}
bool operator > ( const basic_string<charT> &other ) const {
return self->compare( other ) == 1;
}
bool operator <= ( const basic_string<charT> &other ) const {
return self->compare( other ) != 1;
}
bool operator >= ( const basic_string<charT> &other ) const {
return self->compare( other ) != -1;
}
}
};

%template(string) basic_string<char>;
%template(wstring) basic_string<wchar_t>;
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;

/* Overloading check */
%typemap(in) string {
if (caml_ptr_check($input))
$1.assign((char *)caml_ptr_val($input,0),
caml_string_len($input));
else
SWIG_exception(SWIG_TypeError, "string expected");
}

%typemap(in) const string & (std::string temp) {
if (caml_ptr_check($input)) {
temp.assign((char *)caml_ptr_val($input,0),
caml_string_len($input));
$1 = &temp;
} else {
SWIG_exception(SWIG_TypeError, "string expected");
}
}
%naturalvar string;
%naturalvar wstring;

class string;
class wstring;

/* Overloading check */
%typemap(in) string {
/* %typemap(in) string */
if (caml_ptr_check($input))
$1.assign((char *)caml_ptr_val($input,0), caml_string_len($input));
else
SWIG_exception(SWIG_TypeError, "string expected");
}

%typemap(in) string & (std::string temp) {
if (caml_ptr_check($input)) {
temp.assign((char *)caml_ptr_val($input,0),
caml_string_len($input));
$1 = &temp;
} else {
SWIG_exception(SWIG_TypeError, "string expected");
}
}
%typemap(in) const string & (std::string temp) {
/* %typemap(in) const string & */
if (caml_ptr_check($input)) {
temp.assign((char *)caml_ptr_val($input,0), caml_string_len($input));
$1 = &temp;
} else {
SWIG_exception(SWIG_TypeError, "string expected");
}
}

%typemap(in) string * (std::string *temp) {
if (caml_ptr_check($input)) {
temp = new std::string((char *)caml_ptr_val($input,0),
caml_string_len($input));
$1 = temp;
} else {
SWIG_exception(SWIG_TypeError, "string expected");
}
}
%typemap(in) string & (std::string temp) {
/* %typemap(in) string & */
if (caml_ptr_check($input)) {
temp.assign((char *)caml_ptr_val($input,0), caml_string_len($input));
$1 = &temp;
} else {
SWIG_exception(SWIG_TypeError, "string expected");
}
}

%typemap(free) string * (std::string *temp) {
delete temp;
}
%typemap(in) string * (std::string *temp) {
/* %typemap(in) string * */
if (caml_ptr_check($input)) {
temp = new std::string((char *)caml_ptr_val($input,0), caml_string_len($input));
$1 = temp;
} else {
SWIG_exception(SWIG_TypeError, "string expected");
}
}

%typemap(argout) string & {
caml_list_append(swig_result,caml_val_string_len((*$1).c_str(),
(*$1).size()));
}
%typemap(free) string * (std::string *temp) {
delete temp;
}

%typemap(directorout) string {
$result.assign((char *)caml_ptr_val($input,0),
caml_string_len($input));
}
%typemap(argout) string & {
/* %typemap(argout) string & */
swig_result = caml_list_append(swig_result,caml_val_string_len((*$1).c_str(), (*$1).size()));
}

%typemap(out) string {
$result = caml_val_string_len($1.c_str(),$1.size());
}
%typemap(directorout) string {
/* %typemap(directorout) string */
$result.assign((char *)caml_ptr_val($input,0), caml_string_len($input));
}

%typemap(out) string {
/* %typemap(out) string */
$result = caml_val_string_len($1.c_str(),$1.size());
}

%typemap(out) string * {
%typemap(out) string * {
/* %typemap(out) string * */
$result = caml_val_string_len((*$1).c_str(),(*$1).size());
}
}
}

#ifdef ENABLE_CHARPTR_ARRAY
char **c_charptr_array( const std::vector <string > &str_v );
char **c_charptr_array( const std::vector <std::string > &str_v );

%{
SWIGEXT char **c_charptr_array( const std::vector <string > &str_v ) {
SWIGEXT char **c_charptr_array( const std::vector <std::string > &str_v ) {
char **out = new char *[str_v.size() + 1];
out[str_v.size()] = 0;
for( int i = 0; i < str_v.size(); i++ ) {
Expand All @@ -157,7 +107,7 @@ char **c_charptr_array( const std::vector <string > &str_v );
#endif

#ifdef ENABLE_STRING_VECTOR
%template (StringVector) std::vector<string >;
%template (StringVector) std::vector<std::string >;

%insert(ml) %{
(* Some STL convenience items *)
Expand Down
Loading

0 comments on commit 1fab53b

Please sign in to comment.