diff options
author | Luciano Wolf <luciano.wolf@openbossa.org> | 2009-11-30 18:30:39 -0300 |
---|---|---|
committer | Hugo Lima <hugo.lima@openbossa.org> | 2009-11-30 20:43:08 -0200 |
commit | 659ce4c3b9854b3670a7a2c6f685f73eafac53d4 (patch) | |
tree | bfc84d3fb9fa67d6b0d98721c373fcc4d7737256 | |
parent | fe71552f4f254d84f2563ee24f108f5dd2c559c6 (diff) | |
download | shiboken-659ce4c3b9854b3670a7a2c6f685f73eafac53d4.tar.gz shiboken-659ce4c3b9854b3670a7a2c6f685f73eafac53d4.tar.xz shiboken-659ce4c3b9854b3670a7a2c6f685f73eafac53d4.zip |
Implement 'child return' examples and support.
-rw-r--r-- | tests/libsample/objecttype.cpp | 44 | ||||
-rw-r--r-- | tests/libsample/objecttype.h | 4 | ||||
-rwxr-xr-x | tests/samplebinding/child_return_test.py | 53 | ||||
-rw-r--r-- | tests/samplebinding/typesystem_sample.xml | 30 |
4 files changed, 131 insertions, 0 deletions
diff --git a/tests/libsample/objecttype.cpp b/tests/libsample/objecttype.cpp index ef72f11f..d691c696 100644 --- a/tests/libsample/objecttype.cpp +++ b/tests/libsample/objecttype.cpp @@ -50,6 +50,16 @@ ObjectType::~ObjectType() delete *child_iter; } +ObjectType* +ObjectType::createWithChild() +{ + ObjectType* parent = create(); + ObjectType* child = create(); + child->setObjectName("child"); + child->setParent(parent); + return parent; +} + void ObjectType::removeChild(ObjectType* child) { @@ -63,6 +73,40 @@ ObjectType::removeChild(ObjectType* child) } } +ObjectType* +ObjectType::takeChild(ObjectType* child) +{ + if (!child) + return 0; + + ObjectTypeList::iterator child_iter = std::find(m_children.begin(), m_children.end(), child); + if (child_iter != m_children.end()) { + m_children.erase(child_iter); + child->m_parent = 0; + return child; + } + return 0; +} + +ObjectType* +ObjectType::takeChild(const Str& name) +{ + return takeChild(findChild(name)); + +} + +ObjectType* +ObjectType::findChild(const Str& name) +{ + for (ObjectTypeList::iterator child_iter = m_children.begin(); + child_iter != m_children.end(); ++child_iter) { + + if ((*child_iter)->objectName() == name) + return *child_iter; + } + return 0; +} + void ObjectType::killChild(const Str& name) { diff --git a/tests/libsample/objecttype.h b/tests/libsample/objecttype.h index 4435ab6c..bd58b7b6 100644 --- a/tests/libsample/objecttype.h +++ b/tests/libsample/objecttype.h @@ -67,12 +67,16 @@ public: // factory method static ObjectType* create() { return new ObjectType(); } + static ObjectType* createWithChild(); void setParent(ObjectType* parent); ObjectType* parent() const { return m_parent; } const ObjectTypeList& children() const { return m_children; } void killChild(const Str& name); void removeChild(ObjectType* child); + ObjectType* takeChild(ObjectType* child); + ObjectType* takeChild(const Str& name); + ObjectType* findChild(const Str& name); Str objectName() const; void setObjectName(const Str& name); diff --git a/tests/samplebinding/child_return_test.py b/tests/samplebinding/child_return_test.py new file mode 100755 index 00000000..4fde7f5c --- /dev/null +++ b/tests/samplebinding/child_return_test.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# This file is part of the Shiboken Python Bindings Generator project. +# +# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +# +# Contact: PySide team <contact@pyside.org> +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public License +# version 2.1 as published by the Free Software Foundation. Please +# review the following information to ensure the GNU Lesser General +# Public License version 2.1 requirements will be met: +# http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +# # +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA + +'''The BlackBox class has cases of ownership transference between C++ and Python.''' + +import sys +import unittest + +from sample import * + +class ReturnOfChildTest(unittest.TestCase): + '''The BlackBox class has cases of ownership transference between C++ and Python.''' + + def testKillParentKeepingChild(self): + '''Ownership transference from Python to C++ and back again.''' + o1 = ObjectType.createWithChild() + child = o1.children()[0] + del o1 + self.assertRaises(RuntimeError, child.objectName) + + def testKillParentKeepingChild2(self): + '''Ownership transference from Python to C++ and back again.''' + o1 = ObjectType.createWithChild() + child = o1.findChild("child") + del o1 + self.assertRaises(RuntimeError, child.objectName) + +if __name__ == '__main__': + unittest.main() + diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml index 647eacf5..315bc03c 100644 --- a/tests/samplebinding/typesystem_sample.xml +++ b/tests/samplebinding/typesystem_sample.xml @@ -69,6 +69,11 @@ <define-ownership owner="target"/> </modify-argument> </modify-function> + <modify-function signature="createWithChild()"> + <modify-argument index="return"> + <define-ownership owner="target"/> + </modify-argument> + </modify-function> <modify-function signature="setParent(ObjectType*)"> <modify-argument index="1"> <parent index="this" action="add"/> @@ -83,6 +88,31 @@ } </inject-code> </modify-function> + <modify-function signature="takeChild(ObjectType*)"> + <modify-argument index="return"> + <define-ownership owner="target"/> + <parent index="this" action="remove"/> + </modify-argument> + </modify-function> + <modify-function signature="takeChild(const Str&)"> + <modify-argument index="return"> + <define-ownership owner="target"/> + <parent index="this" action="remove"/> + </modify-argument> + </modify-function> + <modify-function signature="findChild(const Str&)"> + <modify-argument index="this"> + <parent index="return" action="add"/> + </modify-argument> + </modify-function> + <modify-function signature="children()const"> + <inject-code class="target" position="end"> + Py_ssize_t max = PyList_GET_SIZE(%0); + for (int i = 0; i < max; ++i) { + Shiboken::setParent(%PYSELF, PyList_GET_ITEM(%0, i)); + } + </inject-code> + </modify-function> </object-type> <object-type name="ObjectTypeLayout" /> |