diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2019-11-12 15:58:15 +0100 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2019-11-14 09:03:25 +0100 |
commit | 03e233550f2c49797c69080d4923e46f2d82d98c (patch) | |
tree | 082d1b98a617ed9cd8068754ecd0dc0a689c4ae5 | |
parent | da5f43b6cbecac437f80fbb1b1405292b3962d3b (diff) |
shiboken: Split the headers of libsmart
Prepare for adding further tests.
On this occasion, polish the code a bit, move the logging of the shared
pointer into a base class to get rid of the iostream include in the
header.
Task-number: PYSIDE-454
Change-Id: Ifd05a7d3ceeae1c85e7193991907bf6d1e8e98ee
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
-rw-r--r-- | sources/shiboken2/tests/libsmart/smart.cpp | 49 | ||||
-rw-r--r-- | sources/shiboken2/tests/libsmart/smart.h | 194 | ||||
-rw-r--r-- | sources/shiboken2/tests/libsmart/smart_integer.h | 52 | ||||
-rw-r--r-- | sources/shiboken2/tests/libsmart/smart_obj.h | 60 | ||||
-rw-r--r-- | sources/shiboken2/tests/libsmart/smart_registry.h | 68 | ||||
-rw-r--r-- | sources/shiboken2/tests/libsmart/smart_sharedptr.h | 140 |
6 files changed, 370 insertions, 193 deletions
diff --git a/sources/shiboken2/tests/libsmart/smart.cpp b/sources/shiboken2/tests/libsmart/smart.cpp index 8d85d67a1..6a4deb50a 100644 --- a/sources/shiboken2/tests/libsmart/smart.cpp +++ b/sources/shiboken2/tests/libsmart/smart.cpp @@ -28,10 +28,52 @@ #include "smart.h" -bool shouldPrint() { +#include <algorithm> +#include <iostream> + +static inline bool shouldPrint() +{ return Registry::getInstance()->shouldPrint(); } +void SharedPtrBase::logDefaultConstructor(const void *t) +{ + if (shouldPrint()) + std::cout << "shared_ptr default constructor " << t << '\n'; +} + +void SharedPtrBase::logConstructor(const void *t, const void *pointee) +{ + if (shouldPrint()) { + std::cout << "shared_ptr constructor " << t << " with pointer " + << pointee << '\n'; + } +} + +void SharedPtrBase::logCopyConstructor(const void *t, const void *refData) +{ + if (shouldPrint()) { + std::cout << "shared_ptr copy constructor " << t << " with pointer " + << refData << '\n'; + } +} + +void SharedPtrBase::logAssignment(const void *t, const void *refData) +{ + if (shouldPrint()) { + std::cout << "shared_ptr assignment operator " << t << " with pointer " + << refData << "\n"; + } +} + +void SharedPtrBase::logDestructor(const void *t, int remainingRefCount) +{ + if (shouldPrint()) { + std::cout << "shared_ptr destructor " << t << " remaining refcount " + << remainingRefCount << '\n'; + } +} + Obj::Obj() : m_integer(123), m_internalInteger(new Integer) { Registry::getInstance()->add(this); @@ -143,10 +185,9 @@ Registry *Registry::getInstance() return ®istry; } -Registry::Registry() : m_printStuff(false) -{ +Registry::Registry() = default; -} +Registry::~Registry() = default; void Registry::add(Obj *p) { diff --git a/sources/shiboken2/tests/libsmart/smart.h b/sources/shiboken2/tests/libsmart/smart.h index 3347b22c1..6238f27d5 100644 --- a/sources/shiboken2/tests/libsmart/smart.h +++ b/sources/shiboken2/tests/libsmart/smart.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2017 The Qt Company Ltd. +** Copyright (C) 2019 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the test suite of Qt for Python. @@ -29,193 +29,9 @@ #ifndef SMART_H #define SMART_H -#include <algorithm> -#include <iostream> -#include <vector> - -#include "libsmartmacros.h" - -// Forward declarations. -template <class T> -class SharedPtr; -class Integer; -class Obj; - -LIB_SMART_API bool shouldPrint(); - -// Used to track which C++ objects are alive. -class LIB_SMART_API Registry { -public: - static Registry *getInstance(); - - void add(Obj *p); - void add(Integer *p); - void remove(Obj *p); - void remove(Integer *p); - int countObjects() const; - int countIntegers() const; - bool shouldPrint() const; - void setShouldPrint(bool flag); - -protected: - Registry(); - -private: - bool m_printStuff; - std::vector<Obj *> m_objects; - std::vector<Integer *> m_integers; -}; - -template <class T> -class RefData { -public: - RefData(T *ptr) : m_refCount(1), m_heldPtr(ptr) {} - ~RefData() { delete m_heldPtr; } - int inc() { return ++m_refCount; } - int dec() { return --m_refCount; } - int useCount() { return m_refCount; } - int m_refCount; - T *m_heldPtr; -}; - -template <class T> -class SharedPtr { -public: - SharedPtr() : m_refData(nullptr) { - if (shouldPrint()) - std::cout << "shared_ptr default constructor " << this << "\n"; - } - - SharedPtr(T *v) - { - if (shouldPrint()) - std::cout << "shared_ptr constructor " << this << " with pointer " << v << "\n"; - if (v) - m_refData = new RefData<T>(v); - } - - SharedPtr(const SharedPtr<T> &other) : m_refData(other.m_refData) - { - if (shouldPrint()) - std::cout << "shared_ptr copy constructor " << this << " with pointer " - << other.m_refData << "\n"; - if (m_refData) - m_refData->inc(); - } - - SharedPtr<T> &operator=(const SharedPtr<T>& other) - { - if (this != &other) { - if (shouldPrint()) - std::cout << "shared_ptr assignment operator " << this << " with pointer " - << other.m_refData << "\n"; - if (m_refData && m_refData->dec() == 0) - delete m_refData; - m_refData = other.m_refData; - if (m_refData) - m_refData->inc(); - } - return *this; - } - - T *data() const - { - if (m_refData) - return m_refData->m_heldPtr; - return nullptr; - } - - int useCount() const - { - if (m_refData) - return m_refData->useCount(); - return 0; - } - - void dummyMethod1() - { - - } - - T& operator*() const - { - // Crashes if smart pointer is empty (just like std::shared_ptr). - return *(m_refData->m_heldPtr); - } - - T *operator->() const - { - if (m_refData) - return m_refData->m_heldPtr; - return nullptr; - } - - bool operator!() const - { - return !m_refData || !m_refData->m_heldPtr; - } - - bool isNull() const - { - return !m_refData || !m_refData->m_heldPtr; - } - - operator bool() const - { - return m_refData && m_refData->m_heldPtr; - } - - ~SharedPtr() - { - if (m_refData) { - if (shouldPrint()) - std::cout << "shared_ptr destructor " << this << " remaining refcount " - << m_refData->useCount() - 1 << "\n"; - } - if (m_refData && m_refData->dec() == 0) - delete m_refData; - } - - RefData<T> *m_refData; -}; - -class LIB_SMART_API Integer { -public: - Integer(); - Integer(const Integer &other); - Integer &operator=(const Integer &other); - ~Integer(); - void printInteger(); - int m_int; -}; - -namespace Smart { -class LIB_SMART_API Integer2 : public Integer { -public: - Integer2(); - Integer2(const Integer2 &other); -}; -} - - -// Couldn't name it Object because it caused some namespace clashes. -class LIB_SMART_API Obj { -public: - Obj(); - virtual ~Obj(); - - void printObj(); - Integer takeInteger(Integer val); - SharedPtr<Obj> giveSharedPtrToObj(); - std::vector<SharedPtr<Obj> > giveSharedPtrToObjList(int size); - SharedPtr<Integer> giveSharedPtrToInteger(); - SharedPtr<Smart::Integer2> giveSharedPtrToInteger2(); - int takeSharedPtrToObj(SharedPtr<Obj> pObj); - int takeSharedPtrToInteger(SharedPtr<Integer> pInt); - - int m_integer; - Integer *m_internalInteger; -}; +#include "smart_sharedptr.h" +#include "smart_integer.h" +#include "smart_obj.h" +#include "smart_registry.h" #endif // SMART_H - diff --git a/sources/shiboken2/tests/libsmart/smart_integer.h b/sources/shiboken2/tests/libsmart/smart_integer.h new file mode 100644 index 000000000..3756f68b0 --- /dev/null +++ b/sources/shiboken2/tests/libsmart/smart_integer.h @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of Qt for Python. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SMART_INTEGER_H +#define SMART_INTEGER_H + +#include "libsmartmacros.h" + +class LIB_SMART_API Integer { +public: + Integer(); + Integer(const Integer &other); + Integer &operator=(const Integer &other); + ~Integer(); + void printInteger(); + int m_int; +}; + +namespace Smart { +class LIB_SMART_API Integer2 : public Integer { +public: + Integer2(); + Integer2(const Integer2 &other); +}; +} // namespace Smart + +#endif // SMART_INTEGER_H diff --git a/sources/shiboken2/tests/libsmart/smart_obj.h b/sources/shiboken2/tests/libsmart/smart_obj.h new file mode 100644 index 000000000..12425366e --- /dev/null +++ b/sources/shiboken2/tests/libsmart/smart_obj.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of Qt for Python. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SMART_OBJ_H +#define SMART_OBJ_H + +#include "libsmartmacros.h" +#include "smart_sharedptr.h" + +#include <vector> + +class Integer; +class Obj; +namespace Smart { class Integer2; } + +// Couldn't name it Object because it caused some namespace clashes. +class LIB_SMART_API Obj { +public: + Obj(); + virtual ~Obj(); + + void printObj(); + Integer takeInteger(Integer val); + SharedPtr<Obj> giveSharedPtrToObj(); + std::vector<SharedPtr<Obj> > giveSharedPtrToObjList(int size); + SharedPtr<Integer> giveSharedPtrToInteger(); + SharedPtr<Smart::Integer2> giveSharedPtrToInteger2(); + int takeSharedPtrToObj(SharedPtr<Obj> pObj); + int takeSharedPtrToInteger(SharedPtr<Integer> pInt); + + int m_integer; + Integer *m_internalInteger; +}; + +#endif // SMART_OBJ_H diff --git a/sources/shiboken2/tests/libsmart/smart_registry.h b/sources/shiboken2/tests/libsmart/smart_registry.h new file mode 100644 index 000000000..6171ddb59 --- /dev/null +++ b/sources/shiboken2/tests/libsmart/smart_registry.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of Qt for Python. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SMART_REGISTRY_H +#define SMART_REGISTRY_H + +#include <vector> + +#include "libsmartmacros.h" + +class Obj; +class Integer; + +// Used to track which C++ objects are alive. +class LIB_SMART_API Registry { +public: + static Registry *getInstance(); + ~Registry(); + + Registry(const Registry &) = delete; + Registry &operator=(const Registry &) = delete; + Registry(Registry &&) = delete; + Registry &operator=(Registry &&) = delete; + + void add(Obj *p); + void add(Integer *p); + void remove(Obj *p); + void remove(Integer *p); + int countObjects() const; + int countIntegers() const; + bool shouldPrint() const; + void setShouldPrint(bool flag); + +protected: + Registry(); + +private: + std::vector<Obj *> m_objects; + std::vector<Integer *> m_integers; + bool m_printStuff = false; +}; + +#endif // SMART_REGISTRY_H diff --git a/sources/shiboken2/tests/libsmart/smart_sharedptr.h b/sources/shiboken2/tests/libsmart/smart_sharedptr.h new file mode 100644 index 000000000..84184e1f8 --- /dev/null +++ b/sources/shiboken2/tests/libsmart/smart_sharedptr.h @@ -0,0 +1,140 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of Qt for Python. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SMART_SHARED_PTR_H +#define SMART_SHARED_PTR_H + +#include "libsmartmacros.h" + +template <class T> +class RefData { +public: + RefData(T *ptr) : m_refCount(1), m_heldPtr(ptr) {} + ~RefData() { delete m_heldPtr; } + int inc() { return ++m_refCount; } + int dec() { return --m_refCount; } + int useCount() { return m_refCount; } + int m_refCount; + T *m_heldPtr; +}; + +struct SharedPtrBase +{ + LIB_SMART_API static void logDefaultConstructor(const void *t); + LIB_SMART_API static void logConstructor(const void *t, const void *pointee); + LIB_SMART_API static void logCopyConstructor(const void *t, const void *refData); + LIB_SMART_API static void logAssignment(const void *t, const void *refData); + LIB_SMART_API static void logDestructor(const void *t, int remainingRefCount); +}; + +template <class T> +class SharedPtr : public SharedPtrBase { +public: + SharedPtr() { logDefaultConstructor(this); } + + SharedPtr(T *v) + { + logConstructor(this, v); + if (v) + m_refData = new RefData<T>(v); + } + + SharedPtr(const SharedPtr<T> &other) : m_refData(other.m_refData) + { + logCopyConstructor(this, other.m_refData); + if (m_refData) + m_refData->inc(); + } + + SharedPtr<T> &operator=(const SharedPtr<T>& other) + { + if (this != &other) { + logAssignment(this, other.m_refData); + if (m_refData && m_refData->dec() == 0) + delete m_refData; + m_refData = other.m_refData; + if (m_refData) + m_refData->inc(); + } + return *this; + } + + T *data() const + { + return m_refData ? m_refData->m_heldPtr : nullptr; + } + + int useCount() const + { + return m_refData ? m_refData->useCount() : 0; + } + + void dummyMethod1() + { + + } + + T& operator*() const + { + // Crashes if smart pointer is empty (just like std::shared_ptr). + return *(m_refData->m_heldPtr); + } + + T *operator->() const + { + return m_refData ? m_refData->m_heldPtr : nullptr; + } + + bool operator!() const + { + return !m_refData || !m_refData->m_heldPtr; + } + + bool isNull() const + { + return !m_refData || !m_refData->m_heldPtr; + } + + operator bool() const + { + return m_refData && m_refData->m_heldPtr; + } + + ~SharedPtr() + { + if (m_refData) + logDestructor(this, m_refData->useCount() - 1); + if (m_refData && m_refData->dec() == 0) + delete m_refData; + } + +private: + RefData<T> *m_refData = nullptr; +}; + +#endif // SMART_SHARED_PTR_H |