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.

360 lines
12 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/util/struct_initialisers.hpp
  3. *
  4. * Purpose: Functions for initialising Win32 structures.
  5. *
  6. * Created: 20th October 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 winstl/util/struct_initialisers.hpp
  40. *
  41. * \brief [C++ only] Definition of the winstl::zero_struct and
  42. * winstl::init_struct structure initialiser functions
  43. * (\ref group__library__utility "Utility" Library).
  44. */
  45. #ifndef WINSTL_INCL_WINSTL_UTIL_HPP_STRUCT_INITIALISERS
  46. #define WINSTL_INCL_WINSTL_UTIL_HPP_STRUCT_INITIALISERS
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define WINSTL_VER_WINSTL_UTIL_HPP_STRUCT_INITIALISERS_MAJOR 4
  49. # define WINSTL_VER_WINSTL_UTIL_HPP_STRUCT_INITIALISERS_MINOR 0
  50. # define WINSTL_VER_WINSTL_UTIL_HPP_STRUCT_INITIALISERS_REVISION 4
  51. # define WINSTL_VER_WINSTL_UTIL_HPP_STRUCT_INITIALISERS_EDIT 220
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef WINSTL_INCL_WINSTL_H_WINSTL
  57. # include <winstl/winstl.h>
  58. #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
  59. /* /////////////////////////////////////////////////////////////////////////
  60. * Namespace
  61. */
  62. #ifndef _WINSTL_NO_NAMESPACE
  63. # if defined(_STLSOFT_NO_NAMESPACE) || \
  64. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  65. /* There is no stlsoft namespace, so must define ::winstl */
  66. namespace winstl
  67. {
  68. # else
  69. /* Define stlsoft::winstl_project */
  70. namespace stlsoft
  71. {
  72. namespace winstl_project
  73. {
  74. # endif /* _STLSOFT_NO_NAMESPACE */
  75. #endif /* !_WINSTL_NO_NAMESPACE */
  76. /* /////////////////////////////////////////////////////////////////////////
  77. * Functions
  78. */
  79. /** \brief Initialises all elements of a structure to zero
  80. *
  81. * \ingroup group__library__utility
  82. */
  83. template <ss_typename_param_k T>
  84. inline void zero_struct(T &t)
  85. {
  86. ::ZeroMemory(&t, sizeof(T));
  87. }
  88. template <ss_typename_param_k T>
  89. struct init_traits;
  90. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  91. struct struct_has
  92. {
  93. struct no_init_member_type
  94. {};
  95. struct cb_member_type
  96. {};
  97. struct cBytes_member_type
  98. {};
  99. struct cbSize_member_type
  100. {};
  101. struct dwLength_member_type
  102. {};
  103. struct dwOSVersionInfoSize_member_type
  104. {};
  105. struct dwSize_member_type
  106. {};
  107. struct nLength_member_type
  108. {};
  109. struct uSize_member_type
  110. {};
  111. template <ss_typename_param_k T>
  112. static void init(T &t, no_init_member_type)
  113. {
  114. zero_struct(t);
  115. }
  116. template <ss_typename_param_k T>
  117. static void init(T &t, cb_member_type)
  118. {
  119. zero_struct(t);
  120. t.cb = sizeof(T);
  121. }
  122. template <ss_typename_param_k T>
  123. static void init(T &t, cBytes_member_type)
  124. {
  125. zero_struct(t);
  126. t.cBytes = sizeof(T);
  127. }
  128. template <ss_typename_param_k T>
  129. static void init(T &t, cbSize_member_type)
  130. {
  131. zero_struct(t);
  132. t.cbSize = sizeof(T);
  133. }
  134. template <ss_typename_param_k T>
  135. static void init(T &t, dwLength_member_type)
  136. {
  137. zero_struct(t);
  138. t.dwLength = sizeof(T);
  139. }
  140. template <ss_typename_param_k T>
  141. static void init(T &t, dwOSVersionInfoSize_member_type)
  142. {
  143. zero_struct(t);
  144. t.dwOSVersionInfoSize = sizeof(T);
  145. }
  146. template <ss_typename_param_k T>
  147. static void init(T &t, dwSize_member_type)
  148. {
  149. zero_struct(t);
  150. t.dwSize = sizeof(T);
  151. }
  152. template <ss_typename_param_k T>
  153. static void init(T &t, nLength_member_type)
  154. {
  155. zero_struct(t);
  156. t.nLength = sizeof(T);
  157. }
  158. template <ss_typename_param_k T>
  159. static void init(T &t, uSize_member_type)
  160. {
  161. zero_struct(t);
  162. t.uSize = sizeof(T);
  163. }
  164. };
  165. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  166. /** \brief Initialises all members of a structure to zero, and
  167. * sets the size member to the size of the structure.
  168. *
  169. * \ingroup group__library__utility
  170. */
  171. template <ss_typename_param_k T>
  172. inline void init_struct(T &t)
  173. {
  174. typedef ss_typename_type_k init_traits<T>::type discriminator_t;
  175. struct_has::init(t, discriminator_t());
  176. }
  177. // Specialisations
  178. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  179. # define WINSTL_The_structure_(S, t) \
  180. \
  181. STLSOFT_TEMPLATE_SPECIALISATION \
  182. struct init_traits<S> \
  183. { \
  184. typedef struct_##t type; \
  185. }
  186. // WinBase.h:
  187. WINSTL_The_structure_(PROCESS_INFORMATION, has::no_init_member_type);
  188. WINSTL_The_structure_(OVERLAPPED, has::no_init_member_type);
  189. WINSTL_The_structure_(FILETIME, has::no_init_member_type);
  190. WINSTL_The_structure_(SYSTEMTIME, has::no_init_member_type);
  191. WINSTL_The_structure_(COMMPROP, has::no_init_member_type);
  192. WINSTL_The_structure_(COMSTAT, has::no_init_member_type);
  193. WINSTL_The_structure_(COMMTIMEOUTS, has::no_init_member_type);
  194. WINSTL_The_structure_(SYSTEM_INFO, has::no_init_member_type);
  195. WINSTL_The_structure_(EXCEPTION_DEBUG_INFO, has::no_init_member_type);
  196. WINSTL_The_structure_(CREATE_THREAD_DEBUG_INFO, has::no_init_member_type);
  197. WINSTL_The_structure_(CREATE_PROCESS_DEBUG_INFO, has::no_init_member_type);
  198. WINSTL_The_structure_(EXIT_THREAD_DEBUG_INFO, has::no_init_member_type);
  199. WINSTL_The_structure_(LOAD_DLL_DEBUG_INFO, has::no_init_member_type);
  200. WINSTL_The_structure_(UNLOAD_DLL_DEBUG_INFO, has::no_init_member_type);
  201. WINSTL_The_structure_(OUTPUT_DEBUG_STRING_INFO, has::no_init_member_type);
  202. WINSTL_The_structure_(RIP_INFO, has::no_init_member_type);
  203. WINSTL_The_structure_(DEBUG_EVENT, has::no_init_member_type);
  204. WINSTL_The_structure_(PROCESS_HEAP_ENTRY, has::no_init_member_type);
  205. WINSTL_The_structure_(BY_HANDLE_FILE_INFORMATION, has::no_init_member_type);
  206. WINSTL_The_structure_(TIME_ZONE_INFORMATION, has::no_init_member_type);
  207. WINSTL_The_structure_(WIN32_STREAM_ID, has::no_init_member_type);
  208. WINSTL_The_structure_(WIN32_FIND_DATAA, has::no_init_member_type);
  209. WINSTL_The_structure_(WIN32_FIND_DATAW, has::no_init_member_type);
  210. WINSTL_The_structure_(WIN32_FILE_ATTRIBUTE_DATA, has::no_init_member_type);
  211. WINSTL_The_structure_(SYSTEM_POWER_STATUS, has::no_init_member_type);
  212. #ifdef WINTRUST_H
  213. WINSTL_The_structure_(WIN_CERTIFICATE, has::no_init_member_type);
  214. WINSTL_The_structure_(WIN_TRUST_ACTDATA_CONTEXT_WITH_SUBJECT, has::no_init_member_type);
  215. WINSTL_The_structure_(WIN_TRUST_ACTDATA_SUBJECT_ONLY, has::no_init_member_type);
  216. WINSTL_The_structure_(WIN_TRUST_SUBJECT_FILE, has::no_init_member_type);
  217. WINSTL_The_structure_(WIN_TRUST_SUBJECT_FILE_AND_DISPLAY, has::no_init_member_type);
  218. WINSTL_The_structure_(WIN_SPUB_TRUSTED_PUBLISHER_DATA, has::no_init_member_type);
  219. #endif /* WINTRUST_H */
  220. WINSTL_The_structure_(STARTUPINFOA, has::cb_member_type);
  221. WINSTL_The_structure_(STARTUPINFOW, has::cb_member_type);
  222. WINSTL_The_structure_(OFSTRUCT, has::cBytes_member_type);
  223. WINSTL_The_structure_(MEMORYSTATUS, has::dwLength_member_type);
  224. WINSTL_The_structure_(OSVERSIONINFOA, has::dwOSVersionInfoSize_member_type);
  225. WINSTL_The_structure_(OSVERSIONINFOW, has::dwOSVersionInfoSize_member_type);
  226. #if !defined(STLSOFT_COMPILER_IS_DMC) && \
  227. ( !defined(STLSOFT_COMPILER_IS_MSVC) || \
  228. _MSC_VER >= 1200)
  229. WINSTL_The_structure_(OSVERSIONINFOEXA, has::dwOSVersionInfoSize_member_type);
  230. WINSTL_The_structure_(OSVERSIONINFOEXW, has::dwOSVersionInfoSize_member_type);
  231. #endif /* compiler */
  232. WINSTL_The_structure_(COMMCONFIG, has::dwSize_member_type);
  233. WINSTL_The_structure_(SECURITY_ATTRIBUTES, has::nLength_member_type);
  234. inline void init_struct(DCB &dcb)
  235. {
  236. zero_struct(dcb);
  237. dcb.DCBlength = sizeof(dcb);
  238. }
  239. // ShellApi.h
  240. #if defined(_INC_SHELLAPI) || \
  241. defined(_SHELLAPI_H) || \
  242. !defined(WIN32_LEAN_AND_MEAN)
  243. WINSTL_The_structure_(SHFILEOPSTRUCTA, has::no_init_member_type);
  244. WINSTL_The_structure_(SHFILEOPSTRUCTW, has::no_init_member_type);
  245. #if !defined(STLSOFT_COMPILER_IS_GCC)
  246. WINSTL_The_structure_(SHNAMEMAPPINGA, has::no_init_member_type);
  247. WINSTL_The_structure_(SHNAMEMAPPINGW, has::no_init_member_type);
  248. #endif /* compiler */
  249. WINSTL_The_structure_(SHFILEINFOA, has::no_init_member_type);
  250. WINSTL_The_structure_(SHFILEINFOW, has::no_init_member_type);
  251. #if !defined(STLSOFT_COMPILER_IS_DMC) && \
  252. !defined(STLSOFT_COMPILER_IS_GCC) && \
  253. ( !defined(STLSOFT_COMPILER_IS_MSVC) || \
  254. _MSC_VER >= 1200) && \
  255. defined(WINVER) && \
  256. WINVER >= 0x0400
  257. WINSTL_The_structure_(DRAGINFOA, has::uSize_member_type);
  258. WINSTL_The_structure_(DRAGINFOW, has::uSize_member_type);
  259. #endif /* WINVER */
  260. WINSTL_The_structure_(APPBARDATA, has::cbSize_member_type);
  261. WINSTL_The_structure_(SHELLEXECUTEINFOA, has::cbSize_member_type);
  262. WINSTL_The_structure_(SHELLEXECUTEINFOW, has::cbSize_member_type);
  263. #if !defined(STLSOFT_COMPILER_IS_DMC) && \
  264. ( !defined(STLSOFT_COMPILER_IS_GCC) || \
  265. __GNUC__ > 3 || \
  266. ( __GNUC__ == 3 && \
  267. __GNUC_MINOR__ > 2)) && \
  268. ( !defined(STLSOFT_COMPILER_IS_MSVC) || \
  269. _MSC_VER >= 1200)
  270. WINSTL_The_structure_(SHQUERYRBINFO, has::cbSize_member_type);
  271. #endif /* compiler */
  272. WINSTL_The_structure_(NOTIFYICONDATAA, has::cbSize_member_type);
  273. WINSTL_The_structure_(NOTIFYICONDATAW, has::cbSize_member_type);
  274. #endif /* _INC_SHELLAPI || _SHELLAPI_H || !WIN32_LEAN_AND_MEAN */
  275. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  276. ////////////////////////////////////////////////////////////////////////////
  277. // Unit-testing
  278. #ifdef STLSOFT_UNITTEST
  279. # include "./unittest/struct_initialisers_unittest_.h"
  280. #endif /* STLSOFT_UNITTEST */
  281. /* ////////////////////////////////////////////////////////////////////// */
  282. #ifndef _WINSTL_NO_NAMESPACE
  283. # if defined(_STLSOFT_NO_NAMESPACE) || \
  284. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  285. } // namespace winstl
  286. # else
  287. } // namespace winstl_project
  288. } // namespace stlsoft
  289. # endif /* _STLSOFT_NO_NAMESPACE */
  290. #endif /* !_WINSTL_NO_NAMESPACE */
  291. /* ////////////////////////////////////////////////////////////////////// */
  292. #endif /* !WINSTL_INCL_WINSTL_UTIL_HPP_STRUCT_INITIALISERS */
  293. /* ///////////////////////////// end of file //////////////////////////// */