You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.7 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: dotnetstl/util/dispose_functions.hpp
  3. *
  4. * Purpose: A useful tool for accessing a String object's content as a c-string.
  5. *
  6. * Created: 24th June 2003
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2003-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file dotnetstl/util/dispose_functions.hpp
  40. *
  41. * \brief [C++ only] Utility functions for handling object disposal
  42. * (\ref group__library__utility "Utility" Library).
  43. */
  44. #ifndef DOTNETSTL_INCL_DOTNETSTL_UTIL_HPP_DISPOSE_FUNCTIONS
  45. #define DOTNETSTL_INCL_DOTNETSTL_UTIL_HPP_DISPOSE_FUNCTIONS
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define DOTNETSTL_VER_DOTNETSTL_UTIL_HPP_DISPOSE_FUNCTIONS_MAJOR 4
  48. # define DOTNETSTL_VER_DOTNETSTL_UTIL_HPP_DISPOSE_FUNCTIONS_MINOR 0
  49. # define DOTNETSTL_VER_DOTNETSTL_UTIL_HPP_DISPOSE_FUNCTIONS_REVISION 1
  50. # define DOTNETSTL_VER_DOTNETSTL_UTIL_HPP_DISPOSE_FUNCTIONS_EDIT 31
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef DOTNETSTL_INCL_DOTNETSTL_HPP_DOTNETSTL
  56. # include <dotnetstl/dotnetstl.hpp>
  57. #endif /* !DOTNETSTL_INCL_DOTNETSTL_HPP_DOTNETSTL */
  58. /* ////////////////////////////////////////////////////////////////////// */
  59. #ifdef _STLSOFT_NO_NAMESPACE
  60. /* There is no stlsoft namespace, so must define ::dotnetstl */
  61. namespace dotnetstl
  62. {
  63. #else
  64. /* Define stlsoft::dotnet_project */
  65. namespace stlsoft
  66. {
  67. namespace dotnetstl_project
  68. {
  69. #endif /* _STLSOFT_NO_NAMESPACE */
  70. /* /////////////////////////////////////////////////////////////////////////
  71. * Classes
  72. */
  73. /** \brief Disposes the managed type, and resets the pointer
  74. *
  75. * \ingroup group__library__utility
  76. *
  77. * \param pt The pointer on which to call Dispose(). Can be null, but otherwise must be convertible to IDispose
  78. */
  79. template <ss_typename_param_k T>
  80. inline void dispose_set_null(T *&pt)
  81. {
  82. if(0 != pt)
  83. {
  84. System::IDisposable *disposable = pt;
  85. disposable->Dispose();
  86. pt = 0;
  87. }
  88. }
  89. /** \brief Disposes all the items in a container
  90. *
  91. * \ingroup group__library__utility
  92. *
  93. * \param pc The container on whose items to call Dispose().
  94. */
  95. template <ss_typename_param_k C>
  96. inline void dispose_contents(C *pc)
  97. {
  98. for(int i = 0, count = pc->get_Count(); i < count; ++i)
  99. {
  100. System::IDisposable *o = dynamic_cast<System::IDisposable*>(pc->get_Item(i));
  101. dispose_set_null(o);
  102. }
  103. }
  104. ////////////////////////////////////////////////////////////////////////////
  105. // Unit-testing
  106. #ifdef STLSOFT_UNITTEST
  107. # include "./unittest/dispose_functions_unittest_.h"
  108. #endif /* STLSOFT_UNITTEST */
  109. /* ////////////////////////////////////////////////////////////////////// */
  110. #ifdef _STLSOFT_NO_NAMESPACE
  111. } // namespace dotnetstl
  112. #else
  113. } // namespace dotnetstl_project
  114. } // namespace stlsoft
  115. #endif /* _STLSOFT_NO_NAMESPACE */
  116. /* ////////////////////////////////////////////////////////////////////// */
  117. #endif /* DOTNETSTL_INCL_DOTNETSTL_UTIL_HPP_DISPOSE_FUNCTIONS */
  118. /* ///////////////////////////// end of file //////////////////////////// */