cont_ptr_utils.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  2. // vim:tabstop=4:shiftwidth=4:expandtab:
  3. /*
  4. * Copyright (C) 2004-2008 Wu Yongwei <adah at users dot sourceforge dot net>
  5. *
  6. * This software is provided 'as-is', without any express or implied
  7. * warranty. In no event will the authors be held liable for any
  8. * damages arising from the use of this software.
  9. *
  10. * Permission is granted to anyone to use this software for any purpose,
  11. * including commercial applications, and to alter it and redistribute
  12. * it freely, subject to the following restrictions:
  13. *
  14. * 1. The origin of this software must not be misrepresented; you must
  15. * not claim that you wrote the original software. If you use this
  16. * software in a product, an acknowledgement in the product
  17. * documentation would be appreciated but is not required.
  18. * 2. Altered source versions must be plainly marked as such, and must
  19. * not be misrepresented as being the original software.
  20. * 3. This notice may not be removed or altered from any source
  21. * distribution.
  22. *
  23. * This file is part of Stones of Nvwa:
  24. * http://sourceforge.net/projects/nvwa
  25. *
  26. */
  27. /*!
  28. * \file cont_ptr_utils.h
  29. * \ingroup QxMemLeak
  30. *
  31. * Utility functors for containers of pointers (adapted from Scott
  32. * Meyers' <em>Effective STL</em>).
  33. *
  34. * \version 1.4, 2007/09/12
  35. * \author Wu Yongwei
  36. *
  37. */
  38. #ifndef QT_NO_DEBUG
  39. #ifndef _QX_MODE_RELEASE
  40. #if _QX_USE_MEM_LEAK_DETECTION
  41. #ifndef _CONT_PTR_UTILS_H
  42. #define _CONT_PTR_UTILS_H
  43. #ifdef _MSC_VER
  44. #pragma once
  45. #endif
  46. namespace qx {
  47. namespace memory {
  48. /**
  49. * Functor to return objects pointed by a container of pointers.
  50. *
  51. * A typical usage might be like:
  52. * @code
  53. * vector<Object*> v;
  54. * ...
  55. * transform(v.begin(), v.end(),
  56. * ostream_iterator<Object>(cout, " "),
  57. * dereference());
  58. * @endcode
  59. */
  60. struct QX_DLL_EXPORT dereference
  61. {
  62. template <typename _Tp>
  63. const _Tp& operator()(const _Tp* __ptr) const
  64. {
  65. return *__ptr;
  66. }
  67. };
  68. /**
  69. * Functor to compare objects pointed by a container of pointers.
  70. *
  71. * @code
  72. * vector<Object*> v;
  73. * ...
  74. * sort(v.begin(), v.end(), dereference_less());
  75. * @endcode
  76. * or
  77. * @code
  78. * set<Object*, dereference_less> s;
  79. * @endcode
  80. */
  81. struct QX_DLL_EXPORT dereference_less
  82. {
  83. template <typename _Pointer>
  84. bool operator()(_Pointer __ptr1, _Pointer __ptr2) const
  85. {
  86. return *__ptr1 < *__ptr2;
  87. }
  88. };
  89. /**
  90. * Functor to delete objects pointed by a container of pointers.
  91. *
  92. * A typical usage might be like:
  93. * @code
  94. * list<Object*> l;
  95. * ...
  96. * for_each(l.begin(), l.end(), delete_object());
  97. * @endcode
  98. */
  99. struct QX_DLL_EXPORT delete_object
  100. {
  101. template <typename _Pointer>
  102. void operator()(_Pointer __ptr) const
  103. {
  104. delete __ptr;
  105. }
  106. };
  107. /**
  108. * Functor to output objects pointed by a container of pointers.
  109. *
  110. * A typical usage might be like:
  111. * @code
  112. * list<Object*> l;
  113. * ...
  114. * for_each(l.begin(), l.end(), output_object<ostream>(cout, " "));
  115. * @endcode
  116. */
  117. template <typename _OutputStrm, typename _StringType = const char*>
  118. struct output_object
  119. {
  120. output_object(_OutputStrm& __outs, const _StringType& __sep)
  121. : _M_outs(__outs), _M_sep(__sep)
  122. {}
  123. template <typename _Tp>
  124. void operator()(const _Tp* __ptr) const
  125. {
  126. _M_outs << *__ptr << _M_sep;
  127. }
  128. private:
  129. _OutputStrm& _M_outs;
  130. _StringType _M_sep;
  131. };
  132. } // namespace memory
  133. } // namespace qx
  134. #endif // _CONT_PTR_UTILS_H
  135. #endif // _QX_USE_MEM_LEAK_DETECTION
  136. #endif // _QX_MODE_RELEASE
  137. #endif // QT_NO_DEBUG