The great merge
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4141 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
5fcae5eb66
commit
12a43edc2d
1508 changed files with 125983 additions and 44037 deletions
460
Lib/mzscheme/std_vector.i
Normal file
460
Lib/mzscheme/std_vector.i
Normal file
|
|
@ -0,0 +1,460 @@
|
|||
//
|
||||
// SWIG typemaps for std::vector
|
||||
// Luigi Ballabio
|
||||
// Apr 8, 2002
|
||||
//
|
||||
// MzScheme implementation
|
||||
|
||||
%include std_common.i
|
||||
%include exception.i
|
||||
|
||||
// containers
|
||||
|
||||
|
||||
%exception std::vector::ref {
|
||||
try {
|
||||
$action
|
||||
} catch (std::out_of_range& e) {
|
||||
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
|
||||
}
|
||||
}
|
||||
|
||||
%exception std::vector::set {
|
||||
try {
|
||||
$action
|
||||
} catch (std::out_of_range& e) {
|
||||
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
|
||||
}
|
||||
}
|
||||
|
||||
%exception std::vector::pop {
|
||||
try {
|
||||
$action
|
||||
} catch (std::out_of_range& e) {
|
||||
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// std::vector
|
||||
//
|
||||
// The aim of all that follows would be to integrate std::vector with
|
||||
// MzScheme as much as possible, namely, to allow the user to pass and
|
||||
// be returned MzScheme vectors or lists.
|
||||
// const declarations are used to guess the intent of the function being
|
||||
// exported; therefore, the following rationale is applied:
|
||||
//
|
||||
// -- f(std::vector<T>), f(const std::vector<T>&), f(const std::vector<T>*):
|
||||
// the parameter being read-only, either a MzScheme sequence or a
|
||||
// previously wrapped std::vector<T> can be passed.
|
||||
// -- f(std::vector<T>&), f(std::vector<T>*):
|
||||
// the parameter must be modified; therefore, only a wrapped std::vector
|
||||
// can be passed.
|
||||
// -- std::vector<T> f():
|
||||
// the vector is returned by copy; therefore, a MzScheme vector of T:s
|
||||
// is returned which is most easily used in other MzScheme functions
|
||||
// -- std::vector<T>& f(), std::vector<T>* f(), const std::vector<T>& f(),
|
||||
// const std::vector<T>* f():
|
||||
// the vector is returned by reference; therefore, a wrapped std::vector
|
||||
// is returned
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
%{
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
%}
|
||||
|
||||
// exported class
|
||||
|
||||
namespace std {
|
||||
|
||||
template<class T> class vector {
|
||||
%typemap(in) vector<T> {
|
||||
if (SCHEME_VECTORP($input)) {
|
||||
unsigned int size = SCHEME_VEC_SIZE($input);
|
||||
$1 = std::vector<T >(size);
|
||||
Scheme_Object** items = SCHEME_VEC_ELS($input);
|
||||
for (unsigned int i=0; i<size; i++) {
|
||||
(($1_type &)$1)[i] =
|
||||
*((T*) SWIG_MustGetPtr(items[i],
|
||||
$descriptor(T *),
|
||||
$argnum));
|
||||
}
|
||||
} else if (SCHEME_NULLP($input)) {
|
||||
$1 = std::vector<T >();
|
||||
} else if (SCHEME_PAIRP($input)) {
|
||||
Scheme_Object *head, *tail;
|
||||
$1 = std::vector<T >();
|
||||
tail = $input;
|
||||
while (!SCHEME_NULLP(tail)) {
|
||||
head = scheme_car(tail);
|
||||
tail = scheme_cdr(tail);
|
||||
$1.push_back(*((T*)SWIG_MustGetPtr(head,
|
||||
$descriptor(T *),
|
||||
$argnum)));
|
||||
}
|
||||
} else {
|
||||
$1 = *(($&1_type)
|
||||
SWIG_MustGetPtr($input,$&1_descriptor,$argnum));
|
||||
}
|
||||
}
|
||||
%typemap(in) const vector<T>& (std::vector<T> temp),
|
||||
const vector<T>* (std::vector<T> temp) {
|
||||
if (SCHEME_VECTORP($input)) {
|
||||
unsigned int size = SCHEME_VEC_SIZE($input);
|
||||
temp = std::vector<T >(size);
|
||||
$1 = &temp;
|
||||
Scheme_Object** items = SCHEME_VEC_ELS($input);
|
||||
for (unsigned int i=0; i<size; i++) {
|
||||
temp[i] = *((T*) SWIG_MustGetPtr(items[i],
|
||||
$descriptor(T *),
|
||||
$argnum));
|
||||
}
|
||||
} else if (SCHEME_NULLP($input)) {
|
||||
temp = std::vector<T >();
|
||||
$1 = &temp;
|
||||
} else if (SCHEME_PAIRP($input)) {
|
||||
temp = std::vector<T >();
|
||||
$1 = &temp;
|
||||
Scheme_Object *head, *tail;
|
||||
tail = $input;
|
||||
while (!SCHEME_NULLP(tail)) {
|
||||
head = scheme_car(tail);
|
||||
tail = scheme_cdr(tail);
|
||||
temp.push_back(*((T*) SWIG_MustGetPtr(head,
|
||||
$descriptor(T *),
|
||||
$argnum)));
|
||||
}
|
||||
} else {
|
||||
$1 = ($1_ltype) SWIG_MustGetPtr($input,$1_descriptor,$argnum);
|
||||
}
|
||||
}
|
||||
%typemap(out) vector<T> {
|
||||
$result = scheme_make_vector($1.size(),scheme_undefined);
|
||||
Scheme_Object** els = SCHEME_VEC_ELS($result);
|
||||
for (unsigned int i=0; i<$1.size(); i++) {
|
||||
T* x = new T((($1_type &)$1)[i]);
|
||||
els[i] = SWIG_MakePtr(x,$descriptor(T *));
|
||||
}
|
||||
}
|
||||
%typecheck(SWIG_TYPECHECK_VECTOR) vector<T> {
|
||||
/* native sequence? */
|
||||
if (SCHEME_VECTORP($input)) {
|
||||
unsigned int size = SCHEME_VEC_SIZE($input);
|
||||
if (size == 0) {
|
||||
/* an empty sequence can be of any type */
|
||||
$1 = 1;
|
||||
} else {
|
||||
/* check the first element only */
|
||||
T* x;
|
||||
Scheme_Object** items = SCHEME_VEC_ELS($input);
|
||||
if (SWIG_GetPtr(items[0],(void**) &x,
|
||||
$descriptor(T *)) != -1)
|
||||
$1 = 1;
|
||||
else
|
||||
$1 = 0;
|
||||
}
|
||||
} else if (SCHEME_NULLP($input)) {
|
||||
/* again, an empty sequence can be of any type */
|
||||
$1 = 1;
|
||||
} else if (SCHEME_PAIRP($input)) {
|
||||
/* check the first element only */
|
||||
T* x;
|
||||
Scheme_Object *head = scheme_car($input);
|
||||
if (SWIG_GetPtr(head,(void**) &x,
|
||||
$descriptor(T *)) != -1)
|
||||
$1 = 1;
|
||||
else
|
||||
$1 = 0;
|
||||
} else {
|
||||
/* wrapped vector? */
|
||||
std::vector<T >* v;
|
||||
if (SWIG_GetPtr($input,(void **) &v,
|
||||
$&1_descriptor) != -1)
|
||||
$1 = 1;
|
||||
else
|
||||
$1 = 0;
|
||||
}
|
||||
}
|
||||
%typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&,
|
||||
const vector<T>* {
|
||||
/* native sequence? */
|
||||
if (SCHEME_VECTORP($input)) {
|
||||
unsigned int size = SCHEME_VEC_SIZE($input);
|
||||
if (size == 0) {
|
||||
/* an empty sequence can be of any type */
|
||||
$1 = 1;
|
||||
} else {
|
||||
/* check the first element only */
|
||||
T* x;
|
||||
Scheme_Object** items = SCHEME_VEC_ELS($input);
|
||||
if (SWIG_GetPtr(items[0],(void**) &x,
|
||||
$descriptor(T *)) != -1)
|
||||
$1 = 1;
|
||||
else
|
||||
$1 = 0;
|
||||
}
|
||||
} else if (SCHEME_NULLP($input)) {
|
||||
/* again, an empty sequence can be of any type */
|
||||
$1 = 1;
|
||||
} else if (SCHEME_PAIRP($input)) {
|
||||
/* check the first element only */
|
||||
T* x;
|
||||
Scheme_Object *head = scheme_car($input);
|
||||
if (SWIG_GetPtr(head,(void**) &x,
|
||||
$descriptor(T *)) != -1)
|
||||
$1 = 1;
|
||||
else
|
||||
$1 = 0;
|
||||
} else {
|
||||
/* wrapped vector? */
|
||||
std::vector<T >* v;
|
||||
if (SWIG_GetPtr($input,(void **) &v,
|
||||
$1_descriptor) != -1)
|
||||
$1 = 1;
|
||||
else
|
||||
$1 = 0;
|
||||
}
|
||||
}
|
||||
public:
|
||||
vector(unsigned int size = 0);
|
||||
vector(unsigned int size, const T& value);
|
||||
vector(const vector<T>&);
|
||||
%rename(length) size;
|
||||
unsigned int size() const;
|
||||
%rename("empty?") empty;
|
||||
bool empty() const;
|
||||
%rename("clear!") clear;
|
||||
void clear();
|
||||
%rename("set!") set;
|
||||
%rename("pop!") pop;
|
||||
%rename("push!") push_back;
|
||||
void push_back(const T& x);
|
||||
%extend {
|
||||
T pop() {
|
||||
if (self->size() == 0)
|
||||
throw std::out_of_range("pop from empty vector");
|
||||
T x = self->back();
|
||||
self->pop_back();
|
||||
return x;
|
||||
}
|
||||
T& ref(int i) {
|
||||
int size = int(self->size());
|
||||
if (i>=0 && i<size)
|
||||
return (*self)[i];
|
||||
else
|
||||
throw std::out_of_range("vector index out of range");
|
||||
}
|
||||
void set(int i, const T& x) {
|
||||
int size = int(self->size());
|
||||
if (i>=0 && i<size)
|
||||
(*self)[i] = x;
|
||||
else
|
||||
throw std::out_of_range("vector index out of range");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// specializations for built-ins
|
||||
|
||||
%define specialize_std_vector(T,CHECK,CONVERT_FROM,CONVERT_TO)
|
||||
template<> class vector<T> {
|
||||
%typemap(in) vector<T> {
|
||||
if (SCHEME_VECTORP($input)) {
|
||||
unsigned int size = SCHEME_VEC_SIZE($input);
|
||||
$1 = std::vector<T >(size);
|
||||
Scheme_Object** items = SCHEME_VEC_ELS($input);
|
||||
for (unsigned int i=0; i<size; i++) {
|
||||
Scheme_Object* o = items[i];
|
||||
if (CHECK(o))
|
||||
(($1_type &)$1)[i] = (T)(CONVERT_FROM(o));
|
||||
else
|
||||
scheme_wrong_type(FUNC_NAME, "vector<" #T ">",
|
||||
$argnum, argc, argv);
|
||||
}
|
||||
} else if (SCHEME_NULLP($input)) {
|
||||
$1 = std::vector<T >();
|
||||
} else if (SCHEME_PAIRP($input)) {
|
||||
Scheme_Object *head, *tail;
|
||||
$1 = std::vector<T >();
|
||||
tail = $input;
|
||||
while (!SCHEME_NULLP(tail)) {
|
||||
head = scheme_car(tail);
|
||||
tail = scheme_cdr(tail);
|
||||
if (CHECK(head))
|
||||
$1.push_back((T)(CONVERT_FROM(head)));
|
||||
else
|
||||
scheme_wrong_type(FUNC_NAME, "vector<" #T ">",
|
||||
$argnum, argc, argv);
|
||||
}
|
||||
} else {
|
||||
$1 = *(($&1_type)
|
||||
SWIG_MustGetPtr($input,$&1_descriptor,$argnum));
|
||||
}
|
||||
}
|
||||
%typemap(in) const vector<T>& (std::vector<T> temp),
|
||||
const vector<T>* (std::vector<T> temp) {
|
||||
if (SCHEME_VECTORP($input)) {
|
||||
unsigned int size = SCHEME_VEC_SIZE($input);
|
||||
temp = std::vector<T >(size);
|
||||
$1 = &temp;
|
||||
Scheme_Object** items = SCHEME_VEC_ELS($input);
|
||||
for (unsigned int i=0; i<size; i++) {
|
||||
Scheme_Object* o = items[i];
|
||||
if (CHECK(o))
|
||||
temp[i] = (T)(CONVERT_FROM(o));
|
||||
else
|
||||
scheme_wrong_type(FUNC_NAME, "vector<" #T ">",
|
||||
$argnum, argc, argv);
|
||||
}
|
||||
} else if (SCHEME_NULLP($input)) {
|
||||
temp = std::vector<T >();
|
||||
$1 = &temp;
|
||||
} else if (SCHEME_PAIRP($input)) {
|
||||
temp = std::vector<T >();
|
||||
$1 = &temp;
|
||||
Scheme_Object *head, *tail;
|
||||
tail = $input;
|
||||
while (!SCHEME_NULLP(tail)) {
|
||||
head = scheme_car(tail);
|
||||
tail = scheme_cdr(tail);
|
||||
if (CHECK(head))
|
||||
temp.push_back((T)(CONVERT_FROM(head)));
|
||||
else
|
||||
scheme_wrong_type(FUNC_NAME, "vector<" #T ">",
|
||||
$argnum, argc, argv);
|
||||
}
|
||||
} else {
|
||||
$1 = ($1_ltype) SWIG_MustGetPtr($input,$1_descriptor,$argnum);
|
||||
}
|
||||
}
|
||||
%typemap(out) vector<T> {
|
||||
$result = scheme_make_vector($1.size(),scheme_undefined);
|
||||
Scheme_Object** els = SCHEME_VEC_ELS($result);
|
||||
for (unsigned int i=0; i<$1.size(); i++)
|
||||
els[i] = CONVERT_TO((($1_type &)$1)[i]);
|
||||
}
|
||||
%typecheck(SWIG_TYPECHECK_VECTOR) vector<T> {
|
||||
/* native sequence? */
|
||||
if (SCHEME_VECTORP($input)) {
|
||||
unsigned int size = SCHEME_VEC_SIZE($input);
|
||||
if (size == 0) {
|
||||
/* an empty sequence can be of any type */
|
||||
$1 = 1;
|
||||
} else {
|
||||
/* check the first element only */
|
||||
T* x;
|
||||
Scheme_Object** items = SCHEME_VEC_ELS($input);
|
||||
$1 = CHECK(items[0]) ? 1 : 0;
|
||||
}
|
||||
} else if (SCHEME_NULLP($input)) {
|
||||
/* again, an empty sequence can be of any type */
|
||||
$1 = 1;
|
||||
} else if (SCHEME_PAIRP($input)) {
|
||||
/* check the first element only */
|
||||
T* x;
|
||||
Scheme_Object *head = scheme_car($input);
|
||||
$1 = CHECK(head) ? 1 : 0;
|
||||
} else {
|
||||
/* wrapped vector? */
|
||||
std::vector<T >* v;
|
||||
$1 = (SWIG_GetPtr($input,(void **) &v,
|
||||
$&1_descriptor) != -1) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
%typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&,
|
||||
const vector<T>* {
|
||||
/* native sequence? */
|
||||
if (SCHEME_VECTORP($input)) {
|
||||
unsigned int size = SCHEME_VEC_SIZE($input);
|
||||
if (size == 0) {
|
||||
/* an empty sequence can be of any type */
|
||||
$1 = 1;
|
||||
} else {
|
||||
/* check the first element only */
|
||||
T* x;
|
||||
Scheme_Object** items = SCHEME_VEC_ELS($input);
|
||||
$1 = CHECK(items[0]) ? 1 : 0;
|
||||
}
|
||||
} else if (SCHEME_NULLP($input)) {
|
||||
/* again, an empty sequence can be of any type */
|
||||
$1 = 1;
|
||||
} else if (SCHEME_PAIRP($input)) {
|
||||
/* check the first element only */
|
||||
T* x;
|
||||
Scheme_Object *head = scheme_car($input);
|
||||
$1 = CHECK(head) ? 1 : 0;
|
||||
} else {
|
||||
/* wrapped vector? */
|
||||
std::vector<T >* v;
|
||||
$1 = (SWIG_GetPtr($input,(void **) &v,
|
||||
$1_descriptor) != -1) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
public:
|
||||
vector(unsigned int size = 0);
|
||||
vector(unsigned int size, const T& value);
|
||||
vector(const vector<T>&);
|
||||
%rename(length) size;
|
||||
unsigned int size() const;
|
||||
%rename("empty?") empty;
|
||||
bool empty() const;
|
||||
%rename("clear!") clear;
|
||||
void clear();
|
||||
%rename("set!") set;
|
||||
%rename("pop!") pop;
|
||||
%rename("push!") push_back;
|
||||
void push_back(T x);
|
||||
%extend {
|
||||
T pop() {
|
||||
if (self->size() == 0)
|
||||
throw std::out_of_range("pop from empty vector");
|
||||
T x = self->back();
|
||||
self->pop_back();
|
||||
return x;
|
||||
}
|
||||
T ref(int i) {
|
||||
int size = int(self->size());
|
||||
if (i>=0 && i<size)
|
||||
return (*self)[i];
|
||||
else
|
||||
throw std::out_of_range("vector index out of range");
|
||||
}
|
||||
void set(int i, T x) {
|
||||
int size = int(self->size());
|
||||
if (i>=0 && i<size)
|
||||
(*self)[i] = x;
|
||||
else
|
||||
throw std::out_of_range("vector index out of range");
|
||||
}
|
||||
}
|
||||
};
|
||||
%enddef
|
||||
|
||||
specialize_std_vector(bool,SCHEME_BOOLP,SCHEME_TRUEP,\
|
||||
swig_make_boolean);
|
||||
specialize_std_vector(int,SCHEME_INTP,SCHEME_INT_VAL,\
|
||||
scheme_make_integer_value);
|
||||
specialize_std_vector(short,SCHEME_INTP,SCHEME_INT_VAL,\
|
||||
scheme_make_integer_value);
|
||||
specialize_std_vector(long,SCHEME_INTP,SCHEME_INT_VAL,\
|
||||
scheme_make_integer_value);
|
||||
specialize_std_vector(unsigned int,SCHEME_INTP,SCHEME_INT_VAL,\
|
||||
scheme_make_integer_value);
|
||||
specialize_std_vector(unsigned short,SCHEME_INTP,SCHEME_INT_VAL,\
|
||||
scheme_make_integer_value);
|
||||
specialize_std_vector(unsigned long,SCHEME_INTP,SCHEME_INT_VAL,\
|
||||
scheme_make_integer_value);
|
||||
specialize_std_vector(float,SCHEME_REALP,scheme_real_to_double,\
|
||||
scheme_make_double);
|
||||
specialize_std_vector(double,SCHEME_REALP,scheme_real_to_double,\
|
||||
scheme_make_double);
|
||||
specialize_std_vector(std::string,SCHEME_STRINGP,swig_scm_to_string,\
|
||||
swig_make_string);
|
||||
|
||||
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue