Added more STL (more exceptions, map, size_t), fixed test case: conversion_ns_template.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9628 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Mark Gossage 2006-12-26 10:15:35 +00:00
commit 515e846e2a
7 changed files with 135 additions and 16 deletions

View file

@ -1,6 +1,10 @@
Version 1.3.32 (in progress) Version 1.3.32 (in progress)
============================ ============================
12/26/2006: mgossage
[Lua] Added more STL (more exceptions, map, size_t),
fixed test case: conversion_ns_template.
12/21/2006: mgossage 12/21/2006: mgossage
[Lua] Update to throw errors when setting immutables, [Lua] Update to throw errors when setting immutables,
and allowing user addition of module variables. and allowing user addition of module variables.

View file

@ -43,6 +43,9 @@
%typemap(out) enum SWIGTYPE %typemap(out) enum SWIGTYPE
%{ lua_pushnumber(L, (lua_Number)(int)($1)); SWIG_arg++;%} %{ lua_pushnumber(L, (lua_Number)(int)($1)); SWIG_arg++;%}
// size_t (which is just a unsigned long
%apply unsigned long { size_t };
// boolean (which is a special type in lua) // boolean (which is a special type in lua)
// note: 1 & 0 are not booleans in lua, only true & false // note: 1 & 0 are not booleans in lua, only true & false
%typemap(in,checkfn="lua_isboolean") bool %typemap(in,checkfn="lua_isboolean") bool
@ -209,7 +212,8 @@ parmeters match which function
const unsigned int &, const unsigned short &, const unsigned long &, const unsigned int &, const unsigned short &, const unsigned long &,
const long long &, const unsigned long long &, const long long &, const unsigned long long &,
enum SWIGTYPE, float, double, enum SWIGTYPE, float, double,
const float &, const double & const float &, const double&,
size_t,const size_t&
{ {
$1 = lua_isnumber(L,$input); $1 = lua_isnumber(L,$input);
} }
@ -301,6 +305,8 @@ SWIG_NUMBER_BY_CONST_REF(long long);
SWIG_NUMBER_BY_CONST_REF(signed long long); SWIG_NUMBER_BY_CONST_REF(signed long long);
SWIG_NUMBER_BY_CONST_REF(unsigned long long); SWIG_NUMBER_BY_CONST_REF(unsigned long long);
%apply const unsigned long & { const size_t & };
// Also needed for object ptrs by const ref // Also needed for object ptrs by const ref
// eg const A* ref_pointer(A* const& a); // eg const A* ref_pointer(A* const& a);

5
Lib/lua/std_common.i Normal file
View file

@ -0,0 +1,5 @@
%include <std_except.i>
%apply size_t { std::size_t };
%apply const size_t& { const std::size_t& };

View file

@ -15,11 +15,29 @@
%} %}
%include <exception.i> %include <exception.i>
%typemap(throws) std::out_of_range %{ namespace std
SWIG_exception(SWIG_IndexError, $1.what()); %} {
%ignore exception; // not sure if I should ignore this...
class exception
{
public:
exception() throw() { }
virtual ~exception() throw();
virtual const char* what() const throw();
};
}
%typemap(throws) std::exception %{ // normally object whihc are thrown are returned to interpreter as errors
SWIG_exception(SWIG_SystemError, $1.what()); %} // (which potentally may have problems if they are not copied)
// therefore all classes based upon std::exception are converted to their strings & returned as errors
%typemap(throws) std::exception& %{ %typemap(throws) std::bad_exception "SWIG_exception(SWIG_RuntimeError, $1.what());"
SWIG_exception(SWIG_SystemError, ($1)->what()); %} %typemap(throws) std::domain_error "SWIG_exception(SWIG_ValueError, $1.what());"
%typemap(throws) std::exception "SWIG_exception(SWIG_SystemError, $1.what());"
%typemap(throws) std::invalid_argument "SWIG_exception(SWIG_ValueError, $1.what());"
%typemap(throws) std::length_error "SWIG_exception(SWIG_IndexError, $1.what());"
%typemap(throws) std::logic_error "SWIG_exception(SWIG_RuntimeError, $1.what());"
%typemap(throws) std::out_of_range "SWIG_exception(SWIG_IndexError, $1.what());"
%typemap(throws) std::overflow_error "SWIG_exception(SWIG_OverflowError, $1.what());"
%typemap(throws) std::range_error "SWIG_exception(SWIG_IndexError, $1.what());"
%typemap(throws) std::runtime_error "SWIG_exception(SWIG_RuntimeError, $1.what());"
%typemap(throws) std::underflow_error "SWIG_exception(SWIG_RuntimeError, $1.what());"

63
Lib/lua/std_map.i Normal file
View file

