Dataquay 0.8
Dataquay::ObjectBuilder Class Reference

ObjectBuilder is a singleton object factory capable of constructing new objects of classes that are subclassed from QObject. More...

#include <dataquay/objectmapper/ObjectBuilder.h>

Public Member Functions

template<typename T >
void registerClass ()
 Register type T, a subclass of QObject, as a class that can be constructed by calling a zero-argument constructor.
 
template<typename T , typename Parent >
void registerClass ()
 Register type T, a subclass of QObject, as a class that can be constructed by calling a single-argument constructor whose argument is of pointer-to-Parent type, where Parent is also a subclass of QObject.
 
template<typename T >
void registerClass (QString pointerName)
 Register type T, a subclass of QObject, as a class that can be constructed by calling a zero-argument constructor.
 
template<typename T , typename Parent >
void registerClass (QString pointerName)
 Register type T, a subclass of QObject, as a class that can be constructed by calling a single-argument constructor whose argument is of pointer-to-Parent type, where Parent is also a subclass of QObject.
 
template<typename T >
void registerInterface (QString pointerName)
 Register type T, a subclass of QObject, as an interface (a pure virtual class) and pointerName to be the meta type name for pointers to type T, such that QVariant can be used to store such pointers.
 
bool knows (QString className)
 Return true if the class whose class name (according to its meta object) is className has been registered for building.
 
QObject * build (QString className, QObject *parent)
 Return a new object whose class name (according to its meta object) is className, with the given parent (cast appropriately) passed to its single argument constructor.
 
QObject * build (QString className)
 Return a new object whose class name (according to its meta object) is className, constructed with no parent.
 
