i6engine  1.0
Singleton.h
Go to the documentation of this file.
1 /*
2  * i6engine
3  * Copyright (2016) Daniel Bonrath, Michael Baer, All rights reserved.
4  *
5  * This file is part of i6engine; i6engine is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
25 #ifndef __I6ENGINE_UTILS_SINGLETON_H__
26 #define __I6ENGINE_UTILS_SINGLETON_H__
27 
28 #include "boost/interprocess/sync/scoped_lock.hpp"
29 #include "boost/make_shared.hpp"
30 #include "boost/thread/mutex.hpp"
31 
32 namespace i6e {
33 namespace utils {
34 
43  template<typename T, typename... Dependencies>
44  class Singleton {
45  public:
49  static inline T * GetSingletonPtr() {
50  return Init().get();
51  }
52 
56  static inline T & GetSingleton() {
57  return *Init();
58  }
59 
60  protected:
62  }
63 
64  virtual ~Singleton() {
65  }
66 
67  private:
71  template<typename... B>
72  struct InitDependencies {
73  InitDependencies() {
74  }
75  static void Init() {
76  }
77  };
78 
82  template<typename A, typename... B>
83  struct InitDependencies<A, B...> {
84  InitDependencies() {
85  Init();
86  }
87  ~InitDependencies() {
88  }
89  static void Init() {
90  A::GetSingleton();
91  InitDependencies<B...>::Init();
92  }
93  };
94 
98  static boost::shared_ptr<T> & Init() {
99  static const InitDependencies<Dependencies...> id;
100  static boost::shared_ptr<T> _ptrSingleton = boost::shared_ptr<T>(new T());
101  return _ptrSingleton;
102  }
103 
104  Singleton(const Singleton &) = delete;
105 
106  const Singleton & operator=(const Singleton &) = delete;
107  };
108 
109 } /* namespace utils */
110 } /* namespace i6e */
111 
112 #endif /* __I6ENGINE_UTILS_SINGLETON_H__ */
113 
Derive from this templated class to make a class a singleton. Refer to the Singleton Design Pattern i...
Definition: Singleton.h:44
static T * GetSingletonPtr()
Returns a pointer to the Singleton class.
Definition: Singleton.h:49
static T & GetSingleton()
Returns a reference to the Singleton class.
Definition: Singleton.h:56
virtual ~Singleton()
Definition: Singleton.h:64