summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuciano Wolf <luciano.wolf@openbossa.org>2010-04-14 19:14:21 -0300
committerLuciano Wolf <luciano.wolf@openbossa.org>2010-04-21 12:26:52 -0300
commitda4915fd1ddfd1e04a6ef21b0117d83fc7fe2d5d (patch)
treef3e5320f87887ddd64d4760afb020d2b995205a6
parentf0f1aa01139a073430eb2c5feb767dedc45d3b4f (diff)
downloadshiboken-da4915fd1ddfd1e04a6ef21b0117d83fc7fe2d5d.tar.gz
shiboken-da4915fd1ddfd1e04a6ef21b0117d83fc7fe2d5d.tar.xz
shiboken-da4915fd1ddfd1e04a6ef21b0117d83fc7fe2d5d.zip
Support to "only private constructor available" case.
-rw-r--r--cppgenerator.cpp12
-rw-r--r--tests/libsample/privatector.h61
-rw-r--r--tests/samplebinding/CMakeLists.txt1
-rw-r--r--tests/samplebinding/global.h1
-rwxr-xr-xtests/samplebinding/privatector_test.py79
-rw-r--r--tests/samplebinding/typesystem_sample.xml1
6 files changed, 151 insertions, 4 deletions
diff --git a/cppgenerator.cpp b/cppgenerator.cpp
index 68222adb..226f7e50 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 00000000..5591f15f
--- /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 <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.
+ *
+ * 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 7510b5b3..06e35ecb 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 f54fb516..c037a5b6 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 00000000..ea713670
--- /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 <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
+
+'''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 cfe709a6..6a56a195 100644
--- a/tests/samplebinding/typesystem_sample.xml
+++ b/tests/samplebinding/typesystem_sample.xml
@@ -988,6 +988,7 @@
<value-type name="VoidHolder"/>
+ <object-type name="PrivateCtor" />
<object-type name="PrivateDtor" />
<object-type name="Base1"/>