GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
Singleton.hpp
Go to the documentation of this file.
1 // This file is part of GranOO, a workbench for DEM simulation.
2 //
3 // Author(s) : - Jean-luc CHARLES I2M-DuMAS/ENSAM Talence France
4 // <jean-luc.charles@ensam.eu>
5 // - Damien ANDRE SPCTS/ENS Ceramique industrielle, Limoges France
6 // <damien.andre@unilim.fr>
7 // - Jeremie GIRARDOT I2M-DuMAS/ENSAM Talence France
8 // <jeremie.girardot@ensam.eu>
9 // - Cedric Hubert LAMIH/UVHC Valenciennes France
10 // <cedric.hubert@univ-valenciennes.fr>
11 // - Ivan IORDANOFF I2M-DuMAS-MPI/ENSAM Talence France
12 // <ivan.iordanoff@ensam.eu>
13 //
14 // Copyright (C) 2008-2016 JL. CHARLES, D. ANDRE, I. IORDANOFF, J. GIRARDOT
15 //
16 //
17 //
18 // This program is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 // This program is distributed in the hope that it will be useful,
24 // but WITHOUT ANY WARRANTY; without even the implied warranty of
25 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 // GNU General Public License for more details.
27 //
28 // You should have received a copy of the GNU General Public License
29 // along with this program. If not, see <http://www.gnu.org/licenses/>.
30 
31 
32 #ifndef _CommonLibs_Util_Singleton_HPP_
33 #define _CommonLibs_Util_Singleton_HPP_
34 
35 #include <type_traits>
36 
37 #include "GranOO3/Core/Macro.hpp"
38 
39 // Macro and class that help to implement the singleton pattern.
40 
41 // Exemple :
42 // class A
43 // {
44 // GRANOO_SINGLETON_CLASS(A);
45 // etc...
46 // };
47 
48 
49 
50 # define GRANOO_SINGLETON_CLASS(CLASS) \
51 private: \
52  friend class GranOO3::Core::Singleton<CLASS>; \
53 public: \
54  static CLASS & get() \
55  { \
56  return GranOO3::Core::Singleton<CLASS>::get(); \
57  } \
58  static const CLASS & get_const() \
59  { \
60  return GranOO3::Core::Singleton<CLASS>::get(); \
61  } \
62  static void instanciate() \
63  { \
64  return GranOO3::Core::Singleton<CLASS>::instanciate(); \
65  }
66 
67 
68 namespace GranOO3
69 {
70  namespace Core
71  {
72  // The famous singleton design pattern
73  template <typename T>
74  class Singleton
75  {
76  protected:
78  virtual ~Singleton();
79 
80  public:
81  static bool exist();
82  static T& get();
83  static void instanciate();
84  static void delete_me();
85 
86  protected:
87  static T* _me;
88  };
89 
90  // The famous singleton design pattern
91  template <typename T, class Base>
92  class SingletonB : public Singleton<T>
93  {
94  protected:
96  virtual ~SingletonB();
97  };
98 
99  template<class T, class Base>
101  : Singleton<T>() {
102  static_assert(std::is_base_of<Base, T>::value,
103  "T must be a descendant of Base");
106  }
107 
108  template<class T, class Base>
110  }
111 
112  template<class T> T* Singleton<T>::_me = nullptr;
113 
114  template<class T>
116  AssertMsg(_me==0, "The singleton was already instanciate");
117  _me = static_cast<T*>(this);
118  }
119 
120  template<class T>
122  _me = nullptr;
123  }
124 
125  template<class T>
126  T&
128  static_assert(std::is_base_of<Singleton<T>,T>::value,"T must inherit from Singleton");
129  if (_me == nullptr)
130  new T;
131  return *_me;
132  }
133 
134  template<class T>
135  void
137  static_assert(std::is_base_of<Singleton<T>,T>::value,"T must inherit from Singleton");
138  if (_me == nullptr)
139  new T;
140  }
141 
142  template<class T>
143  void
145  static_assert(std::is_base_of<Singleton<T>,T>::value,"T must inherit from Singleton");
146  if (_me != nullptr)
147  delete _me;
148  _me = nullptr;
149  }
150 
151  template<class T>
152  bool
154  return (_me != nullptr);
155  }
156 
157  }
158 }
159 
160 #endif
#define AssertMsg(condition, message)
Definition: Macro.hpp:67
Definition: Singleton.hpp:93
virtual ~SingletonB()
Definition: Singleton.hpp:109
SingletonB()
Definition: Singleton.hpp:100
Definition: Singleton.hpp:75
Singleton()
Definition: Singleton.hpp:115
static T * _me
Definition: Singleton.hpp:87
static T & get()
Definition: Singleton.hpp:127
virtual ~Singleton()
Definition: Singleton.hpp:121
static bool exist()
Definition: Singleton.hpp:153
static void delete_me()
Definition: Singleton.hpp:144
static void instanciate()
Definition: Singleton.hpp:136
Definition: Common.hpp:198
T value(details::expression_node< T > *n)
Definition: Exprtk.hpp:15070