github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/3rd_party/fflib/include/ff/functionflow/para/para_helper.h (about)

     1  /***********************************************
     2  The MIT License (MIT)
     3  
     4  Copyright (c) 2012 Athrun Arthur <athrunarthur@gmail.com>
     5  
     6  Permission is hereby granted, free of charge, to any person obtaining a copy
     7  of this software and associated documentation files (the "Software"), to deal
     8  in the Software without restriction, including without limitation the rights
     9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10  copies of the Software, and to permit persons to whom the Software is
    11  furnished to do so, subject to the following conditions:
    12  
    13  The above copyright notice and this permission notice shall be included in
    14  all copies or substantial portions of the Software.
    15  
    16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    22  THE SOFTWARE.
    23  *************************************************/
    24  #ifndef FF_PARA_PARA_HELPER_H_
    25  #define FF_PARA_PARA_HELPER_H_
    26  #include "ff/functionflow/common/common.h"
    27  #include "ff/functionflow/para/para_impl.h"
    28  #include "ff/functionflow/runtime/rtcmn.h"
    29  #include "ff/functionflow/runtime/runtime.h"
    30  #include "ff/util/function_traits.h"
    31  
    32  namespace ff {
    33  template <class RT>
    34  class para;
    35  
    36  namespace internal {
    37  using namespace ff::util;
    38  template <class RT>
    39  class para_impl;
    40  
    41  template <class PT, class RT>
    42  class para_accepted_call {
    43   public:
    44    para_accepted_call(PT& p) : m_refP(p) {}
    45  
    46    template <class FT>
    47    auto then(FT&& f) -> typename std::enable_if<
    48        std::is_void<typename function_res_traits<FT>::ret_type>::value &&
    49            is_function_with_arg_type<FT, RT>::value,
    50        void>::type {
    51      if (!m_refP.check_if_over())
    52        ::ff::rt::yield_and_ret_until(
    53            [this]() { return m_refP.check_if_over(); });
    54      f(m_refP.get());
    55    }
    56  
    57    template <class FT>
    58    auto then(FT&& f) -> typename std::enable_if<
    59        is_function_with_arg_type<FT, RT>::value,
    60        typename std::remove_reference<
    61            typename function_res_traits<FT>::ret_type>::type&&>::type {
    62      if (!m_refP.check_if_over())
    63        ::ff::rt::yield_and_ret_until(
    64            [this]() { return m_refP.check_if_over(); });
    65      return std::move(f(m_refP.get()));
    66    }
    67  
    68    template <class FT>
    69    auto then(FT&& f) ->
    70        typename std::enable_if<is_callable<FT>::value &&
    71                                    !is_function_with_arg_type<FT, RT>::value,
    72                                void>::type {
    73      static_assert(Please_Check_The_Assert_Msg<FT>::value,
    74                    FF_EM_THEN_WITH_TYPE_MISMATCH);
    75    }
    76  
    77    template <class FT>
    78    auto then(FT&& f) ->
    79        typename std::enable_if<!is_callable<FT>::value, void>::type {
    80      static_assert(Please_Check_The_Assert_Msg<FT>::value,
    81                    FF_EM_THEN_WITH_NON_FUNC_TYPE);
    82    }
    83  
    84    template <class T>
    85    void operator[](T&& t) {
    86      static_assert(Please_Check_The_Assert_Msg<T>::value,
    87                    FF_EM_CALL_SQPAREN_AFTER_PAREN);
    88    }
    89  
    90    template <class T>
    91    void operator()(T&& t) {
    92      static_assert(Please_Check_The_Assert_Msg<T>::value,
    93                    FF_EM_CALL_PAREN_AFTER_PAREN);
    94    }
    95  
    96   protected:
    97    PT& m_refP;
    98  };  // end class para_accepted_call
    99  
   100  template <class PT>
   101  class para_accepted_call<PT, void> {
   102   public:
   103    para_accepted_call(PT& p) : m_refP(p) {}
   104  
   105    template <class FT>
   106    auto then(FT&& f) -> typename std::enable_if<
   107        std::is_void<typename function_res_traits<FT>::ret_type>::value &&
   108            is_function_with_arg_type<FT, void>::value,
   109        void>::type {
   110      if (!m_refP.check_if_over())
   111        ::ff::rt::yield_and_ret_until(
   112            [this]() { return m_refP.check_if_over(); });
   113      f();
   114    }
   115  
   116    template <class FT>
   117    auto then(FT&& f) -> typename std::enable_if<
   118        is_function_with_arg_type<FT, void>::value &&
   119            !std::is_void<typename function_res_traits<FT>::ret_type>::value,
   120        typename std::remove_reference<
   121            typename function_res_traits<FT>::ret_type>::type>::type {
   122      if (!m_refP.check_if_over())
   123        ::ff::rt::yield_and_ret_until(
   124            [this]() { return m_refP.check_if_over(); });
   125      return f();
   126    }
   127  
   128    template <class FT>
   129    auto then(FT&& f) ->
   130        typename std::enable_if<is_callable<FT>::value &&
   131                                    !is_function_with_arg_type<FT, void>::value,
   132                                void>::type {
   133      static_assert(Please_Check_The_Assert_Msg<FT>::value,
   134                    FF_EM_THEN_WITH_TYPE_MISMATCH);
   135    }
   136  
   137    template <class FT>
   138    auto then(FT&& f) ->
   139        typename std::enable_if<!is_callable<FT>::value, void>::type {
   140      static_assert(Please_Check_The_Assert_Msg<FT>::value,
   141                    FF_EM_THEN_WITH_NON_FUNC_TYPE);
   142    }
   143  
   144    template <class T>
   145    void operator[](T&& t) {
   146      static_assert(Please_Check_The_Assert_Msg<T>::value,
   147                    FF_EM_CALL_SQPAREN_AFTER_PAREN);
   148    }
   149  
   150    template <class T>
   151    void operator()(T&& t) {
   152      static_assert(Please_Check_The_Assert_Msg<T>::value,
   153                    FF_EM_CALL_PAREN_AFTER_PAREN);
   154    }
   155  
   156   protected:
   157    PT& m_refP;
   158  };  // end class para_accepted_call
   159  
   160  }  // end namespace internal;
   161  }  // end namespace ff
   162  #endif