![]() |
QxOrm
1.4.6
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 <QxFactory/IxFactory.h> 00047 00048 #include <QxTraits/get_base_class.h> 00049 #include <QxTraits/get_class_name.h> 00050 #include <QxTraits/get_primary_key.h> 00051 00052 #define QX_STR_CANNOT_INSTANTIATE_ABSTRACT_CLASS "[QxOrm] qx::QxFactory<T> ---> cannot instantiate abstract class '%s'" 00053 00054 #if _QX_AUTO_REGISTER_REPOSITORY 00055 #define QX_AUTO_REGISTER_REPOSITORY(className, sKey) qx::register_repository< className >(sKey); 00056 #else // _QX_AUTO_REGISTER_REPOSITORY 00057 #define QX_AUTO_REGISTER_REPOSITORY(className, sKey) /* Nothing */ 00058 #endif // _QX_AUTO_REGISTER_REPOSITORY 00059 00060 namespace qx { 00061 00062 template <class T> 00063 class QxClass; 00064 00065 #if _QX_AUTO_REGISTER_REPOSITORY 00066 template <class T> 00067 inline void register_repository(const QString & sKey); 00068 #endif // _QX_AUTO_REGISTER_REPOSITORY 00069 00074 template <class T> 00075 class QxFactory : public IxFactory 00076 { 00077 00078 public: 00079 00080 QxFactory(const QString & sKey) : IxFactory(sKey) { QX_AUTO_REGISTER_REPOSITORY(T, sKey); } 00081 virtual ~QxFactory() { ; } 00082 00083 virtual qx::any createObject(bool bRawPointer = false) const 00084 { QxClass<T>::getSingleton(); return qxCreateInstance<std::is_abstract<T>::value, 0>::create(bRawPointer); } 00085 00086 virtual void * createObjectNudePtr() const 00087 { QxClass<T>::getSingleton(); return qxCreateInstance<std::is_abstract<T>::value, 0>::createNudePtr(); } 00088 00089 #ifndef _QX_NO_RTTI 00090 virtual const std::type_info & typeInfo() const 00091 { return typeid(T); } 00092 #endif // _QX_NO_RTTI 00093 00094 private: 00095 00096 template <bool bIsAbstract /* = false */, int dummy> 00097 struct qxCreateInstance 00098 { 00099 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); } 00100 static inline void * createNudePtr() { return static_cast<void *>(new T()); } 00101 }; 00102 00103 template <int dummy> 00104 struct qxCreateInstance<true, dummy> 00105 { 00106 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(); } 00107 static inline void * createNudePtr() { qDebug(QX_STR_CANNOT_INSTANTIATE_ABSTRACT_CLASS, qx::trait::get_class_name<T>::get()); return NULL; } 00108 }; 00109 00110 }; 00111 00112 } // namespace qx 00113 00114 #include "../../inl/QxFactory/QxFactory.inl" 00115 00116 #endif // _QX_FACTORY_H_