Gazebo Common

API Reference

3.17.0
gz/common/SingletonT.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef GZ_COMMON_SINGLETONT_HH_
18 #define GZ_COMMON_SINGLETONT_HH_
19 
20 namespace ignition
21 {
22  namespace common
23  {
26  template <class T>
27  class SingletonT
28  {
30  public: static T *Instance()
31  {
32  return &GetInstance();
33  }
34 
36  protected: SingletonT() {}
37 
39  protected: virtual ~SingletonT() {}
40 
42  private: static T &GetInstance()
43  {
44  static T t;
45  return static_cast<T &>(t);
46  }
47 
49  private: static T &myself;
50  };
51 
53  template <class T>
54  T &SingletonT<T>::myself = SingletonT<T>::GetInstance();
55  }
56 }
57 #endif
Forward declarations for the common classes.
virtual ~SingletonT()
Destructor.
Definition: gz/common/SingletonT.hh:39
static T * Instance()
Get an instance of the singleton.
Definition: gz/common/SingletonT.hh:30
Singleton template class.
Definition: gz/common/SingletonT.hh:27
SingletonT()
Constructor.
Definition: gz/common/SingletonT.hh:36