QxOrm  1.2.9
C++ Object Relational Mapping library
QxFactory.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** http://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 <boost/shared_ptr.hpp>
00047 #include <boost/static_assert.hpp>
00048 #include <boost/type_traits/is_abstract.hpp>
00049 
00050 #include <QxFactory/IxFactory.h>
00051 
00052 #include <QxTraits/get_base_class.h>
00053 #include <QxTraits/get_class_name.h>
00054 #include <QxTraits/get_primary_key.h>
00055 
00056 #define QX_STR_CANNOT_INSTANTIATE_ABSTRACT_CLASS "[QxOrm] qx::QxFactory<T> ---> cannot instantiate abstract class '%s'"
00057 
00058 #if _QX_AUTO_REGISTER_REPOSITORY
00059 #define QX_AUTO_REGISTER_REPOSITORY(className, sKey) qx::register_repository< className >(sKey);
00060 #else // _QX_AUTO_REGISTER_REPOSITORY
00061 #define QX_AUTO_REGISTER_REPOSITORY(className, sKey) /* Nothing */
00062 #endif // _QX_AUTO_REGISTER_REPOSITORY
00063 
00064 namespace qx {
00065 
00066 template <class T>
00067 class QxClass;
00068 
00069 #if _QX_AUTO_REGISTER_REPOSITORY
00070 template <class T>
00071 inline void register_repository(const QString & sKey);
00072 #endif // _QX_AUTO_REGISTER_REPOSITORY
00073 
00078 template <class T>
00079 class QxFactory : public IxFactory
00080 {
00081 
00082 public:
00083 
00084    QxFactory(const QString & sKey) : IxFactory(sKey) { QX_AUTO_REGISTER_REPOSITORY(T, sKey); }
00085    virtual ~QxFactory() { ; }
00086 
00087    virtual boost::any createObject() const
00088    { QxClass<T>::getSingleton(); return qxCreateInstance<boost::is_abstract<T>::value, 0>::create(); }
00089 
00090    virtual void * createObjectNudePtr() const
00091    { QxClass<T>::getSingleton(); return qxCreateInstance<boost::is_abstract<T>::value, 0>::createNudePtr(); }
00092 
00093    virtual const std::type_info & typeInfo() const
00094    { return typeid(T); }
00095 
00096 private:
00097 
00098    template <bool bIsAbstract /* = false */, int dummy>
00099    struct qxCreateInstance
00100    {
00101       static inline boost::any create()      { boost::shared_ptr<T> ptr; ptr.reset(new T()); return boost::any(ptr); }
00102       static inline void * createNudePtr()   { return static_cast<void *>(new T()); }
00103    };
00104 
00105    template <int dummy>
00106    struct qxCreateInstance<true, dummy>
00107    {
00108       static inline boost::any create()      { qDebug(QX_STR_CANNOT_INSTANTIATE_ABSTRACT_CLASS, qx::trait::get_class_name<T>::get()); return boost::any(); }
00109       static inline void * createNudePtr()   { qDebug(QX_STR_CANNOT_INSTANTIATE_ABSTRACT_CLASS, qx::trait::get_class_name<T>::get()); return NULL; }
00110    };
00111 
00112 };
00113 
00114 } // namespace qx
00115 
00116 #include "../../inl/QxFactory/QxFactory.inl"
00117 
00118 #endif // _QX_FACTORY_H_