aboutsummaryrefslogtreecommitdiff
path: root/Examples/test-suite/li_std_vector.i
blob: ada146cc4f3cb21e354c6b4b60f0f826d3082d27 (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
%module li_std_vector

%include "std_vector.i"
%include "std_string.i"

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

namespace std {
    %template(IntVector) vector<int>;
}

%template(BoolVector) std::vector<bool>;
%template(CharVector) std::vector<char>;
%template(ShortVector) std::vector<short>;
%template(LongVector) std::vector<long>;
%template(UCharVector) std::vector<unsigned char>;
%template(UIntVector) std::vector<unsigned int>;
%template(UShortVector) std::vector<unsigned short>;
%template(ULongVector) std::vector<unsigned long>;
%template(DoubleVector) std::vector<double>;
%template(StringVector) std::vector<std::string>;


%inline %{
typedef float Real;
size_t typedef_test(std::vector<int>::size_type s) { return s; }
%}

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));
}

struct Struct {
  double num;
  Struct() : num(0.0) {}
  Struct(double d) : num(d) {}
};

struct Structure {
  double num;
  Structure() : num(0.0) {}
  Structure(double d) : num(d) {}
};

const std::vector<Real> & vecreal(const std::vector<Real> & vec) { return vec; }

const std::vector<int> & vecintptr(const std::vector<int> & vec) { return vec; }
const std::vector<int *> & vecintptr(const std::vector<int *> & vec) { return vec; }
const std::vector<const int *> & vecintconstptr(const std::vector<const int *> & vec) { return vec; }

const std::vector<Struct> & vecstruct(const std::vector<Struct> & vec) { return vec; }
const std::vector<Struct *> & vecstructptr(const std::vector<Struct *> & vec) { return vec; }
const std::vector<const Struct *> & vecstructconstptr(const std::vector<const Struct *> & vec) { return vec; }
%}

#if !defined(SWIGR)
%template(IntPtrVector) std::vector<int *>;
%template(IntConstPtrVector) std::vector<const int *>;
#endif
%template(StructVector) std::vector<Struct>;
%template(StructPtrVector) std::vector<Struct *>;
%template(StructConstPtrVector) std::vector<const Struct *>;

%inline {
  struct MyClass {};
  typedef MyClass *MyClassPtr;
  typedef std::vector<MyClassPtr> MyClassVector;
}
%template(MyClassPtrVector) std::vector<MyClassPtr>;

%inline {
  class RetsMetadata
  {
  public:
    MyClassVector GetAllResources(size_t n) const
    {
      return MyClassVector(n, 0);
    }
  };
}

#if defined(SWIGRUBY)
%template(LanguageVector) std::vector< swig::LANGUAGE_OBJ >;

%inline {
  std::vector< swig::LANGUAGE_OBJ > LanguageVector; 
}
#endif


// Test that the digraph <::aa::Holder> is not generated
%include <std_vector.i>

%inline %{
namespace aa {
  struct Holder {
    Holder(int n = 0) : number(n) {}
    int number;
  };
}
%}

#if !defined(SWIGOCTAVE)
// To fix: something different in Octave is preventing this from working
%template(VectorTest) std::vector< ::aa::Holder >;

%inline %{
std::vector< ::aa::Holder > vec1(std::vector< ::aa::Holder > x) { return x; }
%}
#endif

// exercising vectors of strings
%inline %{
std::vector<std::string> RevStringVec (const std::vector<std::string> &In)
  {
    std::vector<std::string> result(In);
    std::reverse(result.begin(), result.end());
    return(result);
  }
%}