|
| 1 | +/****************************************************************************** |
| 2 | +Project: M2-Team Common Library |
| 3 | +Description: Definition for the M2::BindableVectorView. |
| 4 | +File Name: M2BindableVectorView.h |
| 5 | +License: The MIT License |
| 6 | +******************************************************************************/ |
| 7 | + |
| 8 | +#pragma once |
| 9 | + |
| 10 | +#ifndef _M2_BINDABLE_VECTOR_VIEW_ |
| 11 | +#define _M2_BINDABLE_VECTOR_VIEW_ |
| 12 | + |
| 13 | +#include <winrt\Windows.Foundation.h> |
| 14 | +#include <winrt\Windows.Foundation.Collections.h> |
| 15 | +#include <winrt\Windows.UI.Xaml.Interop.h> |
| 16 | + |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +namespace M2 |
| 20 | +{ |
| 21 | + template <typename Type> |
| 22 | + struct BindableVectorView : winrt::implements< |
| 23 | + BindableVectorView<Type>, |
| 24 | + winrt::Windows::Foundation::Collections::IVectorView<Type>, |
| 25 | + winrt::Windows::Foundation::Collections::IIterable<Type>, |
| 26 | + winrt::Windows::UI::Xaml::Interop::IBindableVectorView, |
| 27 | + winrt::Windows::UI::Xaml::Interop::IBindableIterable> |
| 28 | + { |
| 29 | + private: |
| 30 | + std::vector<Type> m_values; |
| 31 | + |
| 32 | + class WinRTObject |
| 33 | + { |
| 34 | + private: |
| 35 | + Type m_value; |
| 36 | + |
| 37 | + public: |
| 38 | + WinRTObject(Type value) : |
| 39 | + m_value(value) |
| 40 | + { |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + operator Type() |
| 45 | + { |
| 46 | + return this->m_value; |
| 47 | + } |
| 48 | + |
| 49 | + operator winrt::Windows::Foundation::IInspectable() |
| 50 | + { |
| 51 | + return winrt::box_value(this->m_value); |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + struct iterator : winrt::implements< |
| 56 | + iterator, |
| 57 | + winrt::Windows::Foundation::Collections::IIterator<Type>, |
| 58 | + winrt::Windows::UI::Xaml::Interop::IBindableIterator> |
| 59 | + { |
| 60 | + private: |
| 61 | + winrt::com_ptr<BindableVectorView<Type>> m_owner; |
| 62 | + typename std::vector<Type>::const_iterator m_current; |
| 63 | + typename std::vector<Type>::const_iterator const m_end; |
| 64 | + |
| 65 | + public: |
| 66 | + explicit iterator(BindableVectorView<Type>* owner) noexcept : |
| 67 | + m_current(owner->m_values.begin()), |
| 68 | + m_end(owner->m_values.end()) |
| 69 | + { |
| 70 | + m_owner.copy_from(owner); |
| 71 | + } |
| 72 | + |
| 73 | + WinRTObject Current() const |
| 74 | + { |
| 75 | + if (m_current == m_end) |
| 76 | + { |
| 77 | + throw winrt::hresult_out_of_bounds(); |
| 78 | + } |
| 79 | + |
| 80 | + return WinRTObject(*m_current); |
| 81 | + } |
| 82 | + |
| 83 | + bool HasCurrent() const noexcept |
| 84 | + { |
| 85 | + return m_current != m_end; |
| 86 | + } |
| 87 | + |
| 88 | + bool MoveNext() noexcept |
| 89 | + { |
| 90 | + if (m_current != m_end) |
| 91 | + { |
| 92 | + ++m_current; |
| 93 | + } |
| 94 | + |
| 95 | + return HasCurrent(); |
| 96 | + } |
| 97 | + |
| 98 | + uint32_t GetMany(winrt::array_view<Type> values) |
| 99 | + { |
| 100 | + uint32_t actual = static_cast<uint32_t>(std::distance(m_current, m_end)); |
| 101 | + |
| 102 | + if (actual > values.size()) |
| 103 | + { |
| 104 | + actual = values.size(); |
| 105 | + } |
| 106 | + |
| 107 | + std::copy_n(m_current, actual, values.begin()); |
| 108 | + std::advance(m_current, actual); |
| 109 | + return actual; |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + public: |
| 114 | + explicit BindableVectorView(std::vector<Type>& values) : |
| 115 | + m_values(values) |
| 116 | + { |
| 117 | + } |
| 118 | + |
| 119 | + uint32_t Size() const noexcept |
| 120 | + { |
| 121 | + return static_cast<uint32_t>(m_values.size()); |
| 122 | + } |
| 123 | + |
| 124 | + WinRTObject GetAt(uint32_t const index) const |
| 125 | + { |
| 126 | + if (index >= m_values.size()) |
| 127 | + { |
| 128 | + throw winrt::hresult_out_of_bounds(); |
| 129 | + } |
| 130 | + |
| 131 | + return WinRTObject(m_values[index]); |
| 132 | + } |
| 133 | + |
| 134 | + uint32_t GetMany( |
| 135 | + uint32_t const startIndex, |
| 136 | + winrt::array_view<Type> values) const |
| 137 | + { |
| 138 | + if (startIndex >= m_values.size()) |
| 139 | + { |
| 140 | + return 0; |
| 141 | + } |
| 142 | + |
| 143 | + uint32_t actual = static_cast<uint32_t>( |
| 144 | + m_values.size() - startIndex); |
| 145 | + |
| 146 | + if (actual > values.size()) |
| 147 | + { |
| 148 | + actual = values.size(); |
| 149 | + } |
| 150 | + |
| 151 | + std::copy_n(m_values.begin() + startIndex, actual, values.begin()); |
| 152 | + return actual; |
| 153 | + } |
| 154 | + |
| 155 | + bool IndexOf(Type const& value, uint32_t& index) const noexcept |
| 156 | + { |
| 157 | + index = static_cast<uint32_t>(std::find( |
| 158 | + m_values.begin(), m_values.end(), value) - m_values.begin()); |
| 159 | + return index < m_values.size(); |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + class IIteratorCreator |
| 164 | + { |
| 165 | + private: |
| 166 | + BindableVectorView<Type>* m_owner; |
| 167 | + |
| 168 | + public: |
| 169 | + IIteratorCreator(BindableVectorView<Type>* owner) : |
| 170 | + m_owner(owner) |
| 171 | + { |
| 172 | + |
| 173 | + } |
| 174 | + |
| 175 | + operator winrt::Windows::Foundation::Collections::IIterator<Type>() |
| 176 | + { |
| 177 | + return winrt::make<iterator>(this->m_owner); |
| 178 | + } |
| 179 | + |
| 180 | + operator winrt::Windows::UI::Xaml::Interop::IBindableIterator() |
| 181 | + { |
| 182 | + return winrt::make<iterator>(this->m_owner).try_as< |
| 183 | + winrt::Windows::UI::Xaml::Interop::IBindableIterator>(); |
| 184 | + } |
| 185 | + }; |
| 186 | + |
| 187 | + IIteratorCreator First() |
| 188 | + { |
| 189 | + return IIteratorCreator(this); |
| 190 | + } |
| 191 | + |
| 192 | + bool IndexOf( |
| 193 | + winrt::Windows::Foundation::IInspectable const& value, |
| 194 | + uint32_t& index) const |
| 195 | + { |
| 196 | + return this->IndexOf(winrt::unbox_value<Type>(value), index); |
| 197 | + } |
| 198 | + }; |
| 199 | +} |
| 200 | + |
| 201 | +#endif // _M2_BINDABLE_VECTOR_VIEW_ |
0 commit comments