aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2011-05-28 15:09:55 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:15:25 -0300
commitc1253405144232e4c6db791ce43bd6d4f25ea9f6 (patch)
tree25d2af0b2ed919633b7c85077b4dda4185bb04d4
parent3112b255beedf6cd4ac56ad08e9a90d011620d04 (diff)
Added class to be treated as a specific (bool, in this case) primitive type in Python.
Unit tests were also added.
-rw-r--r--tests/libminimal/minbool.h62
-rw-r--r--tests/minimalbinding/CMakeLists.txt1
-rw-r--r--tests/minimalbinding/global.h1
-rw-r--r--tests/minimalbinding/minbool_conversions.h10
-rw-r--r--tests/minimalbinding/minbool_test.py59
-rw-r--r--tests/minimalbinding/typesystem_minimal.xml7
6 files changed, 140 insertions, 0 deletions
diff --git a/tests/libminimal/minbool.h b/tests/libminimal/minbool.h
new file mode 100644
index 000000000..6b32d1892
--- /dev/null
+++ b/tests/libminimal/minbool.h
@@ -0,0 +1,62 @@
+/*
+ * This file is part of the Shiboken Python Binding Generator project.
+ *
+ * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+ *
+ * Contact: PySide team <contact@pyside.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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 library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef MINBOOL_H
+#define MINBOOL_H
+
+#include "libminimalmacros.h"
+
+class LIBMINIMAL_API MinBool
+{
+public:
+ inline explicit MinBool(bool b) : m_value(b) {}
+ bool value() const { return m_value; }
+ inline MinBool operator!() const { return MinBool(!m_value); }
+ inline MinBool& operator|=(const MinBool& other) {
+ m_value = m_value | other.m_value;
+ return *this;
+ }
+private:
+ bool m_value;
+};
+
+inline bool operator==(MinBool b1, bool b2) { return !b1 == !b2; }
+inline bool operator==(bool b1, MinBool b2) { return !b1 == !b2; }
+inline bool operator==(MinBool b1, MinBool b2) { return !b1 == !b2; }
+inline bool operator!=(MinBool b1, bool b2) { return !b1 != !b2; }
+inline bool operator!=(bool b1, MinBool b2) { return !b1 != !b2; }
+inline bool operator!=(MinBool b1, MinBool b2) { return !b1 != !b2; }
+
+class LIBMINIMAL_API MinBoolUser
+{
+public:
+ MinBoolUser() : m_minbool(MinBool(false)) {}
+ virtual ~MinBoolUser() {}
+ inline MinBool minBool() { return m_minbool; }
+ inline void setMinBool(MinBool minBool) { m_minbool = minBool; }
+ virtual MinBool invertedMinBool() { return !m_minbool; }
+ inline MinBool callInvertedMinBool() { return invertedMinBool(); }
+private:
+ MinBool m_minbool;
+};
+
+#endif
diff --git a/tests/minimalbinding/CMakeLists.txt b/tests/minimalbinding/CMakeLists.txt
index 2a78ae7a3..af93dd365 100644
--- a/tests/minimalbinding/CMakeLists.txt
+++ b/tests/minimalbinding/CMakeLists.txt
@@ -8,6 +8,7 @@ set(minimal_SRC
${CMAKE_CURRENT_BINARY_DIR}/minimal/minimal_module_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/minimal/obj_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/minimal/val_wrapper.cpp
+${CMAKE_CURRENT_BINARY_DIR}/minimal/minbooluser_wrapper.cpp
)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/minimal-binding.txt.in"
diff --git a/tests/minimalbinding/global.h b/tests/minimalbinding/global.h
index 112023202..c27c686b7 100644
--- a/tests/minimalbinding/global.h
+++ b/tests/minimalbinding/global.h
@@ -1,2 +1,3 @@
#include "obj.h"
#include "val.h"
+#include "minbool.h"
diff --git a/tests/minimalbinding/minbool_conversions.h b/tests/minimalbinding/minbool_conversions.h
new file mode 100644
index 000000000..f343a4711
--- /dev/null
+++ b/tests/minimalbinding/minbool_conversions.h
@@ -0,0 +1,10 @@
+namespace Shiboken {
+template <>
+struct Converter<MinBool> : public ValueTypeConverter<MinBool>
+{
+ static bool isConvertible(PyObject* pyObj) { return PyBool_Check(pyObj); }
+ using ValueTypeConverter<MinBool>::toPython;
+ static PyObject* toPython(const MinBool& holder) { return PyBool_FromLong(holder.value()); }
+ static MinBool toCpp(PyObject* pyobj) { return MinBool(pyobj == Py_True); }
+};
+}
diff --git a/tests/minimalbinding/minbool_test.py b/tests/minimalbinding/minbool_test.py
new file mode 100644
index 000000000..0b183d7aa
--- /dev/null
+++ b/tests/minimalbinding/minbool_test.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# This file is part of the Shiboken Python Bindings Generator project.
+#
+# Copyright (C) 2011 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
+
+import unittest
+
+from minimal import MinBoolUser
+
+class DerivedMinBoolUser (MinBoolUser):
+ def returnMyselfVirtual(self):
+ return MinBoolUser()
+
+class MinBoolTest(unittest.TestCase):
+
+ def testMinBoolUser(self):
+ mbuTrue = MinBoolUser()
+ mbuFalse = MinBoolUser()
+ mbuTrue.setMinBool(True)
+ self.assertEqual(mbuFalse.minBool(), False)
+ self.assertEqual(mbuTrue.minBool(), True)
+ self.assertEqual(mbuTrue.callInvertedMinBool(), False)
+
+ self.assertEqual(mbuTrue.minBool() == True, True)
+ self.assertEqual(False == mbuFalse.minBool(), True)
+ self.assertEqual(mbuTrue.minBool() == mbuFalse.minBool(), False)
+
+ self.assertEqual(mbuFalse.minBool() != True, True)
+ self.assertEqual(True != mbuFalse.minBool(), True)
+ self.assertEqual(mbuTrue.minBool() != mbuFalse.minBool(), True)
+
+ def testVirtuals(self):
+ dmbu = DerivedMinBoolUser()
+ self.assertEqual(dmbu.invertedMinBool(), True)
+
+if __name__ == '__main__':
+ unittest.main()
+
diff --git a/tests/minimalbinding/typesystem_minimal.xml b/tests/minimalbinding/typesystem_minimal.xml
index da9d9b12a..c3e681177 100644
--- a/tests/minimalbinding/typesystem_minimal.xml
+++ b/tests/minimalbinding/typesystem_minimal.xml
@@ -2,7 +2,14 @@
<typesystem package="minimal">
<primitive-type name="bool"/>
<primitive-type name="int"/>
+
+ <primitive-type name="MinBool" target-lang-api-name="PyBool">
+ <conversion-rule file="minbool_conversions.h"/>
+ <include file-name="minbool.h" location="global"/>
+ </primitive-type>
+
<object-type name="Obj"/>
<value-type name="Val"/>
+ <value-type name="MinBoolUser"/>
</typesystem>