00001 /* ///////////////////////////////////////////////////////////////////////// 00002 * File: mfcstl/string/grab_cstring_buffer.hpp (originally MFGrbStr.h: ::SynesisMfc) 00003 * 00004 * Purpose: CString Get/ReleaseBuffer scoping class. 00005 * 00006 * Created: 12th February 1999 00007 * Updated: 10th August 2009 00008 * 00009 * Home: http://stlsoft.org/ 00010 * 00011 * Copyright (c) 1999-2009, Matthew Wilson and Synesis Software 00012 * All rights reserved. 00013 * 00014 * Redistribution and use in source and binary forms, with or without 00015 * modification, are permitted provided that the following conditions are met: 00016 * 00017 * - Redistributions of source code must retain the above copyright notice, this 00018 * list of conditions and the following disclaimer. 00019 * - Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of 00023 * any contributors may be used to endorse or promote products derived from 00024 * this software without specific prior written permission. 00025 * 00026 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00027 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00028 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00029 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00030 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00031 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00032 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00033 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00034 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00035 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00036 * POSSIBILITY OF SUCH DAMAGE. 00037 * 00038 * ////////////////////////////////////////////////////////////////////// */ 00039 00040 00047 #ifndef MFCSTL_INCL_MFCSTL_STRING_HPP_GRAB_CSTRING_BUFFER 00048 #define MFCSTL_INCL_MFCSTL_STRING_HPP_GRAB_CSTRING_BUFFER 00049 00050 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION 00051 # define MFCSTL_VER_MFCSTL_STRING_HPP_GRAB_CSTRING_BUFFER_MAJOR 4 00052 # define MFCSTL_VER_MFCSTL_STRING_HPP_GRAB_CSTRING_BUFFER_MINOR 0 00053 # define MFCSTL_VER_MFCSTL_STRING_HPP_GRAB_CSTRING_BUFFER_REVISION 1 00054 # define MFCSTL_VER_MFCSTL_STRING_HPP_GRAB_CSTRING_BUFFER_EDIT 59 00055 #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00056 00057 /* ///////////////////////////////////////////////////////////////////////// 00058 * Includes 00059 */ 00060 00061 #ifndef MFCSTL_INCL_MFCSTL_HPP_MFCSTL 00062 # include <mfcstl/mfcstl.hpp> 00063 #endif /* !MFCSTL_INCL_MFCSTL_HPP_MFCSTL */ 00064 00065 /* ///////////////////////////////////////////////////////////////////////// 00066 * Namespace 00067 */ 00068 00069 #ifndef _MFCSTL_NO_NAMESPACE 00070 # if defined(_STLSOFT_NO_NAMESPACE) || \ 00071 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION) 00072 /* There is no stlsoft namespace, so must define ::mfcstl */ 00073 namespace mfcstl 00074 { 00075 # else 00076 /* Define stlsoft::mfcstl_project */ 00077 00078 namespace stlsoft 00079 { 00080 00081 namespace mfcstl_project 00082 { 00083 00084 # endif /* _STLSOFT_NO_NAMESPACE */ 00085 #endif /* !_MFCSTL_NO_NAMESPACE */ 00086 00087 /* ///////////////////////////////////////////////////////////////////////// 00088 * grab_cstring_buffer 00089 * 00090 * This class is used to scope the aquisition and release of a CString buffer 00091 * ensuring that ReleaseBuffer() is called on the given CString object. 00092 */ 00093 00101 class grab_cstring_buffer 00102 { 00103 private: 00104 typedef grab_cstring_buffer class_type; 00105 00106 // Construction 00107 public: 00112 grab_cstring_buffer(CString &str, int length) stlsoft_throw_1(CMemoryException *); 00114 ~grab_cstring_buffer() stlsoft_throw_0(); 00115 00116 // Conversion operators 00117 public: 00119 LPCTSTR c_str() const; 00120 00122 operator LPTSTR(); 00123 #if !defined(STLSOFT_COMPILER_IS_BORLAND) 00125 operator LPCTSTR() const; 00126 #endif /* compiler */ 00127 00128 // Attributes 00129 public: 00131 int length() const; 00133 int original_length() const; 00134 00135 // Members 00136 private: 00137 CString &m_str; 00138 const int m_len; 00139 const int m_originalLen; 00140 const LPTSTR m_psz; 00141 00142 // Not implemented 00143 private: 00144 grab_cstring_buffer(class_type const& rhs); 00145 class_type& operator =(class_type const& rhs); 00146 }; 00147 00149 // Unit-testing 00150 00151 #ifdef STLSOFT_UNITTEST 00152 # include "./unittest/grab_cstring_buffer_unittest_.h" 00153 #endif /* STLSOFT_UNITTEST */ 00154 00156 // Implementation 00157 00158 inline grab_cstring_buffer::grab_cstring_buffer(CString &str, int length) stlsoft_throw_1(CMemoryException *) 00159 : m_str(str) 00160 , m_len(length) 00161 , m_originalLen(str.GetLength()) 00162 , m_psz(str.GetBuffer(length)) 00163 { 00164 // Fortunately, objects of this class contain no resources of their own, and so 00165 // throwing an exception from the constructor is somewhat benign. 00166 00167 // Newer versions of MFC do not append a nul character to the end of the reallocated 00168 // area, so we do that now 00169 m_psz[m_len] = '\0'; 00170 } 00171 00172 inline grab_cstring_buffer::~grab_cstring_buffer() stlsoft_throw_0() 00173 { 00174 // Best to check that the end character has not been overwritten 00175 MFCSTL_MESSAGE_ASSERT("The client code has overwritten the managed area of the grab_cstring_buffer instance", '\0' == m_psz[m_len]); 00176 00177 // Dtor will never be called if GetBufferSetLength throws, 00178 // so we can assume we have a valid reference to an open 00179 // string which needs to be released here. 00180 m_str.ReleaseBuffer(); 00181 } 00182 00183 inline LPCTSTR grab_cstring_buffer::c_str() const 00184 { 00185 return m_psz; 00186 } 00187 00188 inline grab_cstring_buffer::operator LPTSTR() 00189 { 00190 return m_psz; 00191 } 00192 00193 #if !defined(STLSOFT_COMPILER_IS_BORLAND) 00194 inline grab_cstring_buffer::operator LPCTSTR() const 00195 { 00196 return m_psz; 00197 } 00198 #endif /* compiler */ 00199 00200 inline int grab_cstring_buffer::length() const 00201 { 00202 return m_len; 00203 } 00204 00205 inline int grab_cstring_buffer::original_length() const 00206 { 00207 return m_originalLen; 00208 } 00209 00210 /* ////////////////////////////////////////////////////////////////////// */ 00211 00212 #ifndef _MFCSTL_NO_NAMESPACE 00213 # if defined(_STLSOFT_NO_NAMESPACE) || \ 00214 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION) 00215 } // namespace mfcstl 00216 # else 00217 } // namespace mfcstl_project 00218 } // namespace stlsoft 00219 # endif /* _STLSOFT_NO_NAMESPACE */ 00220 #endif /* !_MFCSTL_NO_NAMESPACE */ 00221 00222 /* ////////////////////////////////////////////////////////////////////// */ 00223 00224 #endif /* !MFCSTL_INCL_MFCSTL_STRING_HPP_GRAB_CSTRING_BUFFER */ 00225 00226 /* ///////////////////////////// end of file //////////////////////////// */