You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
5.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2010, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  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.
  15. *
  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.
  26. */
  27. #ifndef TALK_BASE_REFERENCECOUNTEDSINGLETONFACTORY_H_
  28. #define TALK_BASE_REFERENCECOUNTEDSINGLETONFACTORY_H_
  29. #include "talk/base/common.h"
  30. #include "talk/base/criticalsection.h"
  31. #include "talk/base/logging.h"
  32. #include "talk/base/scoped_ptr.h"
  33. namespace talk_base {
  34. template <typename Interface> class rcsf_ptr;
  35. // A ReferenceCountedSingletonFactory is an object which owns another object,
  36. // and doles out the owned object to consumers in a reference-counted manner.
  37. // Thus, the factory owns at most one object of the desired kind, and
  38. // hands consumers a special pointer to it, through which they can access it.
  39. // When the consumers delete the pointer, the reference count goes down,
  40. // and if the reference count hits zero, the factory can throw the object
  41. // away. If a consumer requests the pointer and the factory has none,
  42. // it can create one on the fly and pass it back.
  43. template <typename Interface>
  44. class ReferenceCountedSingletonFactory {
  45. friend class rcsf_ptr<Interface>;
  46. public:
  47. ReferenceCountedSingletonFactory() : ref_count_(0) {}
  48. virtual ~ReferenceCountedSingletonFactory() {
  49. ASSERT(ref_count_ == 0);
  50. }
  51. protected:
  52. // Must be implemented in a sub-class. The sub-class may choose whether or not
  53. // to cache the instance across lifetimes by either reset()'ing or not
  54. // reset()'ing the scoped_ptr in CleanupInstance().
  55. virtual bool SetupInstance() = 0;
  56. virtual void CleanupInstance() = 0;
  57. scoped_ptr<Interface> instance_;
  58. private:
  59. Interface* GetInstance() {
  60. talk_base::CritScope cs(&crit_);
  61. if (ref_count_ == 0) {
  62. if (!SetupInstance()) {
  63. LOG(LS_VERBOSE) << "Failed to setup instance";
  64. return NULL;
  65. }
  66. ASSERT(instance_.get() != NULL);
  67. }
  68. ++ref_count_;
  69. LOG(LS_VERBOSE) << "Number of references: " << ref_count_;
  70. return instance_.get();
  71. }
  72. void ReleaseInstance() {
  73. talk_base::CritScope cs(&crit_);
  74. ASSERT(ref_count_ > 0);
  75. ASSERT(instance_.get() != NULL);
  76. --ref_count_;
  77. LOG(LS_VERBOSE) << "Number of references: " << ref_count_;
  78. if (ref_count_ == 0) {
  79. CleanupInstance();
  80. }
  81. }
  82. CriticalSection crit_;
  83. int ref_count_;
  84. DISALLOW_COPY_AND_ASSIGN(ReferenceCountedSingletonFactory);
  85. };
  86. template <typename Interface>
  87. class rcsf_ptr {
  88. public:
  89. // Create a pointer that uses the factory to get the instance.
  90. // This is lazy - it won't generate the instance until it is requested.
  91. explicit rcsf_ptr(ReferenceCountedSingletonFactory<Interface>* factory)
  92. : instance_(NULL),
  93. factory_(factory) {
  94. }
  95. ~rcsf_ptr() {
  96. release();
  97. }
  98. Interface& operator*() {
  99. EnsureAcquired();
  100. return *instance_;
  101. }
  102. Interface* operator->() {
  103. EnsureAcquired();
  104. return instance_;
  105. }
  106. // Gets the pointer, creating the singleton if necessary. May return NULL if
  107. // creation failed.
  108. Interface* get() {
  109. Acquire();
  110. return instance_;
  111. }
  112. // Set instance to NULL and tell the factory we aren't using the instance
  113. // anymore.
  114. void release() {
  115. if (instance_) {
  116. instance_ = NULL;
  117. factory_->ReleaseInstance();
  118. }
  119. }
  120. // Lets us know whether instance is valid or not right now.
  121. // Even though attempts to use the instance will automatically create it, it
  122. // is advisable to check this because creation can fail.
  123. bool valid() const {
  124. return instance_ != NULL;
  125. }
  126. // Returns the factory that this pointer is using.
  127. ReferenceCountedSingletonFactory<Interface>* factory() const {
  128. return factory_;
  129. }
  130. private:
  131. void EnsureAcquired() {
  132. Acquire();
  133. ASSERT(instance_ != NULL);
  134. }
  135. void Acquire() {
  136. // Since we're getting a singleton back, acquire is a noop if instance is
  137. // already populated.
  138. if (!instance_) {
  139. instance_ = factory_->GetInstance();
  140. }
  141. }
  142. Interface* instance_;
  143. ReferenceCountedSingletonFactory<Interface>* factory_;
  144. DISALLOW_IMPLICIT_CONSTRUCTORS(rcsf_ptr);
  145. };
  146. }; // namespace talk_base
  147. #endif // TALK_BASE_REFERENCECOUNTEDSINGLETONFACTORY_H_