56#define WINSTD_STRING_IMPL(x) #x
61#define WINSTD_STRING(x) WINSTD_STRING_IMPL(x)
66#define WINSTD_NONCOPYABLE(C) \
68 C (_In_ const C &h) noexcept; \
69 C& operator=(_In_ const C &h) noexcept;
74#define WINSTD_NONMOVABLE(C) \
76 C (_Inout_ C &&h) noexcept; \
77 C& operator=(_Inout_ C &&h) noexcept;
79#ifndef WINSTD_STACK_BUFFER_BYTES
93#define WINSTD_STACK_BUFFER_BYTES 1024
105#define PRINTF_LPTSTR "ls"
107#define PRINTF_LPTSTR "s"
114#define PRINTF_LPOLESTR "hs"
116#define PRINTF_LPOLESTR "ls"
123#define _tcin (std::wcin )
125#define _tcin (std::cin )
132#define _tcout (std::wcout)
134#define _tcout (std::cout)
141#define _tcerr (std::wcerr)
143#define _tcerr (std::cerr)
150#define _tclog (std::wclog)
152#define _tclog (std::clog)
163#define WINSTD_HANDLE_IMPL(C, INVAL) \
166 C (_In_opt_ handle_type h) noexcept : handle<handle_type, INVAL>( h ) { } \
167 C (_Inout_ C &&h) noexcept : handle<handle_type, INVAL>(std::move(h)) { } \
168 C& operator=(_In_opt_ handle_type h) noexcept { handle<handle_type, INVAL>::operator=( h ); return *this; } \
169 C& operator=(_Inout_ C &&h) noexcept { handle<handle_type, INVAL>::operator=(std::move(h)); return *this; } \
175#define WINSTD_DPLHANDLE_IMPL(C, INVAL) \
178 C (_In_opt_ handle_type h) noexcept : dplhandle<handle_type, INVAL>( h ) { } \
179 C (_In_ const C &h) noexcept : dplhandle<handle_type, INVAL>(duplicate_internal(h.m_h)) { } \
180 C (_Inout_ C &&h) noexcept : dplhandle<handle_type, INVAL>(std::move (h )) { } \
181 C& operator=(_In_opt_ handle_type h) noexcept { dplhandle<handle_type, INVAL>::operator=( h ); return *this; } \
182 C& operator=(_In_ const C &h) noexcept { dplhandle<handle_type, INVAL>::operator=( h ); return *this; } \
183 C& operator=(_Inout_ C &&h) noexcept { dplhandle<handle_type, INVAL>::operator=(std::move(h)); return *this; } \
188#ifndef _FormatMessage_format_string_
189#define _FormatMessage_format_string_ _In_z_
193#ifndef _LPCBYTE_DEFINED
194#define _LPCBYTE_DEFINED
195typedef const BYTE *LPCBYTE;
202#pragma warning(disable: 4995)
203#pragma warning(disable: 4996)
204#pragma warning(disable: 4505)
220static int vsnprintf(_Out_z_cap_(capacity)
char *str, _In_
size_t capacity, _In_z_ _Printf_format_string_
const char *format, _In_ va_list arg)
222 return _vsnprintf(str, capacity, format, arg);
236static 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
238 return _vsnwprintf(str, capacity, format, arg);
250template<
class _Elem,
class _Traits,
class _Ax>
251static int vsprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_
const _Elem *format, _In_ va_list arg)
256 int count =
vsnprintf(buf, _countof(buf) - 1, format, arg);
259 str.assign(buf, count);
263 auto buf_dyn = std::make_unique<_Elem[]>(capacity);
264 count =
vsnprintf(buf_dyn.get(), capacity - 1, format, arg);
266 str.assign(buf_dyn.get(), count);
283template<
class _Elem,
class _Traits,
class _Ax>
284static int sprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_
const _Elem *format, ...)
287 va_start(arg, format);
288 const int res =
vsprintf(str, format, arg);
298template<
class _Traits,
class _Ax>
299static 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)
301 std::unique_ptr<CHAR[], winstd::LocalFree_delete<CHAR[]> > lpBuffer;
302 DWORD dwResult = FormatMessageA(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId,
reinterpret_cast<LPSTR
>((LPSTR*)get_ptr(lpBuffer)), 0, Arguments);
304 str.assign(lpBuffer.get(), dwResult);
313template<
class _Traits,
class _Ax>
314static 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)
316 std::unique_ptr<WCHAR[], winstd::LocalFree_delete<WCHAR[]> > lpBuffer;
317 DWORD dwResult = FormatMessageW(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId,
reinterpret_cast<LPWSTR
>((LPWSTR*)get_ptr(lpBuffer)), 0, Arguments);
319 str.assign(lpBuffer.get(), dwResult);
396 template<
class _Other>
406 template<
class _Ty,
class _Dx>
417 m_ptr(owner.release())
429 other.m_ptr =
nullptr;
437 if (
m_ptr !=
nullptr)
446 operator typename _Ty**()
456 operator typename _Ty*&()
473 template<
class _Ty,
class _Dx>
483 template<
class _Ty,
class _Dx>
494 m_ptr(owner.release())
506 other.m_ptr =
nullptr;
514 if (
m_ptr !=
nullptr)
523 operator typename _Ty**()
noexcept
533 operator typename _Ty*&()
551 template<
class _Ty,
class _Dx>
567 template <
class T, const T INVAL>
612 handle<handle_type, INVAL>& operator=(_In_
const handle<handle_type, INVAL> &h)
noexcept {};
631 #pragma warning(suppress: 26432)
634 if (
this != std::addressof(h)) {
826 template <class T, const T INVAL>
832 template <class T, T INVAL>
866 dplhandle<handle_type, INVAL>(_Inout_ dplhandle<handle_type, INVAL> &&h) noexcept : handle<handle_type, INVAL>(std::move(h))
888 if (
this != std::addressof(h)) {
913 #pragma warning(disable: 26432)
966 template <
typename _Tn>
1061 if (
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, m_num, dwLanguageId, str, NULL)) {
1063 str.erase(str.find_last_not_of(_T(
" \t\n\r\f\v")) + 1);
1065 sprintf(str, m_num >= 0x10000 ? _T(
"Error 0x%X") : _T(
"Error %u"), m_num);
1078 template<
class _Elem,
class _Traits,
class _Ax>
1093 va_start(arg, format);
1112 ATLENSURE(format.LoadString(hInstance, nFormatID));
1115 va_start(arg, nFormatID);
1130 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1133 va_start(arg, nFormatID);
1163 template<
class _Elem,
class _Traits,
class _Ax>
1178 va_start(arg, format);
1179 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *
this, &arg);
1196 _Myt format(GetManager());
1197 ATLENSURE(format.LoadString(hInstance, nFormatID));
1200 va_start(arg, nFormatID);
1201 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *
this, &arg);
1214 _Myt format(GetManager());
1215 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1218 va_start(arg, nFormatID);
1219 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *
this, &arg);
1230 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ va_list *Arguments)
1232 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *
this, Arguments);
1240 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ DWORD_PTR *Arguments)
1242 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *
this, (va_list*)Arguments);
1252 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *
this, Arguments);
1262 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *
this, (va_list*)Arguments);
1288 template<
class _Elem,
class _Traits,
class _Ax>
1303 sprintf<_Elem, _Traits, _Ax>(*
this, format,
1307 guid.Data4[0], guid.Data4[1],
1308 guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
1329 basic_string_guid<char, std::char_traits<char>, std::allocator<char> >(guid,
"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
1351 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}")
1373 #pragma warning(push)
1374 #pragma warning(disable: 4100)
1392 template<
class _Other>
1415 template<
class _Other>
1426 SecureZeroMemory(_Ptr, _Size);
1427 _Mybase::deallocate(_Ptr, _Size);
1431 #pragma warning(pop)
1472 ZeroMemory(m_data, N);
1480 SecureZeroMemory(m_data, N);
1484 unsigned char m_data[N];
Base template class to support converting GUID to string.
Definition: Common.h:1290
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:1301
Base template class to support string formatting using FormatMessage() style templates.
Definition: Common.h:1165
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:1240
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1250
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:1212
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:1230
basic_string_msg(const _Elem *format,...)
Initializes a new string and formats its contents using FormatMessage() style template.
Definition: Common.h:1175
basic_string_msg(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition: Common.h:1194
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1260
Base template class to support string formatting using printf() style templates.
Definition: Common.h:1080
basic_string_printf(const _Elem *format,...)
Initializes a new string and formats its contents using printf() style template.
Definition: Common.h:1090
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:1127
basic_string_printf(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition: Common.h:1109
Base abstract template class to support object handle keeping for objects that support trivial handle...
Definition: Common.h:834
dplhandle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition: Common.h:875
handle_type duplicate() const
Duplicates and returns a new object handle.
Definition: Common.h:925
dplhandle< handle_type, INVAL > & operator=(dplhandle< handle_type, INVAL > &&h) noexcept
Moves the object.
Definition: Common.h:914
bool attach_duplicated(handle_type h)
Duplicates an object handle and sets a new object handle.
Definition: Common.h:939
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:848
dplhandle< handle_type, INVAL > & operator=(const dplhandle< handle_type, INVAL > &h) noexcept
Duplicates the object.
Definition: Common.h:886
dplhandle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition: Common.h:839
Base abstract template class to support generic object handle keeping.
Definition: Common.h:569
handle_type *& operator*() const
Returns the object handle value when the object handle is a pointer to a value (class,...
Definition: Common.h:659
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:584
bool operator>=(handle_type h) const
Is handle greater than or equal to?
Definition: Common.h:735
handle_type operator->() const
Provides object handle member access when the object handle is a pointer to a class or struct.
Definition: Common.h:680
handle_type * operator&()
Returns the object handle reference.
Definition: Common.h:669
T handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:574
handle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition: Common.h:593
bool operator<(handle_type h) const
Is handle less than?
Definition: Common.h:709
handle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition: Common.h:620
bool operator!() const
Tests if the object handle is invalid.
Definition: Common.h:696
handle< handle_type, INVAL > & operator=(handle< handle_type, INVAL > &&h) noexcept
Move assignment.
Definition: Common.h:632
bool operator!=(handle_type h) const
Is handle not equal to?
Definition: Common.h:761
void free()
Destroys the object.
Definition: Common.h:808
handle_type m_h
Object handle.
Definition: Common.h:823
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:786
bool operator==(handle_type h) const
Is handle equal to?
Definition: Common.h:774
handle(handle< handle_type, INVAL > &&h) noexcept
Move constructor.
Definition: Common.h:602
handle_type detach()
Dismisses the object handle from this class.
Definition: Common.h:798
bool operator>(handle_type h) const
Is handle greater than?
Definition: Common.h:748
bool operator<=(handle_type h) const
Is handle less than or equal to?
Definition: Common.h:722
Numerical runtime error.
Definition: Common.h:968
num_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition: Common.h:991
num_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition: Common.h:979
error_type number() const
Returns the Windows error number.
Definition: Common.h:1000
_Tn error_type
Error number type.
Definition: Common.h:970
error_type m_num
Numeric error code.
Definition: Common.h:1006
std::unique_ptr< _Ty[], _Dx > & m_own
Original owner of the pointer.
Definition: Common.h:539
ref_unique_ptr(ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition: Common.h:502
virtual ~ref_unique_ptr()
Returns ownership of the pointer.
Definition: Common.h:512
ref_unique_ptr(std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition: Common.h:492
_Ty * m_ptr
Pointer.
Definition: Common.h:540
Helper class for returning pointers to std::unique_ptr.
Definition: Common.h:408
std::unique_ptr< _Ty, _Dx > & m_own
Original owner of the pointer.
Definition: Common.h:462
_Ty * m_ptr
Pointer.
Definition: Common.h:463
ref_unique_ptr(ref_unique_ptr< _Ty, _Dx > &&other)
Moves object.
Definition: Common.h:425
~ref_unique_ptr()
Returns ownership of the pointer.
Definition: Common.h:435
ref_unique_ptr(std::unique_ptr< _Ty, _Dx > &owner)
Takes ownership of the pointer.
Definition: Common.h:415
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition: Common.h:1385
sanitizing_allocator(const sanitizing_allocator< _Ty > &_Othr)
Construct by copying.
Definition: Common.h:1408
void deallocate(pointer _Ptr, size_type _Size)
Deallocate object at _Ptr sanitizing its content first.
Definition: Common.h:1423
sanitizing_allocator(const sanitizing_allocator< _Other > &_Othr) noexcept
Construct from a related allocator.
Definition: Common.h:1416
std::allocator< _Ty > _Mybase
Base type.
Definition: Common.h:1387
sanitizing_allocator() noexcept
Construct default allocator.
Definition: Common.h:1401
Sanitizing BLOB.
Definition: Common.h:1465
sanitizing_blob()
Constructs uninitialized BLOB.
Definition: Common.h:1470
~sanitizing_blob()
Sanitizes BLOB.
Definition: Common.h:1478
Single-byte character implementation of a class to support converting GUID to string.
Definition: Common.h:1318
string_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1328
Windows runtime error.
Definition: Common.h:1013
tstring msg(DWORD dwLanguageId=0) const
Returns a user-readable Windows error message.
Definition: Common.h:1058
win_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition: Common.h:1031
win_runtime_error(const std::string &msg)
Constructs an exception using GetLastError()
Definition: Common.h:1040
win_runtime_error(const char *msg=nullptr)
Constructs an exception using GetLastError()
Definition: Common.h:1049
win_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition: Common.h:1021
Wide character implementation of a class to support converting GUID to string.
Definition: Common.h:1340
wstring_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1350
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition: Common.h:93
std::string tstring
Multi-byte / Wide-character string (according to _UNICODE)
Definition: Common.h:338
ref_unique_ptr< _Ty, _Dx > get_ptr(std::unique_ptr< _Ty, _Dx > &owner) noexcept
Helper function template for returning pointers to std::unique_ptr.
Definition: Common.h:474
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:1449
sanitizing_string sanitizing_tstring
Multi-byte / Wide-character sanitizing string (according to _UNICODE)
Definition: Common.h:1457
std::basic_string< char, std::char_traits< char >, sanitizing_allocator< char > > sanitizing_string
A sanitizing variant of std::string.
Definition: Common.h:1440
static const T invalid
Invalid handle value.
Definition: Common.h:579
LocalFree_delete() noexcept
Default construct.
Definition: Common.h:381
LocalFree_delete< _Ty > _Myt
This type.
Definition: Common.h:376
void operator()(_Other *) const
Delete a pointer of another type.
Definition: Common.h:397
void operator()(_Ty *_Ptr) const noexcept
Delete a pointer.
Definition: Common.h:386
Deleter for unique_ptr using LocalFree.
Definition: Common.h:346
LocalFree_delete< _Ty > _Myt
This type.
Definition: Common.h:347
LocalFree_delete(const LocalFree_delete< _Ty2 > &)
Construct from another LocalFree_delete.
Definition: Common.h:357
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: Common.h:364
LocalFree_delete()
Default construct.
Definition: Common.h:352
Convert this type to sanitizing_allocator<_Other>
Definition: Common.h:1394
sanitizing_allocator< _Other > other
Other type.
Definition: Common.h:1395