Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 82 additions & 53 deletions cppwinrt/component_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,7 @@ namespace cppwinrt
return;
}

if (settings.component_opt)
{
auto format = R"(void* winrt_make_%();
)";

w.write(format, get_impl_name(type.TypeNamespace(), type.TypeName()));
}
else
if (!settings.component_opt)
{
auto format = R"(#include "%.h"
)";
Expand All @@ -92,7 +85,7 @@ namespace cppwinrt
}
}

static void write_component_activation(writer& w, TypeDef const& type)
static void write_component_make_definition(writer& w, TypeDef const& type)
{
if (!has_factory_members(w, type) || is_always_disabled(type))
{
Expand All @@ -103,35 +96,49 @@ namespace cppwinrt
auto type_namespace = type.TypeNamespace();
auto impl_name = get_impl_name(type_namespace, type_name);

if (settings.component_opt)
{
auto format = R"(
if (requal(name, L"%.%"))
{
return winrt_make_%();
}
auto format = R"(void* winrt_make_%()
{
return winrt::detach_abi(winrt::make<winrt::@::factory_implementation::%>());
}
)";

w.write(format,
type_namespace,
type_name,
impl_name);
w.write(format,
impl_name,
type_namespace,
type_name);
}

static void write_component_activation(writer& w, std::vector<TypeDef> const& classes)
{
if (!settings.component_opt)
{
for (auto&& type : classes)
{
write_component_make_definition(w, type);
}
}
else
{
auto format = R"(
if (requal(name, L"%.%"))
{
return winrt::detach_abi(winrt::make<winrt::@::factory_implementation::%>());
}
auto declarations = R"(
#define WINRT_DECLARE_FUNC(func, name) void* func();
WINRT_ACTIVATION_TABLE(WINRT_DECLARE_FUNC)
)";
w.write(declarations);
}
}

w.write(format,
type_namespace,
type_name,
type_namespace,
type_name);
static void collect_component(writer& w, TypeDef const& type, std::vector<std::string>& components)
{
if (!has_factory_members(w, type) || is_always_disabled(type))
{
return;
}

std::string name;
name += type.TypeNamespace();
name += '.';
name += type.TypeName();
components.push_back(std::move(name));
}

static void write_module_g_cpp(writer& w, std::vector<TypeDef> const& classes)
Expand All @@ -140,7 +147,31 @@ namespace cppwinrt
{
w.write_root_include("base");
}
auto format = R"(%

std::vector<std::string> components;
for (auto&& type : classes)
{
write_component_include(w, type);
collect_component(w, type, components);
}

// Sort the strings so that binary search can be used.
std::sort(components.begin(), components.end());
w.write("#define WINRT_ACTIVATION_TABLE(X)\\\n");

for (std::size_t i = 0; i + 1 < components.size(); ++i)
{
w.write(std::string{ " X(winrt_make_" } + get_impl_name(components[i]) + ", L\"" + components[i] + "\") \\\n");
}

if (!components.empty())
{
w.write(std::string{ " X(winrt_make_" } + get_impl_name(components.back()) + ", L\"" + components.back() + "\")\n");
}

write_component_activation(w, classes);

auto format = R"(
bool __stdcall %_can_unload_now() noexcept
{
if (winrt::get_module_lock())
Expand All @@ -154,20 +185,31 @@ bool __stdcall %_can_unload_now() noexcept

void* __stdcall %_get_activation_factory([[maybe_unused]] std::wstring_view const& name)
{
auto requal = [](std::wstring_view const& left, std::wstring_view const& right) noexcept
#if %
static constexpr std::wstring_view names[] =
{
#define WINRT_NAME_LIST(func, name) name,
WINRT_ACTIVATION_TABLE(WINRT_NAME_LIST)
};

using make_t = void* (*)();
static constexpr make_t makes[] =
{
return std::equal(left.rbegin(), left.rend(), right.rbegin(), right.rend());
#define WINRT_MAKE_LIST(func, name) func,
WINRT_ACTIVATION_TABLE(WINRT_MAKE_LIST)
};
%

auto it = std::lower_bound(std::begin(names), std::end(names), name);
if (it != std::end(names) && *it == name)
{
return makes[it - std::begin(names)]();
}
#endif
return nullptr;
}
)";

w.write(format,
bind_each<write_component_include>(classes),
settings.component_lib,
settings.component_lib,
bind_each<write_component_activation>(classes));
w.write(format, settings.component_lib, settings.component_lib, components.empty() ? 0 : 1);

if (settings.component_lib != "winrt")
{
Expand Down Expand Up @@ -379,21 +421,8 @@ catch (...) { return winrt::to_hresult(); }
{
auto type_name = type.TypeName();
auto type_namespace = type.TypeNamespace();
auto impl_name = get_impl_name(type_namespace, type_name);

if (has_factory_members(w, type))
{
auto format = R"(void* winrt_make_%()
{
return winrt::detach_abi(winrt::make<winrt::@::factory_implementation::%>());
}
)";

w.write(format,
impl_name,
type_namespace,
type_name);
}
write_component_make_definition(w, type);

if (!settings.component_opt)
{
Expand Down