## root / src / org.omnetpp.ide.nativelibs / std_list.i @ 79bb12dc

History | View | Annotate | Download (7.65 KB)

1 |
// |
---|---|

2 |
// SWIG typemaps for std::list |

3 |
// Andras Varga |

4 |
// 2005 |

5 |
// |

6 |
// Java implementation |

7 | |

8 | |

9 |
%include exception.i |

10 | |

11 |
// containers |

12 | |

13 |
// methods which can raise are caused to throw an IndexError |

14 |
%exception std::list::get { |

15 |
try { |

16 |
$action |

17 |
} catch (std::out_of_range& e) { |

18 |
SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, const_cast<char*>(e.what())); |

19 |
return $null; |

20 |
} |

21 |
} |

22 | |

23 |
%exception std::list::set { |

24 |
try { |

25 |
$action |

26 |
} catch (std::out_of_range& e) { |

27 |
SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, const_cast<char*>(e.what())); |

28 |
return $null; |

29 |
} |

30 |
} |

31 | |

32 | |

33 |
// ------------------------------------------------------------------------ |

34 |
// std::list |

35 |
// |

36 |
// The aim of all that follows would be to integrate std::list with |

37 |
// Java as much as possible, namely, to allow the user to pass and |

38 |
// be returned Java (arrays? containers?) |

39 |
// const declarations are used to guess the intent of the function being |

40 |
// exported; therefore, the following rationale is applied: |

41 |
// |

42 |
// -- f(std::list<T>), f(const std::list<T>&), f(const std::list<T>*): |

43 |
// the parameter being read-only, either a Java sequence or a |

44 |
// previously wrapped std::list<T> can be passed. |

45 |
// -- f(std::list<T>&), f(std::list<T>*): |

46 |
// the parameter must be modified; therefore, only a wrapped std::list |

47 |
// can be passed. |

48 |
// -- std::list<T> f(): |

49 |
// the list is returned by copy; therefore, a Java sequence of T:s |

50 |
// is returned which is most easily used in other Java functions |

51 |
// -- std::list<T>& f(), std::list<T>* f(), const std::list<T>& f(), |

52 |
// const std::list<T>* f(): |

53 |
// the list is returned by reference; therefore, a wrapped std::list |

54 |
// is returned |

55 |
// ------------------------------------------------------------------------ |

56 | |

57 |
%{ |

58 |
#include <list> |

59 |
#include <algorithm> |

60 |
#include <stdexcept> |

61 |
%} |

62 | |

63 |
// exported class |

64 | |

