GME  13
CountedPointer.c
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (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  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: CountedPointer.c 471747 2006-11-06 14:31:56Z amassari $
00020  */
00021 
00022 
00023 // ---------------------------------------------------------------------------
00024 //  Includes
00025 // ---------------------------------------------------------------------------
00026 #if defined(XERCES_TMPLSINC)
00027 #include <xercesc/util/CountedPointer.hpp>
00028 #endif
00029 
00030 XERCES_CPP_NAMESPACE_BEGIN
00031 
00032 // ---------------------------------------------------------------------------
00033 //  CountedPointerTo: Constructors and Destructor
00034 // ---------------------------------------------------------------------------
00035 template <class T> CountedPointerTo<T>::
00036 CountedPointerTo(const CountedPointerTo<T>& toCopy) :
00037 
00038     fPtr(toCopy.fPtr)
00039 {
00040     if (fPtr)
00041         fPtr->addRef();
00042 }
00043 
00044 template <class T> CountedPointerTo<T>::CountedPointerTo(T* p) :
00045 
00046     fPtr(p)
00047 {
00048     if (fPtr)
00049         fPtr->addRef();
00050 }
00051 
00052 template <class T> CountedPointerTo<T>::~CountedPointerTo()
00053 {
00054     if (fPtr)
00055         fPtr->removeRef();
00056 }
00057 
00058 
00059 // ---------------------------------------------------------------------------
00060 //  CountedPointerTo: Operators
00061 // ---------------------------------------------------------------------------
00062 template <class T> CountedPointerTo<T>&
00063 CountedPointerTo<T>::operator=(const CountedPointerTo<T>& other)
00064 {
00065     if (this == &other)
00066         return *this;
00067 
00068     if (other.fPtr)
00069         other.fPtr->addRef();
00070 
00071     if (fPtr)
00072         fPtr->removeRef();
00073 
00074     fPtr = other.fPtr;
00075     return *this;
00076 }
00077 
00078 template <class T> CountedPointerTo<T>::operator T*()
00079 {
00080     return fPtr;
00081 }
00082 
00083 template <class T> const T* CountedPointerTo<T>::operator->() const
00084 {
00085     return fPtr;
00086 }
00087 
00088 template <class T> T* CountedPointerTo<T>::operator->()
00089 {
00090     return fPtr;
00091 }
00092 
00093 template <class T> const T& CountedPointerTo<T>::operator*() const
00094 {
00095     if (!fPtr)
00096         ThrowXMLwithMemMgr(NullPointerException, XMLExcepts::CPtr_PointerIsZero, 0);
00097     return *fPtr;
00098 }
00099 
00100 template <class T> T& CountedPointerTo<T>::operator*()
00101 {
00102     if (!fPtr)
00103         ThrowXMLwithMemMgr(NullPointerException, XMLExcepts::CPtr_PointerIsZero, 0);
00104     return *fPtr;
00105 }
00106 
00107 XERCES_CPP_NAMESPACE_END