123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- #pragma once
- #include "Iterator.Base.tlh"
- #if 1
- //-----------------------------------------------------------------------------
- // function_traits
- //-----------------------------------------------------------------------------
- namespace Iterator
- {
- template <typename T>
- struct function_traits;
- #if 1
- template <typename Ret, typename... Args>
- struct function_traits <Ret (Args...)>
- {
- public:
- enum { arity = sizeof... (Args) };
- typedef Ret function_type (Args...);
- typedef Ret return_type;
- using stl_function_type = std::function <function_type>;
- };
- #endif
- #if 1
- // 函数指针.
- template<typename Ret, typename... Args>
- struct function_traits<Ret (*)(Args...)> : function_traits<Ret (Args...)> {};
- // std::function.
- template <typename Ret, typename... Args>
- struct function_traits<std::function<Ret (Args...)>> : function_traits<Ret (Args...)> {};
- #endif
- #if 0
- template <typename Class, typename Ret, typename... Args>
- struct function_traits <Ret (Class::*) (Args...) const>
- {
- typedef Ret function_type (Args...);
- typedef Ret return_type;
- using stl_function_type = std::function <function_type>;
- };
- #endif
- #if 1
- // member function.
- #define FUNCTION_TRAITS(...)\
- template <typename ReturnType, typename ClassType, typename... Args>\
- struct function_traits<ReturnType(ClassType::*)(Args...) __VA_ARGS__> : function_traits<ReturnType(Args...)>{};\
- FUNCTION_TRAITS()
- FUNCTION_TRAITS(const)
- FUNCTION_TRAITS(volatile)
- FUNCTION_TRAITS(const volatile)
- #endif
- #if 1
- // 函数对象 / lambda
- template <typename Callable>
- struct function_traits : function_traits <decltype (&Callable::operator ())>
- {
- };
- #endif
- #if 0
- // 函数对象 / lambda
- template <typename Callable, typename... Args>
- struct function_traits : function_traits <decltype (&Callable::operator (Args...))>
- {
- };
- #endif
- template <typename Function>
- typename function_traits <Function>::stl_function_type ToFunction (Function & lambda)
- {
- return static_cast <typename function_traits <Function>::stl_function_type> (lambda);
- }
- }
- #endif
- //-----------------------------------------------------------------------------
- // Select_Iterator
- //
- // data_type 的获取, 只能通过使用者给出, 然后传递到这里
- // 这里用 function_traits 无法获得类型, 原因不明
- //-----------------------------------------------------------------------------
- namespace Iterator
- {
- template <typename Map, typename ForIterator, typename RetType>
- 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<decltype(Map)()>::type;
- // using data_type = function_traits <Map>::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 <Map>::stl_function_type m_Map;
- ForIterator m_Iter;
- };
- };
|