-
Notifications
You must be signed in to change notification settings - Fork 268
wil::to_array_view provides safer access to IBuffer / IMemoryBuffer #359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
d63111c
dc1a172
19be769
f77b41b
5151b52
6befa97
ff198b0
6102e25
5796acd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -226,6 +226,51 @@ struct dispatcher_traits<winrt::Microsoft::UI::Dispatching::DispatcherQueue> | |||||
#endif // __WIL_CPPWINRT_MICROSOFT_UI_DISPATCHING_HELPERS | ||||||
/// @endcond | ||||||
|
||||||
#if defined(WINRT_Windows_Foundation_H) && !defined(__WIL_CPPWINRT_WINDOWS_FOUNDATION_BUFFER_HELPERS) ||\ | ||||||
defined(WIL_DOXYGEN) | ||||||
/// @cond | ||||||
#define __WIL_CPPWINRT_WINDOWS_FOUNDATION_BUFFER_HELPERS | ||||||
namespace Windows::Foundation | ||||||
dunhor marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
{ | ||||||
struct IMemoryBufferByteAccess; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
To make clang-format happy There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, too many instances to do this manually... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can I on a fork? One thing I'd really like to do if I ever got the time to do it is to add a pipeline/action/something that I can run that will run clang format & commit the result, either as a separate command (e.g. |
||||||
} | ||||||
/// @endcond | ||||||
|
||||||
namespace wil | ||||||
{ | ||||||
//! Returns a view into the underlying bytes of a memory buffer | ||||||
//! provided in the form of an IMemoryBufferReference. | ||||||
//! The caller is responsible for ensuring that the memory buffer's | ||||||
//! lifetime encompasses the lifetime of the returned view. | ||||||
//! By default, returns an array_view<uint8_t>, but you can provide an alternate | ||||||
//! type such as to_array_view<double>. | ||||||
//! You must include memorybuffer.h in order to use this overload of to_array_view. | ||||||
oldnewthing marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
template<typename T = uint8_t> | ||||||
winrt::array_view<T> to_array_view(winrt::Windows::Foundation::IMemoryBufferReference const& reference) | ||||||
{ | ||||||
uint8_t* data; | ||||||
uint32_t capacity; | ||||||
// Make IMemoryBufferByteAccess a dependent type so we can talk about it even if <memorybuffer.h> hasn't been included. | ||||||
using IMemoryBufferByteAccess = std::enable_if_t<std::is_same_v<T, T>, ::Windows::Foundation::IMemoryBufferByteAccess>; | ||||||
winrt::check_hresult(reference.as<IMemoryBufferByteAccess>()->GetBuffer(&data, &capacity)); | ||||||
return { reinterpret_cast<T*>(data), static_cast<uint32_t>(capacity / sizeof(T)) }; | ||||||
} | ||||||
|
||||||
//! Returns a view into the underlying bytes of a memory buffer | ||||||
//! provided in the form of an IMemoryBuffer. | ||||||
//! The caller is responsible for ensuring that the memory buffer's | ||||||
//! lifetime encompasses the lifetime of the returned view. | ||||||
//! By default, returns an array_view<uint8_t>, but you can provide an alternate | ||||||
//! type such as to_array_view<double>. | ||||||
//! You must include memorybuffer.h in order to use this overload of to_array_view. | ||||||
template<typename T = uint8_t> | ||||||
winrt::array_view<T> to_array_view(winrt::Windows::Foundation::IMemoryBuffer const& buffer) | ||||||
{ | ||||||
return to_array_view<T>(buffer.CreateReference()); | ||||||
} | ||||||
} | ||||||
#endif | ||||||
|
||||||
#if (defined(WINRT_Windows_Foundation_Collections_H) && !defined(__WIL_CPPWINRT_WINDOWS_FOUNDATION_COLLECTION_HELPERS)) || \ | ||||||
defined(WIL_DOXYGEN) | ||||||
/// @cond | ||||||
|
@@ -352,6 +397,37 @@ auto to_vector(TSrc const& src) | |||||
} // namespace wil | ||||||
#endif | ||||||
|
||||||
#if defined(WINRT_Windows_Storage_Streams_H) && !defined(__WIL_CPPWINRT_WINDOWS_STORAGE_STREAMS_HELPERS) ||\ | ||||||
defined(WIL_DOXYGEN) | ||||||
/// @cond | ||||||
#define __WIL_CPPWINRT_WINDOWS_STORAGE_STREAMS_HELPERS | ||||||
/// @endcond | ||||||
namespace wil | ||||||
{ | ||||||
//! Returns a view into the underlying bytes of an IBuffer up to its Length. | ||||||
//! The caller is responsible for ensuring that the IBuffer's | ||||||
//! lifetime encompasses the lifetime of the returned view. | ||||||
//! By default, returns an array_view<uint8_t>, but you can provide an alternate | ||||||
//! type such as to_array_view<double>. | ||||||
template<typename T = uint8_t> | ||||||
winrt::array_view<T> to_array_view(winrt::Windows::Storage::Streams::IBuffer const& buffer) | ||||||
{ | ||||||
return { reinterpret_cast<T*>(buffer.data()), static_cast<uint32_t>(buffer.Length() / sizeof(T)) }; | ||||||
} | ||||||
|
||||||
//! Returns a view into the underlying bytes of an IBuffer up to its Capacity. | ||||||
//! The caller is responsible for ensuring that the IBuffer's | ||||||
//! lifetime encompasses the lifetime of the returned view. | ||||||
//! By default, returns an array_view<uint8_t>, but you can provide an alternate | ||||||
//! type such as to_array_view<double>. | ||||||
template<typename T = uint8_t> | ||||||
winrt::array_view<T> to_array_view_for_capacity(winrt::Windows::Storage::Streams::IBuffer const& buffer) | ||||||
{ | ||||||
return { reinterpret_cast<T*>(buffer.data()), static_cast<uint32_t>(buffer.Capacity() / sizeof(T)) }; | ||||||
} | ||||||
} | ||||||
#endif | ||||||
|
||||||
#if (defined(WINRT_Windows_UI_H) && defined(_WINDOWS_UI_INTEROP_H_) && !defined(__WIL_CPPWINRT_WINDOWS_UI_INTEROP_HELPERS)) || \ | ||||||
defined(WIL_DOXYGEN) | ||||||
/// @cond | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make clang-format happy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think extra parens will also help.