• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2020 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_BOOST_H_
16 #define I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_BOOST_H_
17 
18 #include <boost/scoped_ptr.hpp>
19 #include <boost/thread/once.hpp>
20 #include <boost/utility.hpp>
21 
22 namespace i18n {
23 namespace phonenumbers {
24 
25 template <class T>
26 class Singleton : private boost::noncopyable {
27  public:
Singleton()28   Singleton() {}
~Singleton()29   virtual ~Singleton() {}
30 
GetInstance()31   static T* GetInstance() {
32     boost::call_once(Init, flag_);
33     return instance_.get();
34   }
35 
36  private:
Init()37   static void Init() {
38     instance_.reset(new T());
39   }
40 
41   static boost::scoped_ptr<T> instance_;
42   static boost::once_flag flag_;
43 };
44 
45 template <class T> boost::scoped_ptr<T> Singleton<T>::instance_;
46 template <class T> boost::once_flag Singleton<T>::flag_ = BOOST_ONCE_INIT;
47 
48 }  // namespace phonenumbers
49 }  // namespace i18n
50 
51 #endif  // I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_BOOST_H_
52