00001 /****************************************************************************** 00002 * 00003 * package: Log4Qt 00004 * file: logobjectptr.h 00005 * created: September 2007 00006 * author: Martin Heinrich 00007 * 00008 * 00009 * Copyright 2007 Martin Heinrich 00010 * 00011 * Licensed under the Apache License, Version 2.0 (the "License"); 00012 * you may not use this file except in compliance with the License. 00013 * You may obtain a copy of the License at 00014 * 00015 * http://www.apache.org/licenses/LICENSE-2.0 00016 * 00017 * Unless required by applicable law or agreed to in writing, software 00018 * distributed under the License is distributed on an "AS IS" BASIS, 00019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00020 * See the License for the specific language governing permissions and 00021 * limitations under the License. 00022 * 00023 ******************************************************************************/ 00024 00025 #ifndef LOG4QT_LOGOBJECTPTR_H 00026 #define LOG4QT_LOGOBJECTPTR_H 00027 00028 00029 /****************************************************************************** 00030 * Dependencies 00031 ******************************************************************************/ 00032 00033 #include "log4qt/helpers/logobject.h" 00034 00035 namespace Log4Qt 00036 { 00041 template <class T> 00042 class LogObjectPtr 00043 { 00044 public: 00048 LogObjectPtr(); 00049 00055 LogObjectPtr(const LogObjectPtr<T> &rOther); 00056 00062 LogObjectPtr(T *pLogObject); 00063 00071 LogObjectPtr<T> &operator=(const LogObjectPtr<T> &rOther); 00072 00077 ~LogObjectPtr(); 00078 00086 LogObjectPtr<T> &operator=(T *pLogObject); 00087 00091 T *operator->() const; 00092 00097 T &operator*() const; 00098 00103 operator T*() const; 00104 00105 private: 00106 void retain() const; 00107 void release() const; 00108 00109 private: 00110 T *mpLogObject; 00111 }; 00112 00113 00114 /************************************************************************** 00115 * Operators, Helper 00116 **************************************************************************/ 00117 00118 00119 /************************************************************************** 00120 * Inline 00121 **************************************************************************/ 00122 00123 template <class T> 00124 inline LogObjectPtr<T>::LogObjectPtr() : 00125 mpLogObject(0) 00126 {} 00127 00128 template <class T> 00129 inline LogObjectPtr<T>::LogObjectPtr(const LogObjectPtr<T> &rOther) : 00130 mpLogObject(rOther.mpLogObject) 00131 { retain(); } 00132 00133 template <class T> 00134 inline LogObjectPtr<T>::LogObjectPtr(T *pLogObject) : 00135 mpLogObject(pLogObject) 00136 { retain(); } 00137 00138 template <class T> 00139 inline LogObjectPtr<T> &LogObjectPtr<T>::operator=(const LogObjectPtr<T> &rOther) 00140 { rOther.retain(); 00141 release(); 00142 mpLogObject = rOther.mpLogObject; 00143 return *this; } 00144 00145 template <class T> 00146 inline LogObjectPtr<T>::~LogObjectPtr() 00147 { release(); } 00148 00149 template <class T> 00150 inline LogObjectPtr<T> &LogObjectPtr<T>::operator=(T *pLogObject) 00151 { if (pLogObject) 00152 reinterpret_cast<LogObject *>(pLogObject)->retain(); 00153 release(); 00154 mpLogObject = pLogObject; 00155 return *this; } 00156 00157 template <class T> 00158 inline T *LogObjectPtr<T>::operator->() const 00159 { return mpLogObject; } 00160 00161 template <class T> 00162 inline T &LogObjectPtr<T>::operator*() const 00163 { return *mpLogObject; } 00164 00165 template <class T> 00166 inline LogObjectPtr<T>::operator T*() const 00167 { return mpLogObject; } 00168 00169 template <class T> 00170 inline void LogObjectPtr<T>::retain() const 00171 { if (mpLogObject) 00172 reinterpret_cast<LogObject *>(mpLogObject)->retain(); } 00173 00174 template <class T> 00175 inline void LogObjectPtr<T>::release() const 00176 { 00177 if (mpLogObject) 00178 reinterpret_cast<LogObject *>(mpLogObject)->release(); 00179 } 00180 00181 } // namespace Log4Qt 00182 00183 00184 //Q_DECLARE_TYPEINFO(Log4Qt::LogObjectPtr<T>, Q_MOVABLE_TYPE); // Declare within T 00185 00186 00187 #endif // LOG4QT_LOGOBJECTPTR_H