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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
|
/* -----------------------------------------------------------------------------
* See the LICENSE file for information on copyright, usage and redistribution
* of SWIG, and the README file for authors - http://www.swig.org/release.html.
*
* octcontainer.swg
*
* Octave cell <-> C++ container wrapper
*
* This wrapper, and its iterator, allows a general use (and reuse) of
* the the mapping between C++ and Octave, thanks to the C++
* templates.
*
* Of course, it needs the C++ compiler to support templates, but
* since we will use this wrapper with the STL containers, that should
* be the case.
* ----------------------------------------------------------------------------- */
%{
#include <iostream>
%}
#if !defined(SWIG_NO_EXPORT_ITERATOR_METHODS)
# if !defined(SWIG_EXPORT_ITERATOR_METHODS)
# define SWIG_EXPORT_ITERATOR_METHODS SWIG_EXPORT_ITERATOR_METHODS
# endif
#endif
%include <octiterators.swg>
// The Octave C++ Wrap
%insert(header) %{
#include <stdexcept>
%}
%include <std_except.i>
%fragment(SWIG_Traits_frag(octave_value),"header",fragment="StdTraits") {
namespace swig {
template <> struct traits<octave_value > {
typedef value_category category;
static const char* type_name() { return "octave_value"; }
};
template <> struct traits_from<octave_value> {
typedef octave_value value_type;
static octave_value from(const value_type& val) {
return val;
}
};
template <>
struct traits_check<octave_value, value_category> {
static bool check(const octave_value&) {
return true;
}
};
template <> struct traits_asval<octave_value > {
typedef octave_value value_type;
static int asval(const octave_value& obj, value_type *val) {
if (val) *val = obj;
return SWIG_OK;
}
};
}
}
%fragment("OctSequence_Base","header")
{
%#include <functional>
namespace std {
template <>
struct less <octave_value>: public binary_function<octave_value, octave_value, bool>
{
bool
operator()(const octave_value& v, const octave_value& w) const
{
octave_value res = do_binary_op(octave_value::op_le,v,w);
return res.is_true();
}
};
}
namespace swig {
inline size_t
check_index(ptrdiff_t i, size_t size, bool insert = false) {
if ( i < 0 ) {
if ((size_t) (-i) <= size)
return (size_t) (i + size);
} else if ( (size_t) i < size ) {
return (size_t) i;
} else if (insert && ((size_t) i == size)) {
return size;
}
throw std::out_of_range("index out of range");
}
inline size_t
slice_index(ptrdiff_t i, size_t size) {
if ( i < 0 ) {
if ((size_t) (-i) <= size) {
return (size_t) (i + size);
} else {
throw std::out_of_range("index out of range");
}
} else {
return ( (size_t) i < size ) ? ((size_t) i) : size;
}
}
template <class Sequence, class Difference>
inline typename Sequence::iterator
getpos(Sequence* self, Difference i) {
typename Sequence::iterator pos = self->begin();
std::advance(pos, check_index(i,self->size()));
return pos;
}
template <class Sequence, class Difference>
inline typename Sequence::const_iterator
cgetpos(const Sequence* self, Difference i) {
typename Sequence::const_iterator pos = self->begin();
std::advance(pos, check_index(i,self->size()));
return pos;
}
template <class Sequence, class Difference>
inline Sequence*
getslice(const Sequence* self, Difference i, Difference j) {
typename Sequence::size_type size = self->size();
typename Sequence::size_type ii = swig::check_index(i, size);
typename Sequence::size_type jj = swig::slice_index(j, size);
if (jj > ii) {
typename Sequence::const_iterator vb = self->begin();
typename Sequence::const_iterator ve = self->begin();
std::advance(vb,ii);
std::advance(ve,jj);
return new Sequence(vb, ve);
} else {
return new Sequence();
}
}
template <class Sequence, class Difference, class InputSeq>
inline void
setslice(Sequence* self, Difference i, Difference j, const InputSeq& v) {
typename Sequence::size_type size = self->size();
typename Sequence::size_type ii = swig::check_index(i, size, true);
typename Sequence::size_type jj = swig::slice_index(j, size);
if (jj < ii) jj = ii;
size_t ssize = jj - ii;
if (ssize <= v.size()) {
typename Sequence::iterator sb = self->begin();
typename InputSeq::const_iterator vmid = v.begin();
std::advance(sb,ii);
std::advance(vmid, jj - ii);
self->insert(std::copy(v.begin(), vmid, sb), vmid, v.end());
} else {
typename Sequence::iterator sb = self->begin();
typename Sequence::iterator se = self->begin();
std::advance(sb,ii);
std::advance(se,jj);
self->erase(sb,se);
self->insert(sb, v.begin(), v.end());
}
}
template <class Sequence, class Difference>
inline void
delslice(Sequence* self, Difference i, Difference j) {
typename Sequence::size_type size = self->size();
typename Sequence::size_type ii = swig::check_index(i, size, true);
typename Sequence::size_type jj = swig::slice_index(j, size);
if (jj > ii) {
typename Sequence::iterator sb = self->begin();
typename Sequence::iterator se = self->begin();
std::advance(sb,ii);
std::advance(se,jj);
self->erase(sb,se);
}
}
}
}
%fragment("OctSequence_Cont","header",
fragment="StdTraits",
fragment="OctSequence_Base",
fragment="OctSwigIterator_T")
{
namespace swig
{
template <class T>
struct OctSequence_Ref // * octave can't support these, because of how assignment works
{
OctSequence_Ref(const octave_value& seq, int index)
: _seq(seq), _index(index)
{
}
operator T () const
{
// swig::SwigVar_PyObject item = OctSequence_GetItem(_seq, _index);
octave_value item; // * todo
try {
return swig::as<T>(item, true);
} catch (std::exception& e) {
char msg[1024];
sprintf(msg, "in sequence element %d ", _index);
if (!Octave_Error_Occurred()) {
%type_error(swig::type_name<T>());
}
SWIG_Octave_AddErrorMsg(msg);
SWIG_Octave_AddErrorMsg(e.what());
throw;
}
}
OctSequence_Ref& operator=(const T& v)
{
// OctSequence_SetItem(_seq, _index, swig::from<T>(v));
// * todo
return *this;
}
private:
octave_value _seq;
int _index;
};
template <class T>
struct OctSequence_ArrowProxy
{
OctSequence_ArrowProxy(const T& x): m_value(x) {}
const T* operator->() const { return &m_value; }
operator const T*() const { return &m_value; }
T m_value;
};
template <class T, class Reference >
struct OctSequence_InputIterator
{
typedef OctSequence_InputIterator<T, Reference > self;
typedef std::random_access_iterator_tag iterator_category;
typedef Reference reference;
typedef T value_type;
typedef T* pointer;
typedef int difference_type;
OctSequence_InputIterator()
{
}
OctSequence_InputIterator(const octave_value& seq, int index)
: _seq(seq), _index(index)
{
}
reference operator*() const
{
return reference(_seq, _index);
}
OctSequence_ArrowProxy<T>
operator->() const {
return OctSequence_ArrowProxy<T>(operator*());
}
bool operator==(const self& ri) const
{
return (_index == ri._index);
}
bool operator!=(const self& ri) const
{
return !(operator==(ri));
}
self& operator ++ ()
{
++_index;
return *this;
}
self& operator -- ()
{
--_index;
return *this;
}
self& operator += (difference_type n)
{
_index += n;
return *this;
}
self operator +(difference_type n) const
{
return self(_seq, _index + n);
}
self& operator -= (difference_type n)
{
_index -= n;
return *this;
}
self operator -(difference_type n) const
{
return self(_seq, _index - n);
}
difference_type operator - (const self& ri) const
{
return _index - ri._index;
}
bool operator < (const self& ri) const
{
return _index < ri._index;
}
reference
operator[](difference_type n) const
{
return reference(_seq, _index + n);
}
private:
octave_value _seq;
difference_type _index;
};
template <class T>
struct OctSequence_Cont
{
typedef OctSequence_Ref<T> reference;
typedef const OctSequence_Ref<T> const_reference;
typedef T value_type;
typedef T* pointer;
typedef int difference_type;
typedef int size_type;
typedef const pointer const_pointer;
typedef OctSequence_InputIterator<T, reference> iterator;
typedef OctSequence_InputIterator<T, const_reference> const_iterator;
OctSequence_Cont(const octave_value& seq) : _seq(seq)
{
// * assert that we have map type etc.
/*
if (!OctSequence_Check(seq)) {
throw std::invalid_argument("a sequence is expected");
}
_seq = seq;
Py_INCREF(_seq);
*/
}
~OctSequence_Cont()
{
}
size_type size() const
{
// return static_cast<size_type>(OctSequence_Size(_seq));
return 0; // * todo
}
bool empty() const
{
return size() == 0;
}
iterator begin()
{
return iterator(_seq, 0);
}
const_iterator begin() const
{
return const_iterator(_seq, 0);
}
iterator end()
{
return iterator(_seq, size());
}
const_iterator end() const
{
return const_iterator(_seq, size());
}
reference operator[](difference_type n)
{
return reference(_seq, n);
}
const_reference operator[](difference_type n) const
{
return const_reference(_seq, n);
}
bool check(bool set_err = true) const
{
int s = size();
for (int i = 0; i < s; ++i) {
// swig::SwigVar_PyObject item = OctSequence_GetItem(_seq, i);
octave_value item; // * todo
if (!swig::check<value_type>(item)) {
if (set_err) {
char msg[1024];
sprintf(msg, "in sequence element %d", i);
SWIG_Error(SWIG_RuntimeError, msg);
}
return false;
}
}
return true;
}
private:
octave_value _seq;
};
}
}
%define %swig_sequence_iterator(Sequence...)
#if defined(SWIG_EXPORT_ITERATOR_METHODS)
class iterator;
class reverse_iterator;
class const_iterator;
class const_reverse_iterator;
%typemap(out,noblock=1,fragment="OctSequence_Cont")
iterator, reverse_iterator, const_iterator, const_reverse_iterator {
$result = SWIG_NewPointerObj(swig::make_output_iterator(%static_cast($1,const $type &)),
swig::OctSwigIterator::descriptor(),SWIG_POINTER_OWN);
}
%typemap(out,fragment="OctSequence_Cont")
std::pair<iterator, iterator>, std::pair<const_iterator, const_iterator> {
octave_value_list tmpc;
tmpc.append(SWIG_NewPointerObj(swig::make_output_iterator(%static_cast($1,const $type &).first),
swig::OctSwigIterator::descriptor(),SWIG_POINTER_OWN));
tmpc.append(SWIG_NewPointerObj(swig::make_output_iterator(%static_cast($1,const $type &).second),
swig::OctSwigIterator::descriptor(),SWIG_POINTER_OWN));
$result = Cell(tmpc);
}
%fragment("SwigPyPairBoolOutputIterator","header",fragment=SWIG_From_frag(bool),fragment="OctSequence_Cont") {}
%typemap(out,fragment="OctPairBoolOutputIterator")
std::pair<iterator, bool>, std::pair<const_iterator, bool> {
octave_value_list tmpc;
tmpc.append(SWIG_NewPointerObj(swig::make_output_iterator(%static_cast($1,const $type &).first),
swig::OctSwigIterator::descriptor(),SWIG_POINTER_OWN));
tmpc.append(SWIG_From(bool)(%static_cast($1,const $type &).second));
$result = Cell(tmpc);
}
%typemap(in,noblock=1,fragment="OctSequence_Cont")
iterator(swig::OctSwigIterator *iter = 0, int res),
reverse_iterator(swig::OctSwigIterator *iter = 0, int res),
const_iterator(swig::OctSwigIterator *iter = 0, int res),
const_reverse_iterator(swig::OctSwigIterator *iter = 0, int res) {
res = SWIG_ConvertPtr($input, %as_voidptrptr(&iter), swig::OctSwigIterator::descriptor(), 0);
if (!SWIG_IsOK(res) || !iter) {
%argument_fail(SWIG_TypeError, "$type", $symname, $argnum);
} else {
swig::OctSwigIterator_T<$type > *iter_t = dynamic_cast<swig::OctSwigIterator_T<$type > *>(iter);
if (iter_t) {
$1 = iter_t->get_current();
} else {
%argument_fail(SWIG_TypeError, "$type", $symname, $argnum);
}
}
}
%typecheck(%checkcode(ITERATOR),noblock=1,fragment="OctSequence_Cont")
iterator, reverse_iterator, const_iterator, const_reverse_iterator {
swig::OctSwigIterator *iter = 0;
int res = SWIG_ConvertPtr($input, %as_voidptrptr(&iter), swig::OctSwigIterator::descriptor(), 0);
$1 = (SWIG_IsOK(res) && iter && (dynamic_cast<swig::OctSwigIterator_T<$type > *>(iter) != 0));
}
%fragment("OctSequence_Cont");
#endif //SWIG_EXPORT_ITERATOR_METHODS
%enddef
// The octave container methods
%define %swig_container_methods(Container...)
%enddef
%define %swig_sequence_methods_common(Sequence...)
%swig_sequence_iterator(%arg(Sequence))
%swig_container_methods(%arg(Sequence))
%fragment("OctSequence_Base");
%extend {
value_type pop() throw (std::out_of_range) {
if (self->size() == 0)
throw std::out_of_range("pop from empty container");
Sequence::value_type x = self->back();
self->pop_back();
return x;
}
value_type __paren__(difference_type i) throw (std::out_of_range) {
return *(swig::cgetpos(self, i));
}
void __paren_asgn__(difference_type i, value_type x) throw (std::out_of_range) {
*(swig::getpos(self,i)) = x;
}
void append(value_type x) {
self->push_back(x);
}
}
%enddef
%define %swig_sequence_methods(Sequence...)
%swig_sequence_methods_common(%arg(Sequence))
%enddef
%define %swig_sequence_methods_val(Sequence...)
%swig_sequence_methods_common(%arg(Sequence))
%enddef
//
// Common fragments
//
%fragment("StdSequenceTraits","header",
fragment="StdTraits",
fragment="OctSequence_Cont")
{
namespace swig {
template <class OctSeq, class Seq>
inline void
assign(const OctSeq& octseq, Seq* seq) {
%#ifdef SWIG_STD_NOASSIGN_STL
typedef typename OctSeq::value_type value_type;
typename OctSeq::const_iterator it = octseq.begin();
for (;it != octseq.end(); ++it) {
seq->insert(seq->end(),(value_type)(*it));
}
%#else
seq->assign(octseq.begin(), octseq.end());
%#endif
}
template <class Seq, class T = typename Seq::value_type >
struct traits_asptr_stdseq {
typedef Seq sequence;
typedef T value_type;
static int asptr(const octave_value& obj, sequence **seq) {
if (!obj.is_defined() || Swig::swig_value_deref(obj)) {
sequence *p;
if (SWIG_ConvertPtr(obj,(void**)&p,
swig::type_info<sequence>(),0) == SWIG_OK) {
if (seq) *seq = p;
return SWIG_OLDOBJ;
}
} else if (obj.is_cell()) {
try {
OctSequence_Cont<value_type> octseq(obj);
if (seq) {
sequence *pseq = new sequence();
assign(octseq, pseq);
*seq = pseq;
return SWIG_NEWOBJ;
} else {
return octseq.check() ? SWIG_OK : SWIG_ERROR;
}
} catch (std::exception& e) {
if (seq&&!error_state)
error("swig type error: %s",e.what());
return SWIG_ERROR;
}
}
return SWIG_ERROR;
}
};
template <class Seq, class T = typename Seq::value_type >
struct traits_from_stdseq {
typedef Seq sequence;
typedef T value_type;
typedef typename Seq::size_type size_type;
typedef typename sequence::const_iterator const_iterator;
static octave_value from(const sequence& seq) {
#ifdef SWIG_OCTAVE_EXTRA_NATIVE_CONTAINERS
swig_type_info *desc = swig::type_info<sequence>();
if (desc && desc->clientdata) {
return SWIG_NewPointerObj(new sequence(seq), desc, SWIG_POINTER_OWN);
}
#endif
size_type size = seq.size();
if (size <= (size_type)INT_MAX) {
Cell c(size,1);
int i = 0;
for (const_iterator it = seq.begin();
it != seq.end(); ++it, ++i) {
c(i) = swig::from<value_type>(*it);
}
return c;
} else {
error("swig overflow error: sequence size not valid in octave");
return octave_value();
}
return octave_value();
}
};
}
}
|