1 %module(directors="1") director_ownership
2 
3 // Github issue #1184
4 
5 %include "std_string.i"
6 
7 %feature("director") example::ContentBase;
8 %feature("director") example::ContentDerived;
9 
10 %newobject example::make_content;
11 
12 %inline %{
13 #include <string>
14 
15 namespace example
16 {
17 
18 class ContentBase
19 {
20 public:
ContentBase()21     ContentBase() {}
~ContentBase()22     virtual ~ContentBase() {}
23     virtual std::string get_name() const = 0;
24 };
25 
26 
27 class ContentDerived: public ContentBase
28 {
29 public:
ContentDerived()30     ContentDerived():ContentBase() { m_name = "ContentDerived"; }
~ContentDerived()31     virtual ~ContentDerived() {}
get_name()32     virtual std::string get_name() const { return m_name; }
33 
34 private:
35     std::string m_name;
36 };
37 
38 
39 class Container
40 {
41 public:
Container()42     Container() { m_content = 0; }
~Container()43     ~Container()
44     {
45         clear_content();
46     }
47     // the container takes the ownership of the content
set_content(ContentBase * content)48     void set_content(ContentBase* content)
49     {
50         clear_content();
51         m_content = content;
52     }
get_content()53     ContentBase* get_content() { return m_content; }
54 
55 private:
clear_content()56     void clear_content()
57     {
58         if(m_content)
59         {
60             delete m_content;
61             m_content = 0;
62         }
63     }
64 
65 private:
66     ContentBase* m_content;
67 };
68 
make_content()69 static ContentBase* make_content() { return new ContentDerived(); }
70 
71 } // namespace example
72 %}
73