File indexing completed on 2024-12-08 09:42:36

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
0003     Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
0004     Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
0005     Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #ifndef POINTER_H
0024 #define POINTER_H
0025 
0026 #include <algorithm>
0027 #include <typeinfo>
0028 #include <QVariant>
0029 
0030 namespace KJSEmbed
0031 {
0032 
0033 struct PointerBase {
0034 public:
0035     virtual ~PointerBase()
0036     {
0037         ;
0038     }
0039     virtual void cleanup() = 0;
0040     virtual const std::type_info &type() const = 0;
0041     virtual void *voidStar() = 0;
0042 };
0043 
0044 template<typename ValueType>
0045 struct Pointer : public PointerBase {
0046 public:
0047     Pointer(ValueType *value) : ptr(value)
0048     {
0049 //         qDebug("new pointer %s %0x", typeid(ValueType).name(), value);
0050     }
0051     ~Pointer() override
0052     {
0053 
0054     }
0055     void cleanup() override
0056     {
0057 //         qDebug("delete pointer %s %0x", typeid(ValueType).name(), ptr );
0058         delete ptr;
0059         ptr = nullptr;
0060     }
0061     const std::type_info &type() const override
0062     {
0063         return typeid(ValueType);
0064     }
0065 
0066     void *voidStar() override
0067     {
0068         return (void *)ptr;
0069     }
0070 
0071     ValueType *ptr;
0072 };
0073 
0074 template<typename ValueType>
0075 struct Value : public PointerBase {
0076 public:
0077     Value(ValueType val) : value(val)
0078     {
0079         //qDebug("new value %s", typeid(ValueType).name());
0080     }
0081     ~Value() override
0082     {
0083         //qDebug("delete value");
0084     }
0085 
0086     void cleanup() override
0087     {
0088 
0089     }
0090 
0091     const std::type_info &type() const override
0092     {
0093         return typeid(ValueType);
0094     }
0095 
0096     void *voidStar() override
0097     {
0098         return (void *)&value;
0099     }
0100 
0101     ValueType value;
0102 };
0103 
0104 struct NullPtr : public PointerBase {
0105     NullPtr() : ptr(nullptr)
0106     {
0107         ;
0108     }
0109     ~NullPtr() override
0110     {
0111         ;
0112     }
0113     void cleanup() override
0114     {
0115         ;
0116     }
0117 
0118     const std::type_info &type() const override
0119     {
0120         return typeid(NullPtr);
0121     }
0122 
0123     void *voidStar() override
0124     {
0125         return &ptr;
0126     }
0127 
0128     void *ptr;
0129 
0130 };
0131 
0132 template<typename ValueType>
0133 ValueType *pointer_cast(PointerBase *pointer)
0134 {
0135 //    qDebug("pointers %s %s", typeid(ValueType).name(), pointer->type().name() );
0136     if (typeid(ValueType) != pointer->type()) {
0137         return nullptr;
0138     }
0139     Pointer<ValueType> *upcast = static_cast< Pointer<ValueType> *>(pointer);
0140     return upcast->ptr;
0141 }
0142 
0143 }
0144 
0145 Q_DECLARE_METATYPE(KJSEmbed::PointerBase *)
0146 
0147 #endif
0148