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.

171 lines
5.1 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/error/errno_scope.hpp (originally MLTErrScp.h, ::SynesisStd)
  3. *
  4. * Purpose: errno scoping class.
  5. *
  6. * Created: 28th November 1998
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 1998-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 stlsoft/error/errno_scope.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::errno_scope class
  42. * (\ref group__library__error "Error" Library).
  43. */
  44. #ifndef STLSOFT_INCL_STLSOFT_ERROR_HPP_ERRNO_SCOPE
  45. #define STLSOFT_INCL_STLSOFT_ERROR_HPP_ERRNO_SCOPE
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define STLSOFT_VER_STLSOFT_ERROR_HPP_ERRNO_SCOPE_MAJOR 3
  48. # define STLSOFT_VER_STLSOFT_ERROR_HPP_ERRNO_SCOPE_MINOR 0
  49. # define STLSOFT_VER_STLSOFT_ERROR_HPP_ERRNO_SCOPE_REVISION 3
  50. # define STLSOFT_VER_STLSOFT_ERROR_HPP_ERRNO_SCOPE_EDIT 40
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Compatibility
  54. */
  55. /*
  56. [DocumentationStatus:Ready]
  57. */
  58. /* /////////////////////////////////////////////////////////////////////////
  59. * Includes
  60. */
  61. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  62. # include <stlsoft/stlsoft.h>
  63. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  64. #ifndef STLSOFT_INCL_H_ERRNO
  65. # define STLSOFT_INCL_H_ERRNO
  66. # include <errno.h>
  67. #endif /* !STLSOFT_INCL_H_ERRNO */
  68. /* /////////////////////////////////////////////////////////////////////////
  69. * Namespace
  70. */
  71. #ifndef _STLSOFT_NO_NAMESPACE
  72. namespace stlsoft
  73. {
  74. #endif /* !_STLSOFT_NO_NAMESPACE */
  75. /* /////////////////////////////////////////////////////////////////////////
  76. * Classes
  77. */
  78. /** \brief A \ref group__pattern__scoping_class "scoping class" that scopes
  79. * the thread's last error.
  80. *
  81. * \ingroup group__library__error
  82. *
  83. \code
  84. DWORD err = errno;
  85. { stlsoft::errno_scope scope; // Scope the error while we change it
  86. // Some code that changes (or may change) the last error
  87. . . .
  88. errno = ENOMEM; // ... we just do this for pedagogical purposes
  89. } // End of scope - error value replaced to former value
  90. assert(errno == err);
  91. \endcode
  92. */
  93. class errno_scope
  94. {
  95. public:
  96. typedef errno_scope class_type;
  97. /// \name Operations
  98. /// @{
  99. public:
  100. /// \brief Remembers the current value of <code>errno</code>.
  101. errno_scope() stlsoft_throw_0()
  102. : m_errno(errno)
  103. {}
  104. /// \brief Remembers the given value as the value of <code>errno</code>.
  105. ss_explicit_k errno_scope(int errno_) stlsoft_throw_0()
  106. : m_errno(errno_)
  107. {
  108. errno = m_errno;
  109. }
  110. /// \brief Replaces the remembered value of <code>errno</code>.
  111. ~errno_scope() stlsoft_throw_0()
  112. {
  113. errno = m_errno;
  114. }
  115. /// @}
  116. /// \name Operations
  117. /// @{
  118. public:
  119. /// \brief The remembered value of <code>errno</code>.
  120. operator int () const
  121. {
  122. return m_errno;
  123. }
  124. /// @}
  125. /// \name Members
  126. /// @{
  127. private:
  128. int m_errno;
  129. /// @}
  130. /// \name Not to be implemented
  131. /// @{
  132. private:
  133. errno_scope(errno_scope const&);
  134. errno_scope& operator =(errno_scope const&);
  135. /// @}
  136. };
  137. /* ////////////////////////////////////////////////////////////////////// */
  138. #ifndef _STLSOFT_NO_NAMESPACE
  139. } // namespace stlsoft
  140. #endif /* !_STLSOFT_NO_NAMESPACE */
  141. /* ////////////////////////////////////////////////////////////////////// */
  142. #endif /* !STLSOFT_INCL_STLSOFT_ERROR_HPP_ERRNO_SCOPE */
  143. /* ///////////////////////////// end of file //////////////////////////// */