#### /trunk/Examples/test-suite/li_std_map.i

#
Swig | 120 lines | 91 code | 29 blank | 0 comment | 0 complexity | b383e5972ab8d8b8c0bf447b8d248c46 MD5 | raw file
```  1%module("templatereduce") li_std_map
2%feature("trackobjects");
3
4%inline %{
5namespace another {
6struct map {
7  int val;
8  map(int x) : val(x) {}
9};
10}
11%}
12
13%include "std_pair.i"
14%include "std_map.i"
15%include "std_string.i"
16
17// Declare some maps to play around with
18%template(IntIntMap) std::map<int, int>;
19%template(StringIntMap) std::map<std::string, int>;
20
21%ignore Struct::operator<;
22%ignore Struct::operator==;
23
24// Add an inline function to test
25%inline %{
26
27double valueAverage(std::map<std::string, int> m) {
28  if (m.size() == 0) {
29    return 0.0;
30  }
31
32  double a = 0.0;
33  for (std::map<std::string, int>::iterator i = m.begin(); i != m.end(); i++) {
34    a += i->second;
35  }
36
37  return a / m.size();
38}
39
40std::string stringifyKeys(std::map<std::string, int> m) {
41  std::string a;
42  for (std::map<std::string, int>::iterator i = m.begin(); i != m.end(); i++) {
43    a += " " + i->first;
44  }
45  return a;
46}
47
48struct Struct {
49  double num;
50  Struct() : num(0.0) {}
51  Struct(double d) : num(d) {}
52  bool operator<(const Struct &other) const { return num < other.num; }
53  bool operator==(const Struct &other) const { return num == other.num; }
54};
55
56%}
57
58//#if !defined(SWIGR)
59
60// Test out some maps with pointer types
61%template(IntIntPtrMap) std::map<int, int *>;
62%template(IntConstIntPtrMap) std::map<int, const int *>;
63
64//#endif
65
66
67// Test out some maps with non-basic types and non-basic pointer types
68%template(IntStructMap) std::map<int, Struct>;
69%template(IntStructPtrMap) std::map<int, Struct *>;
70%template(IntStructConstPtrMap) std::map<int, const Struct *>;
71%template(StructPtrIntMap) std::map<Struct *, int>;
72
73// Test out a non-specialized map
74%template(StructIntMap) std::map<Struct, int>;
75
76// Additional map definitions for Ruby, Python and Octave tests
77%inline %{
78  struct A{
79    int val;
80
81    A(int v = 0): val(v) {
82    }
83  };
84%}
85
86namespace std {
87  %template(pairii) pair<int, int>;
88  %template(pairAA) pair<int, A>;
89  %template(pairA) pair<int, A*>;
90  %template(mapA) map<int, A*>;
91
92  %template(paircA1) pair<const int, A*>;
93  %template(paircA2) pair<const int, const A*>;
94  %template(pairiiA) pair<int,pair<int, A*> >;
95  %template(pairiiAc) pair<int,const pair<int, A*> >;
96
97
98#ifdef SWIGRUBY
99  %template() pair< swig::LANGUAGE_OBJ, swig::LANGUAGE_OBJ >;
100  %template(LanguageMap) map< swig::LANGUAGE_OBJ, swig::LANGUAGE_OBJ >;
101#endif
102
103#ifdef SWIGPYTHON
104  %template() pair<swig::SwigPtr_PyObject, swig::SwigPtr_PyObject>;
105  %template(pymap) map<swig::SwigPtr_PyObject, swig::SwigPtr_PyObject>;
106#endif
107
108}
109
110%inline {
111  std::pair<int, A*> p_identa(std::pair<int, A*> p) {
112    return p;
113  }
114
115  std::map<int, A*> m_identa(const std::map<int,A*>& v) {
116    return v;
117  }
118}
119
120
```