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.

398 lines
12 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: mfcstl/string/resource_string.hpp (originally MWResStr.h: ::SynesisWin)
  3. *
  4. * Purpose: resource_string class.
  5. *
  6. * Created: 1st November 1994
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 1994-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/resource_string.hpp
  40. *
  41. * \brief [C++ only] Definition of the mfcstl::resource_string class
  42. * (\ref group__library__string "String" Library).
  43. */
  44. #ifndef MFCSTL_INCL_MFCSTL_STRING_HPP_RESOURCE_STRING
  45. #define MFCSTL_INCL_MFCSTL_STRING_HPP_RESOURCE_STRING
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define MFCSTL_VER_MFCSTL_STRING_HPP_RESOURCE_STRING_MAJOR 4
  48. # define MFCSTL_VER_MFCSTL_STRING_HPP_RESOURCE_STRING_MINOR 0
  49. # define MFCSTL_VER_MFCSTL_STRING_HPP_RESOURCE_STRING_REVISION 3
  50. # define MFCSTL_VER_MFCSTL_STRING_HPP_RESOURCE_STRING_EDIT 80
  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. #if defined(STLSOFT_COMPILER_IS_BORLAND)
  59. # pragma warn -8022 /* Suppresses "'f()' hides virtual function 'g()'" */
  60. # pragma warn -8084 /* Suppresses "Suggest parentheses to clarify precedence in function 'f()'" */
  61. #endif /* compiler */
  62. #ifndef STLSOFT_INCL_H_AFXWIN
  63. # define STLSOFT_INCL_H_AFXWIN
  64. # include <afxwin.h> // for AfxThrowResourceException()
  65. #endif /* !STLSOFT_INCL_H_AFXWIN */
  66. #if defined(STLSOFT_COMPILER_IS_BORLAND)
  67. # pragma warn .8022 /* Suppresses "'f()' hides virtual function 'g()'" */
  68. # pragma warn .8084 /* Suppresses "Suggest parentheses to clarify precedence in function 'f()'" */
  69. #endif /* compiler */
  70. #ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS
  71. # include <stlsoft/collections/util/collections.hpp>
  72. #endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS */
  73. /* /////////////////////////////////////////////////////////////////////////
  74. * Namespace
  75. */
  76. #ifndef _MFCSTL_NO_NAMESPACE
  77. # if defined(_STLSOFT_NO_NAMESPACE) || \
  78. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  79. /* There is no stlsoft namespace, so must define ::mfcstl */
  80. namespace mfcstl
  81. {
  82. # else
  83. /* Define stlsoft::mfcstl_project */
  84. namespace stlsoft
  85. {
  86. namespace mfcstl_project
  87. {
  88. # endif /* _STLSOFT_NO_NAMESPACE */
  89. #endif /* !_MFCSTL_NO_NAMESPACE */
  90. /* ////////////////////////////////////////////////////////////////////// */
  91. // resource_string
  92. /** \brief Instances of this class represent Windows string resources, and are
  93. * constructed from instance identifiers.
  94. *
  95. * \ingroup group__library__string
  96. */
  97. class resource_string
  98. : public CString
  99. , public stlsoft_ns_qual(stl_collection_tag)
  100. {
  101. private:
  102. typedef CString parent_class_type;
  103. typedef resource_string class_type;
  104. public:
  105. /// The type of the const (non-mutating) iterator
  106. typedef LPCTSTR const_iterator;
  107. /// The size type
  108. typedef UINT size_type;
  109. // Construction
  110. public:
  111. /// Constructs an around the string loaded from the given \c id
  112. ///
  113. /// \param id identifier of the string resource to load
  114. ss_explicit_k resource_string(ms_uint_t id) stlsoft_throw_2(CMemoryException*, CResourceException*);
  115. /// Constructs an around the string loaded from the given \c id and \c hinst
  116. ///
  117. /// \param hinst The module from which to load the string
  118. /// \param id identifier of the string resource to load
  119. resource_string(HINSTANCE hinst, ms_uint_t id) stlsoft_throw_2(CMemoryException*, CResourceException*);
  120. /// Copy constructor
  121. ///
  122. /// \param rhs The instance from which to copy-construct
  123. resource_string(resource_string const& rhs);
  124. /// Copy constructor
  125. ///
  126. /// \param rhs The instance from which to copy-construct
  127. resource_string(CString const& rhs);
  128. /// Copy assignment operator
  129. ///
  130. /// \param rhs The instance from which to copy-assign
  131. resource_string const& operator =(resource_string const& rhs);
  132. /// Copy assignment operator
  133. ///
  134. /// \param rhs The instance from which to copy-assign
  135. resource_string const& operator =(CString const& rhs);
  136. // Iteration
  137. public:
  138. /// Begins the iteration
  139. ///
  140. /// \return An iterator representing the start of the sequence
  141. const_iterator begin() const;
  142. /// Ends the iteration
  143. ///
  144. /// \return An iterator representing the end of the sequence
  145. const_iterator end() const;
  146. // Attributes
  147. public:
  148. /// Returns the number of elements in the sequence
  149. size_type size() const;
  150. /// Returns the number of elements in the sequence
  151. size_type length() const;
  152. /// Indicates whether the string is empty
  153. ms_bool_t empty() const;
  154. /// Returns a pointer to constant data representing the managed string
  155. LPCTSTR c_str() const;
  156. /// Returns a possibly unterminated pointer to constant data representing the managed string
  157. LPCTSTR data() const;
  158. };
  159. /* /////////////////////////////////////////////////////////////////////////
  160. * Shims
  161. */
  162. inline LPCTSTR c_str_ptr_null(resource_string const& s)
  163. {
  164. return s.empty() ? NULL : s.c_str();
  165. }
  166. #ifdef UNICODE
  167. inline LPCTSTR c_str_ptr_null_w(resource_string const& s)
  168. #else /* ? UNICODE */
  169. inline LPCTSTR c_str_ptr_null_a(resource_string const& s)
  170. #endif /* UNICODE */
  171. {
  172. return c_str_ptr_null(s);
  173. }
  174. inline LPCTSTR c_str_ptr(resource_string const& s)
  175. {
  176. return s.c_str();
  177. }
  178. #ifdef UNICODE
  179. inline LPCTSTR c_str_ptr_w(resource_string const& s)
  180. #else /* ? UNICODE */
  181. inline LPCTSTR c_str_ptr_a(resource_string const& s)
  182. #endif /* UNICODE */
  183. {
  184. return c_str_ptr(s);
  185. }
  186. inline LPCTSTR c_str_data(resource_string const& s)
  187. {
  188. return s.data();
  189. }
  190. #ifdef UNICODE
  191. inline LPCTSTR c_str_data_w(resource_string const& s)
  192. #else /* ? UNICODE */
  193. inline LPCTSTR c_str_data_a(resource_string const& s)
  194. #endif /* UNICODE */
  195. {
  196. return c_str_data(s);
  197. }
  198. inline ms_size_t c_str_len(resource_string const& s)
  199. {
  200. return s.length();
  201. }
  202. template<ss_typename_param_k S>
  203. inline S& operator <<(S& s, resource_string const& str)
  204. {
  205. s << str.c_str();
  206. return s;
  207. }
  208. ////////////////////////////////////////////////////////////////////////////
  209. // Unit-testing
  210. #ifdef STLSOFT_UNITTEST
  211. # include "./unittest/resource_string_unittest_.h"
  212. #endif /* STLSOFT_UNITTEST */
  213. ////////////////////////////////////////////////////////////////////////////
  214. // Implementation
  215. inline resource_string::resource_string(ms_uint_t id) stlsoft_throw_2(CMemoryException*, CResourceException*)
  216. {
  217. if(!parent_class_type::LoadString(id))
  218. {
  219. AfxThrowResourceException();
  220. }
  221. }
  222. inline resource_string::resource_string(HINSTANCE hinst, ms_uint_t id) stlsoft_throw_2(CMemoryException*, CResourceException*)
  223. {
  224. TCHAR sz[1024];
  225. if(0 == ::LoadString(hinst, id, sz, STLSOFT_NUM_ELEMENTS(sz)))
  226. {
  227. AfxThrowResourceException();
  228. }
  229. else
  230. {
  231. parent_class_type *pThis = this;
  232. pThis->operator =(sz);
  233. }
  234. }
  235. inline resource_string::resource_string(resource_string const& rhs)
  236. : parent_class_type(rhs)
  237. {}
  238. inline resource_string::resource_string(CString const& rhs)
  239. : parent_class_type(rhs)
  240. {}
  241. inline resource_string const& resource_string::operator =(resource_string const& rhs)
  242. {
  243. parent_class_type *pThis = this;
  244. pThis->operator =(rhs);
  245. return *this;
  246. }
  247. inline resource_string const& resource_string::operator =(CString const& rhs)
  248. {
  249. parent_class_type *pThis = this;
  250. pThis->operator =(rhs);
  251. return *this;
  252. }
  253. inline resource_string::const_iterator resource_string::begin() const
  254. {
  255. return *this;
  256. }
  257. inline resource_string::const_iterator resource_string::end() const
  258. {
  259. return begin() + length();
  260. }
  261. inline resource_string::size_type resource_string::size() const
  262. {
  263. return GetLength();
  264. }
  265. inline resource_string::size_type resource_string::length() const
  266. {
  267. return GetLength();
  268. }
  269. inline ms_bool_t resource_string::empty() const
  270. {
  271. return 0 == length();
  272. }
  273. inline LPCTSTR resource_string::c_str() const
  274. {
  275. return *this;
  276. }
  277. inline LPCTSTR resource_string::data() const
  278. {
  279. return c_str();
  280. }
  281. /* ////////////////////////////////////////////////////////////////////// */
  282. #ifndef _MFCSTL_NO_NAMESPACE
  283. # if defined(_STLSOFT_NO_NAMESPACE) || \
  284. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  285. } // namespace mfcstl
  286. # else
  287. } // namespace mfcstl_project
  288. } // namespace stlsoft
  289. # endif /* _STLSOFT_NO_NAMESPACE */
  290. #endif /* !_MFCSTL_NO_NAMESPACE */
  291. /* /////////////////////////////////////////////////////////////////////////
  292. * Namespace
  293. *
  294. * The string access shims exist either in the stlsoft namespace, or in the
  295. * global namespace. This is required by the lookup rules.
  296. *
  297. */
  298. #ifndef _MFCSTL_NO_NAMESPACE
  299. # if !defined(_STLSOFT_NO_NAMESPACE) && \
  300. !defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  301. namespace stlsoft
  302. {
  303. # else /* ? _STLSOFT_NO_NAMESPACE */
  304. /* There is no stlsoft namespace, so must define in the global namespace */
  305. # endif /* !_STLSOFT_NO_NAMESPACE */
  306. using ::mfcstl::c_str_ptr_null;
  307. #if defined(UNICODE)
  308. using ::mfcstl::c_str_ptr_null_w;
  309. #else /* ? UNICODE */
  310. using ::mfcstl::c_str_ptr_null_a;
  311. #endif /* UNICODE */
  312. using ::mfcstl::c_str_ptr;
  313. #if defined(UNICODE)
  314. using ::mfcstl::c_str_ptr_w;
  315. #else /* ? UNICODE */
  316. using ::mfcstl::c_str_ptr_a;
  317. #endif /* UNICODE */
  318. using ::mfcstl::c_str_data;
  319. #if defined(UNICODE)
  320. using ::mfcstl::c_str_data_w;
  321. #else /* ? UNICODE */
  322. using ::mfcstl::c_str_data_a;
  323. #endif /* UNICODE */
  324. using ::mfcstl::c_str_len;
  325. # if !defined(_STLSOFT_NO_NAMESPACE) && \
  326. !defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  327. } // namespace stlsoft
  328. # else /* ? _STLSOFT_NO_NAMESPACE */
  329. /* There is no stlsoft namespace, so must define in the global namespace */
  330. # endif /* !_STLSOFT_NO_NAMESPACE */
  331. #endif /* !_MFCSTL_NO_NAMESPACE */
  332. /* ////////////////////////////////////////////////////////////////////// */
  333. #endif /* !MFCSTL_INCL_MFCSTL_STRING_HPP_RESOURCE_STRING */
  334. /* ///////////////////////////// end of file //////////////////////////// */