singleton.hpp 915 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm

#ifndef SINGLETON_HPP
#define SINGLETON_HPP

#include <cstddef>

template <typename T>

/**
 * @brief Singleton template class.
 */
class Singleton {
 protected:
  Singleton() {}
  ~Singleton() {}

 public:
  /**
   * @brief Instantiates the static instance if it hasn't been instantiated.
   * @return Static instance.
   */
  static T *getInstance() {
    if (nullptr == _singleton) {
      _singleton = new T;
    }
    return (static_cast<T *>(_singleton));
  }

  static T &the() { return *getInstance(); }

  /**
   * @brief Destroys the static instance and sets it to nullptr.
   */
  static void destroy() {
    if (nullptr != _singleton) {
      delete _singleton;
      _singleton = nullptr;
    }
  }

 protected:
  static T *_singleton;
};

template <typename T>
T *Singleton<T>::_singleton = nullptr;

#endif  // SINGLETON_HPP