ctkCmdLineModuleBackendFPUtil_p.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <QVariant>
  18. class ctkCmdLineModuleBackendFunctionPointer;
  19. namespace ctk {
  20. namespace CmdLineModuleBackendFunctionPointer {
  21. struct FunctionPointerHolderBase
  22. {
  23. virtual ~FunctionPointerHolderBase();
  24. virtual FunctionPointerHolderBase* clone() const = 0;
  25. virtual void call(const QList<QVariant>& args) = 0;
  26. };
  27. template<typename A>
  28. struct FunctionPointerHolder : public FunctionPointerHolderBase
  29. {
  30. typedef void (*FunctionPointerType)(A);
  31. FunctionPointerHolder(FunctionPointerType fp) : Fp(fp) {}
  32. FunctionPointerHolderBase* clone() const
  33. {
  34. return new FunctionPointerHolder(*this);
  35. }
  36. void call(const QList<QVariant>& args)
  37. {
  38. Q_ASSERT(args.size() > 0);
  39. Q_ASSERT(args.at(0).canConvert<A>());
  40. Fp(args.at(0).value<A>());
  41. }
  42. FunctionPointerType Fp;
  43. };
  44. template<typename A, typename B>
  45. struct FunctionPointerHolder2 : public FunctionPointerHolderBase
  46. {
  47. typedef void (*FunctionPointerType)(A,B);
  48. FunctionPointerHolder2(FunctionPointerType fp) : Fp(fp) {}
  49. FunctionPointerHolderBase* clone() const
  50. {
  51. return new FunctionPointerHolder2(*this);
  52. }
  53. void call(const QList<QVariant>& args)
  54. {
  55. Q_ASSERT(args.size() > 1);
  56. Q_ASSERT(args.at(0).canConvert<A>());
  57. Q_ASSERT(args.at(1).canConvert<B>());
  58. Fp(args.at(0).value<A>(), args.at(1).value<B>());
  59. }
  60. FunctionPointerType Fp;
  61. };
  62. struct FunctionPointerProxy
  63. {
  64. FunctionPointerProxy();
  65. ~FunctionPointerProxy();
  66. FunctionPointerProxy(const FunctionPointerProxy& other);
  67. FunctionPointerProxy& operator=(const FunctionPointerProxy& other);
  68. template<typename A>
  69. FunctionPointerProxy(void (*fp)(A))
  70. : FpHolder(new FunctionPointerHolder<A>(fp)) {}
  71. template<typename A, typename B>
  72. FunctionPointerProxy(void (*fp)(A,B))
  73. : FpHolder(new FunctionPointerHolder2<A,B>(fp)) {}
  74. void call(const QList<QVariant>& args);
  75. private:
  76. friend class ::ctkCmdLineModuleBackendFunctionPointer;
  77. FunctionPointerHolderBase* FpHolder;
  78. };
  79. }
  80. }
  81. #endif // CTKCMDLINEMODULEBACKENDFPUTIL_P_H