Iterator.Select.tlh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #pragma once
  2. #include "Iterator.Base.tlh"
  3. #if 1
  4. //-----------------------------------------------------------------------------
  5. // function_traits
  6. //-----------------------------------------------------------------------------
  7. namespace Iterator
  8. {
  9. template <typename T>
  10. struct function_traits;
  11. #if 1
  12. template <typename Ret, typename... Args>
  13. struct function_traits <Ret (Args...)>
  14. {
  15. public:
  16. enum { arity = sizeof... (Args) };
  17. typedef Ret function_type (Args...);
  18. typedef Ret return_type;
  19. using stl_function_type = std::function <function_type>;
  20. };
  21. #endif
  22. #if 1
  23. // 函数指针.
  24. template<typename Ret, typename... Args>
  25. struct function_traits<Ret (*)(Args...)> : function_traits<Ret (Args...)> {};
  26. // std::function.
  27. template <typename Ret, typename... Args>
  28. struct function_traits<std::function<Ret (Args...)>> : function_traits<Ret (Args...)> {};
  29. #endif
  30. #if 0
  31. template <typename Class, typename Ret, typename... Args>
  32. struct function_traits <Ret (Class::*) (Args...) const>
  33. {
  34. typedef Ret function_type (Args...);
  35. typedef Ret return_type;
  36. using stl_function_type = std::function <function_type>;
  37. };
  38. #endif
  39. #if 1
  40. // member function.
  41. #define FUNCTION_TRAITS(...)\
  42. template <typename ReturnType, typename ClassType, typename... Args>\
  43. struct function_traits<ReturnType(ClassType::*)(Args...) __VA_ARGS__> : function_traits<ReturnType(Args...)>{};\
  44. FUNCTION_TRAITS()
  45. FUNCTION_TRAITS(const)
  46. FUNCTION_TRAITS(volatile)
  47. FUNCTION_TRAITS(const volatile)
  48. #endif
  49. #if 1
  50. // 函数对象 / lambda
  51. template <typename Callable>
  52. struct function_traits : function_traits <decltype (&Callable::operator ())>
  53. {
  54. };
  55. #endif
  56. #if 0
  57. // 函数对象 / lambda
  58. template <typename Callable, typename... Args>
  59. struct function_traits : function_traits <decltype (&Callable::operator (Args...))>
  60. {
  61. };
  62. #endif
  63. template <typename Function>
  64. typename function_traits <Function>::stl_function_type ToFunction (Function & lambda)
  65. {
  66. return static_cast <typename function_traits <Function>::stl_function_type> (lambda);
  67. }
  68. }
  69. #endif
  70. //-----------------------------------------------------------------------------
  71. // Select_Iterator
  72. //
  73. // data_type 的获取, 只能通过使用者给出, 然后传递到这里
  74. // 这里用 function_traits 无法获得类型, 原因不明
  75. //-----------------------------------------------------------------------------
  76. namespace Iterator
  77. {
  78. template <typename Map, typename ForIterator, typename RetType>
  79. class Select_Iterator : public __CoreIterator
  80. {
  81. typedef __CoreIterator inherited;
  82. public:
  83. // 以下语法都不对
  84. // using data_type = decltype (Map::operator () (typename Iterator::data_type));
  85. // using data_type = typename std::result_of<decltype(Map)()>::type;
  86. // using data_type = function_traits <Map>::return_type;
  87. using value_type = RetType;
  88. using data_type = RetType;
  89. using deref_type = RetType;
  90. using IterType = typename ForIterator::IterType;
  91. public:
  92. Select_Iterator () = delete;
  93. Select_Iterator (Map & f, ForIterator & Iter) :
  94. m_Iter (Iter),
  95. inherited (Iter)
  96. {
  97. m_Map = ToFunction (f);
  98. }
  99. public:
  100. inline virtual void Next () override
  101. {
  102. if (! IsEmpty ())
  103. {
  104. m_Iter.Next ();
  105. inherited::Next ();
  106. }
  107. }
  108. inline data_type Current () { return (m_Map (m_Iter.Current ())); }
  109. inline data_type operator () () { return Current (); }
  110. inline data_type operator * () { return Current (); }
  111. inline virtual bool IsEmpty () const override
  112. {
  113. if (inherited::IsEmpty ())
  114. return true;
  115. return m_Iter.IsEmpty ();
  116. }
  117. inline void Skip (int delta)
  118. {
  119. for (;! IsEmpty () && delta > 0; delta --)
  120. Next ();
  121. }
  122. public:
  123. inline IterType stdBegin () { return m_Iter.stdBegin (); }
  124. inline IterType stdCurrent () { return m_Iter.stdCurrent (); }
  125. inline IterType stdEnd () { return m_Iter.stdEnd (); }
  126. protected:
  127. typename function_traits <Map>::stl_function_type m_Map;
  128. ForIterator m_Iter;
  129. };
  130. };