summaryrefslogtreecommitdiff
path: root/Examples/test-suite/director_ownership.i
blob: 48e9097588a942fe0e5bdade2e7b40be946018c4 (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
%module(directors="1") director_ownership

// Github issue #1184

%include "std_string.i"

%feature("director") example::ContentBase;
%feature("director") example::ContentDerived;

%newobject example::make_content;

%inline %{
#include <string>

namespace example
{

class ContentBase
{
public:
    ContentBase() {}
    virtual ~ContentBase() {}
    virtual std::string get_name() const = 0;
};


class ContentDerived: public ContentBase
{
public:
    ContentDerived():ContentBase() { m_name = "ContentDerived"; }
    virtual ~ContentDerived() {}
    virtual std::string get_name() const { return m_name; }

private:
    std::string m_name;
};


class Container
{
public:
    Container() { m_content = 0; }
    ~Container()
    {
        clear_content();
    }
    // the container takes the ownership of the content
    void set_content(ContentBase* content)
    {
        clear_content();
        m_content = content;
    }
    ContentBase* get_content() { return m_content; }

private:
    void clear_content()
    {
        if(m_content)
        {
            delete m_content;
            m_content = 0;
        }
    }
    
private:
    ContentBase* m_content;
};

static ContentBase* make_content() { return new ContentDerived(); }

} // namespace example
%}