Introduction
Microsoft includes the entire source for the collection wrappers in collection.h, yet they provide the WRL wrappers in the conditional compilation only to support C++/CX. If you want to wrap the existing STL vector
s in objects implementing WRL interfaces or wrap WRL vectors in STL vector
s, then you need to roll your own library or port Microsoft's like I have done. This can be done by translating all the C++/CX code to C++.
Background
One should be familiar with C++, IDL, basic WRL, the Windows Runtime and C++/CX to be able to manage an entire project in native C++ and be aware of the significant overhead required with the main savings being in size from not having to link with the C++/CX platform wrapper library.
Using the Code
IDL may need to be added to provide custom template specializations while being sure to include the generated header file where you will use the specialization:
declare
{
interface Windows.Foundation.Collections.IVector<int>;
}
The usage is identical to C++/CX except adding the ABI
namespace and the usual porting of the Platform
namespace objects such as Platform::Object^
to IInspectable*
or Platform::String^
to HSTRING
. For example, using the hidden VectorIterator
is as simple as taking an IVector
returned from WRL and doing range enumeration as the begin and end functions are already defined to utilize it:
ABI::Windows::Foundation::Collections::IVector<IInspectable*> vector;
for (auto item : vector) { }
Using the Vector
class which automatically casts to IVector
is similarly easy though only native WinRT types are supported:
Collections_winrt::Vector<HSTRING> vector;
vector.Append(Microsoft::WRL::Wrappers::HStringReference(L"Example").Get();
Points of Interest
Although this port was relatively straight-forward, caution should be used as the overhead of using WRL objects is greater than that of the native STL objects. Only use these utilities when interacting with WRL code or objects. Microsoft has put in support for the bindable interfaces too which although can be disabled also required a series of wrapper classes to avoid multiple inheritance conflicts.
The Agile
wrapper is translated to WRL to provide proper compatibility for this library.
The library here is contained in the namespace Collections_winrt
as opposed to Platform::Collection
.
This port supports Visual Studio 2012/2013/2015/2017 and Windows 8/8.1/10.0.10240.0-10.0.16299.0 in one library and relatively few changes were made between the versions mostly adding a couple map classes and the use of the new STL initializer_list
in constructors.
History
- 3rd May, 2014: Initial version
- 9th December, 2017: Updated for VS2017