WinStd
Additional templates and function helpers for Microsoft Windows using Standard C++ classes
Common.h
Go to the documentation of this file.
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2022 Amebis
4 Copyright © 2016 GÉANT
5*/
6
7#pragma once
8
9#include <Windows.h>
10#include <assert.h>
11#include <stdarg.h>
12#include <tchar.h>
13#include <iostream>
14#include <memory>
15#include <stdexcept>
16#include <string>
17#include <vector>
18
43
46
50#ifndef __L
51#define __L(x) L ## x
52#endif
53
57#ifndef _L
58#define _L(x) __L(x)
59#endif
60
64#define WINSTD_STRING_IMPL(x) #x
65
69#define WINSTD_STRING(x) WINSTD_STRING_IMPL(x)
70
74#define WINSTD_NONCOPYABLE(C) \
75private: \
76 C (_In_ const C &h) noexcept; \
77 C& operator=(_In_ const C &h) noexcept;
78
82#define WINSTD_NONMOVABLE(C) \
83private: \
84 C (_Inout_ C &&h) noexcept; \
85 C& operator=(_Inout_ C &&h) noexcept;
86
87#ifndef WINSTD_STACK_BUFFER_BYTES
101#define WINSTD_STACK_BUFFER_BYTES 1024
102#endif
103
105
108
112#ifdef UNICODE
113#define PRINTF_LPTSTR "ls"
114#else
115#define PRINTF_LPTSTR "s"
116#endif
117
121#ifdef OLE2ANSI
122#define PRINTF_LPOLESTR "hs"
123#else
124#define PRINTF_LPOLESTR "ls"
125#endif
126
130#ifdef _UNICODE
131#define _tcin (std::wcin )
132#else
133#define _tcin (std::cin )
134#endif
135
139#ifdef _UNICODE
140#define _tcout (std::wcout)
141#else
142#define _tcout (std::cout)
143#endif
144
148#ifdef _UNICODE
149#define _tcerr (std::wcerr)
150#else
151#define _tcerr (std::cerr)
152#endif
153
157#ifdef _UNICODE
158#define _tclog (std::wclog)
159#else
160#define _tclog (std::clog)
161#endif
162
164
167
171#define WINSTD_HANDLE_IMPL(C, INVAL) \
172public: \
173 C ( ) noexcept { } \
174 C (_In_opt_ handle_type h) noexcept : handle<handle_type, INVAL>( h ) { } \
175 C (_Inout_ C &&h) noexcept : handle<handle_type, INVAL>(std::move(h)) { } \
176 C& operator=(_In_opt_ handle_type h) noexcept { handle<handle_type, INVAL>::operator=( h ); return *this; } \
177 C& operator=(_Inout_ C &&h) noexcept { handle<handle_type, INVAL>::operator=(std::move(h)); return *this; } \
178WINSTD_NONCOPYABLE(C)
179
183#define WINSTD_DPLHANDLE_IMPL(C, INVAL) \
184public: \
185 C ( ) noexcept { } \
186 C (_In_opt_ handle_type h) noexcept : dplhandle<handle_type, INVAL>( h ) { } \
187 C (_In_ const C &h) noexcept : dplhandle<handle_type, INVAL>(duplicate_internal(h.m_h)) { } \
188 C (_Inout_ C &&h) noexcept : dplhandle<handle_type, INVAL>(std::move (h )) { } \
189 C& operator=(_In_opt_ handle_type h) noexcept { dplhandle<handle_type, INVAL>::operator=( h ); return *this; } \
190 C& operator=(_In_ const C &h) noexcept { dplhandle<handle_type, INVAL>::operator=( h ); return *this; } \
191 C& operator=(_Inout_ C &&h) noexcept { dplhandle<handle_type, INVAL>::operator=(std::move(h)); return *this; } \
192private:
193
195
196#ifndef _FormatMessage_format_string_
197#define _FormatMessage_format_string_ _In_z_
198#endif
199
201#ifndef _LPCBYTE_DEFINED
202#define _LPCBYTE_DEFINED
203typedef const BYTE *LPCBYTE;
204#endif
206
207#pragma warning(push)
208// Do not use _vsnprintf_s/_vsnwprintf_s(), since it terminates string by force even when we explicitly want to write unterminated string.
209// Threfore turn off compiler warning instead. ;)
210#pragma warning(disable: 4995)
211#pragma warning(disable: 4996)
212#pragma warning(disable: 4505) // Don't warn on unused code
213
216
227#if _MSC_VER <= 1600
228static int vsnprintf(_Out_z_cap_(capacity) char *str, _In_ size_t capacity, _In_z_ _Printf_format_string_ const char *format, _In_ va_list arg)
229{
230 return _vsnprintf(str, capacity, format, arg);
231}
232#endif
233
244static int vsnprintf(_Out_z_cap_(capacity) wchar_t *str, _In_ size_t capacity, _In_z_ _Printf_format_string_ const wchar_t *format, _In_ va_list arg) noexcept
245{
246 return _vsnwprintf(str, capacity, format, arg);
247}
248
258template<class _Elem, class _Traits, class _Ax>
259static int vsprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const _Elem *format, _In_ va_list arg)
260{
261 _Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
262
263 // Try with stack buffer first.
264 int count = vsnprintf(buf, _countof(buf) - 1, format, arg);
265 if (count >= 0) {
266 // Copy from stack.
267 str.assign(buf, count);
268 } else {
269 for (size_t capacity = 2*WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem);; capacity *= 2) {
270 // Allocate on heap and retry.
271 auto buf_dyn = std::make_unique<_Elem[]>(capacity);
272 count = vsnprintf(buf_dyn.get(), capacity - 1, format, arg);
273 if (count >= 0) {
274 str.assign(buf_dyn.get(), count);
275 break;
276 }
277 }
278 }
279
280 return count;
281}
282
291template<class _Elem, class _Traits, class _Ax>
292static int sprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const _Elem *format, ...)
293{
294 va_list arg;
295 va_start(arg, format);
296 const int res = vsprintf(str, format, arg);
297 va_end(arg);
298 return res;
299}
300
306template<class _Traits, class _Ax>
307static DWORD FormatMessage(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _Inout_ std::basic_string<char, _Traits, _Ax> &str, _In_opt_ va_list *Arguments)
308{
309 std::unique_ptr<CHAR[], winstd::LocalFree_delete<CHAR[]> > lpBuffer;
310 DWORD dwResult = FormatMessageA(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPSTR>((LPSTR*)get_ptr(lpBuffer)), 0, Arguments);
311 if (dwResult)
312 str.assign(lpBuffer.get(), dwResult);
313 return dwResult;
314}
315
321template<class _Traits, class _Ax>
322static DWORD FormatMessage(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _Inout_ std::basic_string<wchar_t, _Traits, _Ax> &str, _In_opt_ va_list *Arguments)
323{
324 std::unique_ptr<WCHAR[], winstd::LocalFree_delete<WCHAR[]> > lpBuffer;
325 DWORD dwResult = FormatMessageW(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPWSTR>((LPWSTR*)get_ptr(lpBuffer)), 0, Arguments);
326 if (dwResult)
327 str.assign(lpBuffer.get(), dwResult);
328 return dwResult;
329}
330
332
333#pragma warning(pop)
334
335namespace winstd
336{
339
343#ifdef _UNICODE
344 typedef std::wstring tstring;
345#else
346 typedef std::string tstring;
347#endif
348
352 template <class _Ty>
354 {
356
361
365 template <class _Ty2> LocalFree_delete(const LocalFree_delete<_Ty2>&) {}
366
372 void operator()(_Ty *_Ptr) const
373 {
374 LocalFree(_Ptr);
375 }
376 };
377
381 template <class _Ty>
382 struct LocalFree_delete<_Ty[]>
383 {
385
389 LocalFree_delete() noexcept {}
390
394 void operator()(_Frees_ptr_opt_ _Ty *_Ptr) const noexcept
395 {
396 LocalFree(_Ptr);
397 }
398
404 template<class _Other>
405 void operator()(_Other *) const
406 {
407 LocalFree(_Ptr);
408 }
409 };
410
414 template<class _Ty, class _Dx>
416 {
417 public:
423 ref_unique_ptr(_Inout_ std::unique_ptr<_Ty, _Dx> &owner) :
424 m_own(owner),
425 m_ptr(owner.release())
426 {}
427
434 m_own(other.m_own),
435 m_ptr(other.m_ptr)
436 {
437 other.m_ptr = nullptr;
438 }
439
444 {
445 if (m_ptr != nullptr)
446 m_own.reset(m_ptr);
447 }
448
454 operator typename _Ty**()
455 {
456 return &m_ptr;
457 }
458
464 operator typename _Ty*&()
465 {
466 return m_ptr;
467 }
468
469 protected:
470 std::unique_ptr<_Ty, _Dx> &m_own;
471 _Ty *m_ptr;
472 };
473
481 template<class _Ty, class _Dx>
482 ref_unique_ptr<_Ty, _Dx> get_ptr(_Inout_ std::unique_ptr<_Ty, _Dx> &owner) noexcept
483 {
484 return ref_unique_ptr<_Ty, _Dx>(owner);
485 }
486
495 template<class _Ty, class _Dx>
496 ref_unique_ptr<_Ty[], _Dx> get_ptr(_Inout_ std::unique_ptr<_Ty[], _Dx> &owner) noexcept
497 {
498 return ref_unique_ptr<_Ty[], _Dx>(owner);
499 }
500
505 #pragma warning(push)
506 #pragma warning(disable: 26432) // Copy constructor and assignment operator are also present, but not detected by code analysis as they are using base type source object reference.
507 template<class _Ty, class _Dx>
508 class ref_unique_ptr<_Ty[], _Dx>
509 {
510 public:
516 ref_unique_ptr(_Inout_ std::unique_ptr<_Ty[], _Dx> &owner) noexcept :
517 m_own(owner),
518 m_ptr(owner.release())
519 {}
520
528 ref_unique_ptr& operator=(_Inout_ std::unique_ptr<_Ty[], _Dx> &owner) noexcept
529 {
530 if (this != &other) {
531 m_own = owner;
532 m_ptr = owner.release();
533 }
534
535 return *this;
536 }
537
543 ref_unique_ptr(_Inout_ ref_unique_ptr<_Ty[], _Dx> &&other) :
544 m_own(other.m_own),
545 m_ptr(other.m_ptr)
546 {
547 other.m_ptr = nullptr;
548 }
549
557 ref_unique_ptr& operator=(_Inout_ ref_unique_ptr<_Ty[], _Dx> &&other)
558 {
559 if (this != &other) {
560 m_own = other.m_own;
561 m_ptr = other.m_ptr;
562 other.m_ptr = nullptr;
563 }
564
565 return *this;
566 }
567
572 {
573 if (m_ptr != nullptr)
574 m_own.reset(m_ptr);
575 }
576
582 operator typename _Ty**() noexcept
583 {
584 return &m_ptr;
585 }
586
592 operator typename _Ty*&()
593 {
594 return m_ptr;
595 }
596
597 protected:
598 std::unique_ptr<_Ty[], _Dx> &m_own;
599 _Ty *m_ptr;
600 };
601 #pragma warning(pop)
602
604
607
613 template <class T, const T INVAL>
614 class handle
615 {
616 public:
620 typedef T handle_type;
621
625 static const T invalid;
626
630 handle() noexcept : m_h(invalid)
631 {
632 }
633
639 handle(_In_opt_ handle_type h) noexcept : m_h(h)
640 {
641 }
642
648 handle(_Inout_ handle<handle_type, INVAL> &&h) noexcept
649 {
650 // Transfer handle.
651 m_h = h.m_h;
652 h.m_h = invalid;
653 }
654
655 private:
656 // This class is noncopyable.
657 handle(_In_ const handle<handle_type, INVAL> &h) noexcept {};
658 handle<handle_type, INVAL>& operator=(_In_ const handle<handle_type, INVAL> &h) noexcept {};
659
660 public:
667 {
668 attach(h);
669 return *this;
670 }
671
677 #pragma warning(suppress: 26432) // Move constructor is also present, but not detected by code analysis somehow.
679 {
680 if (this != std::addressof(h)) {
681 // Transfer handle.
682 if (m_h != invalid)
684 m_h = h.m_h;
685 h.m_h = invalid;
686 }
687 return *this;
688 }
689
695 operator handle_type() const
696 {
697 return m_h;
698 }
699
706 {
707 assert(m_h != invalid);
708 return *m_h;
709 }
710
716 {
717 assert(m_h == invalid);
718 return &m_h;
719 }
720
727 {
728 assert(m_h != invalid);
729 return m_h;
730 }
731
739 bool operator!() const
740 {
741 return m_h == invalid;
742 }
743
752 bool operator<(_In_opt_ handle_type h) const
753 {
754 return m_h < h;
755 }
756
765 bool operator<=(_In_opt_ handle_type h) const
766 {
767 return !operator>(h);
768 }
769
778 bool operator>=(_In_opt_ handle_type h) const
779 {
780 return !operator<(h);
781 }
782
791 bool operator>(_In_opt_ handle_type h) const
792 {
793 return h < m_h;
794 }
795
804 bool operator!=(_In_opt_ handle_type h) const
805 {
806 return !operator==(h);
807 }
808
817 bool operator==(_In_opt_ handle_type h) const
818 {
819 return m_h == h;
820 }
821
829 void attach(_In_opt_ handle_type h) noexcept
830 {
831 if (m_h != invalid)
833 m_h = h;
834 }
835
842 {
843 handle_type h = m_h;
844 m_h = invalid;
845 return h;
846 }
847
851 void free()
852 {
853 if (m_h != invalid) {
855 m_h = invalid;
856 }
857 }
858
859 protected:
863 virtual void free_internal() noexcept = 0;
864
865 protected:
867 };
868
869 template <class T, const T INVAL>
870 const T handle<T, INVAL>::invalid = INVAL;
871
875 template <class T, T INVAL>
876 class dplhandle : public handle<T, INVAL>
877 {
878 public:
882 dplhandle() noexcept
883 {
884 }
885
892 {
893 }
894
900 dplhandle<handle_type, INVAL>(_In_ const dplhandle<handle_type, INVAL> &h) noexcept : handle<handle_type, INVAL>(duplicate_internal(h.m_h))
901 {
902 }
903
909 dplhandle<handle_type, INVAL>(_Inout_ dplhandle<handle_type, INVAL> &&h) noexcept : handle<handle_type, INVAL>(std::move(h))
910 {
911 }
912
919 {
921 return *this;
922 }
923
930 {
931 if (this != std::addressof(h)) {
932 if (h.m_h != invalid) {
933 handle_type h_new = duplicate_internal(h.m_h);
934 if (h_new != invalid) {
935 if (m_h != invalid)
937
938 m_h = h_new;
939 } else
940 assert(0); // Could not duplicate the handle
941 } else {
942 if (m_h != invalid)
944
945 m_h = invalid;
946 }
947 }
948 return *this;
949 }
950
956 #pragma warning(disable: 26432) // Move constructor is also present, but not detected by code analysis somehow.
958 {
960 return *this;
961 }
962
969 {
970 return m_h != invalid ? duplicate_internal(m_h) : invalid;
971 }
972
983 {
984 if (m_h != invalid)
986
987 return h != invalid ? (m_h = duplicate_internal(h)) != invalid : (m_h = invalid, true);
988 }
989
990 protected:
998 virtual handle_type duplicate_internal(_In_ handle_type h) const noexcept = 0;
999 };
1000
1002
1005
1009 template <typename _Tn>
1010 class num_runtime_error : public std::runtime_error
1011 {
1012 public:
1013 typedef _Tn error_type;
1014
1015 public:
1022 num_runtime_error(_In_ error_type num, _In_ const std::string& msg) :
1023 m_num(num),
1024 runtime_error(msg)
1025 {
1026 }
1027
1034 num_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) :
1035 m_num(num),
1036 runtime_error(msg)
1037 {
1038 }
1039
1044 {
1045 return m_num;
1046 }
1047
1048 protected:
1050 };
1051
1056 {
1057 public:
1064 win_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<DWORD>(num, msg)
1065 {
1066 }
1067
1074 win_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) : num_runtime_error<DWORD>(num, msg)
1075 {
1076 }
1077
1083 win_runtime_error(_In_ const std::string& msg) : num_runtime_error<DWORD>(GetLastError(), msg)
1084 {
1085 }
1086
1092 win_runtime_error(_In_opt_z_ const char *msg = nullptr) : num_runtime_error<DWORD>(GetLastError(), msg)
1093 {
1094 }
1095
1101 tstring msg(_In_opt_ DWORD dwLanguageId = 0) const
1102 {
1103 tstring str;
1104 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, m_num, dwLanguageId, str, NULL)) {
1105 // Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
1106 str.erase(str.find_last_not_of(_T(" \t\n\r\f\v")) + 1);
1107 } else
1108 sprintf(str, m_num >= 0x10000 ? _T("Error 0x%X") : _T("Error %u"), m_num);
1109 return str;
1110 }
1111 };
1112
1114
1117
1121 template<class _Elem, class _Traits, class _Ax>
1122 class basic_string_printf : public std::basic_string<_Elem, _Traits, _Ax>
1123 {
1124 public:
1127
1133 basic_string_printf(_In_z_ _Printf_format_string_ const _Elem *format, ...)
1134 {
1135 va_list arg;
1136 va_start(arg, format);
1137 vsprintf(*this, format, arg);
1138 va_end(arg);
1139 }
1140
1142
1145
1152 basic_string_printf(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
1153 {
1154 _Myt format;
1155 ATLENSURE(format.LoadString(hInstance, nFormatID));
1156
1157 va_list arg;
1158 va_start(arg, nFormatID);
1159 vsprintf(*this, format, arg);
1160 va_end(arg);
1161 }
1162
1170 basic_string_printf(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
1171 {
1172 _Myt format;
1173 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1174
1175 va_list arg;
1176 va_start(arg, nFormatID);
1177 vsprintf(*this, format, arg);
1178 va_end(arg);
1179 }
1180
1182 };
1183
1188
1193
1197#ifdef _UNICODE
1199#else
1201#endif
1202
1206 template<class _Elem, class _Traits, class _Ax>
1207 class basic_string_msg : public std::basic_string<_Elem, _Traits, _Ax>
1208 {
1209 public:
1212
1218 basic_string_msg(_In_z_ _FormatMessage_format_string_ const _Elem *format, ...)
1219 {
1220 va_list arg;
1221 va_start(arg, format);
1222 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1223 va_end(arg);
1224 }
1225
1227
1230
1237 basic_string_msg(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
1238 {
1239 _Myt format(GetManager());
1240 ATLENSURE(format.LoadString(hInstance, nFormatID));
1241
1242 va_list arg;
1243 va_start(arg, nFormatID);
1244 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1245 va_end(arg);
1246 }
1247
1255 basic_string_msg(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
1256 {
1257 _Myt format(GetManager());
1258 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1259
1260 va_list arg;
1261 va_start(arg, nFormatID);
1262 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1263 va_end(arg);
1264 }
1265
1267
1273 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ va_list *Arguments)
1274 {
1275 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *this, Arguments);
1276 }
1277
1283 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ DWORD_PTR *Arguments)
1284 {
1285 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *this, (va_list*)Arguments);
1286 }
1287
1293 basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ va_list *Arguments)
1294 {
1295 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *this, Arguments);
1296 }
1297
1303 basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ DWORD_PTR *Arguments)
1304 {
1305 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *this, (va_list*)Arguments);
1306 }
1307 };
1308
1313
1318
1322#ifdef _UNICODE
1323 typedef wstring_msg tstring_msg;
1324#else
1326#endif
1327
1331 template<class _Elem, class _Traits, class _Ax>
1332 class basic_string_guid : public std::basic_string<_Elem, _Traits, _Ax>
1333 {
1334 public:
1337
1344 basic_string_guid(_In_ const GUID &guid, _In_z_ _Printf_format_string_ const _Elem *format)
1345 {
1346 sprintf<_Elem, _Traits, _Ax>(*this, format,
1347 guid.Data1,
1348 guid.Data2,
1349 guid.Data3,
1350 guid.Data4[0], guid.Data4[1],
1351 guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
1352 }
1353
1355 };
1356
1360 class string_guid : public basic_string_guid<char, std::char_traits<char>, std::allocator<char> >
1361 {
1362 public:
1365
1371 string_guid(_In_ const GUID &guid) :
1372 basic_string_guid<char, std::char_traits<char>, std::allocator<char> >(guid, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
1373 {
1374 }
1375
1377 };
1378
1382 class wstring_guid : public basic_string_guid<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >
1383 {
1384 public:
1387
1393 wstring_guid(_In_ const GUID &guid) :
1394 basic_string_guid<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >(guid, L"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
1395 {
1396 }
1397
1399 };
1400
1404#ifdef _UNICODE
1405 typedef wstring_guid tstring_guid;
1406#else
1408#endif
1409
1411
1414
1415 // winstd::sanitizing_allocator::destroy() member generates _Ptr parameter not used warning for primitive datatypes _Ty.
1416 #pragma warning(push)
1417 #pragma warning(disable: 4100)
1418
1426 template<class _Ty>
1427 class sanitizing_allocator : public std::allocator<_Ty>
1428 {
1429 public:
1430 typedef std::allocator<_Ty> _Mybase;
1431
1435 template<class _Other>
1436 struct rebind
1437 {
1439 };
1440
1445 {
1446 }
1447
1452 {
1453 }
1454
1458 template<class _Other>
1459 sanitizing_allocator(_In_ const sanitizing_allocator<_Other> &_Othr) noexcept : _Mybase(_Othr)
1460 {
1461 }
1462
1466 void deallocate(_In_ pointer _Ptr, _In_ size_type _Size)
1467 {
1468 // Sanitize then free.
1469 SecureZeroMemory(_Ptr, _Size);
1470 _Mybase::deallocate(_Ptr, _Size);
1471 }
1472 };
1473
1474 #pragma warning(pop)
1475
1483 typedef std::basic_string<char, std::char_traits<char>, sanitizing_allocator<char> > sanitizing_string;
1484
1492 typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, sanitizing_allocator<wchar_t> > sanitizing_wstring;
1493
1497#ifdef _UNICODE
1499#else
1501#endif
1502
1506 template<size_t N>
1508 {
1509 public:
1514 {
1515 ZeroMemory(m_data, N);
1516 }
1517
1522 {
1523 SecureZeroMemory(m_data, N);
1524 }
1525
1526 public:
1527 unsigned char m_data[N];
1528 };
1529
1531}
Base template class to support converting GUID to string.
Definition: Common.h:1333
basic_string_guid(const GUID &guid, const _Elem *format)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1344
Base template class to support string formatting using FormatMessage() style templates.
Definition: Common.h:1208
basic_string_msg(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1283
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1293
basic_string_msg(HINSTANCE hInstance, WORD wLanguageID, UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition: Common.h:1255
basic_string_msg(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1273
basic_string_msg(const _Elem *format,...)
Initializes a new string and formats its contents using FormatMessage() style template.
Definition: Common.h:1218
basic_string_msg(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition: Common.h:1237
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1303
Base template class to support string formatting using printf() style templates.
Definition: Common.h:1123
basic_string_printf(const _Elem *format,...)
Initializes a new string and formats its contents using printf() style template.
Definition: Common.h:1133
basic_string_printf(HINSTANCE hInstance, WORD wLanguageID, UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition: Common.h:1170
basic_string_printf(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition: Common.h:1152
Base abstract template class to support object handle keeping for objects that support trivial handle...
Definition: Common.h:877
dplhandle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition: Common.h:918
handle_type duplicate() const
Duplicates and returns a new object handle.
Definition: Common.h:968
dplhandle< handle_type, INVAL > & operator=(dplhandle< handle_type, INVAL > &&h) noexcept
Moves the object.
Definition: Common.h:957
bool attach_duplicated(handle_type h)
Duplicates an object handle and sets a new object handle.
Definition: Common.h:982
virtual handle_type duplicate_internal(handle_type h) const noexcept=0
Abstract member function that must be implemented by child classes to do the actual object handle dup...
dplhandle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition: Common.h:891
dplhandle< handle_type, INVAL > & operator=(const dplhandle< handle_type, INVAL > &h) noexcept
Duplicates the object.
Definition: Common.h:929
dplhandle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition: Common.h:882
Base abstract template class to support generic object handle keeping.
Definition: Common.h:615
handle_type *& operator*() const
Returns the object handle value when the object handle is a pointer to a value (class,...
Definition: Common.h:705
virtual void free_internal() noexcept=0
Abstract member function that must be implemented by child classes to do the actual object destructio...
handle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition: Common.h:630
bool operator>=(handle_type h) const
Is handle greater than or equal to?
Definition: Common.h:778
handle_type operator->() const
Provides object handle member access when the object handle is a pointer to a class or struct.
Definition: Common.h:726
handle_type * operator&()
Returns the object handle reference.
Definition: Common.h:715
T handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:620
handle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition: Common.h:639
bool operator<(handle_type h) const
Is handle less than?
Definition: Common.h:752
handle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition: Common.h:666
bool operator!() const
Tests if the object handle is INVAL.
Definition: Common.h:739
handle< handle_type, INVAL > & operator=(handle< handle_type, INVAL > &&h) noexcept
Move assignment.
Definition: Common.h:678
bool operator!=(handle_type h) const
Is handle not equal to?
Definition: Common.h:804
void free()
Destroys the object.
Definition: Common.h:851
handle_type m_h
Object handle.
Definition: Common.h:866
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:829
bool operator==(handle_type h) const
Is handle equal to?
Definition: Common.h:817
handle(handle< handle_type, INVAL > &&h) noexcept
Move constructor.
Definition: Common.h:648
handle_type detach()
Dismisses the object handle from this class.
Definition: Common.h:841
bool operator>(handle_type h) const
Is handle greater than?
Definition: Common.h:791
bool operator<=(handle_type h) const
Is handle less than or equal to?
Definition: Common.h:765
Numerical runtime error.
Definition: Common.h:1011
num_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition: Common.h:1034
num_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition: Common.h:1022
error_type number() const
Returns the Windows error number.
Definition: Common.h:1043
_Tn error_type
Error number type.
Definition: Common.h:1013
error_type m_num
Numeric error code.
Definition: Common.h:1049
Helper class for returning pointers to std::unique_ptr (specialization for arrays)
Definition: Common.h:509
std::unique_ptr< _Ty[], _Dx > & m_own
Original owner of the pointer.
Definition: Common.h:598
ref_unique_ptr(ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition: Common.h:543
virtual ~ref_unique_ptr()
Returns ownership of the pointer.
Definition: Common.h:571
ref_unique_ptr & operator=(std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition: Common.h:528
ref_unique_ptr(std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition: Common.h:516
_Ty * m_ptr
Pointer.
Definition: Common.h:599
ref_unique_ptr & operator=(ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition: Common.h:557
Helper class for returning pointers to std::unique_ptr.
Definition: Common.h:416
std::unique_ptr< _Ty, _Dx > & m_own
Original owner of the pointer.
Definition: Common.h:470
_Ty * m_ptr
Pointer.
Definition: Common.h:471
ref_unique_ptr(ref_unique_ptr< _Ty, _Dx > &&other)
Moves object.
Definition: Common.h:433
~ref_unique_ptr()
Returns ownership of the pointer.
Definition: Common.h:443
ref_unique_ptr(std::unique_ptr< _Ty, _Dx > &owner)
Takes ownership of the pointer.
Definition: Common.h:423
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition: Common.h:1428
sanitizing_allocator(const sanitizing_allocator< _Ty > &_Othr)
Construct by copying.
Definition: Common.h:1451
void deallocate(pointer _Ptr, size_type _Size)
Deallocate object at _Ptr sanitizing its content first.
Definition: Common.h:1466
sanitizing_allocator(const sanitizing_allocator< _Other > &_Othr) noexcept
Construct from a related allocator.
Definition: Common.h:1459
std::allocator< _Ty > _Mybase
Base type.
Definition: Common.h:1430
sanitizing_allocator() noexcept
Construct default allocator.
Definition: Common.h:1444
Sanitizing BLOB.
Definition: Common.h:1508
sanitizing_blob()
Constructs uninitialized BLOB.
Definition: Common.h:1513
~sanitizing_blob()
Sanitizes BLOB.
Definition: Common.h:1521
Single-byte character implementation of a class to support converting GUID to string.
Definition: Common.h:1361
string_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1371
Windows runtime error.
Definition: Common.h:1056
tstring msg(DWORD dwLanguageId=0) const
Returns a user-readable Windows error message.
Definition: Common.h:1101
win_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition: Common.h:1074
win_runtime_error(const std::string &msg)
Constructs an exception using GetLastError()
Definition: Common.h:1083
win_runtime_error(const char *msg=nullptr)
Constructs an exception using GetLastError()
Definition: Common.h:1092
win_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition: Common.h:1064
Wide character implementation of a class to support converting GUID to string.
Definition: Common.h:1383
wstring_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1393
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition: Common.h:101
ref_unique_ptr< _Ty[], _Dx > get_ptr(std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Helper function template for returning pointers to std::unique_ptr (specialization for arrays)
Definition: Common.h:496
std::string tstring
Multi-byte / Wide-character string (according to _UNICODE)
Definition: Common.h:346
std::basic_string< wchar_t, std::char_traits< wchar_t >, sanitizing_allocator< wchar_t > > sanitizing_wstring
A sanitizing variant of std::wstring.
Definition: Common.h:1492
sanitizing_string sanitizing_tstring
Multi-byte / Wide-character sanitizing string (according to _UNICODE)
Definition: Common.h:1500
std::basic_string< char, std::char_traits< char >, sanitizing_allocator< char > > sanitizing_string
A sanitizing variant of std::string.
Definition: Common.h:1483
basic_string_printf< wchar_t, std::char_traits< wchar_t >, std::allocator< wchar_t > > wstring_printf
Wide character implementation of a class to support string formatting using printf() style templates.
Definition: Common.h:1192
string_guid tstring_guid
Multi-byte / Wide-character string GUID (according to _UNICODE)
Definition: Common.h:1407
basic_string_msg< wchar_t, std::char_traits< wchar_t >, std::allocator< wchar_t > > wstring_msg
Wide character implementation of a class to support string formatting using FormatMessage() style tem...
Definition: Common.h:1317
static int vsprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format, va_list arg)
Formats string using printf().
Definition: Common.h:259
static DWORD FormatMessage(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, std::basic_string< char, _Traits, _Ax > &str, va_list *Arguments)
Formats a message string.
Definition: Common.h:307
basic_string_printf< char, std::char_traits< char >, std::allocator< char > > string_printf
Single-byte character implementation of a class to support string formatting using printf() style tem...
Definition: Common.h:1187
static int vsnprintf(char *str, size_t capacity, const char *format, va_list arg)
Formats string using printf().
Definition: Common.h:228
string_printf tstring_printf
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition: Common.h:1200
static int sprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format,...)
Formats string using printf().
Definition: Common.h:292
basic_string_msg< char, std::char_traits< char >, std::allocator< char > > string_msg
Single-byte character implementation of a class to support string formatting using FormatMessage() st...
Definition: Common.h:1312
string_msg tstring_msg
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition: Common.h:1325
static const T invalid
Invalid handle value.
Definition: Common.h:625
LocalFree_delete() noexcept
Default construct.
Definition: Common.h:389
LocalFree_delete< _Ty > _Myt
This type.
Definition: Common.h:384
void operator()(_Other *) const
Delete a pointer of another type.
Definition: Common.h:405
void operator()(_Ty *_Ptr) const noexcept
Delete a pointer.
Definition: Common.h:394
Deleter for unique_ptr using LocalFree.
Definition: Common.h:354
LocalFree_delete< _Ty > _Myt
This type.
Definition: Common.h:355
LocalFree_delete(const LocalFree_delete< _Ty2 > &)
Construct from another LocalFree_delete.
Definition: Common.h:365
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: Common.h:372
LocalFree_delete()
Default construct.
Definition: Common.h:360
Convert this type to sanitizing_allocator<_Other>
Definition: Common.h:1437
sanitizing_allocator< _Other > other
Other type.
Definition: Common.h:1438