Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

WRL Collection Library Ported to Native C++

4.60/5 (5 votes)
6 Dec 2015CPOL2 min read 20.9K   246  
WRL Collection library ported to native C++

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 vectors in objects implementing WRL interfaces or wrap WRL vectors in STL vectors, 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:

C++
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:

C++
ABI::Windows::Foundation::Collections::IVector<IInspectable*> vector;
for (auto item : vector) { // process item
} 

Using the Vector class which automatically casts to IVector is similarly easy though only native WinRT types are supported:

C++
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)