IxFunction.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 _IX_FUNCTION_H_
  32. #define _IX_FUNCTION_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file IxFunction.h
  38. * \author Lionel Marty
  39. * \ingroup QxFunction
  40. * \brief Common interface for all functions registered into QxOrm context (used by introspection engine)
  41. */
  42. #include <QxCommon/QxAny.h>
  43. #include <QxCommon/QxBool.h>
  44. #include <QxCommon/QxPropertyBag.h>
  45. #include <QxCollection/QxCollection.h>
  46. #include <QxFunction/QxFunctionError.h>
  47. #include <QxFunction/QxFunctionMacro.h>
  48. #include <QxTraits/remove_attr.h>
  49. namespace qx {
  50. /*!
  51. * \ingroup QxFunction
  52. * \brief qx::IxFunction : common interface for all functions registered into QxOrm context (used by introspection engine)
  53. */
  54. class IxFunction : public qx::QxPropertyBag
  55. {
  56. protected:
  57. QString m_sKey; //!< Function key
  58. QString m_sSeparator; //!< Separator character(s) for 'QString' parameters type
  59. QString m_sDescription; //!< Function description
  60. public:
  61. typedef std::vector<qx::any> type_any_params;
  62. IxFunction() : qx::QxPropertyBag(), m_sSeparator("|") { ; }
  63. virtual ~IxFunction() { ; }
  64. QString getKey() const { return m_sKey; }
  65. QString getSeparator() const { return m_sSeparator; }
  66. QString getDescription() const { return m_sDescription; }
  67. void setKey(const QString & s) { m_sKey = s; }
  68. void setSeparator(const QString & s) { m_sSeparator = s; }
  69. void setDescription(const QString & s) { m_sDescription = s; }
  70. virtual int getParamCount() const = 0;
  71. virtual qx_bool invoke(const QString & params = QString(), qx::any * ret = NULL) const = 0;
  72. virtual qx_bool invoke(const type_any_params & params, qx::any * ret = NULL) const = 0;
  73. virtual qx_bool invoke(void * pOwner, const QString & params = QString(), qx::any * ret = NULL) const = 0;
  74. virtual qx_bool invoke(void * pOwner, const type_any_params & params, qx::any * ret = NULL) const = 0;
  75. virtual qx_bool isValidFct() const = 0;
  76. virtual qx_bool isValidParams(const QString & params) const = 0;
  77. virtual qx_bool isValidParams(const type_any_params & params) const = 0;
  78. template <class T>
  79. qx_bool isValidOwner(void * pOwner, T * dummy) const
  80. {
  81. Q_UNUSED(dummy);
  82. typedef std::is_same<T, void> qx_verify_owner_tmp;
  83. static_assert(! qx_verify_owner_tmp::value, "! qx_verify_owner_tmp::value");
  84. if (! pOwner) { return qx_bool(false, 0, QX_FUNCTION_ERR_NULL_OWNER); }
  85. #ifndef _QX_NO_RTTI
  86. if (! dynamic_cast<T *>(static_cast<T *>(pOwner))) { return qx_bool(false, 0, QX_FUNCTION_ERR_INVALID_OWNER); }
  87. #endif // _QX_NO_RTTI
  88. return true;
  89. }
  90. template <class T>
  91. qx_bool isValid(const T & params) const
  92. {
  93. qx_bool bValid = isValidFct(); if (! bValid) { return bValid; };
  94. bValid = isValidParams(params); if (! bValid) { return bValid; };
  95. return true;
  96. }
  97. template <class T, class U>
  98. qx_bool isValid(void * pOwner, const T & params, U * dummy) const
  99. {
  100. Q_UNUSED(dummy);
  101. qx_bool bValid = isValidFct(); if (! bValid) { return bValid; };
  102. bValid = isValidParams(params); if (! bValid) { return bValid; };
  103. bValid = isValidOwner<U>(pOwner, NULL); if (! bValid) { return bValid; };
  104. return true;
  105. }
  106. };
  107. typedef std::shared_ptr<IxFunction> IxFunction_ptr;
  108. typedef QxCollection<QString, IxFunction_ptr> IxFunctionX;
  109. typedef std::shared_ptr<IxFunctionX> IxFunctionX_ptr;
  110. } // namespace qx
  111. #endif // _IX_FUNCTION_H_