WinStd Additional templates and function helpers for Microsoft Windows using Standard C++ classes

Common.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2022 Amebis
4 Copyright © 2016 GÉANT
5*/
6
7#pragma once
8
9#define _WINSOCKAPI_ // Prevent inclusion of winsock.h in windows.h.
10#include <Windows.h>
11#include <assert.h>
12#include <stdarg.h>
13#include <tchar.h>
14#include <iostream>
15#include <memory>
16#include <stdexcept>
17#include <string>
18#include <vector>
19
25
29#ifndef __L
30#define __L(x) L ## x
31#endif
32
36#ifndef _L
37#define _L(x) __L(x)
38#endif
39
43#define WINSTD_STRING_IMPL(x) #x
44
48#define WINSTD_STRING(x) WINSTD_STRING_IMPL(x)
49
53#define WINSTD_NONCOPYABLE(C) \
54 private: \
55 C (_In_ const C &h) noexcept; \
56 C& operator=(_In_ const C &h) noexcept;
57
61#define WINSTD_NONMOVABLE(C) \
62 private: \
63 C (_Inout_ C &&h) noexcept; \
64 C& operator=(_Inout_ C &&h) noexcept;
65
66#ifndef WINSTD_STACK_BUFFER_BYTES
80#define WINSTD_STACK_BUFFER_BYTES 1024
81#endif
82
84
96
100#ifdef UNICODE
101#define PRINTF_LPTSTR "ls"
102#else
103#define PRINTF_LPTSTR "s"
104#endif
105
109#ifdef OLE2ANSI
110#define PRINTF_LPOLESTR "hs"
111#else
112#define PRINTF_LPOLESTR "ls"
113#endif
114
115#ifdef _UNICODE
116#define _tcin (std::wcin )
117#define _tcout (std::wcout)
118#define _tcerr (std::wcerr)
119#define _tclog (std::wclog)
120#else
121#define _tcin (std::cin )
122#define _tcout (std::cout)
123#define _tcerr (std::cerr)
124#define _tclog (std::clog)
125#endif
126
128
134
138#define WINSTD_HANDLE_IMPL(C, INVAL) \
139 public: \
140 C ( ) noexcept { } \
141 C (_In_opt_ handle_type h) noexcept : handle<handle_type, INVAL>( h ) { } \
142 C (_Inout_ C &&h) noexcept : handle<handle_type, INVAL>(std::move(h)) { } \
143 C& operator=(_In_opt_ handle_type h) noexcept { handle<handle_type, INVAL>::operator=( h ); return *this; } \
144 C& operator=(_Inout_ C &&h) noexcept { handle<handle_type, INVAL>::operator=(std::move(h)); return *this; } \
145 WINSTD_NONCOPYABLE(C)
146
150#define WINSTD_DPLHANDLE_IMPL(C, INVAL) \
151 public: \
152 C ( ) noexcept { } \
153 C (_In_opt_ handle_type h) noexcept : dplhandle<handle_type, INVAL>( h ) { } \
154 C (_In_ const C &h) noexcept : dplhandle<handle_type, INVAL>(duplicate_internal(h.m_h)) { } \
155 C (_Inout_ C &&h) noexcept : dplhandle<handle_type, INVAL>(std::move (h )) { } \
156 C& operator=(_In_opt_ handle_type h) noexcept { dplhandle<handle_type, INVAL>::operator=( h ); return *this; } \
157 C& operator=(_In_ const C &h) noexcept { dplhandle<handle_type, INVAL>::operator=( h ); return *this; } \
158 C& operator=(_Inout_ C &&h) noexcept { dplhandle<handle_type, INVAL>::operator=(std::move(h)); return *this; } \
159 private:
160
162
163#ifndef _FormatMessage_format_string_
164#define _FormatMessage_format_string_ _In_z_
165#endif
166
167#ifndef _LPCBYTE_DEFINED
168#define _LPCBYTE_DEFINED
169 typedef const BYTE *LPCBYTE;
170#endif
171
172#pragma warning(push)
173// Do not use _vsnprintf_s/_vsnwprintf_s(), since it terminates string by force even when we explicitly want to write unterminated string.
174// Threfore turn off compiler warning instead. ;)
175#pragma warning(disable: 4995)
176#pragma warning(disable: 4996)
177#pragma warning(disable: 4505) // Don't warn on unused code
178
181
192#if _MSC_VER <= 1600
193 static int vsnprintf(_Out_z_cap_(capacity) char *str, _In_ size_t capacity, _In_z_ _Printf_format_string_ const char *format, _In_ va_list arg)
194{
195 return _vsnprintf(str, capacity, format, arg);
196}
197#endif
198
209 static 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
210{
211 return _vsnwprintf(str, capacity, format, arg);
212}
213
223 template<class _Elem, class _Traits, class _Ax>
224 static int vsprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const _Elem *format, _In_ va_list arg)
225{
226 _Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
227
228 // Try with stack buffer first.
229 int count = vsnprintf(buf, _countof(buf) - 1, format, arg);
230 if (count >= 0) {
231 // Copy from stack.
232 str.assign(buf, count);
233 } else {
234 for (size_t capacity = 2*WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem);; capacity *= 2) {
235 // Allocate on heap and retry.
236 auto buf_dyn = std::make_unique<_Elem[]>(capacity);
237 count = vsnprintf(buf_dyn.get(), capacity - 1, format, arg);
238 if (count >= 0) {
239 str.assign(buf_dyn.get(), count);
240 break;
241 }
242 }
243 }
244
245 return count;
246}
247
256 template<class _Elem, class _Traits, class _Ax>
257 static int sprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const _Elem *format, ...)
258{
259 va_list arg;
260 va_start(arg, format);
261 const int res = vsprintf(str, format, arg);
262 va_end(arg);
263 return res;
264}
265
271 template<class _Traits, class _Ax>
272 static 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)
273{
274 std::unique_ptr<CHAR[], winstd::LocalFree_delete<CHAR[]> > lpBuffer;
275 DWORD dwResult = FormatMessageA(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPSTR>((LPSTR*)get_ptr(lpBuffer)), 0, Arguments);
276 if (dwResult)
277 str.assign(lpBuffer.get(), dwResult);
278 return dwResult;
279}
280
286 template<class _Traits, class _Ax>
287 static 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)
288{
289 std::unique_ptr<WCHAR[], winstd::LocalFree_delete<WCHAR[]> > lpBuffer;
290 DWORD dwResult = FormatMessageW(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPWSTR>((LPWSTR*)get_ptr(lpBuffer)), 0, Arguments);
291 if (dwResult)
292 str.assign(lpBuffer.get(), dwResult);
293 return dwResult;
294}
295
297
298#pragma warning(pop)
299
300 namespace winstd
301{
304
308#ifdef _UNICODE
309 typedef std::wstring tstring;
310#else
311 typedef std::string tstring;
312#endif
313
317 template <class _Ty>
318 struct LocalFree_delete
319 {
320 typedef LocalFree_delete<_Ty> _Myt;
321
325 LocalFree_delete() {}
326
330 template <class _Ty2> LocalFree_delete(const LocalFree_delete<_Ty2>&) {}
331
337 void operator()(_Ty *_Ptr) const
338 {
339 LocalFree(_Ptr);
340 }
341 };
342
346 template <class _Ty>
347 struct LocalFree_delete<_Ty[]>
348 {
349 typedef LocalFree_delete<_Ty> _Myt;
350
354 LocalFree_delete() noexcept {}
355
359 void operator()(_Frees_ptr_opt_ _Ty *_Ptr) const noexcept
360 {
361 LocalFree(_Ptr);
362 }
363
369 template<class _Other>
370 void operator()(_Other *) const
371 {
372 LocalFree(_Ptr);
373 }
374 };
375
379 template<class _Ty, class _Dx>
380 class ref_unique_ptr
381 {
382 public:
388 ref_unique_ptr(_Inout_ std::unique_ptr<_Ty, _Dx> &owner) :
389 m_own(owner),
390 m_ptr(owner.release())
391 {}
392
398 ref_unique_ptr(_Inout_ ref_unique_ptr<_Ty, _Dx> &&other) :
399 m_own(other.m_own),
400 m_ptr(other.m_ptr)
401 {
402 other.m_ptr = nullptr;
403 }
404
408 ~ref_unique_ptr()
409 {
410 if (m_ptr != nullptr)
411 m_own.reset(m_ptr);
412 }
413
419 operator typename _Ty**()
420 {
421 return &m_ptr;
422 }
423
429 operator typename _Ty*&()
430 {
431 return m_ptr;
432 }
433
434 protected:
435 std::unique_ptr<_Ty, _Dx> &m_own;
436 _Ty *m_ptr;
437 };
438
446 template<class _Ty, class _Dx>
447 ref_unique_ptr<_Ty, _Dx> get_ptr(_Inout_ std::unique_ptr<_Ty, _Dx> &owner) noexcept
448 {
449 return ref_unique_ptr<_Ty, _Dx>(owner);
450 }
451
460 template<class _Ty, class _Dx>
461 ref_unique_ptr<_Ty[], _Dx> get_ptr(_Inout_ std::unique_ptr<_Ty[], _Dx> &owner) noexcept
462 {
463 return ref_unique_ptr<_Ty[], _Dx>(owner);
464 }
465
470 #pragma warning(push)
471 #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.
472 template<class _Ty, class _Dx>
473 class ref_unique_ptr<_Ty[], _Dx>
474 {
475 public:
481 ref_unique_ptr(_Inout_ std::unique_ptr<_Ty[], _Dx> &owner) noexcept :
482 m_own(owner),
483 m_ptr(owner.release())
484 {}
485
493 ref_unique_ptr& operator=(_Inout_ std::unique_ptr<_Ty[], _Dx> &owner) noexcept
494 {
495 if (this != &other) {
496 m_own = owner;
497 m_ptr = owner.release();
498 }
499
500 return *this;
501 }
502
508 ref_unique_ptr(_Inout_ ref_unique_ptr<_Ty[], _Dx> &&other) :
509 m_own(other.m_own),
510 m_ptr(other.m_ptr)
511 {
512 other.m_ptr = nullptr;
513 }
514
522 ref_unique_ptr& operator=(_Inout_ ref_unique_ptr<_Ty[], _Dx> &&other)
523 {
524 if (this != &other) {
525 m_own = other.m_own;
526 m_ptr = other.m_ptr;
527 other.m_ptr = nullptr;
528 }
529
530 return *this;
531 }
532
536 virtual ~ref_unique_ptr()
537 {
538 if (m_ptr != nullptr)
539 m_own.reset(m_ptr);
540 }
541
547 operator typename _Ty**() noexcept
548 {
549 return &m_ptr;
550 }
551
557 operator typename _Ty*&()
558 {
559 return m_ptr;
560 }
561
562 protected:
563 std::unique_ptr<_Ty[], _Dx> &m_own;
564 _Ty *m_ptr;
565 };
566 #pragma warning(pop)
567
569
572
578 template <class T, const T INVAL>
579 class handle
580 {
581 public:
585 typedef T handle_type;
586
590 static const T invalid;
591
595 handle() noexcept : m_h(invalid)
596 {
597 }
598
604 handle(_In_opt_ handle_type h) noexcept : m_h(h)
605 {
606 }
607
613 handle(_Inout_ handle<handle_type, INVAL> &&h) noexcept
614 {
615 // Transfer handle.
616 m_h = h.m_h;
617 h.m_h = invalid;
618 }
619
620 private:
621 // This class is noncopyable.
622 handle(_In_ const handle<handle_type, INVAL> &h) noexcept {};
623 handle<handle_type, INVAL>& operator=(_In_ const handle<handle_type, INVAL> &h) noexcept {};
624
625 public:
631 handle<handle_type, INVAL>& operator=(_In_opt_ handle_type h) noexcept
632 {
633 attach(h);
634 return *this;
635 }
636
642 #pragma warning(suppress: 26432) // Move constructor is also present, but not detected by code analysis somehow.
643 handle<handle_type, INVAL>& operator=(_Inout_ handle<handle_type, INVAL> &&h) noexcept
644 {
645 if (this != std::addressof(h)) {
646 // Transfer handle.
647 if (m_h != invalid)
648 free_internal();
649 m_h = h.m_h;
650 h.m_h = invalid;
651 }
652 return *this;
653 }
654
660 operator handle_type() const
661 {
662 return m_h;
663 }
664
670 handle_type*& operator*() const
671 {
672 assert(m_h != invalid);
673 return *m_h;
674 }
675
680 handle_type* operator&()
681 {
682 assert(m_h == invalid);
683 return &m_h;
684 }
685
691 handle_type operator->() const
692 {
693 assert(m_h != invalid);
694 return m_h;
695 }
696
704 bool operator!() const
705 {
706 return m_h == invalid;
707 }
708
717 bool operator<(_In_opt_ handle_type h) const
718 {
719 return m_h < h;
720 }
721
730 bool operator<=(_In_opt_ handle_type h) const
731 {
732 return !operator>(h);
733 }
734
743 bool operator>=(_In_opt_ handle_type h) const
744 {
745 return !operator<(h);
746 }
747
756 bool operator>(_In_opt_ handle_type h) const
757 {
758 return h < m_h;
759 }
760
769 bool operator!=(_In_opt_ handle_type h) const
770 {
771 return !operator==(h);
772 }
773
782 bool operator==(_In_opt_ handle_type h) const
783 {
784 return m_h == h;
785 }
786
794 void attach(_In_opt_ handle_type h) noexcept
795 {
796 if (m_h != invalid)
797 free_internal();
798 m_h = h;
799 }
800
806 handle_type detach()
807 {
808 handle_type h = m_h;
809 m_h = invalid;
810 return h;
811 }
812
816 void free()
817 {
818 if (m_h != invalid) {
819 free_internal();
820 m_h = invalid;
821 }
822 }
823
825
826 protected:
830 virtual void free_internal() noexcept = 0;
831
832 protected:
833 handle_type m_h;
834 };
835
836 template <class T, const T INVAL>
837 const T handle<T, INVAL>::invalid = INVAL;
838
842 template <class T, T INVAL>
843 class dplhandle : public handle<T, INVAL>
844 {
845 public:
849 dplhandle() noexcept
850 {
851 }
852
858 dplhandle(_In_opt_ handle_type h) noexcept : handle<handle_type, INVAL>(h)
859 {
860 }
861
867 dplhandle<handle_type, INVAL>(_In_ const dplhandle<handle_type, INVAL> &h) noexcept : handle<handle_type, INVAL>(duplicate_internal(h.m_h))
868 {
869 }
870
876 dplhandle<handle_type, INVAL>(_Inout_ dplhandle<handle_type, INVAL> &&h) noexcept : handle<handle_type, INVAL>(std::move(h))
877 {
878 }
879
885 dplhandle<handle_type, INVAL>& operator=(_In_opt_ handle_type h) noexcept
886 {
887 handle<handle_type, INVAL>::operator=(h);
888 return *this;
889 }
890
896 dplhandle<handle_type, INVAL>& operator=(_In_ const dplhandle<handle_type, INVAL> &h) noexcept
897 {
898 if (this != std::addressof(h)) {
899 if (h.m_h != invalid) {
900 handle_type h_new = duplicate_internal(h.m_h);
901 if (h_new != invalid) {
902 if (m_h != invalid)
903 free_internal();
904
905 m_h = h_new;
906 } else
907 assert(0); // Could not duplicate the handle
908 } else {
909 if (m_h != invalid)
910 free_internal();
911
912 m_h = invalid;
913 }
914 }
915 return *this;
916 }
917
923 #pragma warning(disable: 26432) // Move constructor is also present, but not detected by code analysis somehow.
924 dplhandle<handle_type, INVAL>& operator=(_Inout_ dplhandle<handle_type, INVAL> &&h) noexcept
925 {
926 handle<handle_type, INVAL>::operator=(std::move(h));
927 return *this;
928 }
929
935 handle_type duplicate() const
936 {
937 return m_h != invalid ? duplicate_internal(m_h) : invalid;
938 }
939
949 bool attach_duplicated(_In_opt_ handle_type h)
950 {
951 if (m_h != invalid)
952 free_internal();
953
954 return h != invalid ? (m_h = duplicate_internal(h)) != invalid : (m_h = invalid, true);
955 }
956
957 protected:
965 virtual handle_type duplicate_internal(_In_ handle_type h) const noexcept = 0;
966 };
967
969
975
979 template <typename _Tn>
980 class num_runtime_error : public std::runtime_error
981 {
982 public:
983 typedef _Tn error_type;
984
985 public:
992 num_runtime_error(_In_ error_type num, _In_ const std::string& msg) :
993 m_num(num),
994 runtime_error(msg)
995 {
996 }
997
1004 num_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) :
1005 m_num(num),
1006 runtime_error(msg)
1007 {
1008 }
1009
1013 error_type number() const
1014 {
1015 return m_num;
1016 }
1017
1018 protected:
1019 error_type m_num;
1020 };
1021
1025 class win_runtime_error : public num_runtime_error<DWORD>
1026 {
1027 public:
1034 win_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<DWORD>(num, msg)
1035 {
1036 }
1037
1044 win_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) : num_runtime_error<DWORD>(num, msg)
1045 {
1046 }
1047
1053 win_runtime_error(_In_ const std::string& msg) : num_runtime_error<DWORD>(GetLastError(), msg)
1054 {
1055 }
1056
1062 win_runtime_error(_In_opt_z_ const char *msg = nullptr) : num_runtime_error<DWORD>(GetLastError(), msg)
1063 {
1064 }
1065
1071 tstring msg(_In_opt_ DWORD dwLanguageId = 0) const
1072 {
1073 tstring str;
1074 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, m_num, dwLanguageId, str, NULL)) {
1075 // Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
1076 str.erase(str.find_last_not_of(_T(" \t\n\r\f\v")) + 1);
1077 } else
1078 sprintf(str, m_num >= 0x10000 ? _T("Error 0x%X") : _T("Error %u"), m_num);
1079 return str;
1080 }
1081 };
1082
1084
1087
1091 template<class _Elem, class _Traits, class _Ax>
1092 class basic_string_printf : public std::basic_string<_Elem, _Traits, _Ax>
1093 {
1094 public:
1097
1103 basic_string_printf(_In_z_ _Printf_format_string_ const _Elem *format, ...)
1104 {
1105 va_list arg;
1106 va_start(arg, format);
1107 vsprintf(*this, format, arg);
1108 va_end(arg);
1109 }
1110
1112
1115
1122 basic_string_printf(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
1123 {
1124 _Myt format;
1125 ATLENSURE(format.LoadString(hInstance, nFormatID));
1126
1127 va_list arg;
1128 va_start(arg, nFormatID);
1129 vsprintf(*this, format, arg);
1130 va_end(arg);
1131 }
1132
1140 basic_string_printf(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
1141 {
1142 _Myt format;
1143 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1144
1145 va_list arg;
1146 va_start(arg, nFormatID);
1147 vsprintf(*this, format, arg);
1148 va_end(arg);
1149 }
1150
1152 };
1153
1157 typedef basic_string_printf<char, std::char_traits<char>, std::allocator<char> > string_printf;
1158
1162 typedef basic_string_printf<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > wstring_printf;
1163
1167#ifdef _UNICODE
1168 typedef wstring_printf tstring_printf;
1169#else
1170 typedef string_printf tstring_printf;
1171#endif
1172
1176 template<class _Elem, class _Traits, class _Ax>
1177 class basic_string_msg : public std::basic_string<_Elem, _Traits, _Ax>
1178 {
1179 public:
1182
1188 basic_string_msg(_In_z_ _FormatMessage_format_string_ const _Elem *format, ...)
1189 {
1190 va_list arg;
1191 va_start(arg, format);
1192 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1193 va_end(arg);
1194 }
1195
1197
1200
1207 basic_string_msg(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
1208 {
1209 _Myt format(GetManager());
1210 ATLENSURE(format.LoadString(hInstance, nFormatID));
1211
1212 va_list arg;
1213 va_start(arg, nFormatID);
1214 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1215 va_end(arg);
1216 }
1217
1225 basic_string_msg(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
1226 {
1227 _Myt format(GetManager());
1228 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1229
1230 va_list arg;
1231 va_start(arg, nFormatID);
1232 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1233 va_end(arg);
1234 }
1235
1237
1243 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ va_list *Arguments)
1244 {
1245 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *this, Arguments);
1246 }
1247
1253 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ DWORD_PTR *Arguments)
1254 {
1255 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *this, (va_list*)Arguments);
1256 }
1257
1263 basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ va_list *Arguments)
1264 {
1265 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *this, Arguments);
1266 }
1267
1273 basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ DWORD_PTR *Arguments)
1274 {
1275 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *this, (va_list*)Arguments);
1276 }
1277 };
1278
1282 typedef basic_string_msg<char, std::char_traits<char>, std::allocator<char> > string_msg;
1283
1287 typedef basic_string_msg<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > wstring_msg;
1288
1292#ifdef _UNICODE
1293 typedef wstring_msg tstring_msg;
1294#else
1295 typedef string_msg tstring_msg;
1296#endif
1297
1301 template<class _Elem, class _Traits, class _Ax>
1302 class basic_string_guid : public std::basic_string<_Elem, _Traits, _Ax>
1303 {
1304 public:
1307
1314 basic_string_guid(_In_ const GUID &guid, _In_z_ _Printf_format_string_ const _Elem *format)
1315 {
1316 sprintf<_Elem, _Traits, _Ax>(*this, format,
1317 guid.Data1,
1318 guid.Data2,
1319 guid.Data3,
1320 guid.Data4[0], guid.Data4[1],
1321 guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
1322 }
1323
1325 };
1326
1330 class string_guid : public basic_string_guid<char, std::char_traits<char>, std::allocator<char> >
1331 {
1332 public:
1335
1341 string_guid(_In_ const GUID &guid) :
1342 basic_string_guid<char, std::char_traits<char>, std::allocator<char> >(guid, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
1343 {
1344 }
1345
1347 };
1348
1352 class wstring_guid : public basic_string_guid<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >
1353 {
1354 public:
1357
1363 wstring_guid(_In_ const GUID &guid) :
1364 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}")
1365 {
1366 }
1367
1369 };
1370
1374#ifdef _UNICODE
1375 typedef wstring_guid tstring_guid;
1376#else
1377 typedef string_guid tstring_guid;
1378#endif
1379
1381
1387
1388 // winstd::sanitizing_allocator::destroy() member generates _Ptr parameter not used warning for primitive datatypes _Ty.
1389 #pragma warning(push)
1390 #pragma warning(disable: 4100)
1391
1399 template<class _Ty>
1400 class sanitizing_allocator : public std::allocator<_Ty>
1401 {
1402 public:
1403 typedef std::allocator<_Ty> _Mybase;
1404
1408 template<class _Other>
1409 struct rebind
1410 {
1411 typedef sanitizing_allocator<_Other> other;
1412 };
1413
1417 sanitizing_allocator() noexcept : _Mybase()
1418 {
1419 }
1420
1424 sanitizing_allocator(_In_ const sanitizing_allocator<_Ty> &_Othr) : _Mybase(_Othr)
1425 {
1426 }
1427
1431 template<class _Other>
1432 sanitizing_allocator(_In_ const sanitizing_allocator<_Other> &_Othr) noexcept : _Mybase(_Othr)
1433 {
1434 }
1435
1439 void deallocate(_In_ pointer _Ptr, _In_ size_type _Size)
1440 {
1441 // Sanitize then free.
1442 SecureZeroMemory(_Ptr, _Size);
1443 _Mybase::deallocate(_Ptr, _Size);
1444 }
1445 };
1446
1447 #pragma warning(pop)
1448
1456 typedef std::basic_string<char, std::char_traits<char>, sanitizing_allocator<char> > sanitizing_string;
1457
1465 typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, sanitizing_allocator<wchar_t> > sanitizing_wstring;
1466
1470#ifdef _UNICODE
1471 typedef sanitizing_wstring sanitizing_tstring;
1472#else
1473 typedef sanitizing_string sanitizing_tstring;
1474#endif
1475
1479 template<size_t N>
1480 class sanitizing_blob
1481 {
1482 public:
1486 sanitizing_blob()
1487 {
1488 ZeroMemory(m_data, N);
1489 }
1490
1494 ~sanitizing_blob()
1495 {
1496 SecureZeroMemory(m_data, N);
1497 }
1498
1499 public:
1500 unsigned char m_data[N];
1501 };
1502
1504}
winstd::basic_string_guid
Base template class to support converting GUID to string.
Definition: Common.h:1303
winstd::basic_string_guid::basic_string_guid
basic_string_guid(_In_ const GUID &guid, _In_z_ _Printf_format_string_ const _Elem *format)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1314
winstd::basic_string_msg
Base template class to support string formatting using FormatMessage() style templates.
Definition: Common.h:1178
winstd::basic_string_msg::basic_string_msg
basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1243
winstd::basic_string_msg::basic_string_msg
basic_string_msg(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition: Common.h:1225
winstd::basic_string_msg::basic_string_msg
basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1273
winstd::basic_string_msg::basic_string_msg
basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1263
winstd::basic_string_msg::basic_string_msg
basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1253
winstd::basic_string_msg::basic_string_msg
basic_string_msg(_In_ HINSTANCE hInstance, _In_ UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition: Common.h:1207
winstd::basic_string_msg::basic_string_msg
basic_string_msg(_In_z_ _FormatMessage_format_string_ const _Elem *format,...)
Initializes a new string and formats its contents using FormatMessage() style template.
Definition: Common.h:1188
winstd::basic_string_printf
Base template class to support string formatting using printf() style templates.
Definition: Common.h:1093
winstd::basic_string_printf::basic_string_printf
basic_string_printf(_In_z_ _Printf_format_string_ const _Elem *format,...)
Initializes a new string and formats its contents using printf() style template.
Definition: Common.h:1103
winstd::basic_string_printf::basic_string_printf
basic_string_printf(_In_ HINSTANCE hInstance, _In_ UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition: Common.h:1122
winstd::basic_string_printf::basic_string_printf
basic_string_printf(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition: Common.h:1140
winstd::dplhandle
Base abstract template class to support object handle keeping for objects that support handle duplica...
Definition: Common.h:844
winstd::dplhandle::attach_duplicated
bool attach_duplicated(_In_opt_ handle_type h)
Duplicates an object handle and sets a new object handle.
Definition: Common.h:949
winstd::dplhandle::duplicate
handle_type duplicate() const
Duplicates and returns a new object handle.
Definition: Common.h:935
winstd::dplhandle::operator=
dplhandle< handle_type, INVAL > & operator=(_In_opt_ handle_type h) noexcept
Attaches already available object handle.
Definition: Common.h:885
winstd::dplhandle::operator=
dplhandle< handle_type, INVAL > & operator=(_In_ const dplhandle< handle_type, INVAL > &h) noexcept
Duplicates the object.
Definition: Common.h:896
winstd::dplhandle::duplicate_internal
virtual handle_type duplicate_internal(_In_ handle_type h) const noexcept=0
Abstract member function that must be implemented by child classes to do the actual object handle dup...
winstd::dplhandle::dplhandle
dplhandle(_In_opt_ handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition: Common.h:858
winstd::dplhandle::operator=
dplhandle< handle_type, INVAL > & operator=(_Inout_ dplhandle< handle_type, INVAL > &&h) noexcept
Moves the object.
Definition: Common.h:924
winstd::dplhandle::dplhandle
dplhandle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition: Common.h:849
winstd::handle
Base abstract template class to support generic object handle keeping.
Definition: Common.h:580
winstd::handle::operator*
handle_type *& operator*() const
Returns the object handle value when the object handle is a pointer to a value (class,...
Definition: Common.h:670
winstd::handle::free_internal
virtual void free_internal() noexcept=0
Abstract member function that must be implemented by child classes to do the actual object destructio...
winstd::handle::operator<=
bool operator<=(_In_opt_ handle_type h) const
Is handle less than or equal to?
Definition: Common.h:730
winstd::handle::handle
handle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition: Common.h:595
winstd::handle::handle
handle(_In_opt_ handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition: Common.h:604
winstd::handle::operator->
handle_type operator->() const
Provides object handle member access when the object handle is a pointer to a class or struct.
Definition: Common.h:691
winstd::handle::operator&
handle_type * operator&()
Returns the object handle reference.
Definition: Common.h:680
winstd::handle::operator!=
bool operator!=(_In_opt_ handle_type h) const
Is handle not equal to?
Definition: Common.h:769
winstd::handle::operator==
bool operator==(_In_opt_ handle_type h) const
Is handle equal to?
Definition: Common.h:782
winstd::handle::handle_type
T handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:585
winstd::handle::handle
handle(_Inout_ handle< handle_type, INVAL > &&h) noexcept
Move constructor.
Definition: Common.h:613
winstd::handle::operator!
bool operator!() const
Tests if the object handle is INVAL.
Definition: Common.h:704
winstd::handle::free
void free()
Destroys the object.
Definition: Common.h:816
winstd::handle::operator>=
bool operator>=(_In_opt_ handle_type h) const
Is handle greater than or equal to?
Definition: Common.h:743
winstd::handle::operator<
bool operator<(_In_opt_ handle_type h) const
Is handle less than?
Definition: Common.h:717
winstd::handle::operator=
handle< handle_type, INVAL > & operator=(_In_opt_ handle_type h) noexcept
Attaches already available object handle.
Definition: Common.h:631
winstd::handle::operator=
handle< handle_type, INVAL > & operator=(_Inout_ handle< handle_type, INVAL > &&h) noexcept
Move assignment.
Definition: Common.h:643
winstd::handle::attach
void attach(_In_opt_ handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:794
winstd::handle::operator>
bool operator>(_In_opt_ handle_type h) const
Is handle greater than?
Definition: Common.h:756
winstd::handle::m_h
handle_type m_h
Object handle.
Definition: Common.h:833
winstd::handle::detach
handle_type detach()
Dismisses the object handle from this class.
Definition: Common.h:806
winstd::num_runtime_error
Numerical runtime error.
Definition: Common.h:981
winstd::num_runtime_error::num_runtime_error
num_runtime_error(_In_ error_type num, _In_ const std::string &msg)
Constructs an exception.
Definition: Common.h:992
winstd::num_runtime_error::number
error_type number() const
Returns the Windows error number.
Definition: Common.h:1013
winstd::num_runtime_error::error_type
_Tn error_type
Error number type.
Definition: Common.h:983
winstd::num_runtime_error::m_num
error_type m_num
Numeric error code.
Definition: Common.h:1019
winstd::num_runtime_error::num_runtime_error
num_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg=nullptr)
Constructs an exception.
Definition: Common.h:1004
winstd::ref_unique_ptr< _Ty[], _Dx >
Helper class for returning pointers to std::unique_ptr (specialization for arrays)
Definition: Common.h:474
winstd::ref_unique_ptr< _Ty[], _Dx >::ref_unique_ptr
ref_unique_ptr(_Inout_ std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition: Common.h:481
winstd::ref_unique_ptr< _Ty[], _Dx >::m_own
std::unique_ptr< _Ty[], _Dx > & m_own
Original owner of the pointer.
Definition: Common.h:563
winstd::ref_unique_ptr< _Ty[], _Dx >::~ref_unique_ptr
virtual ~ref_unique_ptr()
Returns ownership of the pointer.
Definition: Common.h:536
winstd::ref_unique_ptr< _Ty[], _Dx >::ref_unique_ptr
ref_unique_ptr(_Inout_ ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition: Common.h:508
winstd::ref_unique_ptr< _Ty[], _Dx >::m_ptr
_Ty * m_ptr
Pointer.
Definition: Common.h:564
winstd::ref_unique_ptr< _Ty[], _Dx >::operator=
ref_unique_ptr & operator=(_Inout_ ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition: Common.h:522
winstd::ref_unique_ptr< _Ty[], _Dx >::operator=
ref_unique_ptr & operator=(_Inout_ std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition: Common.h:493
winstd::ref_unique_ptr
Helper class for returning pointers to std::unique_ptr.
Definition: Common.h:381
winstd::ref_unique_ptr::ref_unique_ptr
ref_unique_ptr(_Inout_ std::unique_ptr< _Ty, _Dx > &owner)
Takes ownership of the pointer.
Definition: Common.h:388
winstd::ref_unique_ptr::m_own
std::unique_ptr< _Ty, _Dx > & m_own
Original owner of the pointer.
Definition: Common.h:435
winstd::ref_unique_ptr::ref_unique_ptr
ref_unique_ptr(_Inout_ ref_unique_ptr< _Ty, _Dx > &&other)
Moves object.
Definition: Common.h:398
winstd::ref_unique_ptr::m_ptr
_Ty * m_ptr
Pointer.
Definition: Common.h:436
winstd::ref_unique_ptr::~ref_unique_ptr
~ref_unique_ptr()
Returns ownership of the pointer.
Definition: Common.h:408
winstd::sanitizing_allocator
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition: Common.h:1401
winstd::sanitizing_allocator::deallocate
void deallocate(_In_ pointer _Ptr, _In_ size_type _Size)
Deallocate object at _Ptr sanitizing its content first.
Definition: Common.h:1439
winstd::sanitizing_allocator::sanitizing_allocator
sanitizing_allocator(_In_ const sanitizing_allocator< _Ty > &_Othr)
Construct by copying.
Definition: Common.h:1424
winstd::sanitizing_allocator::sanitizing_allocator
sanitizing_allocator(_In_ const sanitizing_allocator< _Other > &_Othr) noexcept
Construct from a related allocator.
Definition: Common.h:1432
winstd::sanitizing_allocator::_Mybase
std::allocator< _Ty > _Mybase
Base type.
Definition: Common.h:1403
winstd::sanitizing_allocator::sanitizing_allocator
sanitizing_allocator() noexcept
Construct default allocator.
Definition: Common.h:1417
winstd::sanitizing_blob
Sanitizing BLOB.
Definition: Common.h:1481
winstd::sanitizing_blob::sanitizing_blob
sanitizing_blob()
Constructs uninitialized BLOB.
Definition: Common.h:1486
winstd::sanitizing_blob::~sanitizing_blob
~sanitizing_blob()
Sanitizes BLOB.
Definition: Common.h:1494
winstd::string_guid
Single-byte character implementation of a class to support converting GUID to string.
Definition: Common.h:1331
winstd::string_guid::string_guid
string_guid(_In_ const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1341
winstd::win_runtime_error
Windows runtime error.
Definition: Common.h:1026
winstd::win_runtime_error::msg
tstring msg(_In_opt_ DWORD dwLanguageId=0) const
Returns a user-readable Windows error message.
Definition: Common.h:1071
winstd::win_runtime_error::win_runtime_error
win_runtime_error(_In_ const std::string &msg)
Constructs an exception using GetLastError()
Definition: Common.h:1053
winstd::win_runtime_error::win_runtime_error
win_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg=nullptr)
Constructs an exception.
Definition: Common.h:1044
winstd::win_runtime_error::win_runtime_error
win_runtime_error(_In_opt_z_ const char *msg=nullptr)
Constructs an exception using GetLastError()
Definition: Common.h:1062
winstd::win_runtime_error::win_runtime_error
win_runtime_error(_In_ error_type num, _In_ const std::string &msg)
Constructs an exception.
Definition: Common.h:1034
winstd::wstring_guid
Wide character implementation of a class to support converting GUID to string.
Definition: Common.h:1353
winstd::wstring_guid::wstring_guid
wstring_guid(_In_ const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1363
WINSTD_STACK_BUFFER_BYTES
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition: Common.h:80
winstd::tstring
std::string tstring
Multi-byte / Wide-character string (according to _UNICODE)
Definition: Common.h:311
winstd::get_ptr
ref_unique_ptr< _Ty[], _Dx > get_ptr(_Inout_ std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Helper function template for returning pointers to std::unique_ptr (specialization for arrays)
Definition: Common.h:461
winstd::sanitizing_wstring
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:1465
winstd::sanitizing_tstring
sanitizing_string sanitizing_tstring
Multi-byte / Wide-character sanitizing string (according to _UNICODE)
Definition: Common.h:1473
winstd::sanitizing_string
std::basic_string< char, std::char_traits< char >, sanitizing_allocator< char > > sanitizing_string
A sanitizing variant of std::string.
Definition: Common.h:1456
winstd::wstring_printf
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:1162
winstd::tstring_guid
string_guid tstring_guid
Multi-byte / Wide-character string GUID (according to _UNICODE)
Definition: Common.h:1377
winstd::wstring_msg
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:1287
winstd::string_printf
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:1157
winstd::tstring_printf
string_printf tstring_printf
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition: Common.h:1170
winstd::string_msg
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:1282
winstd::tstring_msg
string_msg tstring_msg
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition: Common.h:1295
winstd::handle::invalid
static const T invalid
Invalid handle value.
Definition: Common.h:590
winstd::LocalFree_delete< _Ty[]>::operator()
void operator()(_Frees_ptr_opt_ _Ty *_Ptr) const noexcept
Delete a pointer.
Definition: Common.h:359
winstd::LocalFree_delete< _Ty[]>::LocalFree_delete
LocalFree_delete() noexcept
Default construct.
Definition: Common.h:354
winstd::LocalFree_delete< _Ty[]>::_Myt
LocalFree_delete< _Ty > _Myt
This type.
Definition: Common.h:349
winstd::LocalFree_delete< _Ty[]>::operator()
void operator()(_Other *) const
Delete a pointer of another type.
Definition: Common.h:370
winstd::LocalFree_delete
Deleter for unique_ptr using LocalFree.
Definition: Common.h:319
winstd::LocalFree_delete::_Myt
LocalFree_delete< _Ty > _Myt
This type.
Definition: Common.h:320
winstd::LocalFree_delete::LocalFree_delete
LocalFree_delete(const LocalFree_delete< _Ty2 > &)
Construct from another LocalFree_delete.
Definition: Common.h:330
winstd::LocalFree_delete::operator()
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: Common.h:337
winstd::LocalFree_delete::LocalFree_delete
LocalFree_delete()
Default construct.
Definition: Common.h:325
winstd::sanitizing_allocator::rebind
Convert this type to sanitizing_allocator<_Other>
Definition: Common.h:1410
winstd::sanitizing_allocator::rebind::other
sanitizing_allocator< _Other > other
Other type.
Definition: Common.h:1411