Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simply static lifetime implementation #1375

Closed
27 changes: 25 additions & 2 deletions cppwinrt/component_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,29 @@ catch (...) { return winrt::to_hresult(); }
}
else if (factory.statics)
{
{
auto format = R"( template <int = 0>
static auto Get%Statics()
{
if constexpr (winrt::impl::has_static_lifetime_v<@::factory_implementation::%>)
JaiganeshKumaran marked this conversation as resolved.
Show resolved Hide resolved
{
return winrt::make_self<@::factory_implementation::%>();
}
else
{
return std::add_pointer_t<@::factory_implementation::%::statics_type>{ nullptr };
}
})";
w.write(format,
type_name,
type_namespace,
type_name,
type_namespace,
type_name,
type_namespace,
type_name);
}

for (auto&& method : factory.type.MethodList())
{
method_signature signature{ method };
Expand Down Expand Up @@ -498,7 +521,7 @@ catch (...) { return winrt::to_hresult(); }
{
auto format = R"( % %::%(%)
{
%@::implementation::%::%(%);
%Get%Statics()->%(%);
}
)";

Expand All @@ -510,7 +533,6 @@ catch (...) { return winrt::to_hresult(); }
method_name,
bind<write_consume_params>(signature),
ignore_return ? "" : "return ",
type_namespace,
type_name,
method_name,
bind<write_consume_args>(signature));
Expand Down Expand Up @@ -870,6 +892,7 @@ catch (...) { return winrt::to_hresult(); }
struct WINRT_IMPL_EMPTY_BASES %T : implements<D, winrt::Windows::Foundation::IActivationFactory%, I...>
{
using instance_type = @::%;
using statics_type = T;

hstring GetRuntimeClassName() const
{
Expand Down
15 changes: 15 additions & 0 deletions test/test_component/StaticClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "pch.h"
#include "StaticClass.g.h"

namespace winrt::test_component::factory_implementation
{
struct StaticClass : StaticClassT<StaticClass, StaticClass, static_lifetime>
{
void Method()
{
throw hresult_not_implemented{};
}
};
}

#include "StaticClass.g.cpp"
5 changes: 5 additions & 0 deletions test/test_component/test_component.idl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ namespace test_component
static void StaticMethodWithAsyncReturn();
}

static runtimeclass StaticClass
{
static void Method();
}

namespace Structs
{
struct All
Expand Down
1 change: 1 addition & 0 deletions test/test_component/test_component.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@
<PrecompiledHeader>Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="Simple.cpp" />
<ClCompile Include="StaticClass.cpp" />
<ClCompile Include="test_auto.cpp" />
<ClCompile Include="Velocity.Class1.cpp" />
<ClCompile Include="Velocity.Class2.cpp" />
Expand Down
Loading