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) \
54private: \
55 C (_In_ const C &h) noexcept; \
56 C& operator=(_In_ const C &h) noexcept;
57
61#define WINSTD_NONMOVABLE(C) \
62private: \
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) \
139public: \
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; } \
145WINSTD_NONCOPYABLE(C)
146
150#define WINSTD_DPLHANDLE_IMPL(C, INVAL) \
151public: \
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; } \
159private:
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
169typedef 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
193static 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
209static 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
223template<class _Elem, class _Traits, class _Ax>
224static 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
256template<class _Elem, class _Traits, class _Ax>
257static 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
271template<class _Traits, class _Ax>
272static 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
286template<class _Traits, class _Ax>
287static 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
300namespace 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>
319 {
321
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 {
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>
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
399 m_own(other.m_own),
400 m_ptr(other.m_ptr)
401 {
402 other.m_ptr = nullptr;
403 }
404
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
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:
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.
644 {
645 if (this != std::addressof(h)) {
646 // Transfer handle.
647 if (m_h != invalid)
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
671 {
672 assert(m_h != invalid);
673 return *m_h;
674 }
675
681 {
682 assert(m_h == invalid);
683 return &m_h;
684 }
685
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)
798 m_h = h;
799 }
800
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) {
820 m_h = invalid;
821 }
822 }
823
825
826 protected:
830 virtual void free_internal() noexcept = 0;
831
832 protected:
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
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
886 {
888 return *this;
889 }
890
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)
904
905 m_h = h_new;
906 } else
907 assert(0); // Could not duplicate the handle
908 } else {
909 if (m_h != invalid)
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.
925 {
927 return *this;
928 }
929
936 {
937 return m_h != invalid ? duplicate_internal(m_h) : invalid;
938 }
939
950 {
951 if (m_h != invalid)
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
972
976 template <class T>
978 {
979 public:
983 typedef size_t size_type;
984
988 typedef T value_type;
989
993 typedef T& reference;
994
998 typedef const T& const_reference;
999
1003 typedef T* pointer;
1004
1008 typedef const T* const_pointer;
1009
1010 public:
1016 vector_queue(_In_ size_type size_max) :
1017 m_data(new value_type[size_max]),
1018 m_head(0),
1019 m_count(0),
1020 m_size_max(size_max)
1021 {
1022 }
1023
1030 m_data(new value_type[other.m_size_max]),
1031 m_head(other.m_head),
1032 m_count(other.m_count),
1033 m_size_max(other.m_size_max)
1034 {
1035 // Copy elements.
1036 for (size_type i = 0; i < m_count; i++) {
1037 size_type i_l = abs(i);
1038 m_data[i_l] = other.m_data[i_l];
1039 }
1040 }
1041
1046 {
1047 if (m_data) delete [] m_data;
1048 }
1049
1056 m_data (std::move(other.m_data )),
1057 m_head (std::move(other.m_head )),
1058 m_count (std::move(other.m_count )),
1059 m_size_max(std::move(other.m_size_max))
1060 {
1061 // Reset other to consistent state.
1062 other.m_data = NULL;
1063 other.m_head = 0;
1064 other.m_count = 0;
1065 other.m_size_max = 0;
1066 }
1067
1074 {
1075 if (this != std::addressof(other)) {
1076 m_head = other.m_head;
1077 m_count = other.m_count;
1078 m_size_max = other.m_size_max;
1079
1080 // Copy elements.
1081 if (m_data) delete [] m_data;
1082 m_data = new value_type[other.m_size_max];
1083 for (size_type i = 0; i < m_count; i++) {
1084 size_type i_l = abs(i);
1085 m_data[i_l] = other.m_data[i_l];
1086 }
1087 }
1088
1089 return *this;
1090 }
1091
1098 {
1099 if (this != std::addressof(other)) {
1100 m_data = std::move(other.m_data );
1101 m_head = std::move(other.m_head );
1102 m_count = std::move(other.m_count );
1103 m_size_max = std::move(other.m_size_max);
1104
1105 // Reset other to consistent state.
1106 other.m_data = NULL;
1107 other.m_head = 0;
1108 other.m_count = 0;
1109 other.m_size_max = 0;
1110 }
1111
1112 return *this;
1113 }
1114
1119 {
1120 return m_count;
1121 }
1122
1127 {
1128 return m_size_max;
1129 }
1130
1134 void clear()
1135 {
1136 m_count = 0;
1137 }
1138
1142 bool empty() const
1143 {
1144 return m_count == 0;
1145 }
1146
1153 {
1154 if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
1155 return m_data[abs(pos)];
1156 }
1157
1164 {
1165 if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
1166 return m_data[abs(pos)];
1167 }
1168
1175 {
1176 if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
1177 return m_data[abs(pos)];
1178 }
1179
1186 {
1187 if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
1188 return m_data[abs(pos)];
1189 }
1190
1199 {
1200 if (pos >= m_size_max) throw std::invalid_argument("Invalid subscript");
1201 return m_data[pos];
1202 }
1203
1212 {
1213 if (pos >= m_size_max) throw std::invalid_argument("Invalid subscript");
1214 return m_data[pos];
1215 }
1216
1225 {
1226 if (m_count < m_size_max) {
1227 size_type pos = abs(m_count);
1228 m_data[pos] = v;
1229 m_count++;
1230 return pos;
1231 } else {
1232 size_type pos = m_head;
1233 m_data[pos] = v;
1234 m_head = abs(1);
1235 return pos;
1236 }
1237 }
1238
1247 {
1248 if (m_count < m_size_max) {
1249 size_type pos = abs(m_count);
1250 m_data[pos] = std::move(v);
1251 m_count++;
1252 return pos;
1253 } else {
1254 size_type pos = m_head;
1255 m_data[pos] = std::move(v);
1256 m_head = abs(1);
1257 return pos;
1258 }
1259 }
1260
1265 {
1266 if (!m_count) throw std::invalid_argument("Empty storage");
1267 m_count--;
1268 }
1269
1278 {
1279 m_head = abs(-1);
1280 if (m_count < m_size_max)
1281 m_count++;
1282 m_data[m_head] = v;
1283 return m_head;
1284 }
1285
1294 {
1295 m_head = abs(-1);
1296 if (m_count < m_size_max)
1297 m_count++;
1298 m_data[m_head] = std::move(v);
1299 return m_head;
1300 }
1301
1306 {
1307 if (!m_count) throw std::invalid_argument("Empty storage");
1308 m_head = abs(1);
1309 m_count--;
1310 }
1311
1316 {
1317 if (!m_count) throw std::invalid_argument("Empty storage");
1318 return m_data[m_head];
1319 }
1320
1325 {
1326 if (!m_count) throw std::invalid_argument("Empty storage");
1327 return m_data[m_head];
1328 }
1329
1334 {
1335 return m_data[tail()];
1336 }
1337
1342 {
1343 return m_data[tail()];
1344 }
1345
1350 {
1351 return m_head;
1352 }
1353
1358 {
1359 if (!m_count) throw std::invalid_argument("Empty storage");
1360 return abs(m_count - 1);
1361 }
1362
1365 size_type abs(_In_ size_type pos) const
1366 {
1367 return (m_head + pos) % m_size_max;
1368 }
1369
1370 protected:
1375 };
1376
1378
1384
1388 template <typename _Tn>
1389 class num_runtime_error : public std::runtime_error
1390 {
1391 public:
1392 typedef _Tn error_type;
1393
1394 public:
1401 num_runtime_error(_In_ error_type num, _In_ const std::string& msg) :
1402 m_num(num),
1403 runtime_error(msg)
1404 {
1405 }
1406
1413 num_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) :
1414 m_num(num),
1415 runtime_error(msg)
1416 {
1417 }
1418
1423 {
1424 return m_num;
1425 }
1426
1427 protected:
1429 };
1430
1435 {
1436 public:
1443 win_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<DWORD>(num, msg)
1444 {
1445 }
1446
1453 win_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) : num_runtime_error<DWORD>(num, msg)
1454 {
1455 }
1456
1462 win_runtime_error(_In_ const std::string& msg) : num_runtime_error<DWORD>(GetLastError(), msg)
1463 {
1464 }
1465
1471 win_runtime_error(_In_opt_z_ const char *msg = nullptr) : num_runtime_error<DWORD>(GetLastError(), msg)
1472 {
1473 }
1474
1480 tstring msg(_In_opt_ DWORD dwLanguageId = 0) const
1481 {
1482 tstring str;
1483 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, m_num, dwLanguageId, str, NULL)) {
1484 // Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
1485 str.erase(str.find_last_not_of(_T(" \t\n\r\f\v")) + 1);
1486 } else
1487 sprintf(str, m_num >= 0x10000 ? _T("Error 0x%X") : _T("Error %u"), m_num);
1488 return str;
1489 }
1490 };
1491
1493
1496
1500 template<class _Elem, class _Traits, class _Ax>
1501 class basic_string_printf : public std::basic_string<_Elem, _Traits, _Ax>
1502 {
1503 public:
1506
1512 basic_string_printf(_In_z_ _Printf_format_string_ const _Elem *format, ...)
1513 {
1514 va_list arg;
1515 va_start(arg, format);
1516 vsprintf(*this, format, arg);
1517 va_end(arg);
1518 }
1519
1521
1524
1531 basic_string_printf(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
1532 {
1533 _Myt format;
1534 ATLENSURE(format.LoadString(hInstance, nFormatID));
1535
1536 va_list arg;
1537 va_start(arg, nFormatID);
1538 vsprintf(*this, format, arg);
1539 va_end(arg);
1540 }
1541
1549 basic_string_printf(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
1550 {
1551 _Myt format;
1552 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1553
1554 va_list arg;
1555 va_start(arg, nFormatID);
1556 vsprintf(*this, format, arg);
1557 va_end(arg);
1558 }
1559
1561 };
1562
1567
1572
1576#ifdef _UNICODE
1578#else
1580#endif
1581
1585 template<class _Elem, class _Traits, class _Ax>
1586 class basic_string_msg : public std::basic_string<_Elem, _Traits, _Ax>
1587 {
1588 public:
1591
1597 basic_string_msg(_In_z_ _FormatMessage_format_string_ const _Elem *format, ...)
1598 {
1599 va_list arg;
1600 va_start(arg, format);
1601 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1602 va_end(arg);
1603 }
1604
1606
1609
1616 basic_string_msg(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
1617 {
1618 _Myt format(GetManager());
1619 ATLENSURE(format.LoadString(hInstance, nFormatID));
1620
1621 va_list arg;
1622 va_start(arg, nFormatID);
1623 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1624 va_end(arg);
1625 }
1626
1634 basic_string_msg(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
1635 {
1636 _Myt format(GetManager());
1637 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1638
1639 va_list arg;
1640 va_start(arg, nFormatID);
1641 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1642 va_end(arg);
1643 }
1644
1646
1652 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ va_list *Arguments)
1653 {
1654 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *this, Arguments);
1655 }
1656
1662 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ DWORD_PTR *Arguments)
1663 {
1664 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *this, (va_list*)Arguments);
1665 }
1666
1672 basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ va_list *Arguments)
1673 {
1674 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *this, Arguments);
1675 }
1676
1682 basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ DWORD_PTR *Arguments)
1683 {
1684 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *this, (va_list*)Arguments);
1685 }
1686 };
1687
1692
1697
1701#ifdef _UNICODE
1702 typedef wstring_msg tstring_msg;
1703#else
1705#endif
1706
1710 template<class _Elem, class _Traits, class _Ax>
1711 class basic_string_guid : public std::basic_string<_Elem, _Traits, _Ax>
1712 {
1713 public:
1716
1723 basic_string_guid(_In_ const GUID &guid, _In_z_ _Printf_format_string_ const _Elem *format)
1724 {
1725 sprintf<_Elem, _Traits, _Ax>(*this, format,
1726 guid.Data1,
1727 guid.Data2,
1728 guid.Data3,
1729 guid.Data4[0], guid.Data4[1],
1730 guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
1731 }
1732
1734 };
1735
1739 class string_guid : public basic_string_guid<char, std::char_traits<char>, std::allocator<char> >
1740 {
1741 public:
1744
1750 string_guid(_In_ const GUID &guid) :
1751 basic_string_guid<char, std::char_traits<char>, std::allocator<char> >(guid, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
1752 {
1753 }
1754
1756 };
1757
1761 class wstring_guid : public basic_string_guid<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >
1762 {
1763 public:
1766
1772 wstring_guid(_In_ const GUID &guid) :
1773 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}")
1774 {
1775 }
1776
1778 };
1779
1783#ifdef _UNICODE
1784 typedef wstring_guid tstring_guid;
1785#else
1787#endif
1788
1790
1796
1797 // winstd::sanitizing_allocator::destroy() member generates _Ptr parameter not used warning for primitive datatypes _Ty.
1798 #pragma warning(push)
1799 #pragma warning(disable: 4100)
1800
1808 template<class _Ty>
1809 class sanitizing_allocator : public std::allocator<_Ty>
1810 {
1811 public:
1812 typedef std::allocator<_Ty> _Mybase;
1813
1817 template<class _Other>
1818 struct rebind
1819 {
1821 };
1822
1827 {
1828 }
1829
1834 {
1835 }
1836
1840 template<class _Other>
1841 sanitizing_allocator(_In_ const sanitizing_allocator<_Other> &_Othr) noexcept : _Mybase(_Othr)
1842 {
1843 }
1844
1848 void deallocate(_In_ pointer _Ptr, _In_ size_type _Size)
1849 {
1850 // Sanitize then free.
1851 SecureZeroMemory(_Ptr, _Size);
1852 _Mybase::deallocate(_Ptr, _Size);
1853 }
1854 };
1855
1856 #pragma warning(pop)
1857
1865 typedef std::basic_string<char, std::char_traits<char>, sanitizing_allocator<char> > sanitizing_string;
1866
1874 typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, sanitizing_allocator<wchar_t> > sanitizing_wstring;
1875
1879#ifdef _UNICODE
1881#else
1883#endif
1884
1888 template<size_t N>
1890 {
1891 public:
1896 {
1897 ZeroMemory(m_data, N);
1898 }
1899
1904 {
1905 SecureZeroMemory(m_data, N);
1906 }
1907
1908 public:
1909 unsigned char m_data[N];
1910 };
1911
1913}
Base template class to support converting GUID to string.
Definition: Common.h:1712
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:1723
Base template class to support string formatting using FormatMessage() style templates.
Definition: Common.h:1587
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:1652
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:1634
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:1682
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:1672
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:1662
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:1616
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:1597
Base template class to support string formatting using printf() style templates.
Definition: Common.h:1502
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:1512
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:1531
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:1549
Base abstract template class to support object handle keeping for objects that support handle duplica...
Definition: Common.h:844
bool attach_duplicated(_In_opt_ handle_type h)
Duplicates an object handle and sets a new object handle.
Definition: Common.h:949
handle_type duplicate() const
Duplicates and returns a new object handle.
Definition: Common.h:935
dplhandle< handle_type, INVAL > & operator=(_In_opt_ handle_type h) noexcept
Attaches already available object handle.
Definition: Common.h:885
dplhandle< handle_type, INVAL > & operator=(_In_ const dplhandle< handle_type, INVAL > &h) noexcept
Duplicates the object.
Definition: Common.h:896
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...
dplhandle(_In_opt_ handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition: Common.h:858
dplhandle< handle_type, INVAL > & operator=(_Inout_ dplhandle< handle_type, INVAL > &&h) noexcept
Moves the object.
Definition: Common.h:924
dplhandle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition: Common.h:849
Base abstract template class to support generic object handle keeping.
Definition: Common.h:580
handle_type *& operator*() const
Returns the object handle value when the object handle is a pointer to a value (class,...
Definition: Common.h:670
virtual void free_internal() noexcept=0
Abstract member function that must be implemented by child classes to do the actual object destructio...
bool operator<=(_In_opt_ handle_type h) const
Is handle less than or equal to?
Definition: Common.h:730
handle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition: Common.h:595
handle(_In_opt_ handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition: Common.h:604
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
handle_type * operator&()
Returns the object handle reference.
Definition: Common.h:680
bool operator!=(_In_opt_ handle_type h) const
Is handle not equal to?
Definition: Common.h:769
bool operator==(_In_opt_ handle_type h) const
Is handle equal to?
Definition: Common.h:782
T handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:585
handle(_Inout_ handle< handle_type, INVAL > &&h) noexcept
Move constructor.
Definition: Common.h:613
bool operator!() const
Tests if the object handle is INVAL.
Definition: Common.h:704
void free()
Destroys the object.
Definition: Common.h:816
bool operator>=(_In_opt_ handle_type h) const
Is handle greater than or equal to?
Definition: Common.h:743
bool operator<(_In_opt_ handle_type h) const
Is handle less than?
Definition: Common.h:717
handle< handle_type, INVAL > & operator=(_In_opt_ handle_type h) noexcept
Attaches already available object handle.
Definition: Common.h:631
handle< handle_type, INVAL > & operator=(_Inout_ handle< handle_type, INVAL > &&h) noexcept
Move assignment.
Definition: Common.h:643
void attach(_In_opt_ handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:794
bool operator>(_In_opt_ handle_type h) const
Is handle greater than?
Definition: Common.h:756
handle_type m_h
Object handle.
Definition: Common.h:833
handle_type detach()
Dismisses the object handle from this class.
Definition: Common.h:806
Numerical runtime error.
Definition: Common.h:1390
num_runtime_error(_In_ error_type num, _In_ const std::string &msg)
Constructs an exception.
Definition: Common.h:1401
error_type number() const
Returns the Windows error number.
Definition: Common.h:1422
_Tn error_type
Error number type.
Definition: Common.h:1392
error_type m_num
Numeric error code.
Definition: Common.h:1428
num_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg=nullptr)
Constructs an exception.
Definition: Common.h:1413
Helper class for returning pointers to std::unique_ptr (specialization for arrays)
Definition: Common.h:474
ref_unique_ptr(_Inout_ std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition: Common.h:481
std::unique_ptr< _Ty[], _Dx > & m_own
Original owner of the pointer.
Definition: Common.h:563
virtual ~ref_unique_ptr()
Returns ownership of the pointer.
Definition: Common.h:536
ref_unique_ptr(_Inout_ ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition: Common.h:508
_Ty * m_ptr
Pointer.
Definition: Common.h:564
ref_unique_ptr & operator=(_Inout_ ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition: Common.h:522
ref_unique_ptr & operator=(_Inout_ std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition: Common.h:493
Helper class for returning pointers to std::unique_ptr.
Definition: Common.h:381
ref_unique_ptr(_Inout_ std::unique_ptr< _Ty, _Dx > &owner)
Takes ownership of the pointer.
Definition: Common.h:388
std::unique_ptr< _Ty, _Dx > & m_own
Original owner of the pointer.
Definition: Common.h:435
ref_unique_ptr(_Inout_ ref_unique_ptr< _Ty, _Dx > &&other)
Moves object.
Definition: Common.h:398
_Ty * m_ptr
Pointer.
Definition: Common.h:436
~ref_unique_ptr()
Returns ownership of the pointer.
Definition: Common.h:408
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition: Common.h:1810
void deallocate(_In_ pointer _Ptr, _In_ size_type _Size)
Deallocate object at _Ptr sanitizing its content first.
Definition: Common.h:1848
sanitizing_allocator(_In_ const sanitizing_allocator< _Ty > &_Othr)
Construct by copying.
Definition: Common.h:1833
sanitizing_allocator(_In_ const sanitizing_allocator< _Other > &_Othr) noexcept
Construct from a related allocator.
Definition: Common.h:1841
std::allocator< _Ty > _Mybase
Base type.
Definition: Common.h:1812
sanitizing_allocator() noexcept
Construct default allocator.
Definition: Common.h:1826
Sanitizing BLOB.
Definition: Common.h:1890
sanitizing_blob()
Constructs uninitialized BLOB.
Definition: Common.h:1895
~sanitizing_blob()
Sanitizes BLOB.
Definition: Common.h:1903
Single-byte character implementation of a class to support converting GUID to string.
Definition: Common.h:1740
string_guid(_In_ const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1750
Helper class to allow limited size FIFO queues implemented as vector of elements.
Definition: Common.h:978
size_type push_front(_In_ const value_type &v)
Copies an existing element to the head of the queue, overriding the last one when queue is out of spa...
Definition: Common.h:1277
virtual ~vector_queue()
Destroys the queue.
Definition: Common.h:1045
size_type abs(_In_ size_type pos) const
Returns absolute subscript or position number of the given element in the queue.
Definition: Common.h:1365
vector_queue< value_type > & operator=(_In_ const vector_queue< value_type > &other)
Copies existing queue.
Definition: Common.h:1073
void pop_front()
Removes (dequeues) the head element of the queue.
Definition: Common.h:1305
const_reference back() const
Returns a constant reference to the last element in the queue.
Definition: Common.h:1341
size_t size_type
Type to measure element count and indices in.
Definition: Common.h:983
size_type m_size_max
Maximum size.
Definition: Common.h:1374
size_type m_count
Number of elements.
Definition: Common.h:1373
T & reference
Reference to element type.
Definition: Common.h:993
void clear()
Erases the elements of the queue.
Definition: Common.h:1134
T value_type
Element type.
Definition: Common.h:988
const_reference operator[](_In_ size_type pos) const
Returns a constant reference to the element at a specified location in the queue.
Definition: Common.h:1185
void pop_back()
Removes (dequeues) the last element of the queue.
Definition: Common.h:1264
reference back()
Returns a reference to the last element in the queue.
Definition: Common.h:1333
size_type push_front(_Inout_ value_type &&v)
Moves the element to the head of the queue, overriding the last one when queue is out of space and mo...
Definition: Common.h:1293
size_type push_back(_Inout_ value_type &&v)
Moves the element to the end of the queue, overriding the first one when queue is out of space.
Definition: Common.h:1246
size_type size() const
Returns the number of elements in the vector.
Definition: Common.h:1118
size_type head() const
Returns absolute subscript or position number of the head element in the queue. The element does not ...
Definition: Common.h:1349
size_type push_back(_In_ const value_type &v)
Copies an existing element to the end of the queue, overriding the first one when queue is out of spa...
Definition: Common.h:1224
size_type capacity() const
Returns the number of elements that the queue can contain before overwriting head ones.
Definition: Common.h:1126
reference front()
Returns a reference to the head element in the queue.
Definition: Common.h:1315
vector_queue(_In_ size_type size_max)
Construct queue of fixed size.
Definition: Common.h:1016
const T * const_pointer
Constant pointer to element.
Definition: Common.h:1008
const T & const_reference
Constant reference to element type.
Definition: Common.h:998
const_reference at(_In_ size_type pos) const
Returns a constant reference to the element at a specified location in the queue.
Definition: Common.h:1174
size_type m_head
Index of the first element.
Definition: Common.h:1372
bool empty() const
Tests if the queue is empty.
Definition: Common.h:1142
const_reference front() const
Returns a constant reference to the head element in the queue.
Definition: Common.h:1324
const_reference at_abs(_In_ size_type pos) const
Returns a constant reference to the element at the absolute location in the queue: measured from the ...
Definition: Common.h:1211
vector_queue(_In_ const vector_queue< value_type > &other)
Copies existing queue.
Definition: Common.h:1029
reference at(_In_ size_type pos)
Returns a reference to the element at a specified location in the queue.
Definition: Common.h:1152
reference operator[](_In_ size_type pos)
Returns a reference to the element at a specified location in the queue.
Definition: Common.h:1163
reference at_abs(_In_ size_type pos)
Returns a reference to the element at the absolute location in the queue.
Definition: Common.h:1198
T * pointer
Pointer to element.
Definition: Common.h:1003
value_type * m_data
Underlying data container.
Definition: Common.h:1371
vector_queue(_Inout_ vector_queue< value_type > &&other)
Moves existing queue.
Definition: Common.h:1055
size_type tail() const
Returns absolute subscript or position number of the last element in the queue. The element must exis...
Definition: Common.h:1357
vector_queue< value_type > & operator=(_Inout_ vector_queue< value_type > &&other)
Moves existing queue.
Definition: Common.h:1097
Windows runtime error.
Definition: Common.h:1435
tstring msg(_In_opt_ DWORD dwLanguageId=0) const
Returns a user-readable Windows error message.
Definition: Common.h:1480
win_runtime_error(_In_ const std::string &msg)
Constructs an exception using GetLastError()
Definition: Common.h:1462
win_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg=nullptr)
Constructs an exception.
Definition: Common.h:1453
win_runtime_error(_In_opt_z_ const char *msg=nullptr)
Constructs an exception using GetLastError()
Definition: Common.h:1471
win_runtime_error(_In_ error_type num, _In_ const std::string &msg)
Constructs an exception.
Definition: Common.h:1443
Wide character implementation of a class to support converting GUID to string.
Definition: Common.h:1762
wstring_guid(_In_ const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1772
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition: Common.h:80
std::string tstring
Multi-byte / Wide-character string (according to _UNICODE)
Definition: Common.h:311
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
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:1874
sanitizing_string sanitizing_tstring
Multi-byte / Wide-character sanitizing string (according to _UNICODE)
Definition: Common.h:1882
std::basic_string< char, std::char_traits< char >, sanitizing_allocator< char > > sanitizing_string
A sanitizing variant of std::string.
Definition: Common.h:1865
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:1571
string_guid tstring_guid
Multi-byte / Wide-character string GUID (according to _UNICODE)
Definition: Common.h:1786
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:1696
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:1566
string_printf tstring_printf
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition: Common.h:1579
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:1691
string_msg tstring_msg
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition: Common.h:1704
static const T invalid
Invalid handle value.
Definition: Common.h:590
void operator()(_Frees_ptr_opt_ _Ty *_Ptr) const noexcept
Delete a pointer.
Definition: Common.h:359
LocalFree_delete() noexcept
Default construct.
Definition: Common.h:354
LocalFree_delete< _Ty > _Myt
This type.
Definition: Common.h:349
void operator()(_Other *) const
Delete a pointer of another type.
Definition: Common.h:370
Deleter for unique_ptr using LocalFree.
Definition: Common.h:319
LocalFree_delete< _Ty > _Myt
This type.
Definition: Common.h:320
LocalFree_delete(const LocalFree_delete< _Ty2 > &)
Construct from another LocalFree_delete.
Definition: Common.h:330
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: Common.h:337
LocalFree_delete()
Default construct.
Definition: Common.h:325
Convert this type to sanitizing_allocator<_Other>
Definition: Common.h:1819
sanitizing_allocator< _Other > other
Other type.
Definition: Common.h:1820