| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_BASE_REFERENCECOUNTEDSINGLETONFACTORY_H_ | 11 #ifndef WEBRTC_BASE_REFERENCECOUNTEDSINGLETONFACTORY_H_ |
| 12 #define WEBRTC_BASE_REFERENCECOUNTEDSINGLETONFACTORY_H_ | 12 #define WEBRTC_BASE_REFERENCECOUNTEDSINGLETONFACTORY_H_ |
| 13 | 13 |
| 14 #include <memory> |
| 15 |
| 14 #include "webrtc/base/common.h" | 16 #include "webrtc/base/common.h" |
| 15 #include "webrtc/base/criticalsection.h" | 17 #include "webrtc/base/criticalsection.h" |
| 16 #include "webrtc/base/logging.h" | 18 #include "webrtc/base/logging.h" |
| 17 #include "webrtc/base/scoped_ptr.h" | |
| 18 | 19 |
| 19 namespace rtc { | 20 namespace rtc { |
| 20 | 21 |
| 21 template <typename Interface> class rcsf_ptr; | 22 template <typename Interface> class rcsf_ptr; |
| 22 | 23 |
| 23 // A ReferenceCountedSingletonFactory is an object which owns another object, | 24 // A ReferenceCountedSingletonFactory is an object which owns another object, |
| 24 // and doles out the owned object to consumers in a reference-counted manner. | 25 // and doles out the owned object to consumers in a reference-counted manner. |
| 25 // Thus, the factory owns at most one object of the desired kind, and | 26 // Thus, the factory owns at most one object of the desired kind, and |
| 26 // hands consumers a special pointer to it, through which they can access it. | 27 // hands consumers a special pointer to it, through which they can access it. |
| 27 // When the consumers delete the pointer, the reference count goes down, | 28 // When the consumers delete the pointer, the reference count goes down, |
| 28 // and if the reference count hits zero, the factory can throw the object | 29 // and if the reference count hits zero, the factory can throw the object |
| 29 // away. If a consumer requests the pointer and the factory has none, | 30 // away. If a consumer requests the pointer and the factory has none, |
| 30 // it can create one on the fly and pass it back. | 31 // it can create one on the fly and pass it back. |
| 31 template <typename Interface> | 32 template <typename Interface> |
| 32 class ReferenceCountedSingletonFactory { | 33 class ReferenceCountedSingletonFactory { |
| 33 friend class rcsf_ptr<Interface>; | 34 friend class rcsf_ptr<Interface>; |
| 34 public: | 35 public: |
| 35 ReferenceCountedSingletonFactory() : ref_count_(0) {} | 36 ReferenceCountedSingletonFactory() : ref_count_(0) {} |
| 36 | 37 |
| 37 virtual ~ReferenceCountedSingletonFactory() { | 38 virtual ~ReferenceCountedSingletonFactory() { |
| 38 ASSERT(ref_count_ == 0); | 39 ASSERT(ref_count_ == 0); |
| 39 } | 40 } |
| 40 | 41 |
| 41 protected: | 42 protected: |
| 42 // Must be implemented in a sub-class. The sub-class may choose whether or not | 43 // Must be implemented in a sub-class. The sub-class may choose whether or not |
| 43 // to cache the instance across lifetimes by either reset()'ing or not | 44 // to cache the instance across lifetimes by either reset()'ing or not |
| 44 // reset()'ing the scoped_ptr in CleanupInstance(). | 45 // reset()'ing the unique_ptr in CleanupInstance(). |
| 45 virtual bool SetupInstance() = 0; | 46 virtual bool SetupInstance() = 0; |
| 46 virtual void CleanupInstance() = 0; | 47 virtual void CleanupInstance() = 0; |
| 47 | 48 |
| 48 scoped_ptr<Interface> instance_; | 49 std::unique_ptr<Interface> instance_; |
| 49 | 50 |
| 50 private: | 51 private: |
| 51 Interface* GetInstance() { | 52 Interface* GetInstance() { |
| 52 rtc::CritScope cs(&crit_); | 53 rtc::CritScope cs(&crit_); |
| 53 if (ref_count_ == 0) { | 54 if (ref_count_ == 0) { |
| 54 if (!SetupInstance()) { | 55 if (!SetupInstance()) { |
| 55 LOG(LS_VERBOSE) << "Failed to setup instance"; | 56 LOG(LS_VERBOSE) << "Failed to setup instance"; |
| 56 return NULL; | 57 return NULL; |
| 57 } | 58 } |
| 58 ASSERT(instance_.get() != NULL); | 59 ASSERT(instance_.get() != NULL); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 | 149 |
| 149 Interface* instance_; | 150 Interface* instance_; |
| 150 ReferenceCountedSingletonFactory<Interface>* factory_; | 151 ReferenceCountedSingletonFactory<Interface>* factory_; |
| 151 | 152 |
| 152 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(rcsf_ptr); | 153 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(rcsf_ptr); |
| 153 }; | 154 }; |
| 154 | 155 |
| 155 }; // namespace rtc | 156 }; // namespace rtc |
| 156 | 157 |
| 157 #endif // WEBRTC_BASE_REFERENCECOUNTEDSINGLETONFACTORY_H_ | 158 #endif // WEBRTC_BASE_REFERENCECOUNTEDSINGLETONFACTORY_H_ |
| OLD | NEW |