• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2013 The Libphonenumber Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_POSIX_H_
16 #define I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_POSIX_H_
17 
18 #include <pthread.h>
19 
20 #include "phonenumbers/base/logging.h"
21 
22 namespace i18n {
23 namespace phonenumbers {
24 
25 template <class T>
26 class Singleton {
27  public:
~Singleton()28   virtual ~Singleton() {}
29 
GetInstance()30   static T* GetInstance() {
31     const int ret = pthread_once(&once_control_, &Init);
32     (void) ret;
33     DCHECK_EQ(0, ret);
34     return instance_;
35   }
36 
37  private:
Init()38   static void Init() {
39     instance_ = new T();
40   }
41 
42   static T* instance_;  // Leaky singleton.
43   static pthread_once_t once_control_;
44 };
45 
46 template <class T> T* Singleton<T>::instance_;
47 template <class T> pthread_once_t Singleton<T>::once_control_ =
48     PTHREAD_ONCE_INIT;
49 
50 }  // namespace phonenumbers
51 }  // namespace i18n
52 
53 #endif  // I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_POSIX_H_
54