Nuria Framework - Core
Core module of the NuriaProject Framework
 All Classes Functions Typedefs Enumerations Enumerator Groups
metaobject.hpp
1 /* Copyright (c) 2014, The Nuria Project
2  * This software is provided 'as-is', without any express or implied
3  * warranty. In no event will the authors be held liable for any damages
4  * arising from the use of this software.
5  * Permission is granted to anyone to use this software for any purpose,
6  * including commercial applications, and to alter it and redistribute it
7  * freely, subject to the following restrictions:
8  * 1. The origin of this software must not be misrepresented; you must not
9  * claim that you wrote the original software. If you use this software
10  * in a product, an acknowledgment in the product documentation would be
11  * appreciated but is not required.
12  * 2. Altered source versions must be plainly marked as such, and must not be
13  * misrepresented as being the original software.
14  * 3. This notice may not be removed or altered from any source
15  * distribution.
16  */
17 
18 #ifndef NURIA_METAOBJECT_HPP
19 #define NURIA_METAOBJECT_HPP
20 
21 #include "essentials.hpp"
22 #include "callback.hpp"
23 
24 #include <QVariant>
25 #include <QString>
26 
27 namespace Nuria {
28 
29 class MetaObject;
30 class MetaMethod;
31 class MetaField;
32 class MetaEnum;
33 
58 class NURIA_CORE_EXPORT MetaAnnotation {
59 public:
60 
62  MetaAnnotation ();
63 
65  bool isValid () const;
66 
68  QByteArray name () const;
69 
71  QVariant value () const;
72 
73 private:
74  friend class MetaObject;
75  friend class MetaMethod;
76  friend class MetaField;
77  friend class MetaEnum;
78 
79  inline MetaAnnotation (MetaObject *meta, short category, short index, short nth)
80  : m_meta (meta), m_category (category), m_index (index), m_nth (nth)
81  {}
82 
83  mutable MetaObject *m_meta;
84  short m_category;
85  short m_index;
86  short m_nth;
87 };
88 
137 class NURIA_CORE_EXPORT MetaMethod {
138 public:
139 
141  enum Type {
142 
144  Method = 0,
145 
147  Static = 1,
148 
150  Constructor = 2
151  };
152 
154  MetaMethod ();
155 
157  bool isValid () const;
158 
160  QByteArray name () const;
161 
163  Type type () const;
164 
166  QByteArray returnType () const;
167 
169  QVector< QByteArray > argumentTypes () const;
170 
172  QVector< QByteArray > argumentNames () const;
173 
186  Callback callback (void *instance = nullptr) const;
187 
195  Callback unsafeCallback (void *instance = nullptr) const;
196 
202  Callback testCallback (void *instance = nullptr) const;
203 
208  int annotationCount () const;
209 
214  MetaAnnotation annotation (int idx) const;
215 
220  int annotationLowerBound (const QByteArray &name) const;
221 
226  int annotationUpperBound (const QByteArray &name) const;
227 
228 private:
229  friend class MetaObject;
230 
231  inline MetaMethod (MetaObject *meta, int index)
232  : m_meta (meta), m_index (index)
233  {}
234 
235  mutable MetaObject *m_meta;
236  int m_index;
237 
238 };
239 
297 class NURIA_CORE_EXPORT MetaField {
298 public:
300  enum Access {
301  NoAccess = 0x00,
302  ReadOnly = 0x01,
303  WriteOnly = 0x02,
304  ReadWrite = ReadOnly | WriteOnly
305  };
306 
308  MetaField ();
309 
311  bool isValid () const;
312 
314  QByteArray name () const;
315 
317  QByteArray typeName () const;
318 
320  Access access () const;
321 
323  QVariant read (void *instance) const;
324 
329  bool write (void *instance, const QVariant &value);
330 
335  int annotationCount () const;
336 
341  MetaAnnotation annotation (int idx) const;
342 
347  int annotationLowerBound (const QByteArray &name) const;
348 
353  int annotationUpperBound (const QByteArray &name) const;
354 
355 private:
356  friend class MetaObject;
357 
358  inline MetaField (MetaObject *meta, short index)
359  : m_meta (meta), m_index (index)
360  {}
361 
362  mutable MetaObject *m_meta;
363  short m_index;
364 };
365 
372 class NURIA_CORE_EXPORT MetaEnum {
373 public:
374 
376  MetaEnum ();
377 
379  bool isValid () const;
380 
382  QByteArray name () const;
383 
385  int elementCount () const;
386 
388  QByteArray key (int at) const;
389 
391  int value (int at) const;
392 
397  QByteArray valueToKey (int value) const;
398 
403  int keyToValue (const QByteArray &key) const;
404 
409  int annotationCount () const;
410 
415  MetaAnnotation annotation (int idx) const;
416 
421  int annotationLowerBound (const QByteArray &name) const;
422 
427  int annotationUpperBound (const QByteArray &name) const;
428 
429 private:
430  friend class MetaObject;
431 
432  inline MetaEnum (MetaObject *meta, int index)
433  : m_meta (meta), m_index (index)
434  {}
435 
436  mutable MetaObject *m_meta;
437  int m_index;
438 
439 };
440 
442 typedef QMap< QByteArray, MetaObject * > MetaObjectMap;
443 
563 class NURIA_CORE_EXPORT MetaObject {
564 public:
565 
570  enum class GateMethod {
571  ClassName = 0, // QByteArray
572  MetaTypeId = 1, // int
573  PointerMetaTypeId = 2, // int
574  BaseClasses = 3, // QVector< QByteArray >
575  AnnotationCount = 4, // int
576  MethodCount = 5, // int
577  FieldCount = 6, // int
578  EnumCount = 7, // int
579 
580  AnnotationName = 10, // QByteArray
581  AnnotationValue = 11, // QVariant
582 
583  MethodName = 20, // QByteArray
584  MethodType = 21, // Nuria::MetaMethod::Type
585  MethodReturnType = 22, // QByteArray
586  MethodArgumentNames = 23, // QVector< QByteArray >
587  MethodArgumentTypes = 24, // QVector< QByteArray >
588  MethodCallback = 25, // Nuria::Callback, additional = void *instance
589  MethodUnsafeCallback = 26, // Nuria::Callback, additional = void *instance
590  MethodArgumentTest = 27, // Nuria::Callback, additional = void *instance
591 
592  FieldName = 30, // QByteArray
593  FieldType = 31, // QByteArray
594  FieldRead = 32, // QVariant, additional = void *instance
595  FieldWrite = 33, // bool, additional = void** = { instance, value }
596  FieldAccess = 34, // MetaField::Access
597 
598  EnumName = 40, // QByteArray
599  EnumElementCount = 41, // int
600  EnumElementKey = 42, // QByteArray
601  EnumElementValue = 43, // int
602 
603  DestroyInstance = 50 // void, additional = void *instance
604  };
605 
611  template< typename T >
612  static MetaObject *of () {
613  int id = qMetaTypeId< T * > ();
614  const char *typeName = QMetaType::typeName (id);
615  return MetaObject::byName (QByteArray (typeName, ::strlen (typeName) - 1));
616  }
617 
622  static MetaObject *byName (const QByteArray &type);
623 
628  static MetaObjectMap typesInheriting (const QByteArray &typeName);
629 
634  static MetaObjectMap typesWithAnnotation (const QByteArray &name);
635 
639  static MetaObjectMap allTypes ();
640 
651  static void registerMetaObject (MetaObject *object);
652 
654  inline MetaObject () {}
655 
657  virtual ~MetaObject () {}
658 
662  QByteArray className ();
663 
668  int metaTypeId ();
669 
673  int pointerMetaTypeId ();
674 
678  QVector< QByteArray > parents ();
679 
684  int annotationCount ();
685 
690  MetaAnnotation annotation (int idx) const;
691 
696  int annotationLowerBound (const QByteArray &name) const;
697 
702  int annotationUpperBound (const QByteArray &name) const;
703 
708  int methodCount ();
709 
714  MetaMethod method (int idx);
715 
720  int methodLowerBound (const QByteArray &name);
721 
726  int methodUpperBound (const QByteArray &name);
727 
736  MetaMethod method (const QVector< QByteArray > &prototype);
737 
743  void destroyInstance (void *instance);
744 
749  int fieldCount ();
750 
755  MetaField field (int idx);
756 
762  MetaField fieldByName (const QByteArray &name);
763 
768  int enumCount ();
769 
774  MetaEnum enumAt (int idx);
775 
781  MetaEnum enumByName (const QByteArray &name);
782 
783 protected:
784 
789  virtual void gateCall (GateMethod method, int category, int index, int nth,
790  void *result, void *additional = 0) = 0;
791 
792 private:
793  friend class MetaAnnotation;
794  friend class MetaMethod;
795  friend class MetaField;
796  friend class MetaEnum;
797 
798 };
799 
800 }
801 
802 #endif // NURIA_METAOBJECT_HPP
A modern style callback mechanism which can be bound to various method types including slots...
Definition: callback.hpp:141
The MetaAnnotation class allows access to annotations.
Definition: metaobject.hpp:58
Access
Definition: metaobject.hpp:300
Type
Definition: metaobject.hpp:141
static MetaObject * byName(const QByteArray &type)
The MetaEnum class provides access to enum's in a MetaObject.
Definition: metaobject.hpp:372
The MetaField class lets you access fields of types.
Definition: metaobject.hpp:297
GateMethod
Definition: metaobject.hpp:570
MetaObject()
Definition: metaobject.hpp:654
static MetaObject * of()
Definition: metaobject.hpp:612
The MetaObject class provides access to meta-data of types at run-time.
Definition: metaobject.hpp:563
The MetaMethod class lets you access methods from registered types.
Definition: metaobject.hpp:137
virtual ~MetaObject()
Definition: metaobject.hpp:657