diff options
author | Oswald Buddenhagen <oswald.buddenhagen@qt.io> | 2017-05-22 17:50:30 +0200 |
---|---|---|
committer | Oswald Buddenhagen <oswald.buddenhagen@qt.io> | 2017-05-22 18:20:59 +0200 |
commit | 9c333ade1a1b694fc6ce22e483f5de3e952c17ad (patch) | |
tree | d3a2e41de3501f91ebfdc8ec81a80156597c2585 /sources/shiboken2/tests/libsmart | |
parent | 8f3761d8eac7780393382ab54dbdb487eb4df027 (diff) |
move everying into sources/shiboken2 (5.9 edition)
in preparation for a subtree merge.
this should not be necessary to do in a separate commit, but git is a
tad stupid about following history correctly without it.
Diffstat (limited to 'sources/shiboken2/tests/libsmart')
-rw-r--r-- | sources/shiboken2/tests/libsmart/CMakeLists.txt | 11 | ||||
-rw-r--r-- | sources/shiboken2/tests/libsmart/libsmartmacros.h | 46 | ||||
-rw-r--r-- | sources/shiboken2/tests/libsmart/smart.cpp | 174 | ||||
-rw-r--r-- | sources/shiboken2/tests/libsmart/smart.h | 210 |
4 files changed, 441 insertions, 0 deletions
diff --git a/sources/shiboken2/tests/libsmart/CMakeLists.txt b/sources/shiboken2/tests/libsmart/CMakeLists.txt new file mode 100644 index 000000000..66c27cdae --- /dev/null +++ b/sources/shiboken2/tests/libsmart/CMakeLists.txt @@ -0,0 +1,11 @@ +project(libsmart) + +set(libsmart_SRC +smart.cpp +) + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) +add_definitions("-DLIBSMART_BUILD") +add_library(libsmart SHARED ${libsmart_SRC}) +set_property(TARGET libsmart PROPERTY PREFIX "") + diff --git a/sources/shiboken2/tests/libsmart/libsmartmacros.h b/sources/shiboken2/tests/libsmart/libsmartmacros.h new file mode 100644 index 000000000..e4166caff --- /dev/null +++ b/sources/shiboken2/tests/libsmart/libsmartmacros.h @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $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 LIB_SMART_MACROS_H +#define LIB_SMART_MACROS_H + +#if defined _WIN32 || defined __CYGWIN__ + #if LIBSMART_BUILD + #define LIB_SMART_API __declspec(dllexport) + #else + #define LIB_SMART_API __declspec(dllimport) + #endif +#else +#if __GNUC__ >= 4 + #define LIB_SMART_API __attribute__ ((visibility("default"))) +#else + #define LIB_SMART_API +#endif +#endif + +#endif diff --git a/sources/shiboken2/tests/libsmart/smart.cpp b/sources/shiboken2/tests/libsmart/smart.cpp new file mode 100644 index 000000000..28c9cf055 --- /dev/null +++ b/sources/shiboken2/tests/libsmart/smart.cpp @@ -0,0 +1,174 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $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$ +** +****************************************************************************/ + +#include "smart.h" + +bool shouldPrint() { + return Registry::getInstance()->shouldPrint(); +} + +Obj::Obj() : m_integer(123), m_internalInteger(new Integer) +{ + Registry::getInstance()->add(this); + if (shouldPrint()) + std::cout << "Object constructor " << this << '\n'; +} + +Obj::~Obj() +{ + Registry::getInstance()->remove(this); + delete m_internalInteger; + if (shouldPrint()) + std::cout << "Object destructor " << this << '\n'; +} + + +void Obj::printObj() { + if (shouldPrint()) { + std::cout << "integer value: " << m_integer + << " internal integer value: " << m_internalInteger->m_int << '\n'; + } +} + + +SharedPtr<Obj> Obj::giveSharedPtrToObj() +{ + SharedPtr<Obj> o(new Obj); + return o; +} + +SharedPtr<Integer> Obj::giveSharedPtrToInteger() +{ + SharedPtr<Integer> o(new Integer); + return o; +} + +int Obj::takeSharedPtrToObj(SharedPtr<Obj> pObj) +{ + pObj->printObj(); + return pObj->m_integer; +} + +int Obj::takeSharedPtrToInteger(SharedPtr<Integer> pInt) +{ + pInt->printInteger(); + return pInt->m_int; +} + +Integer Obj::takeInteger(Integer val) +{ + return val; +} + +Integer::Integer() : m_int(456) +{ + Registry::getInstance()->add(this); + if (shouldPrint()) + std::cout << "Integer constructor " << this << '\n'; +} + +Integer::Integer(const Integer &other) +{ + Registry::getInstance()->add(this); + if (shouldPrint()) + std::cout << "Integer copy constructor " << this << '\n'; + m_int = other.m_int; +} + +Integer &Integer::operator=(const Integer &other) +{ + Registry::getInstance()->add(this); + if (shouldPrint()) + std::cout << "Integer operator= " << this << '\n'; + m_int = other.m_int; + return *this; +} + +Integer::~Integer() +{ + Registry::getInstance()->remove(this); + if (shouldPrint()) + std::cout << "Integer destructor " << this << '\n'; +} + +void Integer::printInteger() +{ + if (shouldPrint()) + std::cout << "Integer value for object " << this << " is " << m_int << '\n'; +} + +Registry *Registry::getInstance() +{ + static Registry registry; + return ®istry; +} + +Registry::Registry() : m_printStuff(false) +{ + +} + +void Registry::add(Obj *p) +{ + m_objects.push_back(p); +} + +void Registry::add(Integer *p) +{ + m_integers.push_back(p); +} + +void Registry::remove(Obj *p) +{ + m_objects.erase(std::remove(m_objects.begin(), m_objects.end(), p), m_objects.end()); +} + +void Registry::remove(Integer *p) +{ + m_integers.erase(std::remove(m_integers.begin(), m_integers.end(), p), m_integers.end()); +} + +int Registry::countObjects() const +{ + return static_cast<int>(m_objects.size()); +} + +int Registry::countIntegers() const +{ + return static_cast<int>(m_integers.size()); +} + +bool Registry::shouldPrint() const +{ + return m_printStuff; +} + +void Registry::setShouldPrint(bool flag) +{ + m_printStuff = flag; +} diff --git a/sources/shiboken2/tests/libsmart/smart.h b/sources/shiboken2/tests/libsmart/smart.h new file mode 100644 index 000000000..c6b0dbbca --- /dev/null +++ b/sources/shiboken2/tests/libsmart/smart.h @@ -0,0 +1,210 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $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_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(0) { + 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 0; + } + + 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 0; + } + + 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; +}; + +// 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(); + SharedPtr<Integer> giveSharedPtrToInteger(); + int takeSharedPtrToObj(SharedPtr<Obj> pObj); + int takeSharedPtrToInteger(SharedPtr<Integer> pInt); + + int m_integer; + Integer *m_internalInteger; +}; + +#endif // SMART_H + |