@ -0,0 +1,63 @@
/* -----------------------------------------------------------------------------
* See the LICENSE file for information on copyright, usage and redistribution
* of SWIG, and the README file for authors - http://www.swig.org/release.html.
*
* std_map.i
*
* SWIG typemaps for std::map
* ----------------------------------------------------------------------------- */
%include <std_common.i>
// ------------------------------------------------------------------------
// std::map
// ------------------------------------------------------------------------
%{
#include <map>
#include <algorithm>
#include <stdexcept>
%}
// exported class
namespace std {
template<class K, class T> class map {
// add typemaps here
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef K key_type;
typedef T mapped_type;
map();
map(const map<K,T> &);
unsigned int size() const;
bool empty() const;
void clear();
%extend {
const T& get(const K& key) throw (std::out_of_range) {
std::map<K,T >::iterator i = self->find(key);
if (i != self->end())
return i->second;
else
throw std::out_of_range("key not found");
}
void set(const K& key, const T& x) {
(*self)[key] = x;
}
void del(const K& key) throw (std::out_of_range) {
std::map<K,T >::iterator i = self->find(key);
if (i != self->end())
self->erase(i);
else
throw std::out_of_range("key not found");
}
bool has_key(const K& key) {
std::map<K,T >::iterator i = self->find(key);
return i != self->end();
}
}
};
}

13
Lib/lua/stl.i Normal file
View file

@ -0,0 +1,13 @@
/* -----------------------------------------------------------------------------
* See the LICENSE file for information on copyright, usage and redistribution
* of SWIG, and the README file for authors - http://www.swig.org/release.html.
*
* stl.i
* ----------------------------------------------------------------------------- */
%include <std_common.i>
%include <std_string.i>
%include <std_vector.i>
%include <std_map.i>
%include <std_pair.i>

View file

