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.

204 lines
6.9 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: dotnetstl/string/string_accessor.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/string/string_accessor.hpp
  40. *
  41. * \brief [C++ only; requires .NET] Definition of the
  42. * dotnetstl::c_string_accessor class template
  43. * (\ref group__library__string "String" Library).
  44. */
  45. #ifndef DOTNETSTL_INCL_DOTNETSTL_STRING_HPP_STRING_ACCESSOR
  46. #define DOTNETSTL_INCL_DOTNETSTL_STRING_HPP_STRING_ACCESSOR
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. #define DOTNETSTL_VER_DOTNETSTL_STRING_HPP_STRING_ACCESSOR_MAJOR 4
  49. #define DOTNETSTL_VER_DOTNETSTL_STRING_HPP_STRING_ACCESSOR_MINOR 0
  50. #define DOTNETSTL_VER_DOTNETSTL_STRING_HPP_STRING_ACCESSOR_REVISION 2
  51. #define DOTNETSTL_VER_DOTNETSTL_STRING_HPP_STRING_ACCESSOR_EDIT 47
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef DOTNETSTL_INCL_DOTNETSTL_HPP_DOTNETSTL
  57. # include <dotnetstl/dotnetstl.hpp>
  58. #endif /* !DOTNETSTL_INCL_DOTNETSTL_HPP_DOTNETSTL */
  59. #ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_SAP_CAST
  60. # include <stlsoft/conversion/sap_cast.hpp>
  61. #endif /* !STLSOFT_INCL_STLSOFT_CONVERSION_HPP_SAP_CAST */
  62. #ifdef STLSOFT_UNITTEST
  63. # include <string.h>
  64. # include <wchar.h>
  65. #endif /* STLSOFT_UNITTEST */
  66. /* ////////////////////////////////////////////////////////////////////// */
  67. #ifdef _STLSOFT_NO_NAMESPACE
  68. /* There is no stlsoft namespace, so must define ::dotnetstl */
  69. namespace dotnetstl
  70. {
  71. #else
  72. /* Define stlsoft::dotnet_project */
  73. namespace stlsoft
  74. {
  75. namespace dotnetstl_project
  76. {
  77. #endif /* _STLSOFT_NO_NAMESPACE */
  78. /* /////////////////////////////////////////////////////////////////////////
  79. * Classes
  80. */
  81. /** \brief Template providing scoped C-string access to a String
  82. *
  83. * \ingroup group__library__string
  84. */
  85. template <ss_typename_param_k C>
  86. class c_string_accessor
  87. {
  88. public:
  89. typedef C char_type;
  90. typedef C* pointer;
  91. typedef C const* const_pointer;
  92. typedef c_string_accessor<C> class_type;
  93. #if defined(DOTNETSTL_HAT_SYNTAX_SUPPORT)
  94. typedef System::String const ^string_pointer_const_type_;
  95. typedef System::String ^string_pointer_type_;
  96. #else /* ? DOTNETSTL_HAT_SYNTAX_SUPPORT */
  97. typedef System::String const* string_pointer_const_type_;
  98. typedef System::String* string_pointer_type_;
  99. #endif /* DOTNETSTL_HAT_SYNTAX_SUPPORT */
  100. // Construction
  101. public:
  102. /// Construct from a System::String
  103. ///
  104. /// \param s The String for which C-string access is to be provided. Must not be NULL
  105. ss_explicit_k c_string_accessor(string_pointer_const_type_ s)
  106. : m_h(get_cstring_(s))
  107. {}
  108. /// Release resources
  109. ~c_string_accessor() stlsoft_throw_0()
  110. {
  111. System::Runtime::InteropServices::Marshal::FreeHGlobal(m_h);
  112. }
  113. c_string_accessor(c_string_accessor& rhs)
  114. : m_h(rhs.m_h)
  115. {
  116. rhs.m_h = 0;
  117. }
  118. // Accessors
  119. public:
  120. /// Implicit conversion operator to a C-string
  121. operator const_pointer() const
  122. {
  123. #if defined(DOTNETSTL_HAT_SYNTAX_SUPPORT)
  124. IntPtr h = m_h;
  125. return static_cast<const_pointer>(h.ToPointer());
  126. #else /* ? DOTNETSTL_HAT_SYNTAX_SUPPORT */
  127. return m_h;
  128. #endif /* DOTNETSTL_HAT_SYNTAX_SUPPORT */
  129. }
  130. // Implementation
  131. private:
  132. pointer get_cstring_(string_pointer_const_type_ s);
  133. // Members
  134. private:
  135. #if defined(DOTNETSTL_HAT_SYNTAX_SUPPORT)
  136. System::IntPtr m_h;
  137. #else /* ? DOTNETSTL_HAT_SYNTAX_SUPPORT */
  138. pointer m_h;
  139. #endif /* DOTNETSTL_HAT_SYNTAX_SUPPORT */
  140. // Not to be implemented
  141. private:
  142. c_string_accessor& operator =(class_type const&);
  143. };
  144. ////////////////////////////////////////////////////////////////////////////
  145. // Implementation
  146. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  147. STLSOFT_TEMPLATE_SPECIALISATION
  148. inline c_string_accessor<ds_char_a_t>::pointer c_string_accessor<ds_char_a_t>::get_cstring_(string_pointer_const_type_ s)
  149. {
  150. return sap_cast<ds_char_a_t*>(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(const_cast<string_pointer_type_>(s)).ToPointer());
  151. }
  152. STLSOFT_TEMPLATE_SPECIALISATION
  153. inline c_string_accessor<ds_char_w_t>::pointer c_string_accessor<ds_char_w_t>::get_cstring_(string_pointer_const_type_ s)
  154. {
  155. return sap_cast<ds_char_w_t*>(System::Runtime::InteropServices::Marshal::StringToHGlobalUni(const_cast<string_pointer_type_>(s)).ToPointer());
  156. }
  157. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  158. ////////////////////////////////////////////////////////////////////////////
  159. // Unit-testing
  160. #ifdef STLSOFT_UNITTEST
  161. # include "./unittest/string_accessor_unittest_.h"
  162. #endif /* STLSOFT_UNITTEST */
  163. /* ////////////////////////////////////////////////////////////////////// */
  164. #ifdef _STLSOFT_NO_NAMESPACE
  165. } // namespace dotnetstl
  166. #else
  167. } // namespace dotnetstl_project
  168. } // namespace stlsoft
  169. #endif /* _STLSOFT_NO_NAMESPACE */
  170. /* ////////////////////////////////////////////////////////////////////// */
  171. #endif /* DOTNETSTL_INCL_DOTNETSTL_STRING_HPP_STRING_ACCESSOR */
  172. /* ///////////////////////////// end of file //////////////////////////// */