1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of Qt for Python.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #ifndef PHOTON_H
30 #define PHOTON_H
31 
32 #include <list>
33 #include "libsamplemacros.h"
34 
35 // This namespace and classes simulate
36 // situations found in Qt's phonon module.
37 
38 namespace Photon
39 {
40 
41 enum ClassType {
42     BaseType = 0,
43     IdentityType = 1,
44     DuplicatorType = 2
45 };
46 
47 class LIBSAMPLE_API Base
48 {
49 public:
Base(int value)50     explicit Base(int value) : m_value(value) {}
~Base()51     virtual ~Base() {}
setValue(int value)52     inline void setValue(int value) { m_value = value; }
value()53     inline int value() const { return m_value; }
54 
isType()55     template <class T> bool isType() { return type() == T::staticType; }
isType(ClassType t)56     bool isType(ClassType t) { return type() == t; }
57 
type()58     virtual ClassType type() const { return BaseType; };
59     static const ClassType staticType = BaseType;
60 
61 protected:
62     int m_value;
63 };
64 
65 template<ClassType CLASS_TYPE>
66 class LIBSAMPLE_API TemplateBase : public Base
67 {
68 public:
TemplateBase(int value)69     explicit TemplateBase(int value) : Base(value) {}
multiplicator()70     inline int multiplicator() const { return (int)CLASS_TYPE; }
calculate()71     inline int calculate() const { return m_value * ((int)CLASS_TYPE); }
classType()72     static inline ClassType classType() { return CLASS_TYPE; }
73 
sumValueUsingPointer(TemplateBase<CLASS_TYPE> * other)74     inline int sumValueUsingPointer(TemplateBase<CLASS_TYPE>* other) const { return m_value + other->m_value; }
sumValueUsingReference(TemplateBase<CLASS_TYPE> & other)75     inline int sumValueUsingReference(TemplateBase<CLASS_TYPE>& other) const { return m_value + other.m_value; }
76 
getListOfThisTemplateBase()77     inline std::list<TemplateBase<CLASS_TYPE> > getListOfThisTemplateBase()
78     {
79         std::list<TemplateBase<CLASS_TYPE> > objs;
80         objs.push_back(*this);
81         objs.push_back(*this);
82         return objs;
83     }
84 
passPointerThrough(TemplateBase<CLASS_TYPE> * obj)85     static inline TemplateBase<CLASS_TYPE>* passPointerThrough(TemplateBase<CLASS_TYPE>* obj) { return obj; }
86 
type()87     ClassType type() const override { return CLASS_TYPE; }
88     static const ClassType staticType = CLASS_TYPE;
89 };
90 
91 #if defined _WIN32 || defined __CYGWIN__
92 template class LIBSAMPLE_API TemplateBase<IdentityType>;
93 template class LIBSAMPLE_API TemplateBase<DuplicatorType>;
94 #endif
95 
96 using ValueIdentity = TemplateBase<IdentityType>;
97 using ValueDuplicator = TemplateBase<DuplicatorType>;
98 
99 LIBSAMPLE_API int callCalculateForValueDuplicatorPointer(ValueDuplicator* value);
100 LIBSAMPLE_API int callCalculateForValueDuplicatorReference(ValueDuplicator& value);
101 LIBSAMPLE_API int countValueIdentities(const std::list<ValueIdentity>& values);
102 LIBSAMPLE_API int countValueDuplicators(const std::list<TemplateBase<DuplicatorType> >& values);
103 
104 // This simulates an internal error (SEGV) caused by 'noexcept' in
105 // boost::intrusive_ptr before support for 'noexcept' was added. The ENTIRE
106 // code below is needed to trigger the exception; it isn't seen with just a
107 // 'noexcept' following a declaration.
108 //
109 // NOTE: For reasons that should be fairly obvious, this test unfortunately can
110 //       only be "run" when building in C++11 mode.
111 #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
112 #  define PHOTON_NOEXCEPT noexcept
113 #else
114 #  define PHOTON_NOEXCEPT
115 #endif
116 class Pointer
117 {
118 public:
Pointer()119     Pointer() PHOTON_NOEXCEPT : px(nullptr) {}
Pointer(int * p)120     Pointer(int* p) : px(p) {}
121 
reset()122     void reset() PHOTON_NOEXCEPT { Pointer().swap(*this); }
123 
get()124     int* get() const PHOTON_NOEXCEPT { return px; }
125     int& operator*() const { return *px; }
126 
swap(Pointer & rhs)127     void swap(Pointer& rhs) PHOTON_NOEXCEPT
128     {
129         int* tmp = px;
130         px = rhs.px;
131         rhs.px = tmp;
132     }
133 
134 private:
135     int* px;
136 };
137 
138 } // namespace Photon
139 
140 #endif // PHOTON_H
141