File indexing completed on 2024-05-12 16:42:44

0001 /*
0002     SPDX-FileCopyrightText: 2013-2014 Christian Dávid <christian-david@web.de>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef ONLINEJOBTYPED_H
0007 #define ONLINEJOBTYPED_H
0008 
0009 #include "onlinejob.h"
0010 #include "onlinejobadministration.h"
0011 
0012 /**
0013  * @brief Convenient template if you know the task type of an onlineJob
0014  *
0015  * To prevent using onlineJob.task<T>() repeatingly you can use this class
0016  * where task() has a defined return type.
0017  *
0018  * Any type check is done in the constructors. So an invalid onlineJobTyped
0019  * cannot exist. This class is very fast as well because task() does not need
0020  * any checks.
0021  *
0022  * onlineJobTyped::isNull() is always false. All constructors will throw
0023  * onlineJobTyped::badCast or onlineJobTyped::emptyTask if they fail.
0024  */
0025 template<class T>
0026 class onlineJobTyped : public onlineJob
0027 {
0028     KMM_MYMONEY_UNIT_TESTABLE
0029 public:
0030     /**
0031      * @brief create new task
0032      *
0033      * @throws emptyTask if plugin could not be found/loaded (determined at runtime).
0034      */
0035     explicit onlineJobTyped();
0036 
0037     /**
0038       * @brief Create typed onlineJob
0039       *
0040       * @throws emptyTask if task == 0
0041       */
0042     explicit onlineJobTyped(T* task, const QString& id = QString());
0043 
0044     /** @brief Copy constructor */
0045     onlineJobTyped(onlineJobTyped<T> const& other);
0046 
0047     /**
0048       * @brief Copy from onlineJob
0049       *
0050       * @throws badTaskCast if task in other does not fit T
0051       * @throws emptyTask if other has no task
0052       */
0053     explicit onlineJobTyped(const onlineJob &other);
0054 
0055     /** @brief Copy constructor with new id */
0056     explicit onlineJobTyped(const QString &id, const onlineJobTyped<T>& other);
0057 
0058     /** Does not throw */
0059     inline T* task(); // krazy:exclude=inline
0060 
0061     /** Does not throw */
0062     inline const T* task() const; // krazy:exclude=inline
0063 
0064     /** Does not throw */
0065     inline const T* constTask() const { // krazy:exclude=inline
0066         return task();
0067     }
0068 
0069     /** Does not throw */
0070     onlineJobTyped<T> operator =(onlineJobTyped<T> const& other);
0071 
0072 private:
0073     T* m_taskTyped;
0074 };
0075 
0076 template<class T>
0077 onlineJobTyped<T>::onlineJobTyped()
0078     : onlineJob(onlineJobAdministration::instance()->createOnlineTask(T::name()))
0079 {
0080     m_taskTyped = static_cast<T*>(onlineJob::task());   // this can throw emptyTask
0081 
0082     // Just be safe: an onlineTask developer could have done something wrong
0083     Q_CHECK_PTR(dynamic_cast<T*>(onlineJob::task()));
0084 }
0085 
0086 template<class T>
0087 onlineJobTyped<T>::onlineJobTyped(T* task, const QString& id)
0088     : onlineJob(task, id),
0089       m_taskTyped(task)
0090 {
0091     if (!task)
0092         throw EMPTYTASKEXCEPTION;
0093 }
0094 
0095 template<class T>
0096 onlineJobTyped<T>::onlineJobTyped(onlineJobTyped<T> const& other)
0097     : onlineJob(other)
0098 {
0099     m_taskTyped = dynamic_cast<T*>(onlineJob::task());
0100     Q_CHECK_PTR(m_taskTyped);
0101 }
0102 
0103 template<class T>
0104 onlineJobTyped<T> onlineJobTyped<T>::operator =(onlineJobTyped<T> const & other)
0105 {
0106     onlineJob::operator =(other);
0107     m_taskTyped = dynamic_cast<T*>(onlineJob::task());
0108     Q_CHECK_PTR(m_taskTyped);
0109     return (*this);
0110 }
0111 
0112 template<class T>
0113 onlineJobTyped<T>::onlineJobTyped(const onlineJob &other)
0114     : onlineJob(other)
0115 {
0116     m_taskTyped = dynamic_cast<T*>(onlineJob::task()); // can throw emptyTask
0117     if (!m_taskTyped)
0118         throw BADTASKEXCEPTION;
0119 }
0120 
0121 template<class T>
0122 T* onlineJobTyped<T>::task()
0123 {
0124     Q_CHECK_PTR(m_taskTyped);
0125     return m_taskTyped;
0126 }
0127 
0128 template<class T>
0129 const T* onlineJobTyped<T>::task() const
0130 {
0131     Q_CHECK_PTR(m_taskTyped);
0132     return m_taskTyped;
0133 }
0134 
0135 #endif // ONLINEJOBTYPED_H