I have a pre-existing native C++ library I am trying to expose to UWP applications by projecting the API through a C++/WinRT Runtime Component. I am having difficulty understanding how to expose templated functions.
For the following example C++ class:
struct ExampleClass
{
ExampleClass() = default;
template <typename T>
std::vector<T> create_a_vector();
};
This example is obviously silly; in reality the templated factory is creating another library-specific type (not std::vector
), and there are reasons why it's a member of ExampleClass.
I'm hoping to expose a subset of specializations of create_a_vector
by specifically declaring and implementing them in my IDL and Runtime Component. For example, the following IDL would be ideal:
namespace ExampleClassWinRT
{
[default_interface]
runtimeclass ExampleClass
{
ExampleClass();
IVector<String> CreateAVector<String>();
IVector<Int32> CreateAVector<Int32>();
IVector<UInt8> CreateAVector<UInt8>();
IVector<ExampleClassWinRT.SomeOtherType> CreateAVector<ExampleClassWinRT.SomeOtherType>();
// .. and so on
}
}
And then in the implementation (in C++/WinRT) of each of the versions of CreateAVector
I would take care of converting between WinRT and C++ types as needed and invoking the correct specialization of create_a_vector
.
Then, consuming C# code might look something like this:
using ExampleClassWinRT;
namespace ExampleApp
{
public sealed partial class MainPage : Page
{
public MyFunc()
{
var c = ExampleClassWinRT.ExampleClass();
var v1 = c.CreateAVector<String>();
var v2 = c.CreateAVector<Int32>();
var v3 = c.CreateAVector<ExampleClassWinRT.SomeOtherType>();
}
}
}
It seems like the syntax used in the goal C# example code is legal, and in fact many WinRT APIs appear templated (IVector<T>
for example). However my MIDL syntax above is not valid, and I'm not finding any documentation on how I might create a templated WinRT API surface through MIDL and/or WinRT/C++.
Is this approach for projecting a native C++ library into WinRT even feasible? Am I barking down the wrong tree?