Click here to Skip to main content
Click here to Skip to main content

WRL Collection Library Ported to Native C++

, 6 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

    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) { // process item
} 

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 both Windows 8 and Windows 8.1 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

License

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

Share

About the Author

Gregory Morse
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411028.1 | Last Updated 6 May 2014
Article Copyright 2014 by Gregory Morse
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid