File tree Expand file tree Collapse file tree 7 files changed +78
-4
lines changed
Expand file tree Collapse file tree 7 files changed +78
-4
lines changed Original file line number Diff line number Diff line change @@ -1507,13 +1507,25 @@ namespace cppwinrt
15071507 }
15081508 else if (type_name == " Windows.Foundation.IReference`1" )
15091509 {
1510- w.write (R"( IReference(T const& value) : IReference<T> (impl::reference_traits<T>::make(value))
1510+ w.write (R"( IReference(T const& value) : IReference(impl::reference_traits<T>::make(value))
15111511 {
15121512 }
1513-
1513+ IReference(std::optional<T> const& value) : IReference(value ? IReference(value.value()) : nullptr)
1514+ {
1515+ }
1516+ operator std::optional<T>() const
1517+ {
1518+ if (*this)
1519+ {
1520+ return this->Value();
1521+ }
1522+ else
1523+ {
1524+ return std::nullopt;
1525+ }
1526+ }
15141527 private:
1515-
1516- IReference<T>(IInspectable const& value) : IReference<T>(value.as<IReference<T>>())
1528+ IReference(IInspectable const& value) : IReference(value.as<IReference>())
15171529 {
15181530 }
15191531)" );
Original file line number Diff line number Diff line change 1+ #include " pch.h"
2+ #include " winrt/test_component.h"
3+
4+ using namespace winrt ;
5+
6+ TEST_CASE (" optional" )
7+ {
8+ test_component::Optional object;
9+
10+ object.Property (Windows::Foundation::IReference (123 ));
11+ REQUIRE (object.Property ().Value () == 123 );
12+
13+ object.Property (nullptr );
14+ REQUIRE (object.Property () == nullptr );
15+
16+ object.Property (456 );
17+ REQUIRE (object.Property ().Value () == 456 );
18+
19+ object.Property (std::optional (789 ));
20+ std::optional<int32_t > value = object.Property ();
21+ REQUIRE (value == std::optional (789 ));
22+ }
Original file line number Diff line number Diff line change 418418 <ClCompile Include =" no_make_detection.cpp" />
419419 <ClCompile Include =" numerics.cpp" />
420420 <ClCompile Include =" observable_index_of.cpp" />
421+ <ClCompile Include =" optional.cpp" />
421422 <ClCompile Include =" out_params.cpp" />
422423 <ClCompile Include =" out_params_abi.cpp" />
423424 <ClCompile Include =" out_params_bad.cpp" />
Original file line number Diff line number Diff line change 1+ #include " pch.h"
2+ #include " Optional.h"
3+ #include " Optional.g.cpp"
Original file line number Diff line number Diff line change 1+ #pragma once
2+ #include " Optional.g.h"
3+
4+ namespace winrt ::test_component::implementation
5+ {
6+ struct Optional : OptionalT<Optional>
7+ {
8+ Optional () = default ;
9+
10+ Windows::Foundation::IReference<int32_t > Property ()
11+ {
12+ return m_property;
13+ }
14+
15+ void Property (Windows::Foundation::IReference<int32_t > const & value)
16+ {
17+ m_property = value;
18+ }
19+
20+ std::optional<int32_t > m_property;
21+ };
22+ }
23+ namespace winrt ::test_component::factory_implementation
24+ {
25+ struct Optional : OptionalT<Optional, implementation::Optional>
26+ {
27+ };
28+ }
Original file line number Diff line number Diff line change @@ -64,6 +64,12 @@ namespace test_component
6464 void IncrementCounter();
6565 }
6666
67+ runtimeclass Optional
68+ {
69+ Optional();
70+ Windows.Foundation.IReference<Int32> Property;
71+ }
72+
6773 runtimeclass Class
6874 {
6975 Class();
Original file line number Diff line number Diff line change 612612 <ClCompile Include =" Class.cpp" />
613613 <ClCompile Include =" Generated Files\module.g.cpp" />
614614 <ClCompile Include =" module.cpp" />
615+ <ClCompile Include =" Optional.cpp" />
615616 <ClCompile Include =" pch.cpp" >
616617 <PrecompiledHeader >Create</PrecompiledHeader >
617618 </ClCompile >
624625 </ItemGroup >
625626 <ItemGroup >
626627 <ClInclude Include =" Class.h" />
628+ <ClInclude Include =" Optional.h" />
627629 <ClInclude Include =" pch.h" />
628630 <ClInclude Include =" Simple.h" />
629631 <ClInclude Include =" Velocity.Class1.h" />
You can’t perform that action at this time.
0 commit comments