ctkCmdLineModuleBackendFPUtil_p.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #ifndef CTKCMDLINEMODULEBACKENDFPUTIL_P_H
  16. #define CTKCMDLINEMODULEBACKENDFPUTIL_P_H
  17. #include "ctkCommandLineModulesBackendFunctionPointerExport.h"
  18. #include <QVariant>
  19. class ctkCmdLineModuleBackendFunctionPointer;
  20. namespace ctk {
  21. namespace CmdLineModuleBackendFunctionPointer {
  22. struct CTK_CMDLINEMODULEBACKENDFP_EXPORT FunctionPointerHolderBase
  23. {
  24. virtual ~FunctionPointerHolderBase();
  25. virtual FunctionPointerHolderBase* clone() const = 0;
  26. virtual void call(const QList<QVariant>& args) = 0;
  27. };
  28. template<typename A>
  29. struct FunctionPointerHolder : public FunctionPointerHolderBase
  30. {
  31. typedef void (*FunctionPointerType)(A);
  32. FunctionPointerHolder(FunctionPointerType fp) : Fp(fp) {}
  33. FunctionPointerHolderBase* clone() const
  34. {
  35. return new FunctionPointerHolder(*this);
  36. }
  37. void call(const QList<QVariant>& args)
  38. {
  39. Q_ASSERT(args.size() > 0);
  40. Q_ASSERT(args.at(0).canConvert<A>());
  41. Fp(args.at(0).value<A>());
  42. }
  43. FunctionPointerType Fp;
  44. };
  45. template<typename A, typename B>
  46. struct FunctionPointerHolder2 : public FunctionPointerHolderBase
  47. {
  48. typedef void (*FunctionPointerType)(A,B);
  49. FunctionPointerHolder2(FunctionPointerType fp) : Fp(fp) {}
  50. FunctionPointerHolderBase* clone() const
  51. {
  52. return new FunctionPointerHolder2(*this);
  53. }
  54. void call(const QList<QVariant>& args)
  55. {
  56. Q_ASSERT(args.size() > 1);
  57. Q_ASSERT(args.at(0).canConvert<A>());
  58. Q_ASSERT(args.at(1).canConvert<B>());
  59. Fp(args.at(0).value<A>(), args.at(1).value<B>());
  60. }
  61. FunctionPointerType Fp;
  62. };
  63. struct CTK_CMDLINEMODULEBACKENDFP_EXPORT FunctionPointerProxy
  64. {
  65. FunctionPointerProxy();
  66. ~FunctionPointerProxy();
  67. FunctionPointerProxy(const FunctionPointerProxy& other);
  68. FunctionPointerProxy& operator=(const FunctionPointerProxy& other);
  69. template<typename A>
  70. FunctionPointerProxy(void (*fp)(A))
  71. : FpHolder(new FunctionPointerHolder<A>(fp)) {}
  72. template<typename A, typename B>
  73. FunctionPointerProxy(void (*fp)(A,B))
  74. : FpHolder(new FunctionPointerHolder2<A,B>(fp)) {}
  75. void call(const QList<QVariant>& args);
  76. private:
  77. friend class ::ctkCmdLineModuleBackendFunctionPointer;
  78. FunctionPointerHolderBase* FpHolder;
  79. };
  80. }
  81. }
  82. #endif // CTKCMDLINEMODULEBACKENDFPUTIL_P_H