bool canExtract (QString pointerName)
 Return true if the class whose pointer has meta-type name pointerName has been registered with that pointer name (i.e.
 
bool canInject (QString pointerName)
 Return true if the class whose pointer has meta-type name pointerName has been registered with that pointer name (i.e.
 
QObject * extract (QString pointerName, QVariant &v)
 Provided the given pointerName has been registered using one of the registerClass(pointerName) methods or registerInterface, take the given variant containing that pointer type and extract and return the pointer.
 
QVariant inject (QString pointerName, QObject *p)
 Provided the given pointerName has been registered using one of the registerClass(pointerName) methods or registerInterface, take the given pointer and stuff it into a variant, returning the result.
 
QString getClassNameForPointerName (QString pointerName) const
 Provided the given pointerName has been registered using one of the registerClass(pointerName) methods, return the name of the class that was used as the template argument for that method.
 
QString getPointerNameForClassName (QString className) const
 If the class whose class name (according to its meta object) is className has been registered using one of the registerClass(pointerName) methods, return the pointerName that was passed to that method.
 

Static Public Member Functions

static ObjectBuildergetInstance ()
 Retrieve the single global instance of ObjectBuilder.
 

Detailed Description

ObjectBuilder is a singleton object factory capable of constructing new objects of classes that are subclassed from QObject.

Given the class name as a string, and optionally a parent object, it will return a new instance of the class. To be capable of construction using ObjectBuilder, a class must be declared using Q_OBJECT as well as subclassed from QObject.

All candidate object classes need to be registered with the builder before they can be constructed. The only class that ObjectBuilder is able to construct without registration is QObject itself.

This class permits code to construct new objects dynamically, without needing to know anything about them except for their class names, and without needing their definitions to be visible. (The definitions must be visible when the object classes are registered, but not when the objects are constructed.)

Definition at line 63 of file ObjectBuilder.h.

Member Function Documentation

◆ getInstance()

static ObjectBuilder * Dataquay::ObjectBuilder::getInstance ( )
static

Retrieve the single global instance of ObjectBuilder.

◆ registerClass() [1/4]

template<typename T >
void Dataquay::ObjectBuilder::registerClass ( )
inline

Register type T, a subclass of QObject, as a class that can be constructed by calling a zero-argument constructor.

For example, registerClass<QAction>() declares that QAction is a subclass of QObject that may be built by calling QAction::QAction().

A subsequent call to ObjectBuilder::build("QAction") would return a new QAction built with that constructor (since "QAction" is the class name of QAction returned by its meta object).

Definition at line 85 of file ObjectBuilder.h.

◆ registerClass() [2/4]

template<typename T , typename Parent >
void Dataquay::ObjectBuilder::registerClass ( )
inline

Register type T, a subclass of QObject, as a class that can be constructed by calling a single-argument constructor whose argument is of pointer-to-Parent type, where Parent is also a subclass of QObject.

For example, registerClass<QWidget, QWidget>() declares that QWidget is a subclass of QObject that may be built by calling QWidget::QWidget(QWidget *parent).

A subsequent call to ObjectBuilder::build("QWidget", parent) would return a new QWidget built with that constructor (since "QWidget" is the class name of QWidget returned by its meta object).

Definition at line 105 of file ObjectBuilder.h.

◆ registerClass() [3/4]

template<typename T >
void Dataquay::ObjectBuilder::registerClass ( QString pointerName)
inline

Register type T, a subclass of QObject, as a class that can be constructed by calling a zero-argument constructor.

Also declare pointerName to be the meta type name for pointers to type T, such that QVariant can be used to store such pointers.

For example, registerClass<QAction>("QAction*") declares that QAction is a subclass of QObject that may be built by calling QAction::QAction(), and that "QAction*" has been registered (using qRegisterMetaType) as the meta type name for pointer-to-QAction.

A subsequent call to ObjectBuilder::build("QAction") would return a new QAction built with that constructor (since "QAction" is the class name of QAction returned by its meta object).

Definition at line 127 of file ObjectBuilder.h.

◆ registerClass() [4/4]

template<typename T , typename Parent >
void Dataquay::ObjectBuilder::registerClass ( QString pointerName)
inline

Register type T, a subclass of QObject, as a class that can be constructed by calling a single-argument constructor whose argument is of pointer-to-Parent type, where Parent is also a subclass of QObject.

Also declare pointerName to be the meta type name for pointers to type T, such that QVariant can be used to store such pointers.

For example, registerClass<QWidget, QWidget>("QWidget*") declares that QWidget is a subclass of QObject that may be built by calling QWidget::QWidget(QWidget <em>parent), and that "QWidget</em>" has been registered (using qRegisterMetaType) as the meta type name for pointer-to-QWidget.

A subsequent call to ObjectBuilder::build("QWidget", parent) would return a new QWidget built with that constructor (since "QWidget" is the class name of QWidget returned by its meta object).

Definition at line 155 of file ObjectBuilder.h.

◆ registerInterface()

template<typename T >
void Dataquay::ObjectBuilder::registerInterface ( QString pointerName)
inline

Register type T, a subclass of QObject, as an interface (a pure virtual class) and pointerName to be the meta type name for pointers to type T, such that QVariant can be used to store such pointers.

For example, registerClass<Command>("Command*") declares that Command is a subclass of QObject that may not be built directly but that "Command*" has been registered (using qRegisterMetaType) as the meta type name for pointer-to-QAction.

A subsequent call to ObjectBuilder::extract("Command*", v) would extract a pointer of type Command* from the QVariant v.

Definition at line 179 of file ObjectBuilder.h.

◆ knows()

bool Dataquay::ObjectBuilder::knows ( QString className)
inline

Return true if the class whose class name (according to its meta object) is className has been registered for building.

Definition at line 190 of file ObjectBuilder.h.

Referenced by build(), and build().

◆ build() [1/2]

QObject * Dataquay::ObjectBuilder::build ( QString className,
QObject * parent )
inline

Return a new object whose class name (according to its meta object) is className, with the given parent (cast appropriately) passed to its single argument constructor.

Definition at line 199 of file ObjectBuilder.h.

References knows().

◆ build() [2/2]

QObject * Dataquay::ObjectBuilder::build ( QString className)
inline

Return a new object whose class name (according to its meta object) is className, constructed with no parent.

Definition at line 208 of file ObjectBuilder.h.

References knows().

◆ canExtract()

bool Dataquay::ObjectBuilder::canExtract ( QString pointerName)
inline

Return true if the class whose pointer has meta-type name pointerName has been registered with that pointer name (i.e.

using one of the registerClass(pointerName) methods or registerInterface).

Definition at line 219 of file ObjectBuilder.h.

Referenced by extract().

◆ canInject()

bool Dataquay::ObjectBuilder::canInject ( QString pointerName)
inline

Return true if the class whose pointer has meta-type name pointerName has been registered with that pointer name (i.e.

using one of the registerClass(pointerName) methods or registerInterface).

Definition at line 229 of file ObjectBuilder.h.

Referenced by inject().

◆ extract()

QObject * Dataquay::ObjectBuilder::extract ( QString pointerName,
QVariant & v )
inline

Provided the given pointerName has been registered using one of the registerClass(pointerName) methods or registerInterface, take the given variant containing that pointer type and extract and return the pointer.

Definition at line 239 of file ObjectBuilder.h.

References canExtract().

◆ inject()

QVariant Dataquay::ObjectBuilder::inject ( QString pointerName,
QObject * p )
inline

Provided the given pointerName has been registered using one of the registerClass(pointerName) methods or registerInterface, take the given pointer and stuff it into a variant, returning the result.

Definition at line 250 of file ObjectBuilder.h.

References canInject().

◆ getClassNameForPointerName()

QString Dataquay::ObjectBuilder::getClassNameForPointerName ( QString pointerName) const
inline

Provided the given pointerName has been registered using one of the registerClass(pointerName) methods, return the name of the class that was used as the template argument for that method.

Definition at line 260 of file ObjectBuilder.h.

◆ getPointerNameForClassName()

QString Dataquay::ObjectBuilder::getPointerNameForClassName ( QString className) const
inline

If the class whose class name (according to its meta object) is className has been registered using one of the registerClass(pointerName) methods, return the pointerName that was passed to that method.

Definition at line 271 of file ObjectBuilder.h.


The documentation for this class was generated from the following file: