Gazebo Common

API Reference

5.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 GZ_COMMON_SINGLETONT_HH_
18 #define GZ_COMMON_SINGLETONT_HH_
19 
20 namespace gz
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