Skip to content
This repository was archived by the owner on Jan 28, 2022. It is now read-only.

Commit 545fe0f

Browse files
committed
Add M2BindableVectorView.
1 parent 3e6c8af commit 545fe0f

3 files changed

Lines changed: 208 additions & 0 deletions

File tree

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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_

M2TeamCommonLibrary/M2TeamCommonLibrary.vcxitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
</ItemGroup>
2323
<ItemGroup>
2424
<ClInclude Include="$(MSBuildThisFileDirectory)M2BaseHelpers.h" />
25+
<ClInclude Include="$(MSBuildThisFileDirectory)M2BindableVectorView.h" />
2526
<ClInclude Include="$(MSBuildThisFileDirectory)M2CXHelpers.h" />
2627
<ClInclude Include="$(MSBuildThisFileDirectory)M2MessageDialogResource.h" />
2728
<ClInclude Include="$(MSBuildThisFileDirectory)M2Win32GUIHelpers.h" />

M2TeamCommonLibrary/M2TeamCommonLibrary.vcxitems.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
<Filter Include="M2WinRTHelpers">
1717
<UniqueIdentifier>{4d531645-d79a-43b5-8917-abeb8cdacf41}</UniqueIdentifier>
1818
</Filter>
19+
<Filter Include="M2BindableVectorView">
20+
<UniqueIdentifier>{a92727ff-c07e-4599-8317-f845bd196131}</UniqueIdentifier>
21+
</Filter>
1922
</ItemGroup>
2023
<ItemGroup>
2124
<ClCompile Include="$(MSBuildThisFileDirectory)M2CXHelpers.cpp">
@@ -53,6 +56,9 @@
5356
<ClInclude Include="$(MSBuildThisFileDirectory)M2WinRTHelpers.h">
5457
<Filter>M2WinRTHelpers</Filter>
5558
</ClInclude>
59+
<ClInclude Include="$(MSBuildThisFileDirectory)M2BindableVectorView.h">
60+
<Filter>M2BindableVectorView</Filter>
61+
</ClInclude>
5662
</ItemGroup>
5763
<ItemGroup>
5864
<ResourceCompile Include="$(MSBuildThisFileDirectory)M2MessageDialogResource.rc">

0 commit comments

Comments
 (0)