1af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com// Copyright (C) 2011 The Libphonenumber Authors
2af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com//
3af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com// Licensed under the Apache License, Version 2.0 (the "License");
4af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com// you may not use this file except in compliance with the License.
5af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com// You may obtain a copy of the License at
6af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com//
7af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com// http://www.apache.org/licenses/LICENSE-2.0
8af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com//
9af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com// Unless required by applicable law or agreed to in writing, software
10af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com// distributed under the License is distributed on an "AS IS" BASIS,
11af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com// See the License for the specific language governing permissions and
13af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com// limitations under the License.
14af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com
15af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com// Author: Philippe Liard
16af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com
17b886df44145f2b98006e6f394c4fbe0a5892c9a2philip.liard@gmail.com#ifndef I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_H_
18b886df44145f2b98006e6f394c4fbe0a5892c9a2philip.liard@gmail.com#define I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_H_
19af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com
2001e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com#if defined(I18N_PHONENUMBERS_USE_BOOST)
2101e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com
22af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com#include <boost/scoped_ptr.hpp>
23af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com#include <boost/thread/once.hpp>
24af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com#include <boost/utility.hpp>
25af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com
26af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.comnamespace i18n {
27af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.comnamespace phonenumbers {
28af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com
29af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.comtemplate <class T>
30af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.comclass Singleton : private boost::noncopyable {
31af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com public:
32af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com  virtual ~Singleton() {}
33af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com
34af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com  static T* GetInstance() {
35af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com    boost::call_once(Init, flag);
36af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com    return instance.get();
37af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com  }
38af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com
39af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com private:
40af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com  static void Init() {
41af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com    instance.reset(new T());
42af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com  }
43af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com
44af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com  static boost::scoped_ptr<T> instance;
45af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com  static boost::once_flag flag;
46af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com};
47af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com
48af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.comtemplate <class T> boost::scoped_ptr<T> Singleton<T>::instance;
49af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.comtemplate <class T> boost::once_flag Singleton<T>::flag = BOOST_ONCE_INIT;
50af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com
51b886df44145f2b98006e6f394c4fbe0a5892c9a2philip.liard@gmail.com}  // namespace phonenumbers
52b886df44145f2b98006e6f394c4fbe0a5892c9a2philip.liard@gmail.com}  // namespace i18n
53b886df44145f2b98006e6f394c4fbe0a5892c9a2philip.liard@gmail.com
5401e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com#else  // !I18N_PHONENUMBERS_USE_BOOST
5501e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com
5696b80a3b937dfdd22d738ab9a71bfb10d5c5ad60philip.liard@gmail.com#include "phonenumbers/base/logging.h"
5796b80a3b937dfdd22d738ab9a71bfb10d5c5ad60philip.liard@gmail.com#include "phonenumbers/base/thread_checker.h"
5801e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com
59b886df44145f2b98006e6f394c4fbe0a5892c9a2philip.liard@gmail.com#if !defined(__linux__) && !defined(__APPLE__)
60b886df44145f2b98006e6f394c4fbe0a5892c9a2philip.liard@gmail.com
6101e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.comnamespace i18n {
6201e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.comnamespace phonenumbers {
6301e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com
6401e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com// Note that this implementation is not thread-safe. For a thread-safe
65b886df44145f2b98006e6f394c4fbe0a5892c9a2philip.liard@gmail.com// implementation on non-POSIX platforms, please compile with
66b886df44145f2b98006e6f394c4fbe0a5892c9a2philip.liard@gmail.com// -DI18N_PHONENUMBERS_USE_BOOST.
6701e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.comtemplate <class T>
6801e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.comclass Singleton {
6901e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com public:
7096b80a3b937dfdd22d738ab9a71bfb10d5c5ad60philip.liard@gmail.com  Singleton() : thread_checker_() {}
7196b80a3b937dfdd22d738ab9a71bfb10d5c5ad60philip.liard@gmail.com
7201e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com  virtual ~Singleton() {}
7301e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com
7401e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com  static T* GetInstance() {
7501e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com    static T* instance = NULL;
7601e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com    if (!instance) {
7701e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com      instance = new T();
7801e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com    }
7996b80a3b937dfdd22d738ab9a71bfb10d5c5ad60philip.liard@gmail.com    DCHECK(instance->thread_checker_.CalledOnValidThread());
8001e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com    return instance;
8101e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com  }
8296b80a3b937dfdd22d738ab9a71bfb10d5c5ad60philip.liard@gmail.com
8396b80a3b937dfdd22d738ab9a71bfb10d5c5ad60philip.liard@gmail.com private:
8496b80a3b937dfdd22d738ab9a71bfb10d5c5ad60philip.liard@gmail.com  const ThreadChecker thread_checker_;
8501e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com};
8601e7fad6acf43df86b266928f602d9709af67baephilip.liard@gmail.com
87af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com}  // namespace phonenumbers
88af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com}  // namespace i18n
89af4a2ce290b619b39c2cb2a682ea4d7746d3fb21philip.liard@gmail.com
90b886df44145f2b98006e6f394c4fbe0a5892c9a2philip.liard@gmail.com#else
91b886df44145f2b98006e6f394c4fbe0a5892c9a2philip.liard@gmail.com#include "phonenumbers/base/memory/singleton_posix.h"
92b886df44145f2b98006e6f394c4fbe0a5892c9a2philip.liard@gmail.com#endif  // !defined(__linux__) && !defined(__APPLE__)
93b886df44145f2b98006e6f394c4fbe0a5892c9a2philip.liard@gmail.com
94b886df44145f2b98006e6f394c4fbe0a5892c9a2philip.liard@gmail.com#endif  // !I18N_PHONENUMBERS_USE_BOOST
95b886df44145f2b98006e6f394c4fbe0a5892c9a2philip.liard@gmail.com#endif  // I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_H_
96