github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/util/singleton.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 <functional>
    24  #include <boost/noncopyable.hpp>
    25  
    26  namespace neb {
    27  namespace util {
    28  
    29  template <typename T> class singleton : boost::noncopyable {
    30  public:
    31    static T &instance() {
    32      std::call_once(s_init_once, std::bind(singleton<T>::init));
    33      if (!s_pInstance) {
    34        throw std::runtime_error("already deallocated!");
    35      }
    36      return *s_pInstance;
    37    }
    38  
    39    void release() {
    40      std::call_once(s_dealloc_once, std::bind(singleton<T>::dealloc));
    41    }
    42  
    43  protected:
    44    singleton() = default;
    45  
    46  private:
    47    static void init() { s_pInstance = std::shared_ptr<T>(new T()); }
    48    static void dealloc() { s_pInstance.reset(); }
    49  
    50  protected:
    51    static std::shared_ptr<T> s_pInstance;
    52    static std::once_flag s_init_once;
    53    static std::once_flag s_dealloc_once;
    54  };
    55  template <typename T> std::shared_ptr<T> singleton<T>::s_pInstance;
    56  template <typename T> std::once_flag singleton<T>::s_init_once;
    57  template <typename T> std::once_flag singleton<T>::s_dealloc_once;
    58  
    59  template <typename T> class singleton_guard : boost::noncopyable {
    60  public:
    61    singleton_guard() = default;
    62    ~singleton_guard() { singleton<T>::instance().release(); }
    63  }; // end class singleton_guard
    64  }
    65  }