logobjectptr.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: logobjectptr.h
  5. * created: September 2007
  6. * author: Martin Heinrich
  7. *
  8. *
  9. * Copyright 2007 Martin Heinrich
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ******************************************************************************/
  24. #ifndef LOG4QT_LOGOBJECTPTR_H
  25. #define LOG4QT_LOGOBJECTPTR_H
  26. /******************************************************************************
  27. * Dependencies
  28. ******************************************************************************/
  29. #include "log4qt/helpers/logobject.h"
  30. namespace Log4Qt
  31. {
  32. /*!
  33. * \brief The class LogObjectPtr implements automatic reference counting
  34. * for LogObject objects.
  35. */
  36. template <class T>
  37. class LogObjectPtr
  38. {
  39. public:
  40. /*!
  41. * Constructs a 0 LogObject pointer.
  42. */
  43. LogObjectPtr();
  44. /*!
  45. * Constructs a LogObject pointer that points to the same object then
  46. * \a rOther. The reference counter of the object is incremented by
  47. * one.
  48. */
  49. LogObjectPtr(const LogObjectPtr<T> &rOther);
  50. /*!
  51. * Constructs a LogObject pointer that points to the object
  52. * \a LogObject. The reference counter of the object is incremented by
  53. * one.
  54. */
  55. LogObjectPtr(T *pLogObject);
  56. /*!
  57. * Assignment operator. Sets the LogObject pointer to point to the
  58. * same object that \a rOther points to. The reference counter of the
  59. * object the LogObjectPtr pointed to before the assignment is
  60. * decremented by one. The reference counter of the object \a rOther
  61. * is pointing to is incremented by one.
  62. */
  63. LogObjectPtr<T> &operator=(const LogObjectPtr<T> &rOther);
  64. /*!
  65. * Destructs the object. The reference counter of the object the
  66. * LogObjectPtr points to is decremented by one.
  67. */
  68. ~LogObjectPtr();
  69. /*!
  70. * Assignment operator. Sets the LogObject pointer to point to the
  71. * object \a pLogObject. The reference counter of the object the
  72. * LogObjectPtr pointed to before the assignment is decremented by
  73. * one. The reference counter of the object \a pLogObject is pointing
  74. * to is incremented by one.
  75. */
  76. LogObjectPtr<T> &operator=(T *pLogObject);
  77. /*!
  78. * Arrow operator. Returns the LogObject the object points to.
  79. */
  80. T *operator->() const;
  81. /*!
  82. * Dereference operator. Returns a pointer to the LogObject the
  83. * object points to.
  84. */
  85. T &operator*() const;
  86. /*!
  87. * Cast operator. Cast the object to the LogObject the object points
  88. * to.
  89. */
  90. operator T*() const;
  91. private:
  92. void retain() const;
  93. void release() const;
  94. private:
  95. T *mpLogObject;
  96. };
  97. /**************************************************************************
  98. * Operators, Helper
  99. **************************************************************************/
  100. /**************************************************************************
  101. * Inline
  102. **************************************************************************/
  103. template <class T>
  104. inline LogObjectPtr<T>::LogObjectPtr() :
  105. mpLogObject(0)
  106. {}
  107. template <class T>
  108. inline LogObjectPtr<T>::LogObjectPtr(const LogObjectPtr<T> &rOther) :
  109. mpLogObject(rOther.mpLogObject)
  110. { retain(); }
  111. template <class T>
  112. inline LogObjectPtr<T>::LogObjectPtr(T *pLogObject) :
  113. mpLogObject(pLogObject)
  114. { retain(); }
  115. template <class T>
  116. inline LogObjectPtr<T> &LogObjectPtr<T>::operator=(const LogObjectPtr<T> &rOther)
  117. { rOther.retain();
  118. release();
  119. mpLogObject = rOther.mpLogObject;
  120. return *this; }
  121. template <class T>
  122. inline LogObjectPtr<T>::~LogObjectPtr()
  123. { release(); }
  124. template <class T>
  125. inline LogObjectPtr<T> &LogObjectPtr<T>::operator=(T *pLogObject)
  126. { if (pLogObject)
  127. reinterpret_cast<LogObject *>(pLogObject)->retain();
  128. release();
  129. mpLogObject = pLogObject;
  130. return *this; }
  131. template <class T>
  132. inline T *LogObjectPtr<T>::operator->() const
  133. { return mpLogObject; }
  134. template <class T>
  135. inline T &LogObjectPtr<T>::operator*() const
  136. { return *mpLogObject; }
  137. template <class T>
  138. inline LogObjectPtr<T>::operator T*() const
  139. { return mpLogObject; }
  140. template <class T>
  141. inline void LogObjectPtr<T>::retain() const
  142. { if (mpLogObject)
  143. reinterpret_cast<LogObject *>(mpLogObject)->retain(); }
  144. template <class T>
  145. inline void LogObjectPtr<T>::release() const
  146. {
  147. if (mpLogObject)
  148. reinterpret_cast<LogObject *>(mpLogObject)->release();
  149. }
  150. } // namespace Log4Qt
  151. //Q_DECLARE_TYPEINFO(Log4Qt::LogObjectPtr<T>, Q_MOVABLE_TYPE); // Declare within T
  152. #endif // LOG4QT_LOGOBJECTPTR_H