Skip to content

Commit

Permalink
Support binding std::optional<T> implementation to IReference<T> para…
Browse files Browse the repository at this point in the history
…meters (#1030)
  • Loading branch information
kennykerr authored Sep 30, 2021
1 parent 6270dcc commit 24af032
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 4 deletions.
20 changes: 16 additions & 4 deletions cppwinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -1507,13 +1507,25 @@ namespace cppwinrt
}
else if (type_name == "Windows.Foundation.IReference`1")
{
w.write(R"( IReference(T const& value) : IReference<T>(impl::reference_traits<T>::make(value))
w.write(R"( IReference(T const& value) : IReference(impl::reference_traits<T>::make(value))
{
}
IReference(std::optional<T> const& value) : IReference(value ? IReference(value.value()) : nullptr)
{
}
operator std::optional<T>() const
{
if (*this)
{
return this->Value();
}
else
{
return std::nullopt;
}
}
private:
IReference<T>(IInspectable const& value) : IReference<T>(value.as<IReference<T>>())
IReference(IInspectable const& value) : IReference(value.as<IReference>())
{
}
)");
Expand Down
22 changes: 22 additions & 0 deletions test/test/optional.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "pch.h"
#include "winrt/test_component.h"

using namespace winrt;

TEST_CASE("optional")
{
test_component::Optional object;

object.Property(Windows::Foundation::IReference(123));
REQUIRE(object.Property().Value() == 123);

object.Property(nullptr);
REQUIRE(object.Property() == nullptr);

object.Property(456);
REQUIRE(object.Property().Value() == 456);

object.Property(std::optional(789));
std::optional<int32_t> value = object.Property();
REQUIRE(value == std::optional(789));
}
1 change: 1 addition & 0 deletions test/test/test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@
<ClCompile Include="no_make_detection.cpp" />
<ClCompile Include="numerics.cpp" />
<ClCompile Include="observable_index_of.cpp" />
<ClCompile Include="optional.cpp" />
<ClCompile Include="out_params.cpp" />
<ClCompile Include="out_params_abi.cpp" />
<ClCompile Include="out_params_bad.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions test/test_component/Optional.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "pch.h"
#include "Optional.h"
#include "Optional.g.cpp"
28 changes: 28 additions & 0 deletions test/test_component/Optional.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
#include "Optional.g.h"

namespace winrt::test_component::implementation
{
struct Optional : OptionalT<Optional>
{
Optional() = default;

Windows::Foundation::IReference<int32_t> Property()
{
return m_property;
}

void Property(Windows::Foundation::IReference<int32_t> const& value)
{
m_property = value;
}

std::optional<int32_t> m_property;
};
}
namespace winrt::test_component::factory_implementation
{
struct Optional : OptionalT<Optional, implementation::Optional>
{
};
}
6 changes: 6 additions & 0 deletions test/test_component/test_component.idl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ namespace test_component
void IncrementCounter();
}

runtimeclass Optional
{
Optional();
Windows.Foundation.IReference<Int32> Property;
}

runtimeclass Class
{
Class();
Expand Down
2 changes: 2 additions & 0 deletions test/test_component/test_component.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@
<ClCompile Include="Class.cpp" />
<ClCompile Include="Generated Files\module.g.cpp" />
<ClCompile Include="module.cpp" />
<ClCompile Include="Optional.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader>Create</PrecompiledHeader>
</ClCompile>
Expand All @@ -624,6 +625,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="Class.h" />
<ClInclude Include="Optional.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="Simple.h" />
<ClInclude Include="Velocity.Class1.h" />
Expand Down

0 comments on commit 24af032

Please sign in to comment.