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  *   Alec Flett <alecf@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 /*
00040 
00041   DO NOT USE THIS. IT IS INTENDED FOR TEMPORARY USE BY GLOBAL HISTORY,
00042   PENDING CONVERSION OF THE MDB INTERFACES TO XPCOM.
00043 
00044 */
00045 
00046 
00047 #ifndef nsMdbPtr_h__
00048 #define nsMdbPtr_h__
00049 
00050 #include "mdb.h"
00051 
00052 template <class T>
00053 class nsMdbDerivedSafe : public T
00054 {
00055 private:
00056     virtual mdb_err AddStrongRef(nsIMdbEnv* aEnv) { return 0; }        // NOT TO BE IMPLEMENTED
00057     virtual mdb_err CutStrongRef(nsIMdbEnv* aEnv) { return 0; }        // NOT TO BE IMPLEMENTED
00058     virtual nsMdbDerivedSafe<T>& operator=(const T&) { return *this; } // NOT TO BE IMPLEMENTED
00059     void operator delete(void*, size_t) {}                             // NOT TO BE IMPLEMENTED
00060 };
00061 
00062 
00063 template <class T>
00064 class nsMdbPtr
00065 {
00066 private:
00067     nsIMdbEnv* mEnv;
00068     T* mRawPtr;
00069 
00070 public:
00071     nsMdbPtr(nsIMdbEnv* aEnv) : mEnv(aEnv), mRawPtr(0)
00072     {
00073         NS_PRECONDITION(aEnv != 0, "null ptr");
00074     }
00075 
00076     nsMdbPtr(nsIMdbEnv* aEnv, T* aRawPtr) : mEnv(aEnv), mRawPtr(0)
00077     {
00078         NS_PRECONDITION(aEnv != 0, "null ptr");
00079         if (mEnv) {
00080             if (aRawPtr) {
00081                 mRawPtr = aRawPtr;
00082                 mRawPtr->AddStrongRef(mEnv);
00083             }
00084         }
00085     }
00086 
00087     nsMdbPtr(const nsMdbPtr<T>& aSmartPtr) : mEnv(aSmartPtr.mEnv), mRawPtr(0)
00088     {
00089         if (mEnv) {
00090             if (aSmartPtr) {
00091                 mRawPtr = aSmartPtr;
00092                 mRawPtr->AddStrongRef(mEnv);
00093             }
00094         }
00095     }
00096 
00097     nsMdbPtr<T>&
00098     operator=(const nsMdbPtr<T>& aSmartPtr)
00099     {
00100         if (mEnv) {
00101             if (mRawPtr) {
00102                 mRawPtr->CutStrongRef(mEnv);
00103                 mRawPtr = 0;
00104             }
00105         }
00106         mEnv = aSmartPtr.mEnv;
00107         if (mEnv) {
00108             mRawPtr = aSmartPtr.mRawPtr;
00109             if (mRawPtr)
00110                 mRawPtr->AddStrongRef(mEnv);
00111         }
00112 
00113         return *this;
00114     }
00115 
00116     ~nsMdbPtr()
00117     {
00118         if (mEnv) {
00119             if (mRawPtr)
00120                 mRawPtr->CutStrongRef(mEnv);
00121         }
00122     }
00123 
00124     nsMdbDerivedSafe<T>*
00125     get() const
00126     {
00127         return NS_REINTERPRET_CAST(nsMdbDerivedSafe<T>*, mRawPtr);
00128     }
00129 
00130     nsMdbDerivedSafe<T>*
00131     operator->() const
00132     {
00133         return get();
00134     }
00135 
00136 
00137     nsMdbDerivedSafe<T>&
00138     operator*() const
00139     {
00140         return *get();
00141     }
00142 
00143     operator nsMdbDerivedSafe<T>*() const
00144     {
00145         return get();
00146     }
00147 
00148 
00149     T**
00150     StartAssignment()
00151     {
00152         if (mRawPtr) {
00153             mRawPtr->CutStrongRef(mEnv);
00154             mRawPtr = 0;
00155         }
00156         return &mRawPtr;
00157     }
00158 };
00159 
00160 template <class T, class U>
00161 inline
00162 PRBool
00163 operator==(const nsMdbPtr<T>& lhs, const nsMdbPtr<U>& rhs)
00164 {
00165     return NS_STATIC_CAST(const void*, lhs.get()) == NS_STATIC_CAST(const void*, rhs.get());
00166 }
00167 
00168 template <class T, class U>
00169 inline
00170 PRBool
00171 operator==(const nsMdbPtr<T>& lhs, const U* rhs)
00172 {
00173     return NS_STATIC_CAST(const void*, lhs.get()) == NS_STATIC_CAST(const void*, rhs);
00174 }
00175 
00176 
00177 template <class T, class U>
00178 inline
00179 PRBool
00180 operator==(const U* lhs, const nsMdbPtr<T>& rhs)
00181 {
00182     return NS_STATIC_CAST(const void*, lhs) == NS_STATIC_CAST(const void*, rhs.get());
00183 }
00184 
00185 
00186 
00187 
00188 template <class T, class U>
00189 inline
00190 PRBool
00191 operator!=(const nsMdbPtr<T>& lhs, const nsMdbPtr<U>& rhs)
00192 {
00193     return NS_STATIC_CAST(const void*, lhs.get()) != NS_STATIC_CAST(const void*, rhs.get());
00194 }
00195 
00196 template <class T, class U>
00197 inline
00198 PRBool
00199 operator!=(const nsMdbPtr<T>& lhs, const U* rhs)
00200 {
00201     return NS_STATIC_CAST(const void*, lhs.get()) != NS_STATIC_CAST(const void*, rhs);
00202 }
00203 
00204 
00205 template <class T, class U>
00206 inline
00207 PRBool
00208 operator!=(const U* lhs, const nsMdbPtr<T>& rhs)
00209 {
00210     return NS_STATIC_CAST(const void*, lhs) != NS_STATIC_CAST(const void*, rhs.get());
00211 }
00212 
00213 
00214 
00215 template <class T>
00216 class nsGetterAcquires
00217 {
00218 private:
00219     nsMdbPtr<T>& mTargetSmartPtr;
00220 
00221 public:
00222     explicit
00223     nsGetterAcquires(nsMdbPtr<T>& aSmartPtr) : mTargetSmartPtr(aSmartPtr)
00224     {
00225         // nothing else to do
00226     }
00227 
00228     operator T**()
00229     {
00230         return mTargetSmartPtr.StartAssignment();
00231     }        
00232 };
00233 
00234 
00235 template <class T>
00236 inline
00237 nsGetterAcquires<T>
00238 getter_Acquires(nsMdbPtr<T>& aSmartPtr)
00239 {
00240     return nsGetterAcquires<T>(aSmartPtr);
00241 }
00242 
00243 
00244 #endif // nsMdbPtr_h__
00245