QxFunctionMacro.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_MACRO_H_
  32. #define _QX_FUNCTION_MACRO_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. #include <QxConvert/QxConvert.h>
  37. #define QX_FUNCTION_CLASS_FCT(className) \
  38. public: \
  39. type_fct m_fct; \
  40. className(type_fct fct) : IxFunction(), m_fct(fct) { ; }; \
  41. virtual ~className() { ; }; \
  42. virtual qx_bool invoke(const QString & params = QString(), qx::any * ret = NULL) const \
  43. { return QxInvokerFct<QString, ! std::is_same<R, void>::value>::invoke(params, ret, this); } \
  44. virtual qx_bool invoke(const type_any_params & params, qx::any * ret = NULL) const \
  45. { return QxInvokerFct<type_any_params, ! std::is_same<R, void>::value>::invoke(params, ret, this); } \
  46. virtual qx_bool invoke(void * pOwner, const QString & params = QString(), qx::any * ret = NULL) const \
  47. { Q_UNUSED(pOwner); Q_UNUSED(params); Q_UNUSED(ret); qAssert(false); return qx_bool(false, 0, QX_FUNCTION_ERR_INVALID_INVOKE_CALL); } \
  48. virtual qx_bool invoke(void * pOwner, const type_any_params & params, qx::any * ret = NULL) const \
  49. { Q_UNUSED(pOwner); Q_UNUSED(params); Q_UNUSED(ret); qAssert(false); return qx_bool(false, 0, QX_FUNCTION_ERR_INVALID_INVOKE_CALL); } \
  50. virtual qx_bool isValidFct() const \
  51. { return ((! m_fct) ? qx_bool(false, 0, QX_FUNCTION_ERR_EMPTY_FCT) : qx_bool(true)); }
  52. #define QX_FUNCTION_CLASS_MEMBER_FCT(className) \
  53. public: \
  54. type_fct m_fct; \
  55. className(type_fct fct) : IxFunction(), m_fct(fct) { ; }; \
  56. virtual ~className() { ; }; \
  57. virtual qx_bool invoke(void * pOwner, const QString & params = QString(), qx::any * ret = NULL) const \
  58. { return QxInvokerFct<QString, ! std::is_same<R, void>::value>::invoke(pOwner, params, ret, this); } \
  59. virtual qx_bool invoke(void * pOwner, const type_any_params & params, qx::any * ret = NULL) const \
  60. { return QxInvokerFct<type_any_params, ! std::is_same<R, void>::value>::invoke(pOwner, params, ret, this); } \
  61. virtual qx_bool invoke(const QString & params = QString(), qx::any * ret = NULL) const \
  62. { Q_UNUSED(params); Q_UNUSED(ret); qAssert(false); return qx_bool(false, 0, QX_FUNCTION_ERR_INVALID_INVOKE_CALL); } \
  63. virtual qx_bool invoke(const type_any_params & params, qx::any * ret = NULL) const \
  64. { Q_UNUSED(params); Q_UNUSED(ret); qAssert(false); return qx_bool(false, 0, QX_FUNCTION_ERR_INVALID_INVOKE_CALL); } \
  65. virtual qx_bool isValidFct() const \
  66. { return ((! m_fct) ? qx_bool(false, 0, QX_FUNCTION_ERR_EMPTY_MEMBER_FCT) : qx_bool(true)); }
  67. #define QX_FUNCTION_CATCH_AND_RETURN_INVOKE() \
  68. catch (const std::exception & e) { bValid = qx_bool(false, 0, e.what()); } \
  69. catch (...) { bValid = qx_bool(false, 0, QX_FUNCTION_ERR_UNKNOWN_ERROR); } \
  70. if (! bValid) { QString sMsgDebug = bValid.getDesc(); qDebug("[QxOrm] %s", qPrintable(sMsgDebug)); qAssert(false); } \
  71. return bValid;
  72. #define QX_FUNCTION_INVOKE_START_WITH_OWNER() \
  73. if (ret) { (* ret) = qx::any(); } \
  74. qx_bool bValid = pThis->isValid<T, Owner>(pOwner, params, NULL); \
  75. if (! bValid) { QString sMsgDebug = bValid.getDesc(); qDebug("[QxOrm] %s", qPrintable(sMsgDebug)); qAssert(false); return bValid; }
  76. #define QX_FUNCTION_INVOKE_START_WITHOUT_OWNER() \
  77. if (ret) { (* ret) = qx::any(); } \
  78. qx_bool bValid = pThis->isValid(params); \
  79. if (! bValid) { QString sMsgDebug = bValid.getDesc(); qDebug("[QxOrm] %s", qPrintable(sMsgDebug)); qAssert(false); return bValid; }
  80. #define QX_FUNCTION_FETCH_PARAM(TYPE, VALUE, FCT) \
  81. typename std::remove_const< TYPE >::type VALUE; \
  82. { qx_bool bTmp = qx::function::detail::FCT(params, VALUE, pThis); \
  83. if (! bTmp) { QString sMsgDebug = bTmp.getDesc(); qDebug("[QxOrm] %s", qPrintable(sMsgDebug)); qAssert(false); return bTmp; } }
  84. #define QX_FUNCTION_GET_PARAM_TYPE_ANY(PARAMCOUNT) \
  85. Q_UNUSED(qx_fct); \
  86. if (params.size() < PARAMCOUNT) { return qx_bool(false, 0, QX_FUNCTION_ERR_NUMBER_PARAMS); } \
  87. qx_bool bValid = true; \
  88. try { p = qx::any_cast<P>(params[PARAMCOUNT - 1]); } \
  89. catch (...) { bValid = qx_bool(false, 0, QString(QX_FUNCTION_ERR_INVALID_PARAM).replace("XXX", QString::number(PARAMCOUNT))); } \
  90. return bValid;
  91. #define QX_FUNCTION_GET_PARAM_TYPE_STRING(PARAMCOUNT) \
  92. if (! qx_fct) { return qx_bool(false, 0, QX_FUNCTION_ERR_UNKNOWN_ERROR); } \
  93. QStringList lst = params.split(qx_fct->getSeparator()); \
  94. if (lst.size() < PARAMCOUNT) { return qx_bool(false, 0, QX_FUNCTION_ERR_NUMBER_PARAMS); } \
  95. qx_bool bValid = true; \
  96. try { bValid = qx::cvt::from_string(lst.at(PARAMCOUNT - 1), p); } \
  97. catch (...) { bValid = qx_bool(false, 0, QString(QX_FUNCTION_ERR_INVALID_PARAM).replace("XXX", QString::number(PARAMCOUNT))); } \
  98. return bValid;
  99. #define QX_FUNCTION_GET_PARAM_TYPE_STRING_TO_QSTRING(PARAMCOUNT) \
  100. if (! qx_fct) { return qx_bool(false, 0, QX_FUNCTION_ERR_UNKNOWN_ERROR); } \
  101. QStringList lst = params.split(qx_fct->getSeparator()); \
  102. if (lst.size() < PARAMCOUNT) { return qx_bool(false, 0, QX_FUNCTION_ERR_NUMBER_PARAMS); } \
  103. p = lst.at(PARAMCOUNT - 1); \
  104. return true;
  105. #endif // _QX_FUNCTION_MACRO_H_