QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
QxFactory.h
Go to the documentation of this file.
1/****************************************************************************
2**
3** https://www.qxorm.com/
4** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
5**
6** This file is part of the QxOrm library
7**
8** This software is provided 'as-is', without any express or implied
9** warranty. In no event will the authors be held liable for any
10** damages arising from the use of this software
11**
12** Commercial Usage
13** Licensees holding valid commercial QxOrm licenses may use this file in
14** accordance with the commercial license agreement provided with the
15** Software or, alternatively, in accordance with the terms contained in
16** a written agreement between you and Lionel Marty
17**
18** GNU General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU
20** General Public License version 3.0 as published by the Free Software
21** Foundation and appearing in the file 'license.gpl3.txt' included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU General Public License version 3.0 requirements will be
24** met : http://www.gnu.org/copyleft/gpl.html
25**
26** If you are unsure which license is appropriate for your use, or
27** if you have questions regarding the use of this file, please contact :
28** contact@qxorm.com
29**
30****************************************************************************/
31
32#ifndef _QX_FACTORY_H_
33#define _QX_FACTORY_H_
34
35#ifdef _MSC_VER
36#pragma once
37#endif
38
46#include <QtCore/qobject.h>
47
48#include <QxFactory/IxFactory.h>
49
53
54#define QX_STR_CANNOT_INSTANTIATE_ABSTRACT_CLASS "[QxOrm] qx::QxFactory<T> ---> cannot instantiate abstract class '%s'"
55
56#if _QX_AUTO_REGISTER_REPOSITORY
57#define QX_AUTO_REGISTER_REPOSITORY(className, sKey) qx::register_repository< className >(sKey);
58#else // _QX_AUTO_REGISTER_REPOSITORY
59#define QX_AUTO_REGISTER_REPOSITORY(className, sKey) /* Nothing */
60#endif // _QX_AUTO_REGISTER_REPOSITORY
61
62namespace qx {
63
64class IxPersistable;
65template <class T> class QxClass;
66
67#ifdef _QX_ENABLE_QT_NETWORK
68namespace service {
69class IxService;
70class IxParameter;
71} // namespace service
72#endif // _QX_ENABLE_QT_NETWORK
73
74#if _QX_AUTO_REGISTER_REPOSITORY
75template <class T>
76inline void register_repository(const QString & sKey);
77#endif // _QX_AUTO_REGISTER_REPOSITORY
78
83template <class T>
84class QxFactory : public IxFactory
85{
86
87public:
88
89 QxFactory(const QString & sKey) : IxFactory(sKey) { QX_AUTO_REGISTER_REPOSITORY(T, sKey); }
90 virtual ~QxFactory() { ; }
91
92#ifdef _QX_ENABLE_QT_NETWORK
93 virtual qx::any createObject(bool bRawPointer = false) const
94 { QxClass<T>::getSingleton(); return qxCreateInstance<std::is_abstract<T>::value, std::is_base_of<qx::IxPersistable, T>::value, std::is_base_of<qx::service::IxService, T>::value, std::is_base_of<qx::service::IxParameter, T>::value, std::is_base_of<QObject, T>::value, 0>::create(bRawPointer); }
95
96 virtual void * createObjectNudePtr() const
97 { QxClass<T>::getSingleton(); return qxCreateInstance<std::is_abstract<T>::value, std::is_base_of<qx::IxPersistable, T>::value, std::is_base_of<qx::service::IxService, T>::value, std::is_base_of<qx::service::IxParameter, T>::value, std::is_base_of<QObject, T>::value, 0>::createNudePtr(); }
98#else // _QX_ENABLE_QT_NETWORK
99 virtual qx::any createObject(bool bRawPointer = false) const
100 { QxClass<T>::getSingleton(); return qxCreateInstance<std::is_abstract<T>::value, std::is_base_of<qx::IxPersistable, T>::value, false, false, std::is_base_of<QObject, T>::value, 0>::create(bRawPointer); }
101
102 virtual void * createObjectNudePtr() const
103 { QxClass<T>::getSingleton(); return qxCreateInstance<std::is_abstract<T>::value, std::is_base_of<qx::IxPersistable, T>::value, false, false, std::is_base_of<QObject, T>::value, 0>::createNudePtr(); }
104#endif // _QX_ENABLE_QT_NETWORK
105
106#ifndef _QX_NO_RTTI
107 virtual const std::type_info & typeInfo() const
108 { return typeid(T); }
109#endif // _QX_NO_RTTI
110
111private:
112
113 template <bool bIsAbstract /* = false */, bool bIsIxPersistable /* = false */, bool bIsIxService /* = false */, bool bIsIxParameter /* = false */, bool bIsQObject /* = false */, int dummy>
115 {
116 static inline qx::any create(bool bRawPointer) { if (bRawPointer) { T * p = new T(); return qx::any(p); }; std::shared_ptr<T> ptr = std::make_shared<T>(); return qx::any(ptr); }
117 static inline void * createNudePtr() { return static_cast<void *>(new T()); }
118 };
119
120 template <bool bIsIxPersistable, bool bIsIxService, bool bIsIxParameter, bool bIsQObject, int dummy>
121 struct qxCreateInstance<true, bIsIxPersistable, bIsIxService, bIsIxParameter, bIsQObject, dummy>
122 {
123 static inline qx::any create(bool bRawPointer) { Q_UNUSED(bRawPointer); qDebug(QX_STR_CANNOT_INSTANTIATE_ABSTRACT_CLASS, qx::trait::get_class_name<T>::get()); return qx::any(); }
125 };
126
127 template <bool bIsQObject, int dummy>
128 struct qxCreateInstance<false, true, false, false, bIsQObject, dummy>
129 {
130 static inline qx::any create(bool bRawPointer) { if (bRawPointer) { T * p = new T(); return qx::any(p); }; std::shared_ptr<T> ptr = std::make_shared<T>(); return qx::any(ptr); }
131 static inline void * createNudePtr() { return static_cast<qx::IxPersistable *>(new T()); }
132 };
133
134#ifdef _QX_ENABLE_QT_NETWORK
135 template <bool bIsQObject, int dummy>
136 struct qxCreateInstance<false, false, true, false, bIsQObject, dummy>
137 {
138 static inline qx::any create(bool bRawPointer) { if (bRawPointer) { T * p = new T(); return qx::any(p); }; std::shared_ptr<T> ptr = std::make_shared<T>(); return qx::any(ptr); }
139 static inline void * createNudePtr() { return static_cast<qx::service::IxService *>(new T()); }
140 };
141
142 template <bool bIsQObject, int dummy>
143 struct qxCreateInstance<false, false, false, true, bIsQObject, dummy>
144 {
145 static inline qx::any create(bool bRawPointer) { if (bRawPointer) { T * p = new T(); return qx::any(p); }; std::shared_ptr<T> ptr = std::make_shared<T>(); return qx::any(ptr); }
146 static inline void * createNudePtr() { return static_cast<qx::service::IxParameter *>(new T()); }
147 };
148#endif // _QX_ENABLE_QT_NETWORK
149
150 template <int dummy>
151 struct qxCreateInstance<false, false, false, false, true, dummy>
152 {
153 static inline qx::any create(bool bRawPointer) { if (bRawPointer) { T * p = new T(); return qx::any(p); }; std::shared_ptr<T> ptr = std::make_shared<T>(); return qx::any(ptr); }
154 static inline void * createNudePtr() { return static_cast<QObject *>(new T()); }
155 };
156
157};
158
159} // namespace qx
160
161#include "../../inl/QxFactory/QxFactory.inl"
162
163#endif // _QX_FACTORY_H_
Common interface for all classes that can be created dynamically using the class name.
#define QX_AUTO_REGISTER_REPOSITORY(className, sKey)
Definition QxFactory.h:59
#define QX_STR_CANNOT_INSTANTIATE_ABSTRACT_CLASS
Definition QxFactory.h:54
qx::IxFactory : common interface for all classes that can be created dynamically using the class name
Definition IxFactory.h:59
qx::IxPersistable : common interface (abstract class) for persistents classes using QX_PERSISTABLE_HP...
qx::QxFactory<T> : concrete factory class to create object of type T dynamically using the class name
Definition QxFactory.h:85
QxFactory(const QString &sKey)
Definition QxFactory.h:89
virtual qx::any createObject(bool bRawPointer=false) const
Definition QxFactory.h:93
virtual const std::type_info & typeInfo() const
Definition QxFactory.h:107
virtual ~QxFactory()
Definition QxFactory.h:90
virtual void * createObjectNudePtr() const
Definition QxFactory.h:96
qx::service::IxParameter : common interface for all parameters transfered by QxService module of QxOr...
Definition IxParameter.h:74
qx::service::IxService : common interface for all services defined with QxService module of QxOrm lib...
Definition IxService.h:94
qx::trait::get_base_class<T>::type : retrieve base class of type T registered into QxOrm context and ...
qx::trait::get_class_name<T>::get() : return class name of type T under const char * format,...
qx::trait::get_primary_key<T>::type : return primary key type of T, by default primary key is long ty...
qx::any create(const QString &sKey, bool bRawPointer=false)
Return a smart-pointer new instance of object (std::shared_ptr<T>) associated by key sKey using qx::a...
Definition QxFactoryX.h:110
Root namespace for all QxOrm library features.
void register_repository(const QString &sKey)
static qx::any create(bool bRawPointer)
Definition QxFactory.h:116
qx::trait::get_class_name<T>::get() : return class name of type T under const char * format,...