github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/test/common/ipc/ipc_instance.h (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // the go-nebulas library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with the go-nebulas library.  If not, see
    18  // <http://www.gnu.org/licenses/>.
    19  //
    20  
    21  #pragma once
    22  #include "common/common.h"
    23  #include "common/util/singleton.h"
    24  #include <unordered_set>
    25  
    26  namespace neb {
    27  class ipc_instance_base {
    28  public:
    29    virtual std::string get_fixture_name() = 0;
    30    virtual std::string get_instance_name() = 0;
    31    virtual void run() const = 0;
    32  }; // end class ipc_instance_base
    33  
    34  typedef std::shared_ptr<ipc_instance_base> ipc_instance_base_ptr;
    35  
    36  class ipc_instances : public util::singleton<ipc_instances> {
    37  public:
    38    void init_ipc_instances(int argc, char *argv[]);
    39  
    40    virtual ~ipc_instances();
    41  
    42    int run_all_ipc_instances();
    43  
    44    int run_one_ipc_instance(const std::string &fixture,
    45                             const std::string &instance);
    46  
    47    size_t register_ipc_instance(const ipc_instance_base_ptr &b);
    48  
    49  protected:
    50    void show_all_instances();
    51  
    52  protected:
    53    std::vector<ipc_instance_base_ptr> m_all_instances;
    54    std::string m_enabled_instance_name;
    55    std::string m_enabled_fixture_name;
    56    std::string m_exe_name;
    57    bool m_debug_mode;
    58  }; // end class ipc_instances
    59  } // namespace neb
    60  
    61  #define GEN_NAME_VAR(name) _##name##_nouse
    62  #define CLASS_NAME(f, n) _class_##f##_##n##_
    63  #define TMP_VAR_NAME(f, n) _tmp_var_##f##_##n##_
    64  
    65  #define _IPC_INSTANCE(fixture, name)                                           \
    66    class CLASS_NAME(fixture, name) : public neb::ipc_instance_base {            \
    67    public:                                                                      \
    68      virtual std::string get_fixture_name() { return #fixture; }                \
    69      virtual std::string get_instance_name() { return #name; }                  \
    70      virtual void run() const;                                                  \
    71    };                                                                           \
    72    static int TMP_VAR_NAME(fixture, name) =                                     \
    73        neb::ipc_instances::instance().register_ipc_instance(                    \
    74            std::static_pointer_cast<neb::ipc_instance_base>(                    \
    75                std::make_shared<CLASS_NAME(fixture, name)>()));                 \
    76    void CLASS_NAME(fixture, name)::run() const
    77  
    78  #define IPC_PRELUDE(fixture) _IPC_INSTANCE(fixture, prelude)
    79  
    80  #define IPC_SERVER(fixture) _IPC_INSTANCE(fixture, server)
    81  
    82  #define IPC_CLIENT(fixture) _IPC_INSTANCE(fixture, client)