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.

252 lines
8.8 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/system/module_directory.hpp
  3. *
  4. * Purpose: Simple class that gets, and makes accessible, the module's
  5. * directory.
  6. *
  7. * Created: 5th June 2003
  8. * Updated: 12th August 2010
  9. *
  10. * Home: http://stlsoft.org/
  11. *
  12. * Copyright (c) 2003-2010, Matthew Wilson and Synesis Software
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright notice, this
  19. * list of conditions and the following disclaimer.
  20. * - Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  24. * any contributors may be used to endorse or promote products derived from
  25. * this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  28. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  31. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  35. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * ////////////////////////////////////////////////////////////////////// */
  40. /** \file winstl/system/module_directory.hpp
  41. *
  42. * \brief [C++ only] Definition of the winstl::basic_module_directory class
  43. * template
  44. * (\ref group__library__system "System" Library).
  45. */
  46. #ifndef WINSTL_INCL_WINSTL_SYSTEM_HPP_MODULE_DIRECTORY
  47. #define WINSTL_INCL_WINSTL_SYSTEM_HPP_MODULE_DIRECTORY
  48. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  49. # define WINSTL_VER_WINSTL_SYSTEM_HPP_MODULE_DIRECTORY_MAJOR 4
  50. # define WINSTL_VER_WINSTL_SYSTEM_HPP_MODULE_DIRECTORY_MINOR 2
  51. # define WINSTL_VER_WINSTL_SYSTEM_HPP_MODULE_DIRECTORY_REVISION 1
  52. # define WINSTL_VER_WINSTL_SYSTEM_HPP_MODULE_DIRECTORY_EDIT 56
  53. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  54. /* /////////////////////////////////////////////////////////////////////////
  55. * Compatibility
  56. */
  57. /*
  58. [Incompatibilies-start]
  59. STLSOFT_COMPILER_IS_MSVC: _MSC_VER<1200
  60. [Incompatibilies-end]
  61. */
  62. /* /////////////////////////////////////////////////////////////////////////
  63. * Includes
  64. */
  65. #ifndef WINSTL_INCL_WINSTL_H_WINSTL
  66. # include <winstl/winstl.h>
  67. #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
  68. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_SPECIAL_STRING_INSTANCE
  69. # include <stlsoft/string/special_string_instance.hpp>
  70. #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_SPECIAL_STRING_INSTANCE */
  71. #ifndef WINSTL_INCL_WINSTL_MEMORY_HPP_PROCESSHEAP_ALLOCATOR
  72. # include <winstl/memory/processheap_allocator.hpp>
  73. #endif /* !WINSTL_INCL_WINSTL_MEMORY_HPP_PROCESSHEAP_ALLOCATOR */
  74. #ifndef WINSTL_INCL_WINSTL_SYSTEM_HPP_SYSTEM_TRAITS
  75. # include <winstl/system/system_traits.hpp>
  76. #endif /* !WINSTL_INCL_WINSTL_SYSTEM_HPP_SYSTEM_TRAITS */
  77. #if defined(STLSOFT_COMPILER_IS_BORLAND)
  78. // Borland is a bit of a thicky, and requires a (valid) spin_mutex_type
  79. # include <winstl/synch/spin_mutex.hpp>
  80. #endif /* compiler */
  81. #ifdef STLSOFT_UNITTEST
  82. # include <stdlib.h> // for malloc(), free()
  83. #endif /* STLSOFT_UNITTEST */
  84. /* /////////////////////////////////////////////////////////////////////////
  85. * Namespace
  86. */
  87. #ifndef _WINSTL_NO_NAMESPACE
  88. # if defined(_STLSOFT_NO_NAMESPACE) || \
  89. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  90. /* There is no stlsoft namespace, so must define ::winstl */
  91. namespace winstl
  92. {
  93. # else
  94. /* Define stlsoft::winstl_project */
  95. namespace stlsoft
  96. {
  97. namespace winstl_project
  98. {
  99. # endif /* _STLSOFT_NO_NAMESPACE */
  100. #endif /* !_WINSTL_NO_NAMESPACE */
  101. /* /////////////////////////////////////////////////////////////////////////
  102. * basic_module_directory
  103. *
  104. * This class determines the directory of a module, and effectively acts as a
  105. * C-string of its value.
  106. */
  107. /** \brief Represents the directory of a module
  108. *
  109. * \ingroup group__library__system
  110. *
  111. * <h2>How</h2>
  112. *
  113. * An instance of basic_module_directory encapsulates the directory of a given module. You instantiate it
  114. * from a given module handle, as follows:
  115. *
  116. * [STLSOFT-DOC:VERBATIM:start;comment:/// ;]
  117. * HINSTANCE hinst = . . .;
  118. * basic_module_directory<char>(hinst) mdir(hinst);
  119. * puts(mdir);
  120. * [STLSOFT-DOC:VERBATIM:end]
  121. *
  122. * You can also use one of the three given typedefs:
  123. * \c module_directory (parameterised on TCHAR),
  124. * \c module_directory_a (parameterised on CHAR),
  125. * \c module_directory_w (parameterised on WCHAR).
  126. *
  127. * <h2>Why</h2>
  128. *
  129. * 1. It affords a simpler syntax. You can use temporary instances of the class, and use the <code>char_type const*</code>
  130. * implicit conversion, or the \c c_str() method inline
  131. *
  132. * [STLSOFT-DOC:VERBATIM:start;comment:/// ;]
  133. * puts(module_directory_a(hinst));
  134. * [STLSOFT-DOC:VERBATIM:end]
  135. *
  136. * You can also use it with the IOStreams:
  137. *
  138. * [STLSOFT-DOC:VERBATIM:start;comment:/// ;]
  139. * cout << L"The module was loaded from the " << module_directory_w(hinst) << L" directory" << endl;
  140. * [STLSOFT-DOC:VERBATIM:end]
  141. *
  142. * 2. It relieves you from the boilerplate coding of calling \c GetModuleFileName() and then parsing the
  143. * returned path to trim off the directory. All that is handled in the class.
  144. *
  145. *
  146. * \param C The character type
  147. * \param T The traits type. On translators that support default template arguments, this defaults to filesystem_traits<C>
  148. */
  149. template <ss_typename_param_k C>
  150. struct moddir_policy
  151. {
  152. /// \name Member Types
  153. /// @{
  154. public:
  155. typedef C char_type;
  156. typedef HINSTANCE argument_0_type;
  157. typedef processheap_allocator<C> allocator_type;
  158. typedef ws_size_t size_type;
  159. typedef size_type (*pfn_type)(argument_0_type, char_type *, size_type);
  160. #if defined(STLSOFT_COMPILER_IS_BORLAND)
  161. // Borland is a bit of a thicky, and requires a (valid) spin_mutex_type
  162. typedef winstl::spin_mutex spin_mutex_type;
  163. typedef winstl::atomic_int_t atomic_int_type;
  164. #endif /* compiler */
  165. /// @}
  166. /// \name Member Constants
  167. /// @{
  168. public:
  169. enum { internalBufferSize = 128 };
  170. enum { allowImplicitConversion = 1 };
  171. enum { sharedState = 0 };
  172. /// @}
  173. /// \name Operations
  174. /// @{
  175. public:
  176. static pfn_type get_fn()
  177. {
  178. return winstl::system_traits<char_type>::get_module_directory;
  179. }
  180. /// @}
  181. };
  182. /* /////////////////////////////////////////////////////////////////////////
  183. * Typedefs for commonly encountered types
  184. */
  185. /** \brief A \ref group__pattern__special_string_instance "Special String Instance"
  186. * that represents the <b>module</b> directory; ANSI specialisation.
  187. *
  188. * \ingroup group__library__system
  189. */
  190. typedef stlsoft_ns_qual(special_string_instance_1)<moddir_policy<ws_char_a_t> > module_directory_a;
  191. /** \brief A \ref group__pattern__special_string_instance "Special String Instance"
  192. * that represents the <b>module</b> directory; 'Unicode' specialisation.
  193. *
  194. * \ingroup group__library__system
  195. */
  196. typedef stlsoft_ns_qual(special_string_instance_1)<moddir_policy<ws_char_w_t> > module_directory_w;
  197. /** \brief A \ref group__pattern__special_string_instance "Special String Instance"
  198. * that represents the <b>module</b> directory; TCHAR specialisation.
  199. *
  200. * \ingroup group__library__system
  201. */
  202. typedef stlsoft_ns_qual(special_string_instance_1)<moddir_policy<TCHAR> > module_directory;
  203. /* /////////////////////////////////////////////////////////////////////////
  204. * Unit-testing
  205. */
  206. #ifdef STLSOFT_UNITTEST
  207. # include "./unittest/module_directory_unittest_.h"
  208. #endif /* STLSOFT_UNITTEST */
  209. /* ////////////////////////////////////////////////////////////////////// */
  210. #ifndef _WINSTL_NO_NAMESPACE
  211. # if defined(_STLSOFT_NO_NAMESPACE) || \
  212. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  213. } // namespace winstl
  214. # else
  215. } // namespace winstl_project
  216. } // namespace stlsoft
  217. # endif /* _STLSOFT_NO_NAMESPACE */
  218. #endif /* !_WINSTL_NO_NAMESPACE */
  219. /* ////////////////////////////////////////////////////////////////////// */
  220. #endif /* WINSTL_INCL_WINSTL_SYSTEM_HPP_MODULE_DIRECTORY */
  221. /* ///////////////////////////// end of file //////////////////////////// */