QtGStreamer  1.2.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
type.h
1 /*
2  Copyright (C) 2009-2010 George Kiagiadakis <kiagiadakis.george@gmail.com>
3  Copyright (C) 2010 Collabora Ltd.
4  @author George Kiagiadakis <george.kiagiadakis@collabora.co.uk>
5 
6  This library is free software; you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation; either version 2.1 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef QGLIB_TYPE_H
20 #define QGLIB_TYPE_H
21 
22 #include "global.h"
23 #include <QtCore/QList>
24 #include <boost/mpl/if.hpp>
25 
26 /*
27  * This is a re-definition of GType inside the QGlib::Private namespace.
28  * It is used in the headers to avoid including <glib-object.h>.
29  *
30  * According to gtype.h, GType is ulong in C++ for historic reasons,
31  * but only if sizeof(size_t) == sizeof(ulong). This template trick
32  * here attempts to express the same logic wigh C++ templates.
33  */
34 namespace QGlib {
35 namespace Private {
36  typedef boost::mpl::if_c<
37  sizeof(size_t) == sizeof(unsigned long),
38  unsigned long,
39  QIntegerForSizeof<size_t>::Unsigned
40  >::type GType;
41 } //namespace Private
42 } //namespace QGlib
43 
44 namespace QGlib { //closing and re-opening namespace QGlib is required to trick codegen
45 
63 class QTGLIB_EXPORT Type
64 {
65 public:
66  enum FundamentalType {
67  Invalid = 0,
68  None = 1<<2,
69  Interface = 2<<2,
70  Char = 3<<2,
71  Uchar = 4<<2,
72  Boolean = 5<<2,
73  Int = 6<<2,
74  Uint = 7<<2,
75  Long = 8<<2,
76  Ulong = 9<<2,
77  Int64 = 10<<2,
78  Uint64 = 11<<2,
79  Enum = 12<<2,
80  Flags = 13<<2,
81  Float = 14<<2,
82  Double = 15<<2,
83  String = 16<<2,
84  Pointer = 17<<2,
85  Boxed = 18<<2,
86  Param = 19<<2,
87  Object = 20<<2
88  };
89 
90  inline Type() : m_type(0) {}
91  inline Type(Private::GType gtype) : m_type(gtype) {}
92  inline Type(FundamentalType ftype) : m_type(ftype) {}
93  inline Type(const Type & other) : m_type(other.m_type) {}
94 
95  inline Type & operator=(Type other);
96  inline bool operator==(Type other) const;
97  inline operator Private::GType() const { return m_type; }
98 
99  static Type fromInstance(void *nativeInstance);
100  static Type fromName(const char *name);
101 
102  QString name() const;
103  Quark nameQuark() const;
104 
105  bool isValid() const;
106  bool isAbstract() const;
107  bool isDerived() const;
108  bool isFundamental() const;
109  bool isValueType() const;
110  bool hasValueTable() const;
111  bool isClassed() const;
112  bool isInstantiatable() const;
113  bool isDerivable() const;
114  bool isDeepDerivable() const;
115  bool isInterface() const;
116 
117  Type fundamental() const;
118  Type parent() const;
119  uint depth() const;
120  Type nextBase(Type rootType) const;
121  bool isA(Type is_a_type) const;
122 
123  template <typename T>
124  inline bool isA() const;
125 
126  QList<Type> children() const;
127  QList<Type> interfaces() const;
128  QList<Type> interfacePrerequisites() const;
129 
130  void *quarkData(const Quark & qname) const;
131  void setQuarkData(const Quark & qname, void *data);
132 
133 private:
134  Private::GType m_type;
135 };
136 
137 inline Type & Type::operator=(Type other)
138 {
139  m_type = other.m_type;
140  return *this;
141 }
142 
143 inline bool Type::operator==(Type other) const
144 {
145  return m_type == other.m_type;
146 }
147 
148 template <class T>
149 inline Type GetType(); //forward-declaration, defined below
150 
151 template <typename T>
152 inline bool Type::isA() const
153 {
154  return isA(GetType<T>());
155 }
156 
157 //***************
158 // -- GetType --
159 //***************
160 
161 /* Used to provide the implementation for GetType() */
162 template <class T>
163 struct GetTypeImpl
164 {
165 //If we have static_assert(), use it to show a more friendly error message to the developer.
166 //The check is dummy and is expected to evaluate to false. It is just used to trick the
167 //compiler to delay the evaluation of the expression until the instantiation of this template
168 //(where T becomes a known type). static_assert(false, "foo"); fails even before instantiation.
169 #if defined(QGLIB_HAVE_CXX0X_STATIC_ASSERT)
170 private:
171  template <class X> struct FailStruct { static const bool value = false; };
172  static_assert(FailStruct<T>::value, "Type T has not been registered with the QGlib type system");
173 #endif
174 };
175 
179 template <class T>
180 inline Type GetType()
181 {
182  return GetTypeImpl<T>();
183 }
184 
185 } //namespace QGlib
186 
187 /* This is to be used to define new Q*_REGISTER_TYPE(T) macros in
188  * other bindings libraries that are based on QtGLib.
189  */
190 #define QGLIB_REGISTER_TYPE_WITH_EXPORT_MACRO(T, EXPORT_MACRO) \
191  namespace QGlib { \
192  template <> \
193  struct EXPORT_MACRO GetTypeImpl<T> { operator Type(); }; \
194  }
195 
196 /* This macro is used to register a class with the QtGLib type system. It forward-declares
197  * a specialization for struct GetTypeImpl and serves as a keyword for codegen,
198  * our code generator, which provides the compiled implementation. This specific macro
199  * is meant to be used only inside QtGLib, for other libraries, define a new macro
200  * using QGLIB_REGISTER_TYPE_WITH_EXPORT_MACRO.
201  * Note: this macro must be used outside of any namespace scope
202  */
203 #define QGLIB_REGISTER_TYPE(T) \
204  QGLIB_REGISTER_TYPE_WITH_EXPORT_MACRO(T, QTGLIB_EXPORT)
205 
206 //**************************
207 // -- type registrations --
208 //**************************
209 
210 #define QGLIB_REGISTER_NATIVE_TYPE(T, GTYPE) \
211  namespace QGlib { \
212  template <> \
213  struct GetTypeImpl<T> { \
214  inline operator Type() { return (GTYPE); }; \
215  }; \
216  }
217 
218 QGLIB_REGISTER_NATIVE_TYPE(bool, Type::Boolean)
219 QGLIB_REGISTER_NATIVE_TYPE(char, Type::Char)
220 QGLIB_REGISTER_NATIVE_TYPE(unsigned char, Type::Uchar)
221 QGLIB_REGISTER_NATIVE_TYPE(int, Type::Int)
222 QGLIB_REGISTER_NATIVE_TYPE(unsigned int, Type::Uint)
223 QGLIB_REGISTER_NATIVE_TYPE(long, Type::Long)
224 QGLIB_REGISTER_NATIVE_TYPE(unsigned long, Type::Ulong)
225 QGLIB_REGISTER_NATIVE_TYPE(qint64, Type::Int64)
226 QGLIB_REGISTER_NATIVE_TYPE(quint64, Type::Uint64)
227 QGLIB_REGISTER_NATIVE_TYPE(float, Type::Float)
228 QGLIB_REGISTER_NATIVE_TYPE(double, Type::Double)
229 QGLIB_REGISTER_NATIVE_TYPE(void*, Type::Pointer)
230 QGLIB_REGISTER_NATIVE_TYPE(const char*, Type::String)
231 QGLIB_REGISTER_NATIVE_TYPE(QByteArray, Type::String)
232 QGLIB_REGISTER_NATIVE_TYPE(QString, Type::String)
233 
234 //partial specializations for string literals
235 namespace QGlib {
236  template <int N>
237  struct GetTypeImpl<const char[N]> { //ISO C++ string literals are const char[]
238  inline operator Type() { return Type::String; };
239  };
240 }
241 
242 namespace QGlib {
243  template <int N>
244  struct GetTypeImpl<char[N]> { //gcc string literals are char[]
245  inline operator Type() { return Type::String; };
246  };
247 }
248 
249 #undef QGLIB_REGISTER_NATIVE_TYPE
250 
251 QGLIB_REGISTER_TYPE(QGlib::Type) //codegen: GType=G_TYPE_GTYPE
252 
253 #endif // QGLIB_TYPE_H
Type GetType()
Definition: type.h:180
Base class for interface wrappers.
Definition: object.h:99
Wrapper class for GObject.
Definition: object.h:89
Wrapper class for GQuark.
Definition: quark.h:42
Wrapper class for GType.
Definition: type.h:63