/**************************************************************************** ** ** https://www.qxorm.com/ ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com) ** ** This file is part of the QxOrm library ** ** This software is provided 'as-is', without any express or implied ** warranty. In no event will the authors be held liable for any ** damages arising from the use of this software ** ** Commercial Usage ** Licensees holding valid commercial QxOrm licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and Lionel Marty ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file 'license.gpl3.txt' included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met : http://www.gnu.org/copyleft/gpl.html ** ** If you are unsure which license is appropriate for your use, or ** if you have questions regarding the use of this file, please contact : ** contact@qxorm.com ** ****************************************************************************/ #ifndef _QX_CONSTRUCT_PTR_H_ #define _QX_CONSTRUCT_PTR_H_ #ifdef _MSC_VER #pragma once #endif /*! * \file construct_ptr.h * \author Lionel Marty * \ingroup QxTraits * \brief qx::trait::construct_ptr::get(T & t, bool bReset = false) : instantiate (or reset) a new pointer, support both nude-pointer and smart-pointer of boost, Qt and QxOrm libraries */ #include #if (QT_VERSION >= 0x040600) #include #endif // (QT_VERSION >= 0x040600) #include namespace qx { namespace trait { /*! * \ingroup QxTraits * \brief qx::trait::construct_ptr::get(T & t, bool bReset = false) : instantiate (or reset) a new pointer, support both nude-pointer and smart-pointer of boost, Qt and QxOrm libraries */ template struct construct_ptr { private: typedef typename std::remove_pointer::type type_ptr; public: static inline void get(T & t, bool bReset = false) { new_ptr::value, 0>::get(t, bReset); } private: template struct new_ptr { static inline void get(T & t, bool bReset) { if (bReset) { t = NULL; } else { t = new type_ptr(); } } }; template struct new_ptr { static inline void get(T & t, bool bReset) { Q_UNUSED(t); Q_UNUSED(bReset); qDebug("[QxOrm] qx::trait::construct_ptr : %s", "cannot instantiate abstract class"); } }; }; #ifdef _QX_ENABLE_BOOST template struct construct_ptr< boost::scoped_ptr > { static inline void get(boost::scoped_ptr & t, bool bReset = false) { if (bReset) { t.reset(); } else { t.reset(new T()); } } }; template struct construct_ptr< boost::shared_ptr > { static inline void get(boost::shared_ptr & t, bool bReset = false) { if (bReset) { t.reset(); } else { t.reset(new T()); } } }; template struct construct_ptr< boost::intrusive_ptr > { static inline void get(boost::intrusive_ptr & t, bool bReset = false) { if (bReset) { t.reset(); } else { t.reset(new T()); } } }; #endif // _QX_ENABLE_BOOST template struct construct_ptr< QSharedPointer > { static inline void get(QSharedPointer & t, bool bReset = false) { if (bReset) { t = QSharedPointer(); } else { t = QSharedPointer(new T()); } } }; #if (QT_VERSION >= 0x040600) template struct construct_ptr< QScopedPointer > { static inline void get(QScopedPointer & t, bool bReset = false) { if (bReset) { t = QScopedPointer(); } else { t = QScopedPointer(new T()); } } }; #endif // (QT_VERSION >= 0x040600) template struct construct_ptr< qx::dao::ptr > { static inline void get(qx::dao::ptr & t, bool bReset = false) { if (bReset) { t = qx::dao::ptr(); } else { t = qx::dao::ptr(new T()); } } }; template struct construct_ptr< std::unique_ptr > { static inline void get(std::unique_ptr & t, bool bReset = false) { if (bReset) { t.reset(); } else { t.reset(new T()); } } }; template struct construct_ptr< std::shared_ptr > { static inline void get(std::shared_ptr & t, bool bReset = false) { if (bReset) { t.reset(); } else { t = std::make_shared(); } } }; } // namespace trait } // namespace qx #endif // _QX_CONSTRUCT_PTR_H_