QxFunction_0.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /****************************************************************************
  2. **
  3. ** https://www.qxorm.com/
  4. ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
  5. **
  6. ** This file is part of the QxOrm library
  7. **
  8. ** This software is provided 'as-is', without any express or implied
  9. ** warranty. In no event will the authors be held liable for any
  10. ** damages arising from the use of this software
  11. **
  12. ** Commercial Usage
  13. ** Licensees holding valid commercial QxOrm licenses may use this file in
  14. ** accordance with the commercial license agreement provided with the
  15. ** Software or, alternatively, in accordance with the terms contained in
  16. ** a written agreement between you and Lionel Marty
  17. **
  18. ** GNU General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU
  20. ** General Public License version 3.0 as published by the Free Software
  21. ** Foundation and appearing in the file 'license.gpl3.txt' included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU General Public License version 3.0 requirements will be
  24. ** met : http://www.gnu.org/copyleft/gpl.html
  25. **
  26. ** If you are unsure which license is appropriate for your use, or
  27. ** if you have questions regarding the use of this file, please contact :
  28. ** contact@qxorm.com
  29. **
  30. ****************************************************************************/
  31. #ifndef _QX_FUNCTION_0_H_
  32. #define _QX_FUNCTION_0_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxFunction_0.h
  38. * \author Lionel Marty
  39. * \ingroup QxFunction
  40. * \brief Concrete function class registered into QxOrm context without parameter
  41. */
  42. #include <QxFunction/IxFunction.h>
  43. #include <QxFunction/QxParameters.h>
  44. namespace qx {
  45. /*!
  46. * \ingroup QxFunction
  47. * \brief qx::QxFunction_0<Owner, R> : concrete function registered into QxOrm context defined into class Owner, returning an object of type R and without parameter
  48. */
  49. template <class Owner, typename R>
  50. class QxFunction_0 : public IxFunction
  51. {
  52. public:
  53. typedef std::function<R (Owner *)> type_fct;
  54. QX_FUNCTION_CLASS_MEMBER_FCT(QxFunction_0);
  55. virtual int getParamCount() const { return 0; }
  56. virtual qx_bool isValidParams(const QString & params) const { Q_UNUSED(params); return true; }
  57. virtual qx_bool isValidParams(const type_any_params & params) const { Q_UNUSED(params); return true; }
  58. private:
  59. template <class T, bool bReturnValue /* = false */>
  60. struct QxInvokerFct
  61. {
  62. static inline qx_bool invoke(void * pOwner, const T & params, qx::any * ret, const QxFunction_0 * pThis)
  63. {
  64. QX_FUNCTION_INVOKE_START_WITH_OWNER();
  65. try { pThis->m_fct(static_cast<Owner *>(pOwner)); }
  66. QX_FUNCTION_CATCH_AND_RETURN_INVOKE();
  67. }
  68. };
  69. template <class T>
  70. struct QxInvokerFct<T, true>
  71. {
  72. static inline qx_bool invoke(void * pOwner, const T & params, qx::any * ret, const QxFunction_0 * pThis)
  73. {
  74. QX_FUNCTION_INVOKE_START_WITH_OWNER();
  75. try { R retTmp = pThis->m_fct(static_cast<Owner *>(pOwner)); if (ret) { (* ret) = qx::any(retTmp); } }
  76. QX_FUNCTION_CATCH_AND_RETURN_INVOKE();
  77. }
  78. };
  79. };
  80. template <typename R>
  81. class QxFunction_0<void, R> : public IxFunction
  82. {
  83. public:
  84. typedef std::function<R ()> type_fct;
  85. QX_FUNCTION_CLASS_FCT(QxFunction_0);
  86. virtual int getParamCount() const { return 0; }
  87. virtual qx_bool isValidParams(const QString & params) const { Q_UNUSED(params); return true; }
  88. virtual qx_bool isValidParams(const type_any_params & params) const { Q_UNUSED(params); return true; }
  89. private:
  90. template <class T, bool bReturnValue /* = false */>
  91. struct QxInvokerFct
  92. {
  93. static inline qx_bool invoke(const T & params, qx::any * ret, const QxFunction_0 * pThis)
  94. {
  95. QX_FUNCTION_INVOKE_START_WITHOUT_OWNER();
  96. try { pThis->m_fct(); }
  97. QX_FUNCTION_CATCH_AND_RETURN_INVOKE();
  98. }
  99. };
  100. template <class T>
  101. struct QxInvokerFct<T, true>
  102. {
  103. static inline qx_bool invoke(const T & params, qx::any * ret, const QxFunction_0 * pThis)
  104. {
  105. QX_FUNCTION_INVOKE_START_WITHOUT_OWNER();
  106. try { R retTmp = pThis->m_fct(); if (ret) { (* ret) = qx::any(retTmp); } }
  107. QX_FUNCTION_CATCH_AND_RETURN_INVOKE();
  108. }
  109. };
  110. };
  111. namespace function {
  112. template <class Owner, typename R>
  113. IxFunction_ptr bind_fct_0(const typename QxFunction_0<Owner, R>::type_fct & fct)
  114. {
  115. typedef std::is_same<Owner, void> qx_verify_owner_tmp;
  116. static_assert(qx_verify_owner_tmp::value, "qx_verify_owner_tmp::value");
  117. IxFunction_ptr ptr = std::make_shared<QxFunction_0<void, R> >(fct);
  118. return ptr;
  119. }
  120. template <class Owner, typename R>
  121. IxFunction_ptr bind_member_fct_0(const typename QxFunction_0<Owner, R>::type_fct & fct)
  122. {
  123. typedef std::is_same<Owner, void> qx_verify_owner_tmp;
  124. static_assert(! qx_verify_owner_tmp::value, "! qx_verify_owner_tmp::value");
  125. IxFunction_ptr ptr = std::make_shared<QxFunction_0<Owner, R> >(fct);
  126. return ptr;
  127. }
  128. } // namespace function
  129. } // namespace qx
  130. #endif // _QX_FUNCTION_0_H_