From 16e238441944ba2cda476f511f78623d43b13ade Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Fri, 18 Oct 2024 23:47:10 +0200 Subject: [PATCH] system: add safearray_accessor_with_size Signed-off-by: Simon Rozman --- include/stdex/stream.hpp | 8 ++------ include/stdex/system.hpp | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/include/stdex/stream.hpp b/include/stdex/stream.hpp index b7e20b42b..cb5c81e9d 100644 --- a/include/stdex/stream.hpp +++ b/include/stdex/stream.hpp @@ -547,12 +547,8 @@ namespace stdex /// size_t write_sa(_In_ LPSAFEARRAY sa) { - long ubound, lbound; - if (FAILED(SafeArrayGetUBound(sa, 1, &ubound)) || - FAILED(SafeArrayGetLBound(sa, 1, &lbound))) - throw std::invalid_argument("SafeArrayGet[UL]Bound failed"); - safearray_accessor a(sa); - return write(a.data(), static_cast(ubound) - lbound + 1); + safearray_accessor_with_size a(sa); + return write(a.data(), a.size()); } #endif diff --git a/include/stdex/system.hpp b/include/stdex/system.hpp index 11cfc3db7..737a8fcff 100644 --- a/include/stdex/system.hpp +++ b/include/stdex/system.hpp @@ -231,6 +231,32 @@ namespace stdex T* m_data; }; + template + class safearray_accessor_with_size : public safearray_accessor + { + public: + safearray_accessor_with_size(_In_ LPSAFEARRAY sa) : safearray_accessor(sa) + { + m_size = SafeArrayGetElemsize(sa); + for (UINT d = 1, dim = SafeArrayGetDim(sa); d <= dim; ++d) { + long ubound, lbound; + if (FAILED(SafeArrayGetUBound(sa, d, &ubound)) || + FAILED(SafeArrayGetLBound(sa, d, &lbound))) + throw std::invalid_argument("SafeArrayGet[UL]Bound failed"); + m_size *= static_cast(ubound) - lbound + 1; + } + m_size /= sizeof(T); + } + + /// + /// Return size in number of elements + /// + size_t size() const { return m_size; } + + protected: + size_t m_size; + }; + /// /// Deleter for unique_ptr using SafeArrayDestroy ///