use exception specification instead of %exception to handle STL error checking

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7352 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2005-07-27 20:09:42 +00:00
commit 03a67698a9
34 changed files with 299 additions and 849 deletions

View file

@ -1,3 +1,4 @@
%include <std/std_except.i>
%apply size_t { std::size_t };

View file

@ -5,26 +5,7 @@
//
// Common implementation
%include std_common.i
%include exception.i
%exception std::map::get {
try {
$action
} catch (std::out_of_range& e) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpIndexOutOfRangeException, const_cast<char*>(e.what()));
return $null;
}
}
%exception std::map::del {
try {
$action
} catch (std::out_of_range& e) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpIndexOutOfRangeException, const_cast<char*>(e.what()));
return $null;
}
}
%include <std_common.i>
// ------------------------------------------------------------------------
// std::map
@ -50,7 +31,7 @@ namespace std {
bool empty() const;
void clear();
%extend {
T& get(const K& key) {
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;
@ -60,7 +41,7 @@ namespace std {
void set(const K& key, const T& x) {
(*self)[key] = x;
}
void del(const K& key) {
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);
@ -89,7 +70,7 @@ namespace std {
bool empty() const;
void clear();
%extend {
T& get(K key) {
T& get(K key) throw (std::out_of_range) {
std::map<K,T >::iterator i = self->find(key);
if (i != self->end())
return i->second;
@ -99,7 +80,7 @@ namespace std {
void set(K key, const T& x) {
(*self)[key] = x;
}
void del(K key) {
void del(K key) throw (std::out_of_range) {
std::map<K,T >::iterator i = self->find(key);
if (i != self->end())
self->erase(i);
@ -125,7 +106,7 @@ namespace std {
bool empty() const;
void clear();
%extend {
T get(const K& key) {
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;
@ -135,7 +116,7 @@ namespace std {
void set(const K& key, T x) {
(*self)[key] = x;
}
void del(const K& key) {
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);
@ -162,7 +143,7 @@ namespace std {
bool empty() const;
void clear();
%extend {
T get(K key) {
T get(K key) throw (std::out_of_range) {
std::map<K,T >::iterator i = self->find(key);
if (i != self->end())
return i->second;
@ -172,7 +153,7 @@ namespace std {
void set(K key, T x) {
(*self)[key] = x;
}
void del(K key) {
void del(K key) throw (std::out_of_range) {
std::map<K,T >::iterator i = self->find(key);
if (i != self->end())
self->erase(i);

View file

@ -8,6 +8,8 @@
* Warning: heavy macro usage in this file. Use swig -E to get a sane view on the real file contents!
*/
%include <std_common.i>
// MACRO for use within the std::vector class body
// CSTYPE and CTYPE respectively correspond to the types in the cstype and ctype typemaps
%define SWIG_STD_VECTOR_MINIMUM(CSTYPE, CTYPE...)
@ -172,7 +174,7 @@
%newobject Repeat(const CTYPE& value, int count);
vector();
%extend {
vector(int capacity) {
vector(int capacity) throw (std::out_of_range) {
std::vector<CTYPE >* pv = 0;
if (capacity >= 0) {
pv = new std::vector<CTYPE >();
@ -182,19 +184,19 @@
}
return pv;
}
CTYPE getitemcopy(int index) {
CTYPE getitemcopy(int index) throw (std::out_of_range) {
if (index>=0 && index<(int)self->size())
return (*self)[index];
else
throw std::out_of_range("index");
}
const CTYPE& getitem(int index) {
const CTYPE& getitem(int index) throw (std::out_of_range) {
if (index>=0 && index<(int)self->size())
return (*self)[index];
else
throw std::out_of_range("index");
}
void setitem(int index, const CTYPE& value) {
void setitem(int index, const CTYPE& value) throw (std::out_of_range) {
if (index>=0 && index<(int)self->size())
(*self)[index] = value;
else
@ -205,44 +207,44 @@
self->insert(self->end(), values.begin(), values.end());
}
// Takes a deep copy of the elements unlike ArrayList.GetRange
std::vector<CTYPE > *GetRange(int index, int count) {
std::vector<CTYPE > *GetRange(int index, int count) throw (std::out_of_range, std::invalid_argument) {
if (index < 0)
throw std::out_of_range("index");
if (count < 0)
throw std::out_of_range("count");
if (index >= (int)self->size()+1 || index+count > (int)self->size())
throw "invalid range";
throw std::invalid_argument("invalid range");
return new std::vector<CTYPE >(self->begin()+index, self->begin()+index+count);
}
void Insert(int index, const CTYPE& value) {
void Insert(int index, const CTYPE& value) throw (std::out_of_range) {
if (index>=0 && index<(int)self->size()+1)
self->insert(self->begin()+index, value);
else
throw std::out_of_range("index");
}
// Takes a deep copy of the elements unlike ArrayList.InsertRange
void InsertRange(int index, const std::vector<CTYPE >& values) {
void InsertRange(int index, const std::vector<CTYPE >& values) throw (std::out_of_range) {
if (index>=0 && index<(int)self->size()+1)
self->insert(self->begin()+index, values.begin(), values.end());
else
throw std::out_of_range("index");
}
void RemoveAt(int index) {
void RemoveAt(int index) throw (std::out_of_range) {
if (index>=0 && index<(int)self->size())
self->erase(self->begin() + index);
else
throw std::out_of_range("index");
}
void RemoveRange(int index, int count) {
void RemoveRange(int index, int count) throw (std::out_of_range, std::invalid_argument) {
if (index < 0)
throw std::out_of_range("index");
if (count < 0)
throw std::out_of_range("count");
if (index >= (int)self->size()+1 || index+count > (int)self->size())
throw "invalid range";
throw std::invalid_argument("invalid range");
self->erase(self->begin()+index, self->begin()+index+count);
}
static std::vector<CTYPE > *Repeat(const CTYPE& value, int count) {
static std::vector<CTYPE > *Repeat(const CTYPE& value, int count) throw (std::out_of_range) {
if (count < 0)
throw std::out_of_range("count");
return new std::vector<CTYPE >(count, value);
@ -250,17 +252,17 @@
void Reverse() {
std::reverse(self->begin(), self->end());
}
void Reverse(int index, int count) {
void Reverse(int index, int count) throw (std::out_of_range, std::invalid_argument) {
if (index < 0)
throw std::out_of_range("index");
if (count < 0)
throw std::out_of_range("count");
if (index >= (int)self->size()+1 || index+count > (int)self->size())
throw "invalid range";
throw std::invalid_argument("invalid range");
std::reverse(self->begin()+index, self->begin()+index+count);
}
// Takes a deep copy of the elements unlike ArrayList.SetRange
void SetRange(int index, const std::vector<CTYPE >& values) {
void SetRange(int index, const std::vector<CTYPE >& values) throw (std::out_of_range) {
if (index < 0)
throw std::out_of_range("index");
if (index+values.size() > self->size())
@ -319,125 +321,6 @@ namespace std {
%enddef
// Methods which can throw an Exception
%exception std::vector::vector(int capacity) {
try {
$action
} catch (std::out_of_range& e) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, 0, e.what());
return $null;
}
}
%exception std::vector::getitemcopy {
try {
$action
} catch (std::out_of_range& e) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, 0, e.what());
return $null;
}
}
%exception std::vector::getitem {
try {
$action
} catch (std::out_of_range& e) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, 0, e.what());
return $null;
}
}
%exception std::vector::setitem {
try {
$action
} catch (std::out_of_range& e) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, 0, e.what());
return $null;
}
}
%exception std::vector::GetRange {
try {
$action
} catch (std::out_of_range& e) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, 0, e.what());
return $null;
} catch (const char *e) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentException, e, "");
return $null;
}
}
%exception std::vector::Insert {
try {
$action
} catch (std::out_of_range& e) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, 0, e.what());
return $null;
}
}
%exception std::vector::InsertRange {
try {
$action
} catch (std::out_of_range& e) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, 0, e.what());
return $null;
}
}
%exception std::vector::RemoveAt {
try {
$action
} catch (std::out_of_range& e) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, 0, e.what());
return $null;
}
}
%exception std::vector::Repeat {
try {
$action
} catch (std::out_of_range& e) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, 0, e.what());
return $null;
}
}
%exception std::vector::RemoveRange {
try {
$action
} catch (std::out_of_range& e) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, 0, e.what());
return $null;
} catch (const char *e) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentException, e, "");
return $null;
}
}
%exception std::vector::Reverse(int index, int count) {
try {
$action
} catch (std::out_of_range& e) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, 0, e.what());
return $null;
} catch (const char *e) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentException, e, "");
return $null;
}
}
%exception std::vector::SetRange {
try {
$action
} catch (std::out_of_range& e) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, 0, e.what());
return $null;
}
}
%{
#include <vector>
#include <algorithm>