github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/3rd_party/fflib/include/ff/net/middleware/event_handler.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  #pragma once
    25  #include "ff/net/common/common.h"
    26  
    27  namespace ff {
    28  namespace net {
    29  class event_handler {
    30  public:
    31    template <class ETy_> void listen(const typename ETy_::Handler_t &h) {
    32      size_t v = ETy_::identifier;
    33      m_oHandlers[v] = boost::any(h);
    34    }
    35  
    36  #define RESTORE_HANDLER                                                        \
    37    size_t v = ETy_::identifier;                                                 \
    38    if (m_oHandlers.find(v) == m_oHandlers.end())                                \
    39      return;                                                                    \
    40    typename ETy_::Handler_t h =                                                 \
    41        boost::any_cast<typename ETy_::Handler_t>(m_oHandlers[v]);
    42  
    43    template <class ETy_, class T1> void triger(const T1 &t1) {
    44      RESTORE_HANDLER
    45      h(t1);
    46    }
    47  
    48    template <class ETy_, class T1, class T2>
    49    void triger(const T1 &t1, const T2 &t2) {
    50      RESTORE_HANDLER
    51      h(t1, t2);
    52    }
    53  
    54    template <class ETy_, class T1, class T2, class T3>
    55    void triger(const T1 &t1, const T2 &t2, const T3 &t3) {
    56      RESTORE_HANDLER
    57      h(t1, t2, t3);
    58    }
    59  #undef RESTORE_HANDLER
    60  
    61  protected:
    62    typedef std::map<size_t, boost::any> ETHandlers_t;
    63    ETHandlers_t m_oHandlers;
    64  };
    65  
    66  } // namespace net
    67  } // namespace ff
    68