Click here to Skip to main content
15,885,899 members
Articles / Programming Languages / C++
Tip/Trick

WRL Collection Library Ported to Native C++

Rate me:
Please Sign up or sign in to vote.
4.60/5 (5 votes)
6 Dec 2015CPOL2 min read 20.3K   244   10   2
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)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDoesn't build in VS2017 Pin
Member 162182613-Sep-17 11:55
Member 162182613-Sep-17 11:55 
AnswerRe: Doesn't build in VS2017 Pin
Gregory Morse9-Dec-17 4:02
Gregory Morse9-Dec-17 4:02 
I had not yet investigated it for VS2017 (usually merely by a diff tool and testing the sample project), but I will do so and update the article.

I had held off long on this task as it is inappropriate for open source libraries given that it is copyrighted code. I had decided the correct approach was to make a diff patching type tool which could transform any version of the files in question to the native C++ version. It would reduce conditional compilation and be really elegant and applicable to any project then. But the time involved is quite great especially maintaining such a large version base now. As for VS2017 it is certainly worth integrating at least the latest by now with the 2017.5 release.

It is now updated and working for VS2017 - needed line 2700 of collection_winrt.h to use the new internal hash name e.g.

#if _MSC_VER >= 1910
return ::std::_Hash_array_representation(reinterpret_cast<const unsigned="" char="" *="">(str), uLen * sizeof(wchar_t));
#else
return ::std::_Hash_seq(reinterpret_cast<const unsigned="" char="" *="">(str), uLen * sizeof(wchar_t));
#endif

modified 9-Dec-17 14:31pm.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.