Debian Codegraph @

tests/foo.h

raw symbols SCIP
1 // -*- Mode: C++; c-file-style: "stroustrup"; indent-tabs-mode:nil; -*-
2 #ifndef FOO_H_
3 # define FOO_H_
4
5 #include <Python.h>
6 #include <string>
7 #include <iostream>
8 #include <sstream>
9 #include <vector>
10 #include <map>
11 #include <set>
12 #include <exception>
13 #include <algorithm>
14 #include <stdexcept>
15
16 #include <stdint.h>
17
18 // Deprecation warnings look ugly and confusing; better to just
19 // disable them and change this macro when we want to specifically
20 // test them.
21 #define ENABLE_DEPRECATIONS 0
22
23 #ifndef DEPRECATED
24 # if ENABLE_DEPRECATIONS && __GNUC__ > 2
25 # define DEPRECATED __attribute__((deprecated))
26 # else
27 # define DEPRECATED
28 # endif
29 #endif
30
31 // Yes, this code is stupid, I know; it is only meant as an example!
32
33 int print_something(const char *message) DEPRECATED;
34 int print_something_else(const char *message2);
35
36 /* -#- name=get_int -#- */
37 int get_int_from_string(const char *from_string, int multiplier=1);
38 // -#- name=get_int; @multiplier(default_value=1) -#-
39 int get_int_from_float(double from_float, int multiplier);
40
41 // In this example PointerHolder<T> automatically implies
42 // caller_owns_return=True when used as ReturnValue, and
43 // transfer_ownership=False when used as parameter.
44 template <typename T>
45 struct PointerHolder
46 {
47 T *thePointer;
48 };
49
50
51 // -#- automatic_type_narrowing=True -#-
52 class Foo
53 {
54 std::string m_datum;
55 bool m_initialized;
56 public:
57 static int instance_count;
58
59 Foo () : m_datum (""), m_initialized (false)
60 { Foo::instance_count++; }
61
62 Foo (int xpto) DEPRECATED : m_initialized (false) { xpto++; }
63
64 Foo (std::string const &datum) : m_datum (datum), m_initialized (false)
65 { Foo::instance_count++; }
66 const std::string get_datum () const { return m_datum; }
67
68 std::string get_datum_deprecated () const DEPRECATED { return m_datum; }
69
70 Foo (Foo const & other) : m_datum (other.get_datum ()), m_initialized (false)
71 { Foo::instance_count++; }
72
73 void initialize () { m_initialized = true; }
74 bool is_initialized () const { return m_initialized; }
75
76 virtual ~Foo() { Foo::instance_count--; }
77
78 static int add_sub (int a, int b=3, bool subtract=false)
79 {
80 return a+b;
81 }
82 };
83
84 inline std::ostream & operator << (std::ostream &os, Foo const &foo)
85 {
86 os << foo.get_datum ();
87 return os;
88 }
89
90 inline bool is_unique(const Foo& foo)
91 {
92 return foo.instance_count == 1;
93 }
94
95 // -#- automatic_type_narrowing=True -#-
96 class Zoo
97 {
98 std::string m_datum;
99 public:
100 Zoo () : m_datum ("")
101 {}
102 Zoo (std::string datum) : m_datum (datum)
103 {}
104 virtual ~Zoo() {}
105 std::string get_datum () const { return m_datum; }
106
107 operator Foo() const {
108 return Foo(m_datum);
109 }
110 };
111
112
113 class ClassThatTakesFoo
114 {
115 Foo m_foo;
116 public:
117 ClassThatTakesFoo(Foo foo) : m_foo(foo) {}
118 Foo get_foo () const { return m_foo; }
119 };
120
121 extern Foo g_foo;
122
123 void function_that_takes_foo(Foo foo);
124 Foo function_that_returns_foo();
125
126
127 class Bar : public Foo
128 {
129 public:
130 static std::string Hooray () {
131 return std::string ("Hooray!");
132 }
133 virtual ~Bar() {}
134 };
135
136 // caller owns return
137 // -#- @return(caller_owns_return=true) -#-
138 Foo* get_hidden_subclass_pointer ();
139
140 // -#- incref_method=Ref; decref_method=Unref; peekref_method=GetReferenceCount -#-
141 class Zbr
142 {
143 int m_refcount;
144 std::string m_datum;
145 public:
146 Zbr () : m_refcount (1), m_datum ("")
147 { Zbr::instance_count++; }
148 Zbr (std::string datum) : m_refcount (1), m_datum (datum)
149 { Zbr::instance_count++; }
150
151 std::string get_datum () const { return m_datum; }
152
153 Zbr (Zbr const & other) :
154 m_refcount (1), m_datum (other.get_datum ())
155 {Zbr::instance_count++;}
156
157 void Ref () {
158 // std::cerr << "Ref Zbr " << this << " from " << m_refcount << std::endl;
159 ++m_refcount;
160 }
161 void Unref () {
162 // std::cerr << "Unref Zbr " << this << " from " << m_refcount << std::endl;
163 if (--m_refcount == 0)
164 delete this;
165 }
166 int GetReferenceCount () const { return m_refcount; }
167
168 virtual int get_int (int x) {
169 return x;
170 }
171
172 static int instance_count;
173
174 virtual ~Zbr () {
175 --Zbr::instance_count;
176 }
177
178 // -#- @foobaz(transfer_ownership=true, direction=out) -#-
179 int get_value (int* foobaz) { *foobaz = 123; return -1; }
180 };
181
182 // -#- @zbr(transfer_ownership=true) -#-
183 void store_zbr (Zbr *zbr);
184 int invoke_zbr (int x);
185 void delete_stored_zbr (void);
186
187
188 // -#- allow_subclassing=true -#-
189 class Foobar
190 {
191 Foobar (const Foobar &);
192 Foobar& operator= (const Foobar &);
193
194 public:
195 static int instance_count;
196
197 Foobar ()
198 { Foobar::instance_count++; }
199
200 virtual ~Foobar() { Foobar::instance_count--; }
201 };
202
203
204 class SomeObject
205 {
206 public:
207 std::string m_prefix;
208
209 enum {
210 TYPE_FOO,
211 TYPE_BAR,
212 } type;
213
214 static int instance_count;
215
216
217 // A nested class
218 // -#- automatic_type_narrowing=True -#-
219 class NestedClass
220 {
221 std::string m_datum;
222 public:
223 static int instance_count;
224
225 NestedClass () : m_datum ("")
226 { Foo::instance_count++; }
227 NestedClass (std::string datum) : m_datum (datum)
228 { Foo::instance_count++; }
229 std::string get_datum () const { return m_datum; }
230
231 NestedClass (NestedClass const & other) : m_datum (other.get_datum ())
232 { Foo::instance_count++; }
233
234 virtual ~NestedClass() { NestedClass::instance_count--; }
235 };
236
237 // A nested enum
238 enum NestedEnum {
239 FOO_TYPE_AAA,
240 FOO_TYPE_BBB,
241 FOO_TYPE_CCC,
242 };
243
244
245 // An anonymous nested enum
246 enum {
247 CONSTANT_A,
248 CONSTANT_B,
249 CONSTANT_C
250 };
251
252
253 private:
254 Foo m_foo_value;
255 Foo *m_foo_ptr;
256 Foo *m_foo_shared_ptr;
257 Zbr *m_zbr;
258 Zbr *m_internal_zbr;
259
260 PyObject *m_pyobject;
261 Foobar *m_foobar;
262
263 SomeObject ();
264
265 public:
266
267 static std::string staticData;
268
269 virtual ~SomeObject ();
270 SomeObject (const SomeObject &other);
271 SomeObject (std::string const prefix);
272 SomeObject (int prefix_len);
273
274 // -#- @message(direction=inout) -#-
275 int add_prefix (std::string& message) {
276 message = m_prefix + message;
277 return message.size ();
278 }
279
280 // -#- @message(direction=inout) -#-
281 int operator() (std::string& message) {
282 message = m_prefix + message;
283 return message.size ();
284 }
285
286 // -------- Virtual methods ----------
287 virtual std::string get_prefix () const {
288 return m_prefix;
289 }
290
291 std::string call_get_prefix () const {
292 return get_prefix();
293 }
294
295 virtual std::string get_prefix_with_foo_value (Foo foo) const {
296 return m_prefix + foo.get_datum();
297 }
298
299 // -#- @foo(direction=inout) -#-
300 virtual std::string get_prefix_with_foo_ref (const Foo &foo) const {
301 return m_prefix + foo.get_datum ();
302 }
303
304 virtual std::string get_prefix_with_foo_ptr (const Foo *foo) const {
305 return m_prefix + foo->get_datum ();
306 }
307
308
309 // A couple of overloaded virtual methods
310 virtual std::string get_something () const {
311 return "something";
312 }
313 virtual std::string get_something (int x) const {
314 std::stringstream out;
315 out << x;
316 return out.str ();
317 }
318
319 // -#- @pyobject(transfer_ownership=false) -#-
320 virtual void set_pyobject (PyObject *pyobject) {
321 if (m_pyobject) {
322 Py_DECREF(m_pyobject);
323 }
324 Py_INCREF(pyobject);
325 m_pyobject = pyobject;
326 }
327
328 // -#- @return(caller_owns_return=true) -#-
329 virtual PyObject* get_pyobject (void) {
330 if (m_pyobject) {
331 Py_INCREF(m_pyobject);
332 return m_pyobject;
333 } else {
334 return NULL;
335 }
336 }
337
338 // pass by value, direction=in
339 void set_foo_value (Foo foo) {
340 m_foo_value = foo;
341 }
342
343 // pass by reference, direction=in
344 void set_foo_by_ref (const Foo& foo) {
345 m_foo_value = foo;
346 }
347
348 // pass by reference, direction=out
349 // -#- @foo(direction=out) -#-
350 void get_foo_by_ref (Foo& foo) {
351 foo = m_foo_value;
352 }
353
354 // -#- @foo(transfer_ownership=true) -#-
355 void set_foo_ptr (Foo *foo) {
356 if (m_foo_ptr)
357 delete m_foo_ptr;
358 m_foo_ptr = foo;
359 }
360
361 // -#- @foo(transfer_ownership=false) -#-
362 void set_foo_shared_ptr (Foo *foo) {
363 m_foo_shared_ptr = foo;
364 }
365
366 // return value
367 Foo get_foo_value () {
368 return m_foo_value;
369 }
370
371 // -#- @return(caller_owns_return=false) -#-
372 const Foo * get_foo_shared_ptr () {
373 return m_foo_shared_ptr;
374 }
375
376 // -#- @return(caller_owns_return=true) -#-
377 Foo * get_foo_ptr () {
378 Foo *foo = m_foo_ptr;
379 m_foo_ptr = NULL;
380 return foo;
381 }
382
383 // -#- @return(caller_owns_return=true) -#-
384 Zbr* get_zbr () {
385 if (m_zbr)
386 {
387 m_zbr->Ref ();
388 return m_zbr;
389 } else
390 return NULL;
391 }
392
393 // -#- @return(caller_owns_return=true) -#-
394 Zbr* get_internal_zbr () {
395 m_internal_zbr->Ref ();
396 return m_internal_zbr;
397 }
398
399 // return reference counted object, caller does not own return
400 // -#- @return(caller_owns_return=false) -#-
401 Zbr* peek_zbr () { return m_zbr; }
402
403 // pass reference counted object, transfer ownership
404 // -#- @zbr(transfer_ownership=true) -#-
405 void set_zbr_transfer (Zbr *zbr) {
406 if (m_zbr)
407 m_zbr->Unref ();
408 m_zbr = zbr;
409 }
410
411 // pass reference counted object, does not transfer ownership
412 // -#- @zbr(transfer_ownership=false) -#-
413 void set_zbr_shared (Zbr *zbr) {
414 if (m_zbr)
415 m_zbr->Unref ();
416 zbr->Ref ();
417 m_zbr = zbr;
418 }
419
420
421 // return reference counted object, caller does not own return
422 PointerHolder<Zbr> get_zbr_pholder () {
423 PointerHolder<Zbr> foo = { m_zbr };
424 m_zbr->Ref ();
425 return foo;
426 }
427
428 // pass reference counted object, transfer ownership
429 void set_zbr_pholder (PointerHolder<Zbr> zbr) {
430 if (m_zbr)
431 m_zbr->Unref ();
432 m_zbr = zbr.thePointer;
433 m_zbr->Ref ();
434 }
435
436 int get_int (const char *from_string);
437 int get_int (double from_float);
438
439 // custodian/ward tests
440
441 // -#- @return(custodian=0, reference_existing_object=true) -#-
442 Foobar* get_foobar_with_self_as_custodian () {
443 if (m_foobar == NULL) {
444 m_foobar = new Foobar;
445 }
446 return m_foobar;
447 }
448 // -#- @return(custodian=1, reference_existing_object=true); @other(transfer_ownership=false) -#-
449 Foobar* get_foobar_with_other_as_custodian (SomeObject *other) {
450 return other->get_foobar_with_self_as_custodian ();
451 }
452 // -#- @foobar(custodian=0, transfer_ownership=True) -#-
453 void set_foobar_with_self_as_custodian (Foobar *foobar) {
454 delete m_foobar;
455 m_foobar = foobar;
456 }
457
458 virtual const char* method_returning_cstring() const { return "foobar"; }
459
460 protected:
461 std::string protected_method_that_is_not_virtual (std::string arg) const;
462 };
463
464
465 // A function that will appear as a method of SomeObject
466 // -#- as_method=get_something_prefixed; of_class=SomeObject -#-
467 // -#- @obj(transfer_ownership=false) -#-
468 std::string some_object_get_something_prefixed(const SomeObject *obj, const std::string something);
469
470 // -#- as_method=val_get_something_prefixed; of_class=SomeObject -#-
471 std::string some_object_val_get_something_prefixed(SomeObject obj, const std::string something);
472 // -#- as_method=ref_get_something_prefixed; of_class=SomeObject -#-
473 std::string some_object_ref_get_something_prefixed(const SomeObject &obj, const std::string something);
474
475
476 // Transfer ownership of 'obj' to the library
477 // -#- @obj(transfer_ownership=true) -#-
478 void store_some_object(SomeObject *obj);
479
480 // Invokes the virtual method in the stored SomeObject
481 std::string invoke_some_object_get_prefix();
482
483 // Transfer ownership of 'obj' away from the library
484 // -#- @return(caller_owns_return=true) -#-
485 SomeObject* take_some_object();
486
487 // Deletes the contained object, if any
488 void delete_some_object();
489
490
491 namespace xpto
492 {
493 typedef uint32_t FlowId;
494 FlowId get_flow_id (FlowId flowId);
495
496 std::string some_function();
497
498 class SomeClass
499 {
500 public:
501 SomeClass() {}
502 };
503
504 enum FooType {
505 FOO_TYPE_AAA,
506 FOO_TYPE_BBB,
507 FOO_TYPE_CCC,
508 };
509
510 FooType get_foo_type ();
511 void set_foo_type (FooType type);
512
513 // -#- @type(direction=inout) -#-
514 void set_foo_type_ptr (FooType *type);
515
516 // -#- @type(direction=inout) -#-
517 void set_foo_type_inout (FooType &type);
518
519 typedef Foo FooXpto;
520
521 std::string get_foo_datum(FooXpto const &foo);
522
523 struct XptoClass
524 {
525 // -#- @return(caller_owns_return=true) -#-
526 SomeClass* GetSomeClass()
527 {
528 return NULL;
529 }
530 };
531 }
532
533 // -#- @return(custodian=1, reference_existing_object=true); @other(transfer_ownership=false) -#-
534 Foobar* get_foobar_with_other_as_custodian(SomeObject *other);
535
536 // -#- @return(caller_owns_return=true) -#-
537 Foobar* create_new_foobar();
538 // -#- @foobar(custodian=2, transfer_ownership=true); @other(transfer_ownership=false) -#-
539 void set_foobar_with_other_as_custodian(Foobar *foobar, SomeObject *other);
540 // -#- @foobar(custodian=-1, transfer_ownership=true); @return(caller_owns_return=true) -#-
541 SomeObject * set_foobar_with_return_as_custodian(Foobar *foobar);
542
543 // -#- is_singleton=true -#-
544 class SingletonClass
545 {
546 private:
547 static SingletonClass *m_instance;
548
549 SingletonClass () {}
550 ~SingletonClass () {}
551
552 public:
553 // -#- @return(caller_owns_return=true) -#-
554 static SingletonClass *GetInstance () {
555 if (!m_instance)
556 m_instance = new SingletonClass;
557 return m_instance;
558 }
559 };
560
561
562 class InterfaceId
563 {
564 public:
565 ~InterfaceId () {}
566 private:
567 InterfaceId () {}
568 friend InterfaceId make_interface_id ();
569 // -#- ignore -#-
570 friend InterfaceId make_object_interface_id ();
571 };
572
573 InterfaceId make_interface_id ();
574
575 template <typename T> std::string TypeNameGet (void)
576 {
577 return "unknown";
578 }
579
580
581 // -#- template_instance_names=int=>IntegerTypeNameGet -#-
582 template <> std::string TypeNameGet<int> (void);
583
584
585 // just to force a template instantiation
586 struct __foo__
587 {
588 std::string get ()
589 {
590 return TypeNameGet<int> ();
591 }
592 };
593
594
595 // Test code generation errors and error handling
596
597 class CannotBeConstructed
598 {
599 public:
600 ~CannotBeConstructed () {}
601
602 // This static method cannot be generated
603 static CannotBeConstructed get_value () {
604 return CannotBeConstructed ();
605 }
606
607 // This static method can be generated because caller-owns-return=true
608 // -#- @return(caller_owns_return=true) -#-
609 static CannotBeConstructed* get_ptr () {
610 return new CannotBeConstructed ();
611 }
612 private:
613 CannotBeConstructed () {}
614 };
615
616 // This function cannot be generated
617 inline CannotBeConstructed get_cannot_be_constructed_value () {
618 return CannotBeConstructed::get_value ();
619 }
620
621 // This static method can be generated because caller-owns-return=true
622 // -#- @return(caller_owns_return=true) -#-
623 inline CannotBeConstructed* get_cannot_be_constructed_ptr () {
624 return CannotBeConstructed::get_ptr ();
625 }
626
627 class AbstractBaseClass
628 {
629 public:
630 virtual ~AbstractBaseClass () {}
631
632 protected:
633 // A pure virtual method with a parameter type so strange that it
634 // cannot be possibly be automatically wrapped by pybindgen; this
635 // will cause the cannot_be_constructed flag on the class to be
636 // set to true, and allow us to test code generation error
637 // handling (see below).
638 virtual void do_something (char ****) = 0;
639
640 AbstractBaseClass () {}
641 };
642
643 class AbstractBaseClassImpl : public AbstractBaseClass
644 {
645 public:
646 virtual ~AbstractBaseClassImpl () {}
647
648 // -#- @return(caller_owns_return=true) -#-
649 static AbstractBaseClass* get_abstract_base_class_ptr1 ()
650 {
651 return new AbstractBaseClassImpl;
652 }
653
654 // This method will be scanned by gccxmlparser and generate an error
655 // that is only detected in code generation time.
656 // -#- @return(caller_owns_return=false) -#-
657 static AbstractBaseClass* get_abstract_base_class_ptr2 ()
658 {
659 static AbstractBaseClassImpl *singleton = NULL;
660 if (!singleton)
661 singleton = new AbstractBaseClassImpl;
662 return singleton;
663 }
664
665 protected:
666 virtual void do_something (char ****) {}
667
668 AbstractBaseClassImpl () {}
669 };
670
671
672 // -#- @return(caller_owns_return=true) -#-
673 inline AbstractBaseClass* get_abstract_base_class_ptr1 ()
674 {
675 return AbstractBaseClassImpl::get_abstract_base_class_ptr1 ();
676 }
677
678 // This function will be scanned by gccxmlparser and generate an error
679 // that is only detected in code generation time.
680 // -#- @return(caller_owns_return=false) -#-
681 inline AbstractBaseClass* get_abstract_base_class_ptr2 ()
682 {
683 static AbstractBaseClass *singleton = NULL;
684 if (!singleton)
685 singleton = AbstractBaseClassImpl::get_abstract_base_class_ptr1 ();
686 return singleton;
687 }
688
689
690 // Class to test private/protected virtual methods
691 class AbstractBaseClass2
692 {
693 protected:
694 AbstractBaseClass2 () {}
695
696 public:
697 virtual ~AbstractBaseClass2 () {}
698
699 int invoke_private_virtual (int x) const {
700 return private_virtual (x);
701 }
702
703 int invoke_protected_virtual (int x) const {
704 return protected_virtual (x);
705 }
706
707 int invoke_protected_pure_virtual (int x) const {
708 return protected_pure_virtual (x);
709 }
710
711 protected:
712 virtual int protected_virtual (int x) const { return x+1; }
713
714 virtual int protected_pure_virtual (int x) const = 0;
715
716 private:
717 virtual int private_virtual (int x) const = 0;
718
719 AbstractBaseClass2 (const AbstractBaseClass2 &other) { other.invoke_protected_virtual(0); }
720 };
721
722 // -#- allow_subclassing=true -#-
723 class AbstractXpto
724 {
725 public:
726 AbstractXpto () {}
727 virtual ~AbstractXpto () {}
728
729 virtual void something (int x) const = 0;
730 };
731
732 class AbstractXptoImpl : public AbstractXpto
733 {
734 public:
735 AbstractXptoImpl () {}
736
737 virtual void something (int x) const { x++; }
738 };
739
740
741 // Test anonymous structures
742 union Word
743 {
744 uint16_t word;
745 struct
746 {
747 uint8_t low, high;
748 };
749 };
750
751 // Test float array
752 // -#- @matrix(direction=in, array_length=6) -#-
753 float matrix_sum_of_elements (float *matrix);
754
755 // -#- @matrix(direction=out, array_length=6) -#-
756 void matrix_identity_new (float *matrix);
757
758 namespace TopNs
759 {
760
761 class OuterBase
762 {
763 public:
764 };
765
766 namespace PrefixBottomNs {
767 class PrefixInner : public OuterBase
768 {
769 public:
770 PrefixInner () {}
771 void Do (void) {}
772 };
773 }
774
775 }
776
777
778 // Bug #245097
779 typedef void (*Callback) (void);
780 void function_that_takes_callback (Callback cb);
781
782
783 // <Bug #246069>
784 struct Socket
785 {
786 virtual int Bind () { return -1; }
787 virtual int Bind (int address) { return address; }
788
789 virtual ~Socket () {}
790 };
791
792 struct UdpSocket : public Socket
793 {
794 virtual int Bind () { return 0; }
795 };
796 // </Bug #246069>
797
798
799 struct simple_struct_t
800 {
801 int xpto;
802 };
803
804 // -- Containers:
805
806 typedef std::vector<simple_struct_t> SimpleStructList;
807 typedef std::vector<simple_struct_t> SimpleStructVec;
808 typedef std::map<std::string, simple_struct_t> SimpleStructMap;
809
810
811 SimpleStructList get_simple_list ();
812 int set_simple_list (SimpleStructList list);
813
814 class TestContainer
815 {
816 public:
817
818 std::set<float> m_floatSet;
819
820 TestContainer () : m_vec (NULL) {
821 m_floatSet.insert (1.0);
822 m_floatSet.insert (2.0);
823 m_floatSet.insert (3.0);
824 }
825 virtual ~TestContainer () {
826 delete m_vec;
827 }
828
829 virtual SimpleStructList get_simple_list ();
830 virtual int set_simple_list (SimpleStructList list);
831
832 // -#- @inout_list(direction=inout) -#-
833 virtual int set_simple_list_by_ref (SimpleStructList &inout_list);
834
835 virtual SimpleStructVec get_simple_vec ();
836 virtual int set_simple_vec (SimpleStructVec vec);
837
838 virtual SimpleStructMap get_simple_map ();
839 virtual int set_simple_map (SimpleStructMap map);
840
841 // -#- @outVec(direction=out) -#-
842 void get_vec (std::vector<std::string> &outVec);
843
844 // -#- @inVec(direction=in, transfer_ownership=true) -#-
845 void set_vec_ptr (std::vector<std::string> *inVec);
846
847 // -#- @outVec(direction=out) -#-
848 void get_vec_ptr (std::vector<std::string> *outVec);
849
850 private:
851 SimpleStructList m_simpleList;
852 SimpleStructMap m_simpleMap;
853
854 std::vector<std::string> *m_vec;
855
856 };
857
858 std::map<std::string, int> get_map ();
859
860 std::set<uint32_t> get_set ();
861
862
863 // test binary operators
864
865 struct Tupl
866 {
867 int x, y;
868
869 inline Tupl operator * (Tupl const &b)
870 {
871 Tupl retval;
872 retval.x = x * b.x;
873 retval.y = y * b.y;
874 return retval;
875 }
876
877 inline Tupl operator / (Tupl const &b)
878 {
879 Tupl retval;
880 retval.x = x / b.x;
881 retval.y = y / b.y;
882 return retval;
883 }
884
885 inline bool operator == (Tupl const &b)
886 {
887 return (x == b.x && y == b.y);
888 }
889
890 inline bool operator != (Tupl const &b)
891 {
892 return (x != b.x || y != b.y);
893 }
894
895
896 inline Tupl operator - ()
897 {
898 Tupl retval;
899 retval.x = -x;
900 retval.y = -y;
901 return retval;
902 }
903
904 };
905
906 inline bool operator < (Tupl const &a, Tupl const &b)
907 {
908 return (a.x < b.x || (a.x == b.x && a.y < b.y));
909 }
910
911 inline bool operator <= (Tupl const &a, Tupl const &b)
912 {
913 return (a.x <= b.x || (a.x == b.x && a.y <= b.y));
914 }
915
916 inline bool operator > (Tupl const &a, Tupl const &b)
917 {
918 return (a.x > b.x || (a.x == b.x && a.y > b.y));
919 }
920
921 inline bool operator >= (Tupl const &a, Tupl const &b)
922 {
923 return (a.x >= b.x || (a.x == b.x && a.y >= b.y));
924 }
925
926 inline Tupl operator + (Tupl const &a, Tupl const &b)
927 {
928 Tupl retval;
929 retval.x = a.x + b.x;
930 retval.y = a.y + b.y;
931 return retval;
932 }
933
934 inline Tupl operator - (Tupl const &a, Tupl const &b)
935 {
936 Tupl retval;
937 retval.x = a.x - b.x;
938 retval.y = a.y - b.y;
939 return retval;
940 }
941
942 inline Tupl & operator += (Tupl &a, Tupl const &b)
943 {
944 a.x += b.x;
945 a.y += b.y;
946 return a;
947 }
948
949 inline Tupl & operator += (Tupl &a, int b)
950 {
951 a.x += b;
952 a.y += b;
953 return a;
954 }
955
956 inline Tupl & operator -= (Tupl &a, Tupl const &b)
957 {
958 a.x -= b.x;
959 a.y -= b.y;
960 return a;
961 }
962
963 inline Tupl & operator *= (Tupl &a, Tupl const &b)
964 {
965 a.x *= b.x;
966 a.y *= b.y;
967 return a;
968 }
969
970 inline Tupl & operator /= (Tupl &a, Tupl const &b)
971 {
972 a.x /= b.x;
973 a.y /= b.y;
974 return a;
975 }
976
977 class ManipulatedObject
978 {
979 private:
980 int m_value;
981
982 // disable the copy constructor
983 ManipulatedObject (const ManipulatedObject &ctor_arg);
984
985 public:
986 ManipulatedObject () : m_value (0) {}
987 void SetValue (int value) { m_value = value; }
988 int GetValue () const { return m_value; }
989 };
990
991
992 class ReferenceManipulator
993 {
994 ManipulatedObject m_obj;
995
996 public:
997 ReferenceManipulator () {}
998 virtual ~ReferenceManipulator () {}
999
1000 int manipulate_object () {
1001 do_manipulate_object (m_obj);
1002 return m_obj.GetValue ();
1003 }
1004
1005 // -#- @obj(direction=inout) -#-
1006 virtual void do_manipulate_object (ManipulatedObject &obj) = 0;
1007
1008 };
1009
1010
1011 class VectorLike
1012 {
1013 std::vector<double> m_vec;
1014
1015 public:
1016 VectorLike ()
1017 {
1018 }
1019 // -#- name=__len__ -#-
1020 std::vector<double>::size_type get_len () const
1021 {
1022 return m_vec.size ();
1023 }
1024 // -#- name=__add__-#-
1025 VectorLike add_VectorLike(const VectorLike& rhs)
1026 {
1027 VectorLike result(*this);
1028 std::copy(rhs.m_vec.begin(), rhs.m_vec.end(), std::back_inserter(result.m_vec));
1029 return result;
1030 }
1031 // -#- name=__iadd__-#-
1032 VectorLike& iadd_VectorLike(const VectorLike& rhs)
1033 {
1034 std::copy(rhs.m_vec.begin(), rhs.m_vec.end(), std::back_inserter(m_vec));
1035 return *this;
1036 }
1037 // -#- name=__mul__-#-
1038 VectorLike mul_VectorLike(const unsigned n)
1039 {
1040 VectorLike result;
1041 if (n > 0)
1042 {
1043 result.m_vec.reserve(n * m_vec.size());
1044 for (unsigned i = 0; i != n; ++i)
1045 {
1046 std::copy(m_vec.begin(), m_vec.end(), std::back_inserter(result.m_vec));
1047 }
1048 }
1049 return result;
1050 }
1051 // -#- name=__imul__-#-
1052 VectorLike& imul_VectorLike(const unsigned n)
1053 {
1054 if (n > 0)
1055 {
1056 const unsigned n0 = m_vec.size();
1057 m_vec.reserve(n * n0);
1058 for (unsigned i = 0; i < n - 1; ++i)
1059 {
1060 std::copy(m_vec.begin(), m_vec.begin() + n0, std::back_inserter(m_vec));
1061 }
1062 }
1063 else
1064 {
1065 *this = VectorLike();
1066 }
1067 return *this;
1068 }
1069 // -#- name=__setitem__ -#-
1070 int set_item (int index, double value)
1071 {
1072 const int n = this->get_len();
1073 index = (index < 0 ? (n + 1 + index) : index);
1074 if (index >= n) return -1;
1075 m_vec[index] = value;
1076 return 0;
1077 }
1078 // -#- name=__getitem__ -#-
1079 double get_item (int index) const
1080 {
1081 const int n = this->get_len();
1082 index = (index < 0 ? (n + 1 + index) : index);
1083 try
1084 {
1085 return m_vec.at(index);
1086 }
1087 catch (std::out_of_range)
1088 {
1089 PyErr_SetString(PyExc_IndexError, "Container index out of range");
1090 return 0.0;
1091 }
1092 }
1093 // -#- name=__setslice__ -#-
1094 int set_slice (int index1, int index2, const VectorLike& values)
1095 {
1096 const int n = this->get_len();
1097 index1 = (index1 < 0 ? (n + 1 + index1) : index1);
1098 index2 = (index2 < 0 ? (n + 1 + index2) : index2);
1099 if (index1 >= n or index2 > n) return -1;
1100 for (int i = index1; i < index2; ++i)
1101 {
1102 if (this->set_item(i, values.get_item(i - index1)) != 0) return -1;
1103 }
1104 return 0;
1105 }
1106 // -#- name=__getslice__ -#-
1107 VectorLike get_slice (int index1, int index2) const
1108 {
1109 VectorLike result;
1110 const int n = this->get_len();
1111 index1 = (index1 < 0 ? (n + 1 + index1) : index1);
1112 index2 = (index2 < 0 ? (n + 1 + index2) : index2);
1113 for (int i = index1; i < index2; ++i)
1114 {
1115 result.m_vec.push_back(this->get_item(i));
1116 }
1117 return result;
1118 }
1119 // -#- name=__contains__ -#-
1120 int contains_value (double value) const
1121 {
1122 return ((std::find(m_vec.begin(), m_vec.end(), value) == m_vec.end()) ?
1123 0 :
1124 1);
1125 }
1126
1127 void append (double value)
1128 {
1129 m_vec.push_back (value);
1130 }
1131
1132 typedef std::vector<double>::iterator Iterator;
1133 Iterator Begin () { return m_vec.begin (); }
1134 Iterator End () { return m_vec.end (); }
1135 };
1136
1137
1138
1139 class VectorLike2
1140 {
1141 std::vector<double> m_vec;
1142
1143 public:
1144 VectorLike2 ()
1145 {
1146 }
1147
1148 void append (double value)
1149 {
1150 m_vec.push_back (value);
1151 }
1152
1153 typedef std::vector<double>::iterator Iterator;
1154 Iterator Begin () { return m_vec.begin (); }
1155 Iterator End () { return m_vec.end (); }
1156 };
1157
1158
1159 class MapLike
1160 {
1161 std::map<int, double> m_map;
1162
1163 public:
1164 MapLike () {}
1165
1166 void set (int key, double value) { m_map[key] = value; }
1167
1168 typedef std::map<int, double>::iterator Iterator;
1169 Iterator Begin () { return m_map.begin (); }
1170 Iterator End () { return m_map.end (); }
1171 };
1172
1173
1174 // -#- exception -#-
1175 struct Error
1176 {
1177 std::string message;
1178 };
1179
1180 // -#- exception -#-
1181 struct DomainError : public Error
1182 {
1183 };
1184
1185
1186 // returns 1/x, raises DomainError if x == 0
1187 double my_inverse_func (double x) throw (DomainError);
1188 double my_inverse_func2 (double x) throw (std::exception);
1189
1190 // the following function throws an exception but forgets to declare it
1191 // -#- throw=std::exception -#-
1192 double my_inverse_func3 (double x);
1193
1194 class ClassThatThrows
1195 {
1196 public:
1197 ClassThatThrows (double x) throw (DomainError);
1198
1199 // returns 1/x, raises DomainError if x == 0
1200 double my_inverse_method (double x) throw (DomainError);
1201 double my_inverse_method2 (double x) throw (std::exception);
1202
1203 // the following method throws an exception but forgets to declare it
1204 // -#- throw=std::exception -#-
1205 double my_inverse_method3 (double x);
1206
1207 // -#- throw=std::out_of_range -#-
1208 int throw_out_of_range() throw (std::out_of_range);
1209
1210 virtual int throw_error () const throw (std::exception);
1211
1212 virtual ~ClassThatThrows () {}
1213 };
1214
1215
1216 // https://bugs.launchpad.net/pybindgen/+bug/450255
1217 class ProtectedConstructor
1218 {
1219 public:
1220 ProtectedConstructor() {}
1221 protected:
1222 // Not to be implemented
1223 ProtectedConstructor(ProtectedConstructor&);
1224 void operator=(ProtectedConstructor&);
1225 };
1226
1227
1228
1229 template<typename ValueType>
1230 class property
1231 {
1232 private:
1233 ValueType m_value;
1234
1235 public:
1236 property<ValueType> ()
1237 {
1238 }
1239
1240 operator ValueType()
1241
1242 {
1243 return m_value;
1244 }
1245
1246 const ValueType &operator =(const ValueType &value)
1247 {
1248 m_value = value;
1249 return value;
1250 }
1251 };
1252
1253 static inline std::string __property_inst()
1254 {
1255 property<std::string> prop;
1256 prop = std::string("foo");
1257 return (std::string) prop;
1258 }
1259
1260
1261
1262 class Box
1263 {
1264 Foobar m_foobar;
1265
1266 public:
1267 static int instance_count;
1268
1269 // -#- return_internal_reference=true -#-
1270 Foobar *m_internalFoobar;
1271
1272 Box () { ++instance_count; m_internalFoobar = &m_foobar; }
1273 virtual ~Box () { --instance_count;}
1274
1275 // -#- @return(reference_existing_object=true) -#-
1276 const Foobar* getFoobarInternalPtr () { return &m_foobar; }
1277
1278 // -#- @return(reference_existing_object=true) -#-
1279 Foobar& getFoobarInternalRef () { return m_foobar; }
1280
1281 // -#- @return(return_internal_reference=true) -#-
1282 Foobar* getFoobarInternalPtr2 () { return &m_foobar; }
1283
1284 // -#- @return(return_internal_reference=true) -#-
1285 Foobar& getFoobarInternalRef2 () { return m_foobar; }
1286
1287 };
1288
1289
1290 // multiple inheritance
1291 class MIRoot
1292 {
1293 public:
1294 MIRoot () {}
1295 int root_method () const { return -1; }
1296 };
1297
1298
1299 class MIBase1 : public virtual MIRoot
1300 {
1301 int m_value;
1302 public:
1303 MIBase1 () : m_value (1) {}
1304 int base1_method () const { return m_value; }
1305 };
1306
1307 class MIBase2 : public virtual MIRoot
1308 {
1309 int m_value;
1310 public:
1311 MIBase2 () : m_value (2) {}
1312 int base2_method () const { return m_value; }
1313 };
1314
1315 class MIMixed : public MIBase1, public MIBase2
1316 {
1317 public:
1318 MIMixed () {}
1319 int mixed_method () const { return 3; }
1320 };
1321
1322
1323 Tupl my_throwing_func () throw (std::exception);
1324
1325
1326 class IFoo
1327 {
1328 public:
1329 virtual void DoSomething() = 0;
1330 protected:
1331 virtual ~IFoo() {}
1332 };
1333
1334
1335 class IFooImpl : public IFoo
1336 {
1337 public:
1338 virtual void DoSomething() {}
1339 ~IFooImpl() {}
1340 };
1341
1342
1343 int test_args_kwargs(const char *args, const char *kwargs);
1344
1345 #endif /* !FOO_H_ */