summaryrefslogtreecommitdiff
path: root/Examples/test-suite/li_std_vector_extra.i
blob: 531898a0e17ff9b677da4f424b3dc45e54da7c4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
%module li_std_vector_extra

%warnfilter(509) overloaded1;
%warnfilter(509) overloaded2;

%include "std_string.i"
%include "std_vector.i"
%include "cpointer.i"
%include "carrays.i"

%{
#include <algorithm>
#include <functional>
#include <numeric>


#if defined(__clang__)
// Suppress:
// warning: destination for this 'memset' call is a pointer to dynamic class
//       'Test::B'; vtable pointer will be overwritten [-Wdynamic-class-memaccess]
//         memset(v_def,0,sizeof(Type));
// Better generated code is probably needed though
#pragma clang diagnostic ignored "-Wdynamic-class-memaccess"
#endif

%}

namespace std {
    %template() vector<short>;
    %template(IntVector) vector<int>;
    %template(BoolVector) vector<bool>;
    %template() vector<string>;
}

%template(DoubleVector) std::vector<double>;


%template(sizeVector) std::vector<size_t>;
%{
  template <class T>
  struct Param
  {
    T val;

    Param(T v = 0): val(v) {
    }
    
    operator T() const { return val; }
  };
%}
specialize_std_vector(Param<int>,PyInt_Check,PyInt_AsLong,PyInt_FromLong);
%template(PIntVector) std::vector<Param<int> >;

%inline %{
typedef float Real;
%}

namespace std {
    %template(RealVector) vector<Real>;
}

%inline %{

double average(std::vector<int> v) {
    return std::accumulate(v.begin(),v.end(),0.0)/v.size();
}

std::vector<Real> half(const std::vector<Real>& v) {
    std::vector<Real> w(v);
    for (std::vector<Real>::size_type i=0; i<w.size(); i++)
        w[i] /= 2.0;
    return w;
}

void halve_in_place(std::vector<double>& v) {
    std::transform(v.begin(),v.end(),v.begin(),
                   std::bind2nd(std::divides<double>(),2.0));
}

%}

%template(IntPtrVector) std::vector<int *>;



//
//
%{
#include <iostream>
%}

%inline %{
  
namespace Test {
struct A {
    virtual ~A() {}    
    virtual int f(const int i) const = 0;
};

struct B : public A {
  int val;
  
  B(int i = 0) : val(i)
  {
  }
  
  int f(const int i) const { return i + val; }
};


int vecAptr(const std::vector<A*>& v) {
    return v[0]->f(1);
}

} 

std::vector<short> halfs(const std::vector<short>& v) {
    std::vector<short> w(v);
    for (std::vector<short>::size_type i=0; i<w.size(); i++)
        w[i] /= 2;
    return w;
}


std::vector<std::string>  vecStr(std::vector<std::string> v) {
  v[0] += v[1];
  return v;
}

%}
%template(VecB) std::vector<Test::B>; 
%template(VecA) std::vector<Test::A*>; 

%pointer_class(int,PtrInt)
%array_functions(int,ArrInt)

%inline %{
  int *makeIntPtr(int v) { return new int(v); }
  const short *makeConstShortPtr(int v) { return new short(v); }
  double *makeDoublePtr(double v) { return new double(v); }
  int extractInt(int *p) { return *p; }
  short extractConstShort(const short *p) { return *p; }
%}

%template(pyvector) std::vector<swig::SwigPtr_PyObject>; 

namespace std {
   %template(ConstShortPtrVector) vector<const short *>;
}

%inline %{
std::string overloaded1(std::vector<double> vi) { return "vector<double>"; }
std::string overloaded1(std::vector<int> vi) { return "vector<int>"; }
std::string overloaded2(std::vector<int> vi) { return "vector<int>"; }
std::string overloaded2(std::vector<double> vi) { return "vector<double>"; }
std::string overloaded3(std::vector<int> *vi) { return "vector<int> *"; }
std::string overloaded3(int i) { return "int"; }
%}