std::map improvements based on patch from Yuval Baror
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@11230 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
6ae7ce1cf1
commit
30335a0198
8 changed files with 946 additions and 206 deletions
|
|
@ -1,40 +1,84 @@
|
|||
/**
|
||||
* @file li_std_map.i
|
||||
* @author gga
|
||||
* @date Mon Apr 30 15:03:58 2007
|
||||
*
|
||||
* @brief a test of map containers.
|
||||
* Languages should define swig::LANGUAGE_OBJ to be
|
||||
* an entity of their native pointer type which can be
|
||||
* included in a STL container.
|
||||
*
|
||||
* For example:
|
||||
* swig::LANGUAGE_OBJ is GC_VALUE in Ruby
|
||||
* swig::LANGUAGE_OBJ is SwigPtr_PyObject in python
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
%module("templatereduce") li_std_map
|
||||
|
||||
%feature("trackobjects");
|
||||
|
||||
%include std_pair.i
|
||||
%include std_map.i
|
||||
%include "std_pair.i"
|
||||
%include "std_map.i"
|
||||
%include "std_string.i"
|
||||
|
||||
// Declare some maps to play around with
|
||||
%template(IntIntMap) std::map<int, int>;
|
||||
%template(StringIntMap) std::map<std::string, int>;
|
||||
|
||||
%ignore Struct::operator<;
|
||||
%ignore Struct::operator==;
|
||||
|
||||
// Add an inline function to test
|
||||
%inline %{
|
||||
struct A{
|
||||
int val;
|
||||
|
||||
A(int v = 0): val(v)
|
||||
{
|
||||
}
|
||||
|
||||
double keyAverage(std::map<int, int> m) {
|
||||
if (m.size() == 0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
double a = 0.0;
|
||||
for (std::map<int, int>::iterator i = m.begin(); i != m.end(); i++) {
|
||||
a += i->first;
|
||||
}
|
||||
|
||||
return a / m.size();
|
||||
}
|
||||
|
||||
struct Struct {
|
||||
double num;
|
||||
Struct() : num(0.0) {}
|
||||
Struct(double d) : num(d) {}
|
||||
bool operator<(const Struct &other) const { return num < other.num; }
|
||||
bool operator==(const Struct &other) const { return num == other.num; }
|
||||
};
|
||||
|
||||
%}
|
||||
|
||||
namespace std
|
||||
{
|
||||
#if defined(SWIGCSHARP)
|
||||
|
||||
// Specialize some more non-default map types
|
||||
SWIG_STD_MAP_SPECIALIZED(int, int *, int, SWIGTYPE_p_int)
|
||||
SWIG_STD_MAP_SPECIALIZED(int, const int *, int, SWIGTYPE_p_int)
|
||||
SWIG_STD_MAP_SPECIALIZED_SIMPLE(int, Struct)
|
||||
SWIG_STD_MAP_SPECIALIZED(int, Struct *, int, Struct)
|
||||
SWIG_STD_MAP_SPECIALIZED(int, const Struct *, int, Struct)
|
||||
SWIG_STD_MAP_SPECIALIZED(Struct *, int, Struct, int)
|
||||
|
||||
#endif
|
||||
|
||||
//#if !defined(SWIGR)
|
||||
|
||||
// Test out some maps with pointer types
|
||||
%template(IntIntPtrMap) std::map<int, int *>;
|
||||
%template(IntConstIntPtrMap) std::map<int, const int *>;
|
||||
|
||||
//#endif
|
||||
|
||||
|
||||
// Test out some maps with non-basic types and non-basic pointer types
|
||||
%template(IntStructMap) std::map<int, Struct>;
|
||||
%template(IntStructPtrMap) std::map<int, Struct *>;
|
||||
%template(IntStructConstPtrMap) std::map<int, const Struct *>;
|
||||
%template(StructPtrIntMap) std::map<Struct *, int>;
|
||||
|
||||
// Test out a non-specialized map
|
||||
%template(StructIntMap) std::map<Struct, int>;
|
||||
|
||||
// Additional map definitions for Ruby, Python and Octave tests
|
||||
%inline %{
|
||||
struct A{
|
||||
int val;
|
||||
|
||||
A(int v = 0): val(v) {
|
||||
}
|
||||
};
|
||||
%}
|
||||
|
||||
namespace std {
|
||||
%template(pairii) pair<int, int>;
|
||||
%template(pairAA) pair<int, A>;
|
||||
%template(pairA) pair<int, A*>;
|
||||
|
|
@ -58,25 +102,14 @@ namespace std
|
|||
|
||||
}
|
||||
|
||||
%inline {
|
||||
std::pair<int, A*> p_identa(std::pair<int, A*> p) {
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
%inline
|
||||
{
|
||||
std::pair<int, A*>
|
||||
p_identa(std::pair<int, A*> p) {
|
||||
return p;
|
||||
}
|
||||
|
||||
std::map<int,A*> m_identa(const std::map<int,A*>& v)
|
||||
{
|
||||
return v;
|
||||
}
|
||||
|
||||
std::map<int, A*> m_identa(const std::map<int,A*>& v) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
namespace std
|
||||
{
|
||||
%template(mapii) map<int,int>;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue