KJsEmbed

pointer.h
1 /* This file is part of the KDE libraries
2  Copyright (C) 2005, 2006 Ian Reinhart Geiser <[email protected]>
3  Copyright (C) 2005, 2006 Matt Broadstone <[email protected]>
4  Copyright (C) 2005, 2006 Richard J. Moore <[email protected]>
5  Copyright (C) 2005, 2006 Erik L. Bunce <[email protected]>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #ifndef POINTER_H
24 #define POINTER_H
25 
26 #include <algorithm>
27 #include <typeinfo>
28 #include <QVariant>
29 
30 namespace KJSEmbed
31 {
32 
33 struct PointerBase {
34 public:
35  virtual ~PointerBase()
36  {
37  ;
38  }
39  virtual void cleanup() = 0;
40  virtual const std::type_info &type() const = 0;
41  virtual void *voidStar() = 0;
42 };
43 
44 template<typename ValueType>
45 struct Pointer : public PointerBase {
46 public:
47  Pointer(ValueType *value) : ptr(value)
48  {
49 // qDebug("new pointer %s %0x", typeid(ValueType).name(), value);
50  }
51  ~Pointer() override
52  {
53 
54  }
55  void cleanup() override
56  {
57 // qDebug("delete pointer %s %0x", typeid(ValueType).name(), ptr );
58  delete ptr;
59  ptr = nullptr;
60  }
61  const std::type_info &type() const override
62  {
63  return typeid(ValueType);
64  }
65 
66  void *voidStar() override
67  {
68  return (void *)ptr;
69  }
70 
71  ValueType *ptr;
72 };
73 
74 template<typename ValueType>
75 struct Value : public PointerBase {
76 public:
77  Value(ValueType val) : value(val)
78  {
79  //qDebug("new value %s", typeid(ValueType).name());
80  }
81  ~Value() override
82  {
83  //qDebug("delete value");
84  }
85 
86  void cleanup() override
87  {
88 
89  }
90 
91  const std::type_info &type() const override
92  {
93  return typeid(ValueType);
94  }
95 
96  void *voidStar() override
97  {
98  return (void *)&value;
99  }
100 
101  ValueType value;
102 };
103 
104 struct NullPtr : public PointerBase {
105  NullPtr() : ptr(nullptr)
106  {
107  ;
108  }
109  ~NullPtr() override
110  {
111  ;
112  }
113  void cleanup() override
114  {
115  ;
116  }
117 
118  const std::type_info &type() const override
119  {
120  return typeid(NullPtr);
121  }
122 
123  void *voidStar() override
124  {
125  return &ptr;
126  }
127 
128  void *ptr;
129 
130 };
131 
132 template<typename ValueType>
133 ValueType *pointer_cast(PointerBase *pointer)
134 {
135 // qDebug("pointers %s %s", typeid(ValueType).name(), pointer->type().name() );
136  if (typeid(ValueType) != pointer->type()) {
137  return nullptr;
138  }
139  Pointer<ValueType> *upcast = static_cast< Pointer<ValueType> *>(pointer);
140  return upcast->ptr;
141 }
142 
143 }
144 
145 Q_DECLARE_METATYPE(KJSEmbed::PointerBase *)
146 
147 #endif
148 
Type type(const QSqlDatabase &db)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Dec 10 2023 03:59:19 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.