Back to index

lightning-sunbird  0.9+nobinonly
nsMdbPtr.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039 
00040   DO NOT USE THIS. IT IS INTENDED FOR TEMPORARY USE BY GLOBAL HISTORY,
00041   PENDING CONVERSION OF THE MDB INTERFACES TO XPCOM.
00042 
00043 */
00044 
00045 
00046 #ifndef nsMdbPtr_h__
00047 #define nsMdbPtr_h__
00048 
00049 #include "mdb.h"
00050 
00051 template <class T>
00052 class nsMdbDerivedSafe : public T
00053 {
00054 private:
00055     virtual mdb_err AddStrongRef(nsIMdbEnv* aEnv) { return 0; }        // NOT TO BE IMPLEMENTED
00056     virtual mdb_err CutStrongRef(nsIMdbEnv* aEnv) { return 0; }        // NOT TO BE IMPLEMENTED
00057     virtual nsMdbDerivedSafe<T>& operator=(const T&) { return *this; } // NOT TO BE IMPLEMENTED
00058     void operator delete(void*, size_t) {}                             // NOT TO BE IMPLEMENTED
00059 };
00060 
00061 
00062 template <class T>
00063 class nsMdbPtr
00064 {
00065 private:
00066     nsIMdbEnv* mEnv;
00067     T* mRawPtr;
00068 
00069 public:
00070     nsMdbPtr(nsIMdbEnv* aEnv) : mEnv(aEnv), mRawPtr(0)
00071     {
00072         NS_PRECONDITION(aEnv != 0, "null ptr");
00073     }
00074 
00075     nsMdbPtr(nsIMdbEnv* aEnv, T* aRawPtr) : mEnv(aEnv), mRawPtr(0)
00076     {
00077         NS_PRECONDITION(aEnv != 0, "null ptr");
00078         if (mEnv) {
00079             if (aRawPtr) {
00080                 mRawPtr = aRawPtr;
00081                 mRawPtr->AddStrongRef(mEnv);
00082             }
00083         }
00084     }
00085 
00086     nsMdbPtr(const nsMdbPtr<T>& aSmartPtr) : mEnv(aSmartPtr.mEnv), mRawPtr(0)
00087     {
00088         if (mEnv) {
00089             if (aSmartPtr) {
00090                 mRawPtr = aSmartPtr;
00091                 mRawPtr->AddStrongRef(mEnv);
00092             }
00093         }
00094     }
00095 
00096     nsMdbPtr<T>&
00097     operator=(const nsMdbPtr<T>& aSmartPtr)
00098     {
00099         if (mEnv) {
00100             if (mRawPtr) {
00101                 mRawPtr->CutStrongRef(mEnv);
00102                 mRawPtr = 0;
00103             }
00104         }
00105         mEnv = aSmartPtr.mEnv;
00106         if (mEnv) {
00107             mRawPtr = aSmartPtr.mRawPtr;
00108             if (mRawPtr)
00109                 mRawPtr->AddStrongRef(mEnv);
00110         }
00111 
00112         return *this;
00113     }
00114 
00115     ~nsMdbPtr()
00116     {
00117         if (mEnv) {
00118             if (mRawPtr)
00119                 mRawPtr->CutStrongRef(mEnv);
00120         }
00121     }
00122 
00123     nsMdbDerivedSafe<T>*
00124     get() const
00125     {
00126         return NS_REINTERPRET_CAST(nsMdbDerivedSafe<T>*, mRawPtr);
00127     }
00128 
00129     nsMdbDerivedSafe<T>*
00130     operator->() const
00131     {
00132         return get();
00133     }
00134 
00135 
00136     nsMdbDerivedSafe<T>&
00137     operator*() const
00138     {
00139         return *get();
00140     }
00141 
00142     operator nsMdbDerivedSafe<T>*() const
00143     {
00144         return get();
00145     }
00146 
00147 
00148     T**
00149     StartAssignment()
00150     {
00151         if (mRawPtr) {
00152             mRawPtr->CutStrongRef(mEnv);
00153             mRawPtr = 0;
00154         }
00155         return &mRawPtr;
00156     }
00157 };
00158 
00159 template <class T, class U>
00160 inline
00161 PRBool
00162 operator==(const nsMdbPtr<T>& lhs, const nsMdbPtr<U>& rhs)
00163 {
00164     return NS_STATIC_CAST(const void*, lhs.get()) == NS_STATIC_CAST(const void*, rhs.get());
00165 }
00166 
00167 template <class T, class U>
00168 inline
00169 PRBool
00170 operator==(const nsMdbPtr<T>& lhs, const U* rhs)
00171 {
00172     return NS_STATIC_CAST(const void*, lhs.get()) == NS_STATIC_CAST(const void*, rhs);
00173 }
00174 
00175 
00176 template <class T, class U>
00177 inline
00178 PRBool
00179 operator==(const U* lhs, const nsMdbPtr<T>& rhs)
00180 {
00181     return NS_STATIC_CAST(const void*, lhs) == NS_STATIC_CAST(const void*, rhs.get());
00182 }
00183 
00184 
00185 
00186 
00187 template <class T, class U>
00188 inline
00189 PRBool
00190 operator!=(const nsMdbPtr<T>& lhs, const nsMdbPtr<U>& rhs)
00191 {
00192     return NS_STATIC_CAST(const void*, lhs.get()) != NS_STATIC_CAST(const void*, rhs.get());
00193 }
00194 
00195 template <class T, class U>
00196 inline
00197 PRBool
00198 operator!=(const nsMdbPtr<T>& lhs, const U* rhs)
00199 {
00200     return NS_STATIC_CAST(const void*, lhs.get()) != NS_STATIC_CAST(const void*, rhs);
00201 }
00202 
00203 
00204 template <class T, class U>
00205 inline
00206 PRBool
00207 operator!=(const U* lhs, const nsMdbPtr<T>& rhs)
00208 {
00209     return NS_STATIC_CAST(const void*, lhs) != NS_STATIC_CAST(const void*, rhs.get());
00210 }
00211 
00212 
00213 
00214 template <class T>
00215 class nsGetterAcquires
00216 {
00217 private:
00218     nsMdbPtr<T>& mTargetSmartPtr;
00219 
00220 public:
00221     explicit
00222     nsGetterAcquires(nsMdbPtr<T>& aSmartPtr) : mTargetSmartPtr(aSmartPtr)
00223     {
00224         // nothing else to do
00225     }
00226 
00227     operator T**()
00228     {
00229         return mTargetSmartPtr.StartAssignment();
00230     }        
00231 };
00232 
00233 
00234 template <class T>
00235 inline
00236 nsGetterAcquires<T>
00237 getter_Acquires(nsMdbPtr<T>& aSmartPtr)
00238 {
00239     return nsGetterAcquires<T>(aSmartPtr);
00240 }
00241 
00242 
00243 #endif // nsMdbPtr_h__
00244