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_UNSAFE_H_ 16 #define I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_UNSAFE_H_ 17 18 #include "phonenumbers/base/logging.h" 19 #include "phonenumbers/base/thread_checker.h" 20 21 namespace i18n { 22 namespace phonenumbers { 23 24 // Note that this implementation is not thread-safe. For a thread-safe 25 // implementation on non-POSIX platforms, please compile with 26 // -DI18N_PHONENUMBERS_USE_BOOST. 27 template <class T> 28 class Singleton { 29 public: Singleton()30 Singleton() : thread_checker_() {} ~Singleton()31 virtual ~Singleton() {} 32 GetInstance()33 static T* GetInstance() { 34 static T* instance = NULL; 35 if (!instance) { 36 instance = new T(); 37 } 38 DCHECK(instance->thread_checker_.CalledOnValidThread()); 39 return instance; 40 } 41 42 private: 43 const ThreadChecker thread_checker_; 44 }; 45 46 } // namespace phonenumbers 47 } // namespace i18n 48 49 #endif // I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_UNSAFE_H_ 50