1/* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18// -*- c++ -*- 19// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 20 21// O S C L _ S H A R E D _ P T R 22 23// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 24 25/*! \addtogroup osclbase OSCL Base 26 * 27 * @{ 28 */ 29 30 31/*! \file oscl_shared_ptr.h 32 \brief This file defines a template class OsclSharedPtr which is a "smart pointer" to the parameterized type. 33*/ 34 35 36#ifndef OSCL_SHARED_PTR_H_INCLUDED 37#define OSCL_SHARED_PTR_H_INCLUDED 38 39#ifndef OSCL_BASE_H_INCLUDED 40#include "oscl_base.h" 41#endif 42 43#ifndef OSCL_REFCOUNTER_H_INCLUDED 44#include "oscl_refcounter.h" 45#endif 46 47#define OSCL_DISABLE_WARNING_RETURN_TYPE_NOT_UDT 48#include "osclconfig_compiler_warnings.h" 49 50//! A parameterized smart pointer class. 51template <class TheClass> 52class OsclSharedPtr 53{ 54 public: 55 //! Constructor 56 OsclSharedPtr() : 57 mpRep(NULL), refcnt(NULL) {} 58 59 //! Constructor 60 /*! 61 \param inClassPtr A pointer to an instance of the parameterized type that the new OsclSharedPtr will wrap. 62 */ 63 OsclSharedPtr(TheClass* inClassPtr, OsclRefCounter* in_refcnt) : 64 mpRep(inClassPtr), refcnt(in_refcnt) {}; 65 66 67 //! Copy constructor 68 OsclSharedPtr(const OsclSharedPtr& inSharedPtr) : 69 mpRep(inSharedPtr.mpRep), refcnt(inSharedPtr.refcnt) 70 { 71 if (refcnt) 72 { 73 refcnt->addRef(); 74 } 75 } 76 77 78 //! Destructor. 79 virtual ~OsclSharedPtr() 80 { 81 if (refcnt != NULL) 82 { 83 refcnt->removeRef(); 84 } 85 } // end destructor 86 87 88 //! The dereferencing operator returns a pointer to the parameterized type and can be used to access member 89 //! elements of TheClass. 90 TheClass* operator->() 91 { 92 return mpRep; 93 } 94 95 //! The indirection operator returns a reference to an object of the parameterized type. 96 TheClass& operator*() 97 { 98 return *mpRep; 99 } 100 101 //! Casting operator 102 operator TheClass*() 103 { 104 return mpRep; 105 } 106 107 //! Use this function to get a pointer to the wrapped object. 108 TheClass* GetRep() 109 { 110 return mpRep; 111 } 112 113 //! Get the refcount pointer. This should primarily be used for conversion operations 114 OsclRefCounter* GetRefCounter() 115 { 116 return refcnt; 117 } 118 119 //! Get a count of how many references to the object exist. 120 int get_count() 121 { 122 return (refcnt == NULL) ? 0 : refcnt->getCount(); 123 } 124 125 //! Use this function to bind an existing OsclSharedPtr to a already-wrapped object. 126 void Bind(const OsclSharedPtr& inHandle); 127 128 //! Use this function to bind an existing OsclSharedPtr to a new (unwrapped) object. 129 void Bind(TheClass* ptr, OsclRefCounter* in_refcnt); 130 131 //! Use this function of unbind an existing OsclSharedPtr. 132 void Unbind() 133 { 134 Bind(NULL, NULL); 135 }; 136 137 //! Assignment operator. 138 OsclSharedPtr& operator=(const OsclSharedPtr& inSharedPtr) 139 { 140 Bind(inSharedPtr); 141 return *this; 142 } 143 144 //! Test for equality to see if two PVHandles wrap the same object. 145 bool operator==(const OsclSharedPtr& b) const; 146 147 private: 148 149 TheClass* mpRep; 150 OsclRefCounter* refcnt; 151 152}; 153 154 155template <class TheClass> inline bool OsclSharedPtr<TheClass>::operator==(const OsclSharedPtr<TheClass>& b) const 156{ 157 if ((this->mpRep == b.mpRep) && 158 (this->refcnt == b.refcnt)) 159 { 160 return true; 161 } 162 return false; 163} 164 165 166template <class TheClass> inline void OsclSharedPtr<TheClass>::Bind(const OsclSharedPtr& inSharedPtr) 167{ 168 if (mpRep == inSharedPtr.mpRep) return; 169 170 if (refcnt != NULL) 171 { 172 refcnt->removeRef(); 173 } 174 175 refcnt = inSharedPtr.refcnt; 176 mpRep = inSharedPtr.mpRep; 177 178 if (refcnt != NULL) 179 { 180 refcnt->addRef(); 181 } 182 183} 184 185template <class TheClass> inline void OsclSharedPtr<TheClass>::Bind(TheClass* ptr, 186 OsclRefCounter* in_refcnt) 187{ 188 if (refcnt != NULL) 189 { 190 refcnt->removeRef(); 191 } 192 193 mpRep = ptr; 194 refcnt = in_refcnt; 195 196} 197 198#endif // OSCL_SHARED_PTR_H_INCLUDED 199