Page Speed Optimization Libraries  1.2.24.1
net/instaweb/util/public/ref_counted_ptr.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http:///www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00030 
00031 #ifndef NET_INSTAWEB_UTIL_PUBLIC_REF_COUNTED_PTR_H_
00032 #define NET_INSTAWEB_UTIL_PUBLIC_REF_COUNTED_PTR_H_
00033 
00034 #include "net/instaweb/util/public/basictypes.h"
00035 
00036 #include "base/memory/ref_counted.h"
00037 
00038 namespace net_instaweb {
00039 
00040 
00041 template<class T>
00042 class RefCounted : public base::RefCountedThreadSafe<T> {
00043 };
00044 
00048 template<class T>
00049 class RefCountedPtr : public scoped_refptr<T> {
00050  public:
00051   RefCountedPtr() : scoped_refptr<T>(NULL) {}
00052   explicit RefCountedPtr(T* t) : scoped_refptr<T>(t) {}
00053 
00054   template<class U>
00055   explicit RefCountedPtr(const RefCountedPtr<U>& src)
00056       : scoped_refptr<T>(src) {
00057   }
00058 
00059   RefCountedPtr<T>& operator=(const RefCountedPtr<T>& other) {
00060     scoped_refptr<T>::operator=(other);
00061     return *this;
00062   }
00063 
00064   template<class U>
00065   RefCountedPtr<T>& operator=(const RefCountedPtr<U>& other) {
00066     scoped_refptr<T>::operator=(other);
00067     return *this;
00068   }
00069 
00073   bool unique() const { return !this->ptr_ || this->ptr_->HasOneRef(); }
00074 
00075   template<typename U>
00076   RefCountedPtr<U> StaticCast() const {
00077     return RefCountedPtr<U>(static_cast<U*>(this->get()));
00078   }
00079 
00080   void clear() {
00081     *this = RefCountedPtr();
00082   }
00083   void reset(T* ptr) {
00084     *this = RefCountedPtr(ptr);
00085   }
00086   void reset(const RefCountedPtr& src) {
00087     *this = src;
00088   }
00089 
00090  private:
00091   operator void*() const; 
00092   operator T*() const; 
00093 
00096 };
00097 
00103 template<class T>
00104 class RefCountedObj {
00105  public:
00106   RefCountedObj() : data_ptr_(new Data()) {}
00107   explicit RefCountedObj(const T& val) : data_ptr_(new Data(val)) {}
00108 
00112   bool unique() const { return data_ptr_.unique(); }
00113 
00114   T* get() { return &data_ptr_->value; }
00115   const T* get() const { return &data_ptr_->value; }
00116   T* operator->() { return &data_ptr_->value; }
00117   const T* operator->() const { return &data_ptr_->value; }
00118   T& operator*() { return data_ptr_->value; }
00119   const T& operator*() const { return data_ptr_->value; }
00120 
00121  protected:
00122   struct Data : public RefCounted<Data> {
00123     Data() {}
00124     explicit Data(const T& val) : value(val) {}
00125     T value;
00126   };
00127 
00128   RefCountedPtr<Data> data_ptr_;
00129 
00130  private:
00131   operator void*() const; 
00132   operator T*() const; 
00133 
00135 };
00136 
00141 #define REFCOUNT_SHARED_MEM_IMPL_CLASS base::RefCountedThreadSafe
00142 
00143 
00153 #define REFCOUNT_FRIEND_DECLARATION(class_name) \
00154   friend class REFCOUNT_SHARED_MEM_IMPL_CLASS<class_name>
00155 
00156 }  
00157 
00158 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_REF_COUNTED_PTR_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines