DeSiGNAR  0.5a
Data Structures General Library
singleton.H
Go to the documentation of this file.
1 /*
2  This file is part of Designar.
3  Copyright (C) 2017 by Alejandro J. Mujica
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Any user request of this software, write to
19 
20  Alejandro Mujica
21 
22  aledrums@gmail.com
23 */
24 
25 # ifndef DSGSINGLETON_H
26 # define DSGSINGLETON_H
27 
28 # include <memory>
29 
30 namespace Designar
31 {
32 
61  template <typename T>
62  class Singleton
63  {
64  static std::unique_ptr<Singleton<T>> instance;
65 
66  protected:
68  {
69  // Empty
70  }
71 
72  Singleton(const Singleton<T> &) = delete;
73 
74  Singleton & operator = (const Singleton<T> &) = delete;
75 
76  public:
77 
78  virtual ~Singleton()
79  {
80  // Empty
81  }
82 
87  static T * get_ptr_instance()
88  {
89  if (instance.get() == nullptr)
90  instance = std::unique_ptr<Singleton<T>>(new T());
91 
92  return static_cast<T *>(instance.get());
93  }
94 
99  static T & get_instance()
100  {
101  return *get_ptr_instance();
102  }
103  };
104 
105  template <typename T>
106  std::unique_ptr<Singleton<T>> Singleton<T>::instance =
107  std::unique_ptr<Singleton<T>>(nullptr);
108 
109 } // end namespace Designar
110 
111 # endif // DSGSINGLETON_H
112 
Definition: singleton.H:62
static T & get_instance()
Definition: singleton.H:99
static T * get_ptr_instance()
Definition: singleton.H:87
Singleton & operator=(const Singleton< T > &)=delete
Definition: array.H:32
Singleton()
Definition: singleton.H:67
virtual ~Singleton()
Definition: singleton.H:78