github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/common/exception_queue.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  #pragma once
    21  #include "common/common.h"
    22  #include "util/singleton.h"
    23  #include <algorithm>
    24  #include <condition_variable>
    25  #include <exception>
    26  #include <thread>
    27  
    28  namespace neb {
    29  
    30  class neb_exception {
    31  public:
    32    enum neb_exception_type {
    33      neb_std_exception,
    34      neb_shm_queue_failure,
    35      neb_shm_service_failure,
    36      neb_shm_session_already_start,
    37      neb_shm_session_timeout,
    38      neb_shm_session_failure,
    39      neb_configure_general_failure,
    40      neb_json_general_failure,
    41      neb_storage_exception_no_such_key,
    42      neb_storage_exception_no_init,
    43      neb_storage_general_failure,
    44    };
    45  
    46    inline neb_exception(neb_exception_type type, const std::string &msg)
    47        : m_msg(msg), m_type(type) {}
    48    inline const char *what() const throw() { return m_msg.c_str(); }
    49  
    50    neb_exception_type type() const { return m_type; }
    51  
    52  protected:
    53    std::string m_msg;
    54    neb_exception_type m_type;
    55  };
    56  
    57  typedef std::shared_ptr<neb_exception> neb_exception_ptr;
    58  
    59  class exception_queue : public neb::util::singleton<exception_queue> {
    60  public:
    61    void push_back(neb_exception::neb_exception_type type, const char *what);
    62  
    63    void push_back(const std::exception &p);
    64  
    65    inline bool empty() const {
    66      std::lock_guard<std::mutex> _l(m_mutex);
    67      return m_exceptions.empty();
    68    }
    69    inline size_t size() const {
    70      std::lock_guard<std::mutex> _l(m_mutex);
    71      return m_exceptions.size();
    72    }
    73  
    74    neb_exception_ptr pop_front();
    75  
    76    inline void for_each(const std::function<void(neb_exception_ptr p)> &func) {
    77      std::lock_guard<std::mutex> _l(m_mutex);
    78      std::for_each(m_exceptions.begin(), m_exceptions.end(), func);
    79    }
    80  
    81    static void catch_exception(const std::function<void()> &func);
    82  
    83  protected:
    84    std::vector<neb_exception_ptr> m_exceptions;
    85    mutable std::mutex m_mutex;
    86    std::condition_variable m_cond_var;
    87  };
    88  }
    89