43#define WINSTD_STRING_IMPL(x) #x
48#define WINSTD_STRING(x) WINSTD_STRING_IMPL(x)
53#define WINSTD_NONCOPYABLE(C) \
55 C (_In_ const C &h) noexcept; \
56 C& operator=(_In_ const C &h) noexcept;
61#define WINSTD_NONMOVABLE(C) \
63 C (_Inout_ C &&h) noexcept; \
64 C& operator=(_Inout_ C &&h) noexcept;
66#ifndef WINSTD_STACK_BUFFER_BYTES
80#define WINSTD_STACK_BUFFER_BYTES 1024
101#define PRINTF_LPTSTR "ls"
103#define PRINTF_LPTSTR "s"
110#define PRINTF_LPOLESTR "hs"
112#define PRINTF_LPOLESTR "ls"
119#define _tcin (std::wcin )
121#define _tcin (std::cin )
128#define _tcout (std::wcout)
130#define _tcout (std::cout)
137#define _tcerr (std::wcerr)
139#define _tcerr (std::cerr)
146#define _tclog (std::wclog)
148#define _tclog (std::clog)
162#define WINSTD_HANDLE_IMPL(C, INVAL) \
165 C (_In_opt_ handle_type h) noexcept : handle<handle_type, INVAL>( h ) { } \
166 C (_Inout_ C &&h) noexcept : handle<handle_type, INVAL>(std::move(h)) { } \
167 C& operator=(_In_opt_ handle_type h) noexcept { handle<handle_type, INVAL>::operator=( h ); return *this; } \
168 C& operator=(_Inout_ C &&h) noexcept { handle<handle_type, INVAL>::operator=(std::move(h)); return *this; } \
174#define WINSTD_DPLHANDLE_IMPL(C, INVAL) \
177 C (_In_opt_ handle_type h) noexcept : dplhandle<handle_type, INVAL>( h ) { } \
178 C (_In_ const C &h) noexcept : dplhandle<handle_type, INVAL>(duplicate_internal(h.m_h)) { } \
179 C (_Inout_ C &&h) noexcept : dplhandle<handle_type, INVAL>(std::move (h )) { } \
180 C& operator=(_In_opt_ handle_type h) noexcept { dplhandle<handle_type, INVAL>::operator=( h ); return *this; } \
181 C& operator=(_In_ const C &h) noexcept { dplhandle<handle_type, INVAL>::operator=( h ); return *this; } \
182 C& operator=(_Inout_ C &&h) noexcept { dplhandle<handle_type, INVAL>::operator=(std::move(h)); return *this; } \
187#ifndef _FormatMessage_format_string_
188#define _FormatMessage_format_string_ _In_z_
191#ifndef _LPCBYTE_DEFINED
192#define _LPCBYTE_DEFINED
193typedef const BYTE *LPCBYTE;
199#pragma warning(disable: 4995)
200#pragma warning(disable: 4996)
201#pragma warning(disable: 4505)
217static int vsnprintf(_Out_z_cap_(capacity)
char *str, _In_
size_t capacity, _In_z_ _Printf_format_string_
const char *format, _In_ va_list arg)
219 return _vsnprintf(str, capacity, format, arg);
233static 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
235 return _vsnwprintf(str, capacity, format, arg);
247template<
class _Elem,
class _Traits,
class _Ax>
248static int vsprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_
const _Elem *format, _In_ va_list arg)
253 int count = vsnprintf(buf, _countof(buf) - 1, format, arg);
256 str.assign(buf, count);
260 auto buf_dyn = std::make_unique<_Elem[]>(capacity);
261 count = vsnprintf(buf_dyn.get(), capacity - 1, format, arg);
263 str.assign(buf_dyn.get(), count);
280template<
class _Elem,
class _Traits,
class _Ax>
281static int sprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_
const _Elem *format, ...)
284 va_start(arg, format);
285 const int res = vsprintf(str, format, arg);
295template<
class _Traits,
class _Ax>
296static 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)
298 std::unique_ptr<CHAR[], winstd::LocalFree_delete<CHAR[]> > lpBuffer;
299 DWORD dwResult = FormatMessageA(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId,
reinterpret_cast<LPSTR
>((LPSTR*)
get_ptr(lpBuffer)), 0, Arguments);
301 str.assign(lpBuffer.get(), dwResult);
310template<
class _Traits,
class _Ax>
311static 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)
313 std::unique_ptr<WCHAR[], winstd::LocalFree_delete<WCHAR[]> > lpBuffer;
314 DWORD dwResult = FormatMessageW(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId,
reinterpret_cast<LPWSTR
>((LPWSTR*)
get_ptr(lpBuffer)), 0, Arguments);
316 str.assign(lpBuffer.get(), dwResult);
393 template<
class _Other>
403 template<
class _Ty,
class _Dx>
414 m_ptr(owner.release())
426 other.m_ptr =
nullptr;
434 if (
m_ptr !=
nullptr)
443 operator typename _Ty**()
453 operator typename _Ty*&()
470 template<
class _Ty,
class _Dx>
484 template<
class _Ty,
class _Dx>
494 #pragma warning(push)
495 #pragma warning(disable: 26432)
496 template<
class _Ty,
class _Dx>
507 m_ptr(owner.release())
519 if (
this != &other) {
521 m_ptr = owner.release();
536 other.m_ptr =
nullptr;
548 if (
this != &other) {
551 other.m_ptr =
nullptr;
562 if (
m_ptr !=
nullptr)
571 operator typename _Ty**()
noexcept
581 operator typename _Ty*&()
602 template <
class T, const T INVAL>
647 handle<handle_type, INVAL>& operator=(_In_
const handle<handle_type, INVAL> &h)
noexcept {};
666 #pragma warning(suppress: 26432)
669 if (
this != std::addressof(h)) {
858 template <class T, const T INVAL>
864 template <class T, T INVAL>
898 dplhandle<handle_type, INVAL>(_Inout_ dplhandle<handle_type, INVAL> &&h) noexcept : handle<handle_type, INVAL>(std::move(h))
920 if (
this != std::addressof(h)) {
945 #pragma warning(disable: 26432)
1001 template <
typename _Tn>
1096 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, m_num, dwLanguageId, str, NULL)) {
1098 str.erase(str.find_last_not_of(_T(
" \t\n\r\f\v")) + 1);
1100 sprintf(str, m_num >= 0x10000 ? _T(
"Error 0x%X") : _T(
"Error %u"), m_num);
1113 template<
class _Elem,
class _Traits,
class _Ax>
1128 va_start(arg, format);
1129 vsprintf(*
this, format, arg);
1147 ATLENSURE(format.LoadString(hInstance, nFormatID));
1150 va_start(arg, nFormatID);
1151 vsprintf(*
this, format, arg);
1165 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1168 va_start(arg, nFormatID);
1169 vsprintf(*
this, format, arg);
1198 template<
class _Elem,
class _Traits,
class _Ax>
1213 va_start(arg, format);
1214 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *
this, &arg);
1231 _Myt format(GetManager());
1232 ATLENSURE(format.LoadString(hInstance, nFormatID));
1235 va_start(arg, nFormatID);
1236 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *
this, &arg);
1249 _Myt format(GetManager());
1250 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1253 va_start(arg, nFormatID);
1254 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *
this, &arg);
1265 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ va_list *Arguments)
1267 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *
this, Arguments);
1275 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ DWORD_PTR *Arguments)
1277 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *
this, (va_list*)Arguments);
1287 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *
this, Arguments);
1297 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *
this, (va_list*)Arguments);
1323 template<
class _Elem,
class _Traits,
class _Ax>
1338 sprintf<_Elem, _Traits, _Ax>(*
this, format,
1342 guid.Data4[0], guid.Data4[1],
1343 guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
1364 basic_string_guid<char, std::char_traits<char>, std::allocator<char> >(guid,
"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
1386 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}")
1411 #pragma warning(push)
1412 #pragma warning(disable: 4100)
1430 template<
class _Other>
1453 template<
class _Other>
1464 SecureZeroMemory(_Ptr, _Size);
1465 _Mybase::deallocate(_Ptr, _Size);
1469 #pragma warning(pop)
1510 ZeroMemory(m_data, N);
1518 SecureZeroMemory(m_data, N);
1522 unsigned char m_data[N];
Base template class to support converting GUID to string.
Definition: Common.h:1325
basic_string_guid(1 const GUID &guid, 1 1 const _Elem *format)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1336
Base template class to support string formatting using FormatMessage() style templates.
Definition: Common.h:1200
basic_string_msg(1 DWORD dwFlags, 1 LPCVOID lpSource, 1 DWORD dwMessageId, 1 DWORD dwLanguageId, 1 DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1275
basic_string_msg(1 HINSTANCE hInstance, 1 UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition: Common.h:1229
basic_string_msg(1 1 const _Elem *format,...)
Initializes a new string and formats its contents using FormatMessage() style template.
Definition: Common.h:1210
basic_string_msg(1 DWORD dwFlags, 1 LPCTSTR pszFormat, 1 va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1285
basic_string_msg(1 DWORD dwFlags, 1 LPCTSTR pszFormat, 1 DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1295
basic_string_msg(1 HINSTANCE hInstance, 1 WORD wLanguageID, 1 UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition: Common.h:1247
basic_string_msg(1 DWORD dwFlags, 1 LPCVOID lpSource, 1 DWORD dwMessageId, 1 DWORD dwLanguageId, 1 va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1265
Base template class to support string formatting using printf() style templates.
Definition: Common.h:1115
basic_string_printf(1 HINSTANCE hInstance, 1 UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition: Common.h:1144
basic_string_printf(1 1 const _Elem *format,...)
Initializes a new string and formats its contents using printf() style template.
Definition: Common.h:1125
basic_string_printf(1 HINSTANCE hInstance, 1 WORD wLanguageID, 1 UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition: Common.h:1162
Base abstract template class to support object handle keeping for objects that support handle duplica...
Definition: Common.h:866
dplhandle(1 handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition: Common.h:880
dplhandle< handle_type, INVAL > & operator=(1 const dplhandle< handle_type, INVAL > &h) noexcept
Duplicates the object.
Definition: Common.h:918
handle_type duplicate() const
Duplicates and returns a new object handle.
Definition: Common.h:957
dplhandle< handle_type, INVAL > & operator=(1 dplhandle< handle_type, INVAL > &&h) noexcept
Moves the object.
Definition: Common.h:946
dplhandle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition: Common.h:871
bool attach_duplicated(1 handle_type h)
Duplicates an object handle and sets a new object handle.
Definition: Common.h:971
virtual handle_type duplicate_internal(1 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, INVAL > & operator=(1 handle_type h) noexcept
Attaches already available object handle.
Definition: Common.h:907
Base abstract template class to support generic object handle keeping.
Definition: Common.h:604
handle_type *& operator*() const
Returns the object handle value when the object handle is a pointer to a value (class,...
Definition: Common.h:694
virtual void free_internal() noexcept=0
Abstract member function that must be implemented by child classes to do the actual object destructio...
bool operator>(1 handle_type h) const
Is handle greater than?
Definition: Common.h:780
handle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition: Common.h:619
handle_type operator->() const
Provides object handle member access when the object handle is a pointer to a class or struct.
Definition: Common.h:715
handle_type * operator&()
Returns the object handle reference.
Definition: Common.h:704
bool operator<(1 handle_type h) const
Is handle less than?
Definition: Common.h:741
T handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:609
bool operator!() const
Tests if the object handle is INVAL.
Definition: Common.h:728
bool operator>=(1 handle_type h) const
Is handle greater than or equal to?
Definition: Common.h:767
void attach(1 handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:818
void free()
Destroys the object.
Definition: Common.h:840
handle(1 handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition: Common.h:628
bool operator==(1 handle_type h) const
Is handle equal to?
Definition: Common.h:806
handle_type m_h
Object handle.
Definition: Common.h:855
handle< handle_type, INVAL > & operator=(1 handle< handle_type, INVAL > &&h) noexcept
Move assignment.
Definition: Common.h:667
bool operator<=(1 handle_type h) const
Is handle less than or equal to?
Definition: Common.h:754
handle_type detach()
Dismisses the object handle from this class.
Definition: Common.h:830
handle(1 handle< handle_type, INVAL > &&h) noexcept
Move constructor.
Definition: Common.h:637
handle< handle_type, INVAL > & operator=(1 handle_type h) noexcept
Attaches already available object handle.
Definition: Common.h:655
bool operator!=(1 handle_type h) const
Is handle not equal to?
Definition: Common.h:793
Numerical runtime error.
Definition: Common.h:1003
error_type number() const
Returns the Windows error number.
Definition: Common.h:1035
_Tn error_type
Error number type.
Definition: Common.h:1005
error_type m_num
Numeric error code.
Definition: Common.h:1041
num_runtime_error(1 error_type num, 1 const std::string &msg)
Constructs an exception.
Definition: Common.h:1014
num_runtime_error(1 error_type num, 1 const char *msg=nullptr)
Constructs an exception.
Definition: Common.h:1026
Helper class for returning pointers to std::unique_ptr (specialization for arrays)
Definition: Common.h:498
std::unique_ptr< _Ty[], _Dx > & m_own
Original owner of the pointer.
Definition: Common.h:587
ref_unique_ptr & operator=(1 std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition: Common.h:517
virtual ~ref_unique_ptr()
Returns ownership of the pointer.
Definition: Common.h:560
ref_unique_ptr & operator=(1 ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition: Common.h:546
ref_unique_ptr(1 ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition: Common.h:532
_Ty * m_ptr
Pointer.
Definition: Common.h:588
ref_unique_ptr(1 std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition: Common.h:505
Helper class for returning pointers to std::unique_ptr.
Definition: Common.h:405
std::unique_ptr< _Ty, _Dx > & m_own
Original owner of the pointer.
Definition: Common.h:459
_Ty * m_ptr
Pointer.
Definition: Common.h:460
~ref_unique_ptr()
Returns ownership of the pointer.
Definition: Common.h:432
ref_unique_ptr(1 ref_unique_ptr< _Ty, _Dx > &&other)
Moves object.
Definition: Common.h:422
ref_unique_ptr(1 std::unique_ptr< _Ty, _Dx > &owner)
Takes ownership of the pointer.
Definition: Common.h:412
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition: Common.h:1423
sanitizing_allocator(1 const sanitizing_allocator< _Ty > &_Othr)
Construct by copying.
Definition: Common.h:1446
void deallocate(1 pointer _Ptr, 1 size_type _Size)
Deallocate object at _Ptr sanitizing its content first.
Definition: Common.h:1461
sanitizing_allocator(1 const sanitizing_allocator< _Other > &_Othr) noexcept
Construct from a related allocator.
Definition: Common.h:1454
std::allocator< _Ty > _Mybase
Base type.
Definition: Common.h:1425
sanitizing_allocator() noexcept
Construct default allocator.
Definition: Common.h:1439
Sanitizing BLOB.
Definition: Common.h:1503
sanitizing_blob()
Constructs uninitialized BLOB.
Definition: Common.h:1508
~sanitizing_blob()
Sanitizes BLOB.
Definition: Common.h:1516
Single-byte character implementation of a class to support converting GUID to string.
Definition: Common.h:1353
string_guid(1 const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1363
Windows runtime error.
Definition: Common.h:1048
win_runtime_error(1 const char *msg=nullptr)
Constructs an exception using GetLastError()
Definition: Common.h:1084
win_runtime_error(1 error_type num, 1 const char *msg=nullptr)
Constructs an exception.
Definition: Common.h:1066
tstring msg(1 DWORD dwLanguageId=0) const
Returns a user-readable Windows error message.
Definition: Common.h:1093
win_runtime_error(1 const std::string &msg)
Constructs an exception using GetLastError()
Definition: Common.h:1075
win_runtime_error(1 error_type num, 1 const std::string &msg)
Constructs an exception.
Definition: Common.h:1056
Wide character implementation of a class to support converting GUID to string.
Definition: Common.h:1375
wstring_guid(1 const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1385
#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:335
ref_unique_ptr< _Ty[], _Dx > get_ptr(1 std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Helper function template for returning pointers to std::unique_ptr (specialization for arrays)
Definition: Common.h:485
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:1487
sanitizing_string sanitizing_tstring
Multi-byte / Wide-character sanitizing string (according to _UNICODE)
Definition: Common.h:1495
std::basic_string< char, std::char_traits< char >, sanitizing_allocator< char > > sanitizing_string
A sanitizing variant of std::string.
Definition: Common.h:1478
static const T invalid
Invalid handle value.
Definition: Common.h:614
LocalFree_delete() noexcept
Default construct.
Definition: Common.h:378
void operator()(1 _Ty *_Ptr) const noexcept
Delete a pointer.
Definition: Common.h:383
LocalFree_delete< _Ty > _Myt
This type.
Definition: Common.h:373
void operator()(_Other *) const
Delete a pointer of another type.
Definition: Common.h:394
Deleter for unique_ptr using LocalFree.
Definition: Common.h:343
LocalFree_delete< _Ty > _Myt
This type.
Definition: Common.h:344
LocalFree_delete(const LocalFree_delete< _Ty2 > &)
Construct from another LocalFree_delete.
Definition: Common.h:354
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: Common.h:361
LocalFree_delete()
Default construct.
Definition: Common.h:349
Convert this type to sanitizing_allocator<_Other>
Definition: Common.h:1432
sanitizing_allocator< _Other > other
Other type.
Definition: Common.h:1433