@ -52,7 +52,7 @@ char cvsroot_lua_cxx[] = "$Header$";
#define REPORT(T,D) // no info: #define REPORT(T,D) // no info:
//#define REPORT(T,D) {Printf(stdout,T"\n");} // only title //#define REPORT(T,D) {Printf(stdout,T"\n");} // only title
//#define REPORT(T,D) {Printf(stdout,T"\n");display_mapping(D);} // the works //#define REPORT(T,D) {Printf(stdout,T"\n");display_mapping(D);} // the works
//#define REPORT(T,D) {Printf(stdout,T"\n");Swig_print_node(D);} // the works //#define REPORT(T,D) {Printf(stdout,T"\n");if(D)Swig_print_node(D);} // the works
void display_mapping(DOH *d) { void display_mapping(DOH *d) {
if (d == 0 || !DohIsMapping(d)) if (d == 0 || !DohIsMapping(d))
@ -107,6 +107,7 @@ private:
int have_destructor; int have_destructor;
String *destructor_action; String *destructor_action;
String *class_name; String *class_name;
String *constructor_name;
public: public:
@ -126,7 +127,6 @@ public:
PrefixPlusUnderscore = 0; PrefixPlusUnderscore = 0;
s_cmd_tab = s_var_tab = s_const_tab = 0; s_cmd_tab = s_var_tab = s_const_tab = 0;
} }
/* NEW LANGUAGE NOTE:*********************************************** /* NEW LANGUAGE NOTE:***********************************************
@ -321,7 +321,7 @@ NEW LANGUAGE NOTE:END ************************************************/
String *iname = Getattr(n, "sym:name"); String *iname = Getattr(n, "sym:name");
SwigType *d = Getattr(n, "type"); SwigType *d = Getattr(n, "type");
ParmList *l = Getattr(n, "parms"); ParmList *l = Getattr(n, "parms");
//Printf(stdout,"functionWrapper %s %s\n",name,iname);
Parm *p; Parm *p;
String *tm; String *tm;
int i; int i;
@ -332,7 +332,10 @@ NEW LANGUAGE NOTE:END ************************************************/
overname = Getattr(n, "sym:overname"); overname = Getattr(n, "sym:overname");
} else { } else {
if (!addSymbol(iname, n)) if (!addSymbol(iname, n))
{
Printf(stderr,"addSymbol(%s) failed\n",iname);
return SWIG_ERROR; return SWIG_ERROR;
}
} }
/* NEW LANGUAGE NOTE:*********************************************** /* NEW LANGUAGE NOTE:***********************************************
@ -676,7 +679,7 @@ so we will just add these into the variable lists
ideally we should not have registered these as functions, ideally we should not have registered these as functions,
only WRT this variable will look into this later. only WRT this variable will look into this later.
NEW LANGUAGE NOTE:END ************************************************/ NEW LANGUAGE NOTE:END ************************************************/
REPORT("variableWrapper", n); // REPORT("variableWrapper", n);
String *iname = Getattr(n, "sym:name"); String *iname = Getattr(n, "sym:name");
// let SWIG generate the wrappers // let SWIG generate the wrappers
int result = Language::variableWrapper(n); int result = Language::variableWrapper(n);
@ -696,7 +699,7 @@ NEW LANGUAGE NOTE:END ************************************************/
if (assignable) { if (assignable) {
setName = Swig_name_wrapper(Swig_name_set(iname)); setName = Swig_name_wrapper(Swig_name_set(iname));
} else { } else {
// TODO: how about calling a 'this is not settable' error message? // how about calling a 'this is not settable' error message?
setName = NewString("SWIG_Lua_set_immutable"); // error message setName = NewString("SWIG_Lua_set_immutable"); // error message
//setName = NewString("0"); //setName = NewString("0");
} }
@ -711,7 +714,7 @@ NEW LANGUAGE NOTE:END ************************************************/
* constantWrapper() * constantWrapper()
* ------------------------------------------------------------ */ * ------------------------------------------------------------ */
virtual int constantWrapper(Node *n) { virtual int constantWrapper(Node *n) {
REPORT("constantWrapper", n); // REPORT("constantWrapper", n);
String *name = Getattr(n, "name"); String *name = Getattr(n, "name");
String *iname = Getattr(n, "sym:name"); String *iname = Getattr(n, "sym:name");
//String *nsname = !nspace ? Copy(iname) : NewStringf("%s::%s",ns_name,iname); //String *nsname = !nspace ? Copy(iname) : NewStringf("%s::%s",ns_name,iname);
@ -758,7 +761,7 @@ NEW LANGUAGE NOTE:END ************************************************/
* ------------------------------------------------------------ */ * ------------------------------------------------------------ */
virtual int nativeWrapper(Node *n) { virtual int nativeWrapper(Node *n) {
REPORT("nativeWrapper", n); // REPORT("nativeWrapper", n);
String *symname = Getattr(n, "sym:name"); String *symname = Getattr(n, "sym:name");
String *wrapname = Getattr(n, "wrap:name"); String *wrapname = Getattr(n, "wrap:name");
if (!addSymbol(wrapname, n)) if (!addSymbol(wrapname, n))
@ -803,6 +806,7 @@ NEW LANGUAGE NOTE:END ************************************************/
String *mangled_classname = 0; String *mangled_classname = 0;
String *real_classname = 0; String *real_classname = 0;
constructor_name = 0;
have_constructor = 0; have_constructor = 0;
have_destructor = 0; have_destructor = 0;
destructor_action = 0; destructor_action = 0;
@ -925,10 +929,13 @@ NEW LANGUAGE NOTE:END ************************************************/
Printv(f_wrappers, "swig_lua_class _wrap_class_", mangled_classname, " = { \"", class_name, "\", &SWIGTYPE", SwigType_manglestr(t), ",", NIL); Printv(f_wrappers, "swig_lua_class _wrap_class_", mangled_classname, " = { \"", class_name, "\", &SWIGTYPE", SwigType_manglestr(t), ",", NIL);
if (have_constructor) { if (have_constructor) {
Printf(f_wrappers, "%s", Swig_name_wrapper(Swig_name_construct(class_name))); Printf(f_wrappers, "%s", Swig_name_wrapper(Swig_name_construct(constructor_name)));
Delete(constructor_name);
constructor_name = 0;
} else { } else {
Printf(f_wrappers, "0"); Printf(f_wrappers, "0");
} }
if (have_destructor) { if (have_destructor) {
Printv(f_wrappers, ", swig_delete_", class_name, NIL); Printv(f_wrappers, ", swig_delete_", class_name, NIL);
} else { } else {
@ -948,6 +955,7 @@ NEW LANGUAGE NOTE:END ************************************************/
virtual int memberfunctionHandler(Node *n) { virtual int memberfunctionHandler(Node *n) {
String *name = Getattr(n, "name"); String *name = Getattr(n, "name");
String *iname = GetChar(n, "sym:name"); String *iname = GetChar(n, "sym:name");
//Printf(stdout,"memberfunctionHandler %s %s\n",name,iname);
String *realname, *rname; String *realname, *rname;
@ -992,7 +1000,9 @@ NEW LANGUAGE NOTE:END ************************************************/
* ------------------------------------------------------------ */ * ------------------------------------------------------------ */
virtual int constructorHandler(Node *n) { virtual int constructorHandler(Node *n) {
REPORT("constructorHandler", n);
Language::constructorHandler(n); Language::constructorHandler(n);
constructor_name = NewString(Getattr(n, "sym:name"));
have_constructor = 1; have_constructor = 1;
return SWIG_OK; return SWIG_OK;
} }