#pragma once #include "Iterator.Base.tlh" #if 1 //----------------------------------------------------------------------------- // function_traits //----------------------------------------------------------------------------- namespace Iterator { template struct function_traits; #if 1 template struct function_traits { public: enum { arity = sizeof... (Args) }; typedef Ret function_type (Args...); typedef Ret return_type; using stl_function_type = std::function ; }; #endif #if 1 // 函数指针. template struct function_traits : function_traits {}; // std::function. template struct function_traits> : function_traits {}; #endif #if 0 template struct function_traits { typedef Ret function_type (Args...); typedef Ret return_type; using stl_function_type = std::function ; }; #endif #if 1 // member function. #define FUNCTION_TRAITS(...)\ template \ struct function_traits : function_traits{};\ FUNCTION_TRAITS() FUNCTION_TRAITS(const) FUNCTION_TRAITS(volatile) FUNCTION_TRAITS(const volatile) #endif #if 1 // 函数对象 / lambda template struct function_traits : function_traits { }; #endif #if 0 // 函数对象 / lambda template struct function_traits : function_traits { }; #endif template typename function_traits ::stl_function_type ToFunction (Function & lambda) { return static_cast ::stl_function_type> (lambda); } } #endif //----------------------------------------------------------------------------- // Select_Iterator // // data_type 的获取, 只能通过使用者给出, 然后传递到这里 // 这里用 function_traits 无法获得类型, 原因不明 //----------------------------------------------------------------------------- namespace Iterator { template class Select_Iterator : public __CoreIterator { typedef __CoreIterator inherited; public: // 以下语法都不对 // using data_type = decltype (Map::operator () (typename Iterator::data_type)); // using data_type = typename std::result_of::type; // using data_type = function_traits ::return_type; using value_type = RetType; using data_type = RetType; using deref_type = RetType; using IterType = typename ForIterator::IterType; public: Select_Iterator () = delete; Select_Iterator (Map & f, ForIterator & Iter) : m_Iter (Iter), inherited (Iter) { m_Map = ToFunction (f); } public: inline virtual void Next () override { if (! IsEmpty ()) { m_Iter.Next (); inherited::Next (); } } inline data_type Current () { return (m_Map (m_Iter.Current ())); } inline data_type operator () () { return Current (); } inline data_type operator * () { return Current (); } inline virtual bool IsEmpty () const override { if (inherited::IsEmpty ()) return true; return m_Iter.IsEmpty (); } inline void Skip (int delta) { for (;! IsEmpty () && delta > 0; delta --) Next (); } public: inline IterType stdBegin () { return m_Iter.stdBegin (); } inline IterType stdCurrent () { return m_Iter.stdCurrent (); } inline IterType stdEnd () { return m_Iter.stdEnd (); } protected: typename function_traits ::stl_function_type m_Map; ForIterator m_Iter; }; };