aboutsummaryrefslogtreecommitdiff
path: root/third_party_mods
diff options
context:
space:
mode:
authorperkj@webrtc.org <perkj@webrtc.org@4adac7df-926f-26a2-2b94-8c16560cd09d>2011-10-28 15:08:54 +0000
committerperkj@webrtc.org <perkj@webrtc.org@4adac7df-926f-26a2-2b94-8c16560cd09d>2011-10-28 15:08:54 +0000
commit8129752c3b331a3e12a7245ae3bd2c021846ac73 (patch)
treeeeaf5708c915b9719a8456f0f927ded383912047 /third_party_mods
parent94cfde7c6686905af707442fe211171b0e7119d0 (diff)
downloadwebrtc-8129752c3b331a3e12a7245ae3bd2c021846ac73.tar.gz
Add refcount and scoped_refptr.
git-svn-id: http://webrtc.googlecode.com/svn/trunk@835 4adac7df-926f-26a2-2b94-8c16560cd09d
Diffstat (limited to 'third_party_mods')
-rw-r--r--third_party_mods/libjingle/source/talk/base/refcount.h89
-rw-r--r--third_party_mods/libjingle/source/talk/base/scoped_refptr.h132
2 files changed, 221 insertions, 0 deletions
diff --git a/third_party_mods/libjingle/source/talk/base/refcount.h b/third_party_mods/libjingle/source/talk/base/refcount.h
new file mode 100644
index 0000000000..b811809fed
--- /dev/null
+++ b/third_party_mods/libjingle/source/talk/base/refcount.h
@@ -0,0 +1,89 @@
+/*
+ * libjingle
+ * Copyright 2011, Google Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+ * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef TALK_BASE_REF_COUNT_H_
+#define TALK_BASE_REF_COUNT_H_
+
+#include <cstring>
+
+#include "talk/base/criticalsection.h"
+
+namespace talk_base {
+
+// Reference count interface.
+class RefCountInterface {
+ public:
+ virtual int AddRef() = 0;
+ virtual int Release() = 0;
+};
+
+template <class T>
+class RefCountedObject : public T {
+ public:
+ RefCountedObject() : ref_count_(0) {
+ }
+
+ template<typename P>
+ explicit RefCountedObject(P p) : ref_count_(0), T(p) {
+ }
+
+ template<typename P1, typename P2>
+ RefCountedObject(P1 p1, P2 p2) : ref_count_(0), T(p1, p2) {
+ }
+
+ template<typename P1, typename P2, typename P3>
+ RefCountedObject(P1 p1, P2 p2, P3 p3) : ref_count_(0), T(p1, p2, p3) {
+ }
+
+ template<typename P1, typename P2, typename P3, typename P4>
+ RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4) : ref_count_(0), T(p1, p2, p3, p4) {
+ }
+
+ template<typename P1, typename P2, typename P3, typename P4, typename P5>
+ RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
+ : ref_count_(0), T(p1, p2, p3, p4, p5) {
+ }
+
+ virtual int AddRef() {
+ return talk_base::AtomicOps::Increment(&ref_count_);
+ }
+
+ virtual int Release() {
+ int count = talk_base::AtomicOps::Decrement(&ref_count_);
+ if (!count) {
+ delete this;
+ }
+ return count;
+ }
+
+ protected:
+ int ref_count_;
+};
+
+} // namespace talk_base
+
+#endif // TALK_BASE_REF_COUNT_H_
diff --git a/third_party_mods/libjingle/source/talk/base/scoped_refptr.h b/third_party_mods/libjingle/source/talk/base/scoped_refptr.h
new file mode 100644
index 0000000000..7709962c94
--- /dev/null
+++ b/third_party_mods/libjingle/source/talk/base/scoped_refptr.h
@@ -0,0 +1,132 @@
+#ifndef TALK_BASE_SCOPED_REFPTR_H_
+#define TALK_BASE_SCOPED_REFPTR_H_
+// Originally these classes are copied from Chromium.
+
+//
+// A smart pointer class for reference counted objects. Use this class instead
+// of calling AddRef and Release manually on a reference counted object to
+// avoid common memory leaks caused by forgetting to Release an object
+// reference. Sample usage:
+//
+// class MyFoo : public RefCounted<MyFoo> {
+// ...
+// };
+//
+// void some_function() {
+// scoped_refptr<MyFoo> foo = new MyFoo();
+// foo->Method(param);
+// // |foo| is released when this function returns
+// }
+//
+// void some_other_function() {
+// scoped_refptr<MyFoo> foo = new MyFoo();
+// ...
+// foo = NULL; // explicitly releases |foo|
+// ...
+// if (foo)
+// foo->Method(param);
+// }
+//
+// The above examples show how scoped_refptr<T> acts like a pointer to T.
+// Given two scoped_refptr<T> classes, it is also possible to exchange
+// references between the two objects, like so:
+//
+// {
+// scoped_refptr<MyFoo> a = new MyFoo();
+// scoped_refptr<MyFoo> b;
+//
+// b.swap(a);
+// // now, |b| references the MyFoo object, and |a| references NULL.
+// }
+//
+// To make both |a| and |b| in the above example reference the same MyFoo
+// object, simply use the assignment operator:
+//
+// {
+// scoped_refptr<MyFoo> a = new MyFoo();
+// scoped_refptr<MyFoo> b;
+//
+// b = a;
+// // now, |a| and |b| each own a reference to the same MyFoo object.
+// }
+//
+namespace talk_base {
+
+template <class T>
+class scoped_refptr {
+ public:
+ scoped_refptr() : ptr_(NULL) {
+ }
+
+ scoped_refptr(T* p) : ptr_(p) {
+ if (ptr_)
+ ptr_->AddRef();
+ }
+
+ scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) {
+ if (ptr_)
+ ptr_->AddRef();
+ }
+
+ template <typename U>
+ scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) {
+ if (ptr_)
+ ptr_->AddRef();
+ }
+
+ ~scoped_refptr() {
+ if (ptr_)
+ ptr_->Release();
+ }
+
+ T* get() const { return ptr_; }
+ operator T*() const { return ptr_; }
+ T* operator->() const { return ptr_; }
+
+ // Release a pointer.
+ // The return value is the current pointer held by this object.
+ // If this object holds a NULL pointer, the return value is NULL.
+ // After this operation, this object will hold a NULL pointer,
+ // and will not own the object any more.
+ T* release() {
+ T* retVal = ptr_;
+ ptr_ = NULL;
+ return retVal;
+ }
+
+ scoped_refptr<T>& operator=(T* p) {
+ // AddRef first so that self assignment should work
+ if (p)
+ p->AddRef();
+ if (ptr_ )
+ ptr_ ->Release();
+ ptr_ = p;
+ return *this;
+ }
+
+ scoped_refptr<T>& operator=(const scoped_refptr<T>& r) {
+ return *this = r.ptr_;
+ }
+
+ template <typename U>
+ scoped_refptr<T>& operator=(const scoped_refptr<U>& r) {
+ return *this = r.get();
+ }
+
+ void swap(T** pp) {
+ T* p = ptr_;
+ ptr_ = *pp;
+ *pp = p;
+ }
+
+ void swap(scoped_refptr<T>& r) {
+ swap(&r.ptr_);
+ }
+
+ protected:
+ T* ptr_;
+};
+
+} // namespace talk_base
+
+#endif // TALK_BASE_SCOPED_REFPTR_H_