QxOrm
1.5.0
C++ Object Relational Mapping library
|
00001 /**************************************************************************** 00002 ** 00003 ** https://www.qxorm.com/ 00004 ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com) 00005 ** 00006 ** This file is part of the QxOrm library 00007 ** 00008 ** This software is provided 'as-is', without any express or implied 00009 ** warranty. In no event will the authors be held liable for any 00010 ** damages arising from the use of this software 00011 ** 00012 ** Commercial Usage 00013 ** Licensees holding valid commercial QxOrm licenses may use this file in 00014 ** accordance with the commercial license agreement provided with the 00015 ** Software or, alternatively, in accordance with the terms contained in 00016 ** a written agreement between you and Lionel Marty 00017 ** 00018 ** GNU General Public License Usage 00019 ** Alternatively, this file may be used under the terms of the GNU 00020 ** General Public License version 3.0 as published by the Free Software 00021 ** Foundation and appearing in the file 'license.gpl3.txt' included in the 00022 ** packaging of this file. Please review the following information to 00023 ** ensure the GNU General Public License version 3.0 requirements will be 00024 ** met : http://www.gnu.org/copyleft/gpl.html 00025 ** 00026 ** If you are unsure which license is appropriate for your use, or 00027 ** if you have questions regarding the use of this file, please contact : 00028 ** contact@qxorm.com 00029 ** 00030 ****************************************************************************/ 00031 00032 #ifndef _QX_FACTORY_H_ 00033 #define _QX_FACTORY_H_ 00034 00035 #ifdef _MSC_VER 00036 #pragma once 00037 #endif 00038 00046 #include <QtCore/qobject.h> 00047 00048 #include <QxFactory/IxFactory.h> 00049 00050 #include <QxTraits/get_base_class.h> 00051 #include <QxTraits/get_class_name.h> 00052 #include <QxTraits/get_primary_key.h> 00053 00054 #define QX_STR_CANNOT_INSTANTIATE_ABSTRACT_CLASS "[QxOrm] qx::QxFactory<T> ---> cannot instantiate abstract class '%s'" 00055 00056 #if _QX_AUTO_REGISTER_REPOSITORY 00057 #define QX_AUTO_REGISTER_REPOSITORY(className, sKey) qx::register_repository< className >(sKey); 00058 #else // _QX_AUTO_REGISTER_REPOSITORY 00059 #define QX_AUTO_REGISTER_REPOSITORY(className, sKey) /* Nothing */ 00060 #endif // _QX_AUTO_REGISTER_REPOSITORY 00061 00062 namespace qx { 00063 00064 class IxPersistable; 00065 template <class T> class QxClass; 00066 00067 #ifdef _QX_ENABLE_QT_NETWORK 00068 namespace service { 00069 class IxService; 00070 class IxParameter; 00071 } // namespace service 00072 #endif // _QX_ENABLE_QT_NETWORK 00073 00074 #if _QX_AUTO_REGISTER_REPOSITORY 00075 template <class T> 00076 inline void register_repository(const QString & sKey); 00077 #endif // _QX_AUTO_REGISTER_REPOSITORY 00078 00083 template <class T> 00084 class QxFactory : public IxFactory 00085 { 00086 00087 public: 00088 00089 QxFactory(const QString & sKey) : IxFactory(sKey) { QX_AUTO_REGISTER_REPOSITORY(T, sKey); } 00090 virtual ~QxFactory() { ; } 00091 00092 #ifdef _QX_ENABLE_QT_NETWORK 00093 virtual qx::any createObject(bool bRawPointer = false) const 00094 { 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); } 00095 00096 virtual void * createObjectNudePtr() const 00097 { 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(); } 00098 #else // _QX_ENABLE_QT_NETWORK 00099 virtual qx::any createObject(bool bRawPointer = false) const 00100 { 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); } 00101 00102 virtual void * createObjectNudePtr() const 00103 { 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(); } 00104 #endif // _QX_ENABLE_QT_NETWORK 00105 00106 #ifndef _QX_NO_RTTI 00107 virtual const std::type_info & typeInfo() const 00108 { return typeid(T); } 00109 #endif // _QX_NO_RTTI 00110 00111 private: 00112 00113 template <bool bIsAbstract /* = false */, bool bIsIxPersistable /* = false */, bool bIsIxService /* = false */, bool bIsIxParameter /* = false */, bool bIsQObject /* = false */, int dummy> 00114 struct qxCreateInstance 00115 { 00116 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); } 00117 static inline void * createNudePtr() { return static_cast<void *>(new T()); } 00118 }; 00119 00120 template <bool bIsIxPersistable, bool bIsIxService, bool bIsIxParameter, bool bIsQObject, int dummy> 00121 struct qxCreateInstance<true, bIsIxPersistable, bIsIxService, bIsIxParameter, bIsQObject, dummy> 00122 { 00123 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(); } 00124 static inline void * createNudePtr() { qDebug(QX_STR_CANNOT_INSTANTIATE_ABSTRACT_CLASS, qx::trait::get_class_name<T>::get()); return NULL; } 00125 }; 00126 00127 template <bool bIsQObject, int dummy> 00128 struct qxCreateInstance<false, true, false, false, bIsQObject, dummy> 00129 { 00130 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); } 00131 static inline void * createNudePtr() { return static_cast<qx::IxPersistable *>(new T()); } 00132 }; 00133 00134 #ifdef _QX_ENABLE_QT_NETWORK 00135 template <bool bIsQObject, int dummy> 00136 struct qxCreateInstance<false, false, true, false, bIsQObject, dummy> 00137 { 00138 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); } 00139 static inline void * createNudePtr() { return static_cast<qx::service::IxService *>(new T()); } 00140 }; 00141 00142 template <bool bIsQObject, int dummy> 00143 struct qxCreateInstance<false, false, false, true, bIsQObject, dummy> 00144 { 00145 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); } 00146 static inline void * createNudePtr() { return static_cast<qx::service::IxParameter *>(new T()); } 00147 }; 00148 #endif // _QX_ENABLE_QT_NETWORK 00149 00150 template <int dummy> 00151 struct qxCreateInstance<false, false, false, false, true, dummy> 00152 { 00153 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); } 00154 static inline void * createNudePtr() { return static_cast<QObject *>(new T()); } 00155 }; 00156 00157 }; 00158 00159 } // namespace qx 00160 00161 #include "../../inl/QxFactory/QxFactory.inl" 00162 00163 #endif // _QX_FACTORY_H_