![]() |
QxOrm
1.2.3
C++ Object Relational Mapping library
|
00001 /**************************************************************************** 00002 ** 00003 ** http://www.qxorm.com/ 00004 ** http://sourceforge.net/projects/qxorm/ 00005 ** Original file by Lionel Marty 00006 ** 00007 ** This file is part of the QxOrm library 00008 ** 00009 ** This software is provided 'as-is', without any express or implied 00010 ** warranty. In no event will the authors be held liable for any 00011 ** damages arising from the use of this software. 00012 ** 00013 ** GNU Lesser General Public License Usage 00014 ** This file must be used under the terms of the GNU Lesser 00015 ** General Public License version 2.1 as published by the Free Software 00016 ** Foundation and appearing in the file 'license.lgpl.txt' included in the 00017 ** packaging of this file. Please review the following information to 00018 ** ensure the GNU Lesser General Public License version 2.1 requirements 00019 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 00020 ** 00021 ** If you have questions regarding the use of this file, please contact : 00022 ** contact@qxorm.com 00023 ** 00024 ****************************************************************************/ 00025 00026 #ifndef _QX_FACTORY_H_ 00027 #define _QX_FACTORY_H_ 00028 00029 #ifdef _MSC_VER 00030 #pragma once 00031 #endif 00032 00040 #include <boost/shared_ptr.hpp> 00041 #include <boost/static_assert.hpp> 00042 #include <boost/type_traits/is_abstract.hpp> 00043 00044 #include <QxFactory/IxFactory.h> 00045 00046 #include <QxTraits/get_base_class.h> 00047 #include <QxTraits/get_class_name.h> 00048 #include <QxTraits/get_primary_key.h> 00049 00050 #define QX_STR_CANNOT_INSTANTIATE_ABSTRACT_CLASS "[QxOrm] qx::QxFactory<T> ---> cannot instantiate abstract class '%s'" 00051 00052 #if _QX_AUTO_REGISTER_REPOSITORY 00053 #define QX_AUTO_REGISTER_REPOSITORY(className, sKey) qx::register_repository< className >(sKey); 00054 #else // _QX_AUTO_REGISTER_REPOSITORY 00055 #define QX_AUTO_REGISTER_REPOSITORY(className, sKey) /* Nothing */ 00056 #endif // _QX_AUTO_REGISTER_REPOSITORY 00057 00058 namespace qx { 00059 00060 template <class T> 00061 class QxClass; 00062 00063 #if _QX_AUTO_REGISTER_REPOSITORY 00064 template <class T> 00065 inline void register_repository(const QString & sKey); 00066 #endif // _QX_AUTO_REGISTER_REPOSITORY 00067 00072 template <class T> 00073 class QxFactory : public IxFactory 00074 { 00075 00076 public: 00077 00078 QxFactory(const QString & sKey) : IxFactory(sKey) { QX_AUTO_REGISTER_REPOSITORY(T, sKey); } 00079 virtual ~QxFactory() { ; } 00080 00081 virtual boost::any createObject() const 00082 { QxClass<T>::getSingleton(); return qxCreateInstance<boost::is_abstract<T>::value, 0>::create(); } 00083 00084 virtual void * createObjectNudePtr() const 00085 { QxClass<T>::getSingleton(); return qxCreateInstance<boost::is_abstract<T>::value, 0>::createNudePtr(); } 00086 00087 virtual const std::type_info & typeInfo() const 00088 { return typeid(T); } 00089 00090 private: 00091 00092 template <bool bIsAbstract /* = false */, int dummy> 00093 struct qxCreateInstance 00094 { 00095 static inline boost::any create() { boost::shared_ptr<T> ptr; ptr.reset(new T()); return boost::any(ptr); } 00096 static inline void * createNudePtr() { return static_cast<void *>(new T()); } 00097 }; 00098 00099 template <int dummy> 00100 struct qxCreateInstance<true, dummy> 00101 { 00102 static inline boost::any create() { qDebug(QX_STR_CANNOT_INSTANTIATE_ABSTRACT_CLASS, qx::trait::get_class_name<T>::get()); return boost::any(); } 00103 static inline void * createNudePtr() { qDebug(QX_STR_CANNOT_INSTANTIATE_ABSTRACT_CLASS, qx::trait::get_class_name<T>::get()); return NULL; } 00104 }; 00105 00106 }; 00107 00108 } // namespace qx 00109 00110 #include "../../inl/QxFactory/QxFactory.inl" 00111 00112 #endif // _QX_FACTORY_H_