import source from 1.3.40
[external/swig.git] / Examples / test-suite / dynamic_cast.i
1 /* File : example.i */
2 %module dynamic_cast
3
4 #if !defined(SWIGJAVA) && !defined(SWIGCSHARP)
5 %apply SWIGTYPE *DYNAMIC { Foo * };
6 #endif
7
8 %inline %{
9
10 class Foo {
11 public:
12   virtual ~Foo() { }
13   
14   virtual Foo *blah() {
15     return this;
16   }
17 };
18 %}
19
20 #if defined(SWIGJAVA) || defined(SWIGCSHARP)
21 %typemap(out) Foo *blah {
22     Bar *downcast = dynamic_cast<Bar *>($1);
23     *(Bar **)&$result = downcast;
24 }
25 #endif
26
27 #if defined(SWIGJAVA)
28 %typemap(javaout) Foo * {
29     return new Bar($jnicall, $owner);
30   }
31 #endif
32
33 #if defined(SWIGCSHARP)
34 %typemap(csout, excode=SWIGEXCODE) Foo * {
35     Bar ret = new Bar($imcall, $owner);$excode
36     return ret;
37   }
38 #endif
39
40 %inline %{
41
42 class Bar : public Foo {
43 public:
44    virtual Foo *blah() {
45        return (Foo *) this;
46    }
47    virtual char * test() {
48        return (char *) "Bar::test";
49    }
50 };
51
52 char *do_test(Bar *b) {
53    return b->test();
54 }
55 %}
56
57 #if !defined(SWIGJAVA) && !defined(SWIGCSHARP)
58 // A general purpose function for dynamic casting of a Foo *
59 %{
60 static swig_type_info *
61 Foo_dynamic(void **ptr) {
62    Bar *b;
63    b = dynamic_cast<Bar *>((Foo *) *ptr);
64    if (b) {
65       *ptr = (void *) b;
66       return SWIGTYPE_p_Bar;
67    }
68    return 0;
69 }
70 %}
71
72 // Register the above casting function
73 DYNAMIC_CAST(SWIGTYPE_p_Foo, Foo_dynamic);
74
75 #endif
76