OLD | NEW |
1 // Copyright (C) 2011 The Libphonenumber Authors | 1 // Copyright (C) 2011 The Libphonenumber Authors |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 // Author: Philippe Liard | 15 // Author: Philippe Liard |
16 | 16 |
17 #ifndef I18N_PHONENUMBERS_BASE_SINGLETON_H_ | 17 #ifndef I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_H_ |
18 #define I18N_PHONENUMBERS_BASE_SINGLETON_H_ | 18 #define I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_H_ |
19 | 19 |
20 #if defined(I18N_PHONENUMBERS_USE_BOOST) | 20 #if defined(I18N_PHONENUMBERS_USE_BOOST) |
21 | 21 |
22 #include <boost/scoped_ptr.hpp> | 22 #include <boost/scoped_ptr.hpp> |
23 #include <boost/thread/once.hpp> | 23 #include <boost/thread/once.hpp> |
24 #include <boost/utility.hpp> | 24 #include <boost/utility.hpp> |
25 | 25 |
26 namespace i18n { | 26 namespace i18n { |
27 namespace phonenumbers { | 27 namespace phonenumbers { |
28 | 28 |
(...skipping 12 matching lines...) Expand all Loading... |
41 instance.reset(new T()); | 41 instance.reset(new T()); |
42 } | 42 } |
43 | 43 |
44 static boost::scoped_ptr<T> instance; | 44 static boost::scoped_ptr<T> instance; |
45 static boost::once_flag flag; | 45 static boost::once_flag flag; |
46 }; | 46 }; |
47 | 47 |
48 template <class T> boost::scoped_ptr<T> Singleton<T>::instance; | 48 template <class T> boost::scoped_ptr<T> Singleton<T>::instance; |
49 template <class T> boost::once_flag Singleton<T>::flag = BOOST_ONCE_INIT; | 49 template <class T> boost::once_flag Singleton<T>::flag = BOOST_ONCE_INIT; |
50 | 50 |
| 51 } // namespace phonenumbers |
| 52 } // namespace i18n |
| 53 |
51 #else // !I18N_PHONENUMBERS_USE_BOOST | 54 #else // !I18N_PHONENUMBERS_USE_BOOST |
52 | 55 |
53 #include "phonenumbers/base/logging.h" | 56 #include "phonenumbers/base/logging.h" |
54 #include "phonenumbers/base/thread_checker.h" | 57 #include "phonenumbers/base/thread_checker.h" |
55 | 58 |
| 59 #if !defined(__linux__) && !defined(__APPLE__) |
| 60 |
56 namespace i18n { | 61 namespace i18n { |
57 namespace phonenumbers { | 62 namespace phonenumbers { |
58 | 63 |
59 // Note that this implementation is not thread-safe. For a thread-safe | 64 // Note that this implementation is not thread-safe. For a thread-safe |
60 // implementation, please compile with -DI18N_PHONENUMBERS_USE_BOOST. | 65 // implementation on non-POSIX platforms, please compile with |
| 66 // -DI18N_PHONENUMBERS_USE_BOOST. |
61 template <class T> | 67 template <class T> |
62 class Singleton { | 68 class Singleton { |
63 public: | 69 public: |
64 Singleton() : thread_checker_() {} | 70 Singleton() : thread_checker_() {} |
65 | 71 |
66 virtual ~Singleton() {} | 72 virtual ~Singleton() {} |
67 | 73 |
68 static T* GetInstance() { | 74 static T* GetInstance() { |
69 static T* instance = NULL; | 75 static T* instance = NULL; |
70 if (!instance) { | 76 if (!instance) { |
71 instance = new T(); | 77 instance = new T(); |
72 } | 78 } |
73 DCHECK(instance->thread_checker_.CalledOnValidThread()); | 79 DCHECK(instance->thread_checker_.CalledOnValidThread()); |
74 return instance; | 80 return instance; |
75 } | 81 } |
76 | 82 |
77 private: | 83 private: |
78 const ThreadChecker thread_checker_; | 84 const ThreadChecker thread_checker_; |
79 }; | 85 }; |
80 | 86 |
81 #endif // !I18N_PHONENUMBERS_USE_BOOST | |
82 | |
83 } // namespace phonenumbers | 87 } // namespace phonenumbers |
84 } // namespace i18n | 88 } // namespace i18n |
85 | 89 |
86 #endif // I18N_PHONENUMBERS_BASE_SINGLETON_H_ | 90 #else |
| 91 #include "phonenumbers/base/memory/singleton_posix.h" |
| 92 #endif // !defined(__linux__) && !defined(__APPLE__) |
| 93 |
| 94 #endif // !I18N_PHONENUMBERS_USE_BOOST |
| 95 #endif // I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_H_ |
OLD | NEW |