From da4915fd1ddfd1e04a6ef21b0117d83fc7fe2d5d Mon Sep 17 00:00:00 2001 From: Luciano Wolf Date: Wed, 14 Apr 2010 19:14:21 -0300 Subject: Support to "only private constructor available" case. --- cppgenerator.cpp | 12 +++-- tests/libsample/privatector.h | 61 ++++++++++++++++++++++++ tests/samplebinding/CMakeLists.txt | 1 + tests/samplebinding/global.h | 1 + tests/samplebinding/privatector_test.py | 79 +++++++++++++++++++++++++++++++ tests/samplebinding/typesystem_sample.xml | 1 + 6 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 tests/libsample/privatector.h create mode 100755 tests/samplebinding/privatector_test.py diff --git a/cppgenerator.cpp b/cppgenerator.cpp index 68222adbe..226f7e507 100644 --- a/cppgenerator.cpp +++ b/cppgenerator.cpp @@ -1827,6 +1827,8 @@ void CppGenerator::writeClassDefinition(QTextStream& s, const AbstractMetaClass* QString cppClassName = metaClass->qualifiedCppName(); QString className = cpythonTypeName(metaClass).replace(QRegExp("_Type$"), ""); QString baseClassName('0'); + AbstractMetaFunctionList ctors = metaClass->queryFunctions(AbstractMetaClass::Constructors); + bool onlyPrivCtor = !metaClass->hasNonPrivateConstructor(); if (metaClass->hasArithmeticOperatorOverload() || metaClass->hasLogicalOperatorOverload() @@ -1847,7 +1849,10 @@ void CppGenerator::writeClassDefinition(QTextStream& s, const AbstractMetaClass* "Shiboken::deallocWrapperWithPrivateDtor" : "0"; tp_init = "0"; } else { - tp_flags = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_CHECKTYPES";//|Py_TPFLAGS_HAVE_GC"; + if (onlyPrivCtor) + tp_flags = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES"; + else + tp_flags = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_CHECKTYPES"; QString deallocClassName; if (shouldGenerateCppWrapper(metaClass)) @@ -1857,14 +1862,13 @@ void CppGenerator::writeClassDefinition(QTextStream& s, const AbstractMetaClass* tp_dealloc = "&Shiboken::deallocWrapper"; cpp_dtor = "&Shiboken::callCppDestructor<" + metaClass->qualifiedCppName() + " >"; - AbstractMetaFunctionList ctors = metaClass->queryFunctions(AbstractMetaClass::Constructors); - tp_init = ctors.isEmpty() ? "0" : cpythonFunctionName(ctors.first()); + tp_init = onlyPrivCtor ? "0" : cpythonFunctionName(ctors.first()); } if (classNeedsGetattroFunction(metaClass)) tp_getattro = cpythonGetattroFunctionName(metaClass); - if (metaClass->hasPrivateDestructor()) + if (metaClass->hasPrivateDestructor() || onlyPrivCtor) tp_new = "0"; else tp_new = "Shiboken::SbkBaseWrapper_TpNew"; diff --git a/tests/libsample/privatector.h b/tests/libsample/privatector.h new file mode 100644 index 000000000..5591f15f3 --- /dev/null +++ b/tests/libsample/privatector.h @@ -0,0 +1,61 @@ +/* + * This file is part of the Shiboken Python Binding Generator project. + * + * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). + * + * Contact: PySide team + * + * 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. + * + * As a special exception to the GNU Lesser General Public License + * version 2.1, the object code form of a "work that uses the Library" + * may incorporate material from a header file that is part of the + * Library. You may distribute such object code under terms of your + * choice, provided that the incorporated material (i) does not exceed + * more than 5% of the total size of the Library; and (ii) is limited to + * numerical parameters, data structure layouts, accessors, macros, + * inline functions and templates. + * + * 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 + */ + +#ifndef PRIVATECTOR_H +#define PRIVATECTOR_H + +#include "libsamplemacros.h" + +class LIBSAMPLE_API PrivateCtor +{ +public: + static PrivateCtor* instance() + { + static PrivateCtor self; + self.m_instanciations++; + return &self; + } + + int instanceCalls() + { + return m_instanciations; + } + +private: + int m_instanciations; + + PrivateCtor() : m_instanciations(0) {} +}; + +#endif diff --git a/tests/samplebinding/CMakeLists.txt b/tests/samplebinding/CMakeLists.txt index 7510b5b3f..06e35ecb8 100644 --- a/tests/samplebinding/CMakeLists.txt +++ b/tests/samplebinding/CMakeLists.txt @@ -45,6 +45,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/sample/pairuser_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/point_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/pointerholder_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/polygon_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/privatector_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/privatedtor_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/protectednonpolymorphic_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/protectedpolymorphic_wrapper.cpp diff --git a/tests/samplebinding/global.h b/tests/samplebinding/global.h index f54fb5166..c037a5b66 100644 --- a/tests/samplebinding/global.h +++ b/tests/samplebinding/global.h @@ -24,6 +24,7 @@ #include "point.h" #include "pointerholder.h" #include "polygon.h" +#include "privatector.h" #include "privatedtor.h" #include "protected.h" #include "reference.h" diff --git a/tests/samplebinding/privatector_test.py b/tests/samplebinding/privatector_test.py new file mode 100755 index 000000000..ea713670f --- /dev/null +++ b/tests/samplebinding/privatector_test.py @@ -0,0 +1,79 @@ +#!/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 +# +# 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 + +'''Test cases for a class with only a private constructor.''' + +import gc +import sys +import unittest + +from sample import PrivateCtor + + +class PrivateCtorTest(unittest.TestCase): + '''Test case for PrivateCtor class''' + + def testPrivateCtorInstanciation(self): + '''Test if instanciation of class with a private constructor raises an exception.''' + self.assertRaises(TypeError, PrivateCtor) + + def testPrivateCtorInheritance(self): + '''Test if inheriting from PrivateCtor raises an exception.''' + def inherit(): + class Foo(PrivateCtor): + pass + self.assertRaises(TypeError, inherit) + + def testPrivateCtorInstanceMethod(self): + '''Test if PrivateCtor.instance() method return the proper singleton.''' + pd1 = PrivateCtor.instance() + calls = pd1.instanceCalls() + self.assertEqual(type(pd1), PrivateCtor) + pd2 = PrivateCtor.instance() + self.assertEqual(pd2, pd1) + self.assertEqual(pd2.instanceCalls(), calls + 1) + + def testPrivateCtorRefCounting(self): + '''Test refcounting of the singleton returned by PrivateCtor.instance().''' + pd1 = PrivateCtor.instance() + calls = pd1.instanceCalls() + refcnt = sys.getrefcount(pd1) + pd2 = PrivateCtor.instance() + self.assertEqual(pd2.instanceCalls(), calls + 1) + self.assertEqual(sys.getrefcount(pd2), sys.getrefcount(pd1)) + self.assertEqual(sys.getrefcount(pd2), refcnt + 1) + del pd1 + self.assertEqual(sys.getrefcount(pd2), refcnt) + del pd2 + gc.collect() + pd3 = PrivateCtor.instance() + self.assertEqual(type(pd3), PrivateCtor) + self.assertEqual(pd3.instanceCalls(), calls + 2) + self.assertEqual(sys.getrefcount(pd3), refcnt) + +if __name__ == '__main__': + unittest.main() + diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml index cfe709a61..6a56a1958 100644 --- a/tests/samplebinding/typesystem_sample.xml +++ b/tests/samplebinding/typesystem_sample.xml @@ -988,6 +988,7 @@ + -- cgit v1.2.3