65 |
namespace std { |

66 | |

67 |
template<class T> class list { |

68 |
// add generic typemaps here |

69 |
%typemap(javacode) list<T> %{ |

70 |
public T[] toArray() { |

71 |
int sz = (int)size(); |

72 |
T[] array = new T[sz]; |

73 |
for (int i=0; i<sz; i++) |

74 |
array[i] = get(i); |

75 |
return array; |

76 |
} |

77 |
%} |

78 |
public: |

79 |
list(); |

80 |
unsigned int size() const; |

81 |
%rename(isEmpty) empty; |

82 |
bool empty() const; |

83 |
void clear(); |

84 |
%rename(add) push_back; |

85 |
void push_back(const T& x); |

86 |
void push_front(const T& x); |

87 |
const T& back(); |

88 |
const T& front(); |

89 |
%extend { |

90 |
T& pop_back() { |

91 |
if (self->empty()) |

92 |
throw std::out_of_range("pop invoked on empty list"); |

93 |
T& x = self->back(); |

94 |
self->pop_back(); |

95 |
return x; |

96 |
} |

97 |
T& pop_front() { |

98 |
if (self->empty()) |

99 |
throw std::out_of_range("pop invoked on empty list"); |

100 |
T& x = self->back(); |

101 |
self->pop_back(); |

102 |
return x; |

103 |
} |

104 |
T& get(int i) { |

105 |
int size = int(self->size()); |

106 |
if (i<0 || i>=size) |

107 |
throw std::out_of_range("list index out of range"); |

108 |
std::list<T>::iterator it = self->begin(); |

109 |
for (; i>0; i--) it++; // linear time! |

110 |
return (*it); |

111 |
} |

112 |
void set(int i, const T& x) { |

113 |
int size = int(self->size()); |

114 |
if (i<0 || i>=size) |

115 |
throw std::out_of_range("list index out of range"); |

116 |
std::list<T>::iterator it = self->begin(); |

117 |
for (; i>0; i--) it++; // linear time! |

118 |
(*it) = x; |

119 |
} |

120 |
void insert(int i, const T& x) { |

121 |
int size = int(self->size()); |

122 |
if (i<0 || i>size) // i==size allowed, for append operation |

123 |
throw std::out_of_range("list index out of range"); |

124 |
std::list<T>::iterator it = self->begin(); |

125 |
for (; i>0; i--) it++; // linear time! |

126 |
self->insert(it, x); |

127 |
} |

128 |
void del(int i) { |

129 |
int size = int(self->size()); |

130 |
if (i<0 || i>=size) |

131 |
throw std::out_of_range("list index out of range"); |

132 |
std::list<T>::iterator it = self->begin(); |

133 |
for (; i>0; i--) it++; // linear time! |

134 |
self->erase(it); |

135 |
} |

136 |
} |

137 |
}; |

138 | |

139 | |

140 |
// specializations for built-ins |

141 | |

142 |
%define specialize_std_list(T) |

143 |
template<> class list<T> { |

144 |
// add generic typemaps here |

145 |
%typemap(javacode) list<T> %{ |

146 |
public T[] toArray() { |

147 |
int sz = (int)size(); |

148 |
T[] array = new T[sz]; |

149 |
for (int i=0; i<sz; i++) |

150 |
array[i] = get(i); |

151 |
return array; |

152 |
} |

153 |
%} |

154 |
public: |

155 |
list(); |

156 |
unsigned int size() const; |

157 |
%rename(isEmpty) empty; |

158 |
bool empty() const; |

159 |
void clear(); |

160 |
%rename(add) push_back; |

161 |
void push_back(T x); |

162 |
void push_front(T x); |

163 |
T back(); |

164 |
T front(); |

165 |
%extend { |

166 |
T pop_back() { |

167 |
if (self->empty()) |

168 |
throw std::out_of_range("pop invoked on empty list"); |

169 |
T x = self->back(); |

170 |
self->pop_back(); |

171 |
return x; |

172 |
} |

173 |
T pop_front() { |

174 |
if (self->empty()) |

175 |
throw std::out_of_range("pop invoked on empty list"); |

176 |
T x = self->back(); |

177 |
self->pop_back(); |

178 |
return x; |

179 |
} |

180 |
T get(int i) { |

181 |
int size = int(self->size()); |

182 |
if (i<0 || i>=size) |

183 |
throw std::out_of_range("list index out of range"); |

184 |
std::list<T>::iterator it = self->begin(); |

185 |
for (; i>0; i--) it++; // linear time! |

186 |
return (*it); |

187 |
} |

188 |
void set(int i, T x) { |

189 |
int size = int(self->size()); |

190 |
if (i<0 || i>=size) |

191 |
throw std::out_of_range("list index out of range"); |

192 |
std::list<T>::iterator it = self->begin(); |

193 |
for (; i>0; i--) it++; // linear time! |

194 |
(*it) = x; |

195 |
} |

196 |
void insert(int i, T x) { |

197 |
int size = int(self->size()); |

198 |
if (i<0 || i>size) // i==size allowed, for append operation |

199 |
throw std::out_of_range("list index out of range"); |

200 |
std::list<T>::iterator it = self->begin(); |

201 |
for (; i>0; i--) it++; // linear time! |

202 |
self->insert(it, x); |

203 |
} |

204 |
void del(int i) { |

205 |
int size = int(self->size()); |

206 |
if (i<0 || i>=size) |

207 |
throw std::out_of_range("list index out of range"); |

208 |
std::list<T>::iterator it = self->begin(); |

209 |
for (; i>0; i--) it++; // linear time! |

210 |
self->erase(it); |

211 |
} |

212 |
} |

213 |
}; |

214 |
%enddef |

215 | |

216 |
specialize_std_list(bool); |

217 |
specialize_std_list(char); |

218 |
specialize_std_list(int); |

219 |
specialize_std_list(short); |

220 |
specialize_std_list(long); |

221 |
specialize_std_list(unsigned char); |

222 |
specialize_std_list(unsigned int); |

223 |
specialize_std_list(unsigned short); |

224 |
specialize_std_list(unsigned long); |

225 |
specialize_std_list(float); |

226 |
specialize_std_list(double); |

227 |
specialize_std_list(std::string); |

228 | |

229 |
} |

230 |