Gazebo Common

API Reference

4.7.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 IGNITION_COMMON_SINGLETONT_HH_
18 #define IGNITION_COMMON_SINGLETONT_HH_
19 
20 #include <gz/common/config.hh>
21 
22 namespace ignition
23 {
24  namespace common
25  {
28  template <class T>
29  class SingletonT
30  {
32  public: static T *Instance()
33  {
34  return &GetInstance();
35  }
36 
38  protected: SingletonT() {}
39 
41  protected: virtual ~SingletonT() {}
42 
44  private: static T &GetInstance()
45  {
46  static T t;
47  return static_cast<T &>(t);
48  }
49 
51  private: static T &myself;
52  };
53 
55  template <class T>
56  T &SingletonT<T>::myself = SingletonT<T>::GetInstance();
57  }
58 }
59 #endif
Forward declarations for the common classes.
virtual ~SingletonT()
Destructor.
Definition: gz/common/SingletonT.hh:41
static T * Instance()
Get an instance of the singleton.
Definition: gz/common/SingletonT.hh:32
Singleton template class.
Definition: gz/common/SingletonT.hh:29
SingletonT()
Constructor.
Definition: gz/common/SingletonT.hh:38