11 #include <winstring.h>
18 #include <wrl\internal.h>
39 namespace HandleTraits
48 return ::CloseHandle(h) != FALSE;
63 return ::CloseHandle(h) != FALSE;
68 return INVALID_HANDLE_VALUE;
77 typedef CRITICAL_SECTION*
Type;
84 _Releases_lock_(*cs)
inline static void Unlock(_In_
Type cs)
throw()
86 ::LeaveCriticalSection(cs);
94 if (::ReleaseMutex(h) == FALSE)
107 if (::ReleaseSemaphore(h, 1, NULL) == FALSE)
128 _Releases_shared_lock_(*srwlock)
inline static void Unlock(_In_
Type srwlock)
throw()
130 ::ReleaseSRWLockShared(srwlock);
143 _Releases_exclusive_lock_(*srwlock)
inline static void Unlock(_In_
Type srwlock)
throw()
145 ::ReleaseSRWLockExclusive(srwlock);
152 template <
typename HandleTraits>
156 explicit HandleT(
typename HandleTraits::Type h = HandleTraits::GetInvalidValue()) throw()
163 h.handle_ = HandleTraits::GetInvalidValue();
175 h.handle_ = HandleTraits::GetInvalidValue();
179 void Attach(
typename HandleTraits::Type h)
throw()
188 typename HandleTraits::Type
Detach() throw()
190 typename HandleTraits::Type h =
handle_;
191 handle_ = HandleTraits::GetInvalidValue();
195 typename HandleTraits::Type
Get()
const throw()
202 if (
handle_ != HandleTraits::GetInvalidValue())
211 handle_ = HandleTraits::GetInvalidValue();
217 return handle_ != HandleTraits::GetInvalidValue();
236 return HandleTraits::Close(
handle_);
250 return rhs.Get() == lhs.Get();
256 return lhs == rhs.Get();
262 return lhs.Get() == rhs;
268 return lhs.Get() != rhs.Get();
274 return lhs != rhs.Get();
280 return lhs.Get() != rhs;
286 return lhs.Get() < rhs.Get();
301 # pragma warning(push)
303 # pragma warning(disable : 26165)
347 _Releases_lock_(*
sync_)
void InternalUnlock() throw()
405 _Releases_exclusive_lock_(*
sync_)
void InternalUnlock() throw()
457 _Releases_shared_lock_(*
sync_)
void InternalUnlock() throw()
468 # pragma warning(pop)
470 # pragma warning(push)
473 # pragma warning(disable : 26165 26167 26135)
476 template <
typename SyncTraits>
484 other.sync_ = SyncTraits::GetInvalidValue();
500 return sync_ != SyncTraits::GetInvalidValue() && (
status_ == 0 ||
status_ == WAIT_ABANDONED);
525 void InternalUnlock() throw()
540 # pragma warning(suppress : 26110)
542 SyncTraits::Unlock(
sync_);
543 sync_ = SyncTraits::GetInvalidValue();
549 # pragma warning(pop)
562 ::InitializeCriticalSectionEx(&
cs_, spincount, 0);
567 ::DeleteCriticalSection(&
cs_);
570 _Acquires_lock_(*
return.sync_) _Post_same_lock_(*
return.sync_,
cs_)
SyncLock Lock() throw()
575 _Acquires_lock_(*
return.sync_) _Post_same_lock_(*
return.sync_, *cs)
static SyncLock
576 Lock(_In_ CRITICAL_SECTION* cs)
throw()
578 ::EnterCriticalSection(cs);
587 _Acquires_lock_(*
return.sync_) _Post_same_lock_(*
return.sync_, *cs)
static SyncLock
590 bool acquired = !!::TryEnterCriticalSection(cs);
591 _Analysis_assume_lock_held_(*cs);
592 return SyncLock((acquired) ? cs :
nullptr);
631 return Lock(
Get(), milliseconds);
636 DWORD
const status = ::WaitForSingleObjectEx(h, milliseconds, FALSE);
637 return SyncLock(h, status == WAIT_OBJECT_0 ? 0 : status);
644 HANDLE* GetAddressOf();
645 HANDLE* ReleaseAndGetAddressOf();
670 return Lock(
Get(), milliseconds);
675 DWORD
const status = ::WaitForSingleObjectEx(h, milliseconds, FALSE);
676 return SyncLock(h, status == WAIT_OBJECT_0 ? 0 : status);
683 HANDLE* GetAddressOf();
684 HANDLE* ReleaseAndGetAddressOf();
691 explicit Event(HANDLE h = HandleT::Traits::GetInvalidValue()) throw()
727 _Acquires_exclusive_lock_(*
return.sync_) _Post_same_lock_(*
return.sync_, *lock)
static SyncLockExclusive
730 ::AcquireSRWLockExclusive(lock);
740 _Acquires_exclusive_lock_(*
return.sync_) _Post_same_lock_(*
return.sync_, *lock)
static SyncLockExclusive
743 bool acquired = !!::TryAcquireSRWLockExclusive(lock);
744 _Analysis_assume_lock_held_(*lock);
754 _Acquires_shared_lock_(*
return.sync_) _Post_same_lock_(*
return.sync_, *lock)
static SyncLockShared
757 ::AcquireSRWLockShared(lock);
767 _Acquires_shared_lock_(*
return.sync_) _Post_same_lock_(*
return.sync_, *lock)
static SyncLockShared
770 bool acquired = !!::TryAcquireSRWLockShared(lock);
771 _Analysis_assume_lock_held_(*lock);
796 other.hstr_ =
nullptr;
808 other.hstr_ =
nullptr;
814 HRESULT
Set(_In_reads_opt_(len)
const wchar_t* str,
unsigned int len)
throw()
817 return ::WindowsCreateString(str, len, &
hstr_);
823 template <
size_t sizeDest>
824 HRESULT
Set(
const wchar_t (&str)[sizeDest])
throw()
827 static_cast<size_t>(
static_cast<UINT32
>(sizeDest - 1)) == sizeDest - 1,
828 "String length underflow or overflow");
830 return Set(str, sizeDest - 1);
842 template <
size_t sizeDest>
843 HRESULT
Set(_In_z_
wchar_t (&strRef)[sizeDest])
throw()
845 const wchar_t* str =
static_cast<const wchar_t*
>(strRef);
847 HRESULT hr = SizeTToUInt32(::wcslen(str), &length);
850 hr =
Set(str, length);
872 template <
typename T>
874 Set(__in_opt
const T& strRef,
876 typename ::Microsoft::WRL::Details::EnableIf<
877 __is_convertible_to(T,
const wchar_t*),
880 HRESULT hr = E_POINTER;
881 const wchar_t* str =
static_cast<PCWSTR
>(strRef);
882 _Analysis_assume_nullterminated_(
static_cast<void*
>(
const_cast<wchar_t*
>(
888 hr = SizeTToUInt32(::wcslen(str), &length);
891 hr =
Set(str, length);
898 HRESULT
Set(
const HSTRING& str)
throw()
906 if (str ==
nullptr || str !=
hstr_)
909 hr = ::WindowsDuplicateString(str, &
hstr_);
915 void Attach(_In_opt_ HSTRING hstr)
throw()
917 ::WindowsDeleteString(
hstr_);
934 HSTRING
Get()
const throw()
941 ::WindowsDeleteString(
hstr_);
947 return hstr_ !=
nullptr;
952 return ::WindowsGetStringRawBuffer(
hstr_, length);
955 HRESULT
CopyTo(_Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING* str)
const throw()
957 return ::WindowsDuplicateString(
hstr_, str);
960 template <
unsigned int sizeDest>
963 template <
unsigned int sizeDest>
977 void CreateReference(
const wchar_t* str,
unsigned int bufferLen,
unsigned int len)
980 if (len >= bufferLen)
985 HRESULT hr = ::WindowsCreateStringReference(str, len, &
header_, &
hstr_);
1006 CreateReference(str, len + 1, len);
1011 template <
unsigned int sizeDest>
1016 static_cast<size_t>(
static_cast<unsigned int>(sizeDest - 1)) == sizeDest - 1,
1017 "String length underflow or overflow");
1019 CreateReference(str, sizeDest, sizeDest - 1);
1025 template <
size_t sizeDest>
1028 const wchar_t* str =
static_cast<const wchar_t*
>(strRef);
1030 unsigned int length;
1031 HRESULT hr = SizeTToUInt32(::wcslen(str), &length);
1037 CreateReference(str, length + 1, length);
1042 template <
typename T>
1044 _In_
const T& strRef,
1046 typename ::Microsoft::WRL::Details::EnableIf<
1047 __is_convertible_to(T,
const wchar_t*),
1051 const wchar_t* str =
static_cast<const wchar_t*
>(strRef);
1052 _Analysis_assume_nullterminated_(
static_cast<void*
>(
const_cast<wchar_t*
>(str)));
1053 _Analysis_assume_(strlen(
reinterpret_cast<const char*
>(str)) >=
sizeof(
wchar_t));
1055 unsigned int length;
1056 HRESULT hr = SizeTToUInt32(::wcslen(str), &length);
1062 CreateReference(str, length + 1, length);
1068 unsigned int length = 0;
1069 const wchar_t* value = other.GetRawBuffer(&length);
1070 CreateReference(value, length + 1, length);
1080 unsigned int length = 0;
1082 CreateReference(value, length + 1, length);
1094 return ::WindowsGetStringRawBuffer(
hstr_, length);
1097 HRESULT
CopyTo(_Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING* str)
const throw()
1099 return ::WindowsDuplicateString(
hstr_, str);
1109 template <
unsigned int sizeDest>
1113 static_cast<size_t>(
static_cast<unsigned int>(sizeDest - 1)) == sizeDest - 1,
1114 "String length underflow or overflow");
1117 hstringRef.CreateReference(str, sizeDest, sizeDest - 1);
1121 template <
unsigned int sizeDest>
1125 hstringRef.CreateReference(str, sizeDest, len);
1136 hr = ::WindowsCompareStringOrdinal(lhs, rhs, &result);
1249 _hr = ::Windows::Foundation::Initialize(flags);
1255 ::Windows::Foundation::Uninitialize();
Definition: corewrappers.h:556
_Acquires_lock_ return _Post_same_lock_ return SyncLock TryLock()
Definition: corewrappers.h:582
_Acquires_lock_ return _Post_same_lock_ static return SyncLock TryLock(_In_ CRITICAL_SECTION *cs)
Definition: corewrappers.h:588
Details::SyncLockCriticalSection SyncLock
Definition: corewrappers.h:558
~CriticalSection()
Definition: corewrappers.h:565
_Acquires_lock_ return _Post_same_lock_ static return SyncLock Lock(_In_ CRITICAL_SECTION *cs)
Definition: corewrappers.h:576
CRITICAL_SECTION cs_
Definition: corewrappers.h:601
_Acquires_lock_ return _Post_same_lock_ return SyncLock Lock()
Definition: corewrappers.h:570
bool IsValid() const
Definition: corewrappers.h:595
CriticalSection(ULONG spincount=0)
Definition: corewrappers.h:560
Definition: corewrappers.h:308
_Releases_lock_ sync_ ~SyncLockCriticalSection()
Definition: corewrappers.h:316
SyncLockCriticalSection(_Inout_ SyncLockCriticalSection &&other)
Definition: corewrappers.h:310
CRITICAL_SECTION * sync_
Definition: corewrappers.h:340
bool IsLocked() const
Definition: corewrappers.h:327
SyncLockCriticalSection(CRITICAL_SECTION *sync=HandleTraits::CriticalSectionTraits::GetInvalidValue())
Definition: corewrappers.h:335
_Releases_lock_ sync_ void Unlock()
Definition: corewrappers.h:321
Definition: corewrappers.h:366
SyncLockExclusive(_Inout_ SyncLockExclusive &&other)
Definition: corewrappers.h:368
_Releases_exclusive_lock_ sync_ ~SyncLockExclusive()
Definition: corewrappers.h:374
bool IsLocked() const
Definition: corewrappers.h:385
SyncLockExclusive(SRWLOCK *sync=HandleTraits::SRWLockExclusiveTraits::GetInvalidValue())
Definition: corewrappers.h:393
SRWLOCK * sync_
Definition: corewrappers.h:398
_Releases_exclusive_lock_ sync_ void Unlock()
Definition: corewrappers.h:379
Definition: corewrappers.h:418
_Releases_shared_lock_ sync_ void Unlock()
Definition: corewrappers.h:431
SyncLockShared(_Inout_ SyncLockShared &&other)
Definition: corewrappers.h:420
SyncLockShared(SRWLOCK *sync=HandleTraits::SRWLockSharedTraits::GetInvalidValue())
Definition: corewrappers.h:445
bool IsLocked() const
Definition: corewrappers.h:437
_Releases_shared_lock_ sync_ ~SyncLockShared()
Definition: corewrappers.h:426
SRWLOCK * sync_
Definition: corewrappers.h:450
Definition: corewrappers.h:478
SyncTraits::Type sync_
Definition: corewrappers.h:518
void Unlock()
Definition: corewrappers.h:492
SyncLockWithStatusT(typename SyncTraits::Type sync, DWORD status)
Definition: corewrappers.h:512
DWORD status_
Definition: corewrappers.h:517
DWORD GetStatus() const
Definition: corewrappers.h:503
~SyncLockWithStatusT()
Definition: corewrappers.h:487
SyncLockWithStatusT(_Inout_ SyncLockWithStatusT &&other)
Definition: corewrappers.h:480
bool IsLocked() const
Definition: corewrappers.h:498
Definition: corewrappers.h:689
Event & operator=(_Inout_ Event &&h)
Definition: corewrappers.h:699
Event(HANDLE h=HandleT::Traits::GetInvalidValue())
Definition: corewrappers.h:691
Event(_Inout_ Event &&h)
Definition: corewrappers.h:695
Definition: corewrappers.h:975
HStringReference(_In_z_ wchar_t(&strRef)[sizeDest])
Definition: corewrappers.h:1026
HStringReference(_In_ const HStringReference &other)
Definition: corewrappers.h:1065
HStringReference & operator=(_In_ const HStringReference &other)
Definition: corewrappers.h:1078
HStringReference(const wchar_t *str, unsigned int len)
Definition: corewrappers.h:1003
~HStringReference()
Definition: corewrappers.h:1073
HSTRING hstr_
Definition: corewrappers.h:1106
HRESULT CopyTo(_Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING *str) const
Definition: corewrappers.h:1097
HStringReference(_In_ const T &strRef, typename ::Microsoft::WRL::Details::EnableIf< __is_convertible_to(T, const wchar_t *), ::Microsoft::WRL::Details::Dummy >::type=::Microsoft::WRL::Details::Dummy())
Definition: corewrappers.h:1043
const wchar_t * GetRawBuffer(_Out_opt_ unsigned int *length) const
Definition: corewrappers.h:1092
HSTRING_HEADER header_
Definition: corewrappers.h:1105
HStringReference(wchar_t const (&str)[sizeDest])
Definition: corewrappers.h:1012
HSTRING Get() const
Definition: corewrappers.h:1087
Definition: corewrappers.h:787
bool IsValid() const
Definition: corewrappers.h:945
HSTRING Detach()
Definition: corewrappers.h:921
void Attach(_In_opt_ HSTRING hstr)
Definition: corewrappers.h:915
HSTRING hstr_
Definition: corewrappers.h:967
HRESULT CopyTo(_Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING *str) const
Definition: corewrappers.h:955
static HStringReference MakeReference(wchar_t const (&str)[sizeDest])
Definition: corewrappers.h:1110
~HString()
Definition: corewrappers.h:799
void Release()
Definition: corewrappers.h:939
HString()
Definition: corewrappers.h:789
HRESULT Set(_In_reads_opt_(len) const wchar_t *str, unsigned int len)
Definition: corewrappers.h:814
HString(_Inout_ HString &&other)
Definition: corewrappers.h:793
const wchar_t * GetRawBuffer(_Out_opt_ unsigned int *length) const
Definition: corewrappers.h:950
HString & operator=(_Inout_ HString &&other)
Definition: corewrappers.h:804
HRESULT Set(const wchar_t(&str)[sizeDest])
Definition: corewrappers.h:824
HRESULT Set(__in_opt const T &strRef, typename ::Microsoft::WRL::Details::EnableIf< __is_convertible_to(T, const wchar_t *), ::Microsoft::WRL::Details::Dummy >::type=::Microsoft::WRL::Details::Dummy())
Definition: corewrappers.h:874
HSTRING * GetAddressOf()
Definition: corewrappers.h:928
HSTRING Get() const
Definition: corewrappers.h:934
HRESULT Set(_In_z_ wchar_t(&strRef)[sizeDest])
Definition: corewrappers.h:843
HRESULT Set(const HSTRING &str)
Definition: corewrappers.h:898
Definition: corewrappers.h:154
virtual bool InternalClose()
Definition: corewrappers.h:234
HandleTraits::Type * GetAddressOf()
Definition: corewrappers.h:220
HandleT(typename HandleTraits::Type h=HandleTraits::GetInvalidValue())
Definition: corewrappers.h:156
HandleTraits::Type * ReleaseAndGetAddressOf()
Definition: corewrappers.h:225
HandleTraits::Type Detach()
Definition: corewrappers.h:188
virtual ~HandleT()
Definition: corewrappers.h:166
HandleT & operator=(_Inout_ HandleT &&h)
Definition: corewrappers.h:171
HandleT(_Inout_ HandleT &&h)
Definition: corewrappers.h:160
HandleTraits Traits
Definition: corewrappers.h:231
void Close()
Definition: corewrappers.h:200
HandleTraits::Type handle_
Definition: corewrappers.h:239
void Attach(typename HandleTraits::Type h)
Definition: corewrappers.h:179
bool IsValid() const
Definition: corewrappers.h:215
HandleTraits::Type Get() const
Definition: corewrappers.h:195
Definition: corewrappers.h:611
Mutex(_Inout_ Mutex &&h)
Definition: corewrappers.h:619
SyncLock Lock(DWORD milliseconds=INFINITE)
Definition: corewrappers.h:629
Mutex & operator=(_Inout_ Mutex &&h)
Definition: corewrappers.h:623
static SyncLock Lock(HANDLE h, DWORD milliseconds=INFINITE)
Definition: corewrappers.h:634
Mutex(HANDLE h)
Definition: corewrappers.h:615
Details::SyncLockWithStatusT< HandleTraits::MutexTraits > SyncLock
Definition: corewrappers.h:613
Definition: corewrappers.h:1243
~RoInitializeWrapper()
Definition: corewrappers.h:1251
RoInitializeWrapper(RO_INIT_TYPE flags)
Definition: corewrappers.h:1247
Definition: corewrappers.h:708
SRWLOCK SRWLock_
Definition: corewrappers.h:776
_Acquires_shared_lock_ return _Post_same_lock_ static return SyncLockShared LockShared(_In_ SRWLOCK *lock)
Definition: corewrappers.h:755
Details::SyncLockExclusive SyncLockExclusive
Definition: corewrappers.h:710
Details::SyncLockShared SyncLockShared
Definition: corewrappers.h:711
SRWLock()
Definition: corewrappers.h:713
~SRWLock()
Definition: corewrappers.h:718
_Acquires_exclusive_lock_ return _Post_same_lock_ return SyncLockExclusive LockExclusive()
Definition: corewrappers.h:722
_Acquires_shared_lock_ return _Post_same_lock_ return SyncLockShared TryLockShared()
Definition: corewrappers.h:762
_Acquires_exclusive_lock_ return _Post_same_lock_ static return SyncLockExclusive LockExclusive(_In_ SRWLOCK *lock)
Definition: corewrappers.h:728
_Acquires_exclusive_lock_ return _Post_same_lock_ static return SyncLockExclusive TryLockExclusive(_In_ SRWLOCK *lock)
Definition: corewrappers.h:741
_Acquires_shared_lock_ return _Post_same_lock_ return SyncLockShared LockShared()
Definition: corewrappers.h:749
_Acquires_exclusive_lock_ return _Post_same_lock_ return SyncLockExclusive TryLockExclusive()
Definition: corewrappers.h:735
_Acquires_shared_lock_ return _Post_same_lock_ static return SyncLockShared TryLockShared(_In_ SRWLOCK *lock)
Definition: corewrappers.h:768
Definition: corewrappers.h:650
Semaphore & operator=(_Inout_ Semaphore &&h)
Definition: corewrappers.h:662
Semaphore(HANDLE h)
Definition: corewrappers.h:654
Semaphore(_Inout_ Semaphore &&h)
Definition: corewrappers.h:658
SyncLock Lock(DWORD milliseconds=INFINITE)
Definition: corewrappers.h:668
static SyncLock Lock(HANDLE h, DWORD milliseconds=INFINITE)
Definition: corewrappers.h:673
Details::SyncLockWithStatusT< HandleTraits::SemaphoreTraits > SyncLock
Definition: corewrappers.h:652
#define __WRL_ASSERT__(cond)
Definition: internal.h:13
void RaiseException(HRESULT hr, DWORD dwExceptionFlags=EXCEPTION_NONCONTINUABLE)
Definition: internal.h:49
RemoveReference< T >::Type && Move(_Inout_ T &&arg)
Definition: internal.h:95
INT32 CompareStringOrdinal(HSTRING lhs, HSTRING rhs)
Definition: corewrappers.h:1131
bool operator==(const HandleT< T > &rhs, const HandleT< T > &lhs)
Definition: corewrappers.h:248
bool operator<(const HandleT< T > &lhs, const HandleT< T > &rhs)
Definition: corewrappers.h:284
HandleT< HandleTraits::FileHandleTraits > FileHandle
Definition: corewrappers.h:289
bool operator!=(const HandleT< T > &lhs, const HandleT< T > &rhs)
Definition: corewrappers.h:266
This file has no copyright assigned and is placed in the Public Domain.
Definition: client.h:26
Definition: corewrappers.h:33
Definition: corewrappers.h:76
CRITICAL_SECTION * Type
Definition: corewrappers.h:77
_Releases_lock_ static cs void Unlock(_In_ Type cs)
Definition: corewrappers.h:84
static Type GetInvalidValue()
Definition: corewrappers.h:79
Definition: corewrappers.h:117
Definition: corewrappers.h:73
Definition: corewrappers.h:43
static Type GetInvalidValue()
Definition: corewrappers.h:51
HANDLE Type
Definition: corewrappers.h:44
static bool Close(_In_ Type h)
Definition: corewrappers.h:46
Definition: corewrappers.h:58
static bool Close(_In_ Type h)
Definition: corewrappers.h:61
static HANDLE GetInvalidValue()
Definition: corewrappers.h:66
HANDLE Type
Definition: corewrappers.h:59
Definition: corewrappers.h:91
_Releases_lock_(h) inline static void Unlock(_In_ Type h)
Definition: corewrappers.h:92
Definition: corewrappers.h:135
SRWLOCK * Type
Definition: corewrappers.h:136
_Releases_exclusive_lock_ static srwlock void Unlock(_In_ Type srwlock)
Definition: corewrappers.h:143
static Type GetInvalidValue()
Definition: corewrappers.h:138
Definition: corewrappers.h:120
static Type GetInvalidValue()
Definition: corewrappers.h:123
SRWLOCK * Type
Definition: corewrappers.h:121
_Releases_shared_lock_ static srwlock void Unlock(_In_ Type srwlock)
Definition: corewrappers.h:128
Definition: corewrappers.h:104
static void Unlock(_In_ Type h)
Definition: corewrappers.h:105