3 * Copyright 2004--2010, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef TALK_BASE_REFERENCECOUNTEDSINGLETONFACTORY_H_
29 #define TALK_BASE_REFERENCECOUNTEDSINGLETONFACTORY_H_
31 #include "talk/base/common.h"
32 #include "talk/base/criticalsection.h"
33 #include "talk/base/logging.h"
34 #include "talk/base/scoped_ptr.h"
38 template <typename Interface> class rcsf_ptr;
40 // A ReferenceCountedSingletonFactory is an object which owns another object,
41 // and doles out the owned object to consumers in a reference-counted manner.
42 // Thus, the factory owns at most one object of the desired kind, and
43 // hands consumers a special pointer to it, through which they can access it.
44 // When the consumers delete the pointer, the reference count goes down,
45 // and if the reference count hits zero, the factory can throw the object
46 // away. If a consumer requests the pointer and the factory has none,
47 // it can create one on the fly and pass it back.
48 template <typename Interface>
49 class ReferenceCountedSingletonFactory {
50 friend class rcsf_ptr<Interface>;
52 ReferenceCountedSingletonFactory() : ref_count_(0) {}
54 virtual ~ReferenceCountedSingletonFactory() {
55 ASSERT(ref_count_ == 0);
59 // Must be implemented in a sub-class. The sub-class may choose whether or not
60 // to cache the instance across lifetimes by either reset()'ing or not
61 // reset()'ing the scoped_ptr in CleanupInstance().
62 virtual bool SetupInstance() = 0;
63 virtual void CleanupInstance() = 0;
65 scoped_ptr<Interface> instance_;
68 Interface* GetInstance() {
69 talk_base::CritScope cs(&crit_);
70 if (ref_count_ == 0) {
71 if (!SetupInstance()) {
72 LOG(LS_VERBOSE) << "Failed to setup instance";
75 ASSERT(instance_.get() != NULL);
79 LOG(LS_VERBOSE) << "Number of references: " << ref_count_;
80 return instance_.get();
83 void ReleaseInstance() {
84 talk_base::CritScope cs(&crit_);
85 ASSERT(ref_count_ > 0);
86 ASSERT(instance_.get() != NULL);
88 LOG(LS_VERBOSE) << "Number of references: " << ref_count_;
89 if (ref_count_ == 0) {
94 CriticalSection crit_;
97 DISALLOW_COPY_AND_ASSIGN(ReferenceCountedSingletonFactory);
100 template <typename Interface>
103 // Create a pointer that uses the factory to get the instance.
104 // This is lazy - it won't generate the instance until it is requested.
105 explicit rcsf_ptr(ReferenceCountedSingletonFactory<Interface>* factory)
114 Interface& operator*() {
119 Interface* operator->() {
124 // Gets the pointer, creating the singleton if necessary. May return NULL if
131 // Set instance to NULL and tell the factory we aren't using the instance
136 factory_->ReleaseInstance();
140 // Lets us know whether instance is valid or not right now.
141 // Even though attempts to use the instance will automatically create it, it
142 // is advisable to check this because creation can fail.
144 return instance_ != NULL;
147 // Returns the factory that this pointer is using.
148 ReferenceCountedSingletonFactory<Interface>* factory() const {
153 void EnsureAcquired() {
155 ASSERT(instance_ != NULL);
159 // Since we're getting a singleton back, acquire is a noop if instance is
160 // already populated.
162 instance_ = factory_->GetInstance();
166 Interface* instance_;
167 ReferenceCountedSingletonFactory<Interface>* factory_;
169 DISALLOW_IMPLICIT_CONSTRUCTORS(rcsf_ptr);
172 }; // namespace talk_base
174 #endif // TALK_BASE_REFERENCECOUNTEDSINGLETONFACTORY_H_