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.

226 lines
7.4 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: mfcstl/string/grab_cstring_buffer.hpp (originally MFGrbStr.h: ::SynesisMfc)
  3. *
  4. * Purpose: CString Get/ReleaseBuffer scoping class.
  5. *
  6. * Created: 12th February 1999
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 1999-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 mfcstl/string/grab_cstring_buffer.hpp
  40. *
  41. * \brief [C++ only] Definition of the mfcstl::grab_cstring_buffer class
  42. * (\ref group__library__string "String" Library).
  43. */
  44. #ifndef MFCSTL_INCL_MFCSTL_STRING_HPP_GRAB_CSTRING_BUFFER
  45. #define MFCSTL_INCL_MFCSTL_STRING_HPP_GRAB_CSTRING_BUFFER
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define MFCSTL_VER_MFCSTL_STRING_HPP_GRAB_CSTRING_BUFFER_MAJOR 4
  48. # define MFCSTL_VER_MFCSTL_STRING_HPP_GRAB_CSTRING_BUFFER_MINOR 0
  49. # define MFCSTL_VER_MFCSTL_STRING_HPP_GRAB_CSTRING_BUFFER_REVISION 1
  50. # define MFCSTL_VER_MFCSTL_STRING_HPP_GRAB_CSTRING_BUFFER_EDIT 59
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef MFCSTL_INCL_MFCSTL_HPP_MFCSTL
  56. # include <mfcstl/mfcstl.hpp>
  57. #endif /* !MFCSTL_INCL_MFCSTL_HPP_MFCSTL */
  58. /* /////////////////////////////////////////////////////////////////////////
  59. * Namespace
  60. */
  61. #ifndef _MFCSTL_NO_NAMESPACE
  62. # if defined(_STLSOFT_NO_NAMESPACE) || \
  63. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  64. /* There is no stlsoft namespace, so must define ::mfcstl */
  65. namespace mfcstl
  66. {
  67. # else
  68. /* Define stlsoft::mfcstl_project */
  69. namespace stlsoft
  70. {
  71. namespace mfcstl_project
  72. {
  73. # endif /* _STLSOFT_NO_NAMESPACE */
  74. #endif /* !_MFCSTL_NO_NAMESPACE */
  75. /* /////////////////////////////////////////////////////////////////////////
  76. * grab_cstring_buffer
  77. *
  78. * This class is used to scope the aquisition and release of a CString buffer
  79. * ensuring that ReleaseBuffer() is called on the given CString object.
  80. */
  81. /** \brief Scopes the aquisition and release of a CString buffer
  82. *
  83. * \ingroup group__library__string
  84. *
  85. * This class is used to scope the aquisition and release of a CString buffer
  86. * ensuring that ReleaseBuffer() is called on the given CString object.
  87. */
  88. class grab_cstring_buffer
  89. {
  90. private:
  91. typedef grab_cstring_buffer class_type;
  92. // Construction
  93. public:
  94. /// \brief Acquires the requested length on the given managed string
  95. ///
  96. /// Calls \c GetBuffer(length) on the given string, after recording the
  97. /// original length. If acquisition fails then a CMemoryException is thrown.
  98. grab_cstring_buffer(CString &str, int length) stlsoft_throw_1(CMemoryException *);
  99. /// Releases the managed string.
  100. ~grab_cstring_buffer() stlsoft_throw_0();
  101. // Conversion operators
  102. public:
  103. /// Returns a pointer to a nul-terminated string
  104. LPCTSTR c_str() const;
  105. /// Provides mutating access to the managed string's internal buffer
  106. operator LPTSTR();
  107. #if !defined(STLSOFT_COMPILER_IS_BORLAND)
  108. /// Provides non-mutating access to the managed string's internal buffer
  109. operator LPCTSTR() const;
  110. #endif /* compiler */
  111. // Attributes
  112. public:
  113. /// Returns the length of the managed string
  114. int length() const;
  115. /// Returns the original length of the managed string
  116. int original_length() const;
  117. // Members
  118. private:
  119. CString &m_str;
  120. const int m_len;
  121. const int m_originalLen;
  122. const LPTSTR m_psz;
  123. // Not implemented
  124. private:
  125. grab_cstring_buffer(class_type const& rhs);
  126. class_type& operator =(class_type const& rhs);
  127. };
  128. ////////////////////////////////////////////////////////////////////////////
  129. // Unit-testing
  130. #ifdef STLSOFT_UNITTEST
  131. # include "./unittest/grab_cstring_buffer_unittest_.h"
  132. #endif /* STLSOFT_UNITTEST */
  133. ////////////////////////////////////////////////////////////////////////////
  134. // Implementation
  135. inline grab_cstring_buffer::grab_cstring_buffer(CString &str, int length) stlsoft_throw_1(CMemoryException *)
  136. : m_str(str)
  137. , m_len(length)
  138. , m_originalLen(str.GetLength())
  139. , m_psz(str.GetBuffer(length))
  140. {
  141. // Fortunately, objects of this class contain no resources of their own, and so
  142. // throwing an exception from the constructor is somewhat benign.
  143. // Newer versions of MFC do not append a nul character to the end of the reallocated
  144. // area, so we do that now
  145. m_psz[m_len] = '\0';
  146. }
  147. inline grab_cstring_buffer::~grab_cstring_buffer() stlsoft_throw_0()
  148. {
  149. // Best to check that the end character has not been overwritten
  150. MFCSTL_MESSAGE_ASSERT("The client code has overwritten the managed area of the grab_cstring_buffer instance", '\0' == m_psz[m_len]);
  151. // Dtor will never be called if GetBufferSetLength throws,
  152. // so we can assume we have a valid reference to an open
  153. // string which needs to be released here.
  154. m_str.ReleaseBuffer();
  155. }
  156. inline LPCTSTR grab_cstring_buffer::c_str() const
  157. {
  158. return m_psz;
  159. }
  160. inline grab_cstring_buffer::operator LPTSTR()
  161. {
  162. return m_psz;
  163. }
  164. #if !defined(STLSOFT_COMPILER_IS_BORLAND)
  165. inline grab_cstring_buffer::operator LPCTSTR() const
  166. {
  167. return m_psz;
  168. }
  169. #endif /* compiler */
  170. inline int grab_cstring_buffer::length() const
  171. {
  172. return m_len;
  173. }
  174. inline int grab_cstring_buffer::original_length() const
  175. {
  176. return m_originalLen;
  177. }
  178. /* ////////////////////////////////////////////////////////////////////// */
  179. #ifndef _MFCSTL_NO_NAMESPACE
  180. # if defined(_STLSOFT_NO_NAMESPACE) || \
  181. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  182. } // namespace mfcstl
  183. # else
  184. } // namespace mfcstl_project
  185. } // namespace stlsoft
  186. # endif /* _STLSOFT_NO_NAMESPACE */
  187. #endif /* !_MFCSTL_NO_NAMESPACE */
  188. /* ////////////////////////////////////////////////////////////////////// */
  189. #endif /* !MFCSTL_INCL_MFCSTL_STRING_HPP_GRAB_CSTRING_BUFFER */
  190. /* ///////////////////////////// end of file //////////////////////////// */