New Features in C++/CX's <collection.h> for VS 2013 RTM

Brandon Jacobs

Introduction:

Hi, I’m Brandon Jacobs, an intern on the Visual C++ Libraries team. For part of my internship, I was tasked with adding new features to Stephan T. Lavavej’s <collection.h>. It was certainly an honor to be one of the few to contribute to <collection.h>. You can find these changes in VS 2013 RTM (these changes are not in 2013 Preview).

 

Summary:

These are the changes I’ve made to <collection.h>:

1.            Added UnorderedMap and UnorderedMapView wrapper classes to <collection.h>.

2.            Added initializer_list constructors to Vector, VectorView, Map, MapView,  UnorderedMap, and UnorderedMapView.

3.            Added validation so that customers use valid WinRT types to instantiate the collections in <collection.h>.

 

Features:

UnorderedMap and UnorderedMapView:

This is the wrapper class for the std::unordered_map class. The functionality is the same as Map and MapView. The only difference is the underlying data structure, which is a hash table instead of a balanced binary tree. So, types must be hashable and must be able to show equality. Just like Map and MapView defaults to std::less,  UnorderedMap and UnorderedMapView defaults to std::hash and std::equal_to. Both the hash and equality predicates are template parameters, so you are allowed to change the actions of UnorderedMap and UnorderedMapView by providing your own predicates.

 

initializer_list constructors:

You can now construct any data structure using the C++11 initializer_list constructors.

An example:

namespace WFC = Windows::Foundation::Collections;

namespace PC = Platform::Collections;

WFC::IVector<int>^ v = ref new PC::Vector<int>{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

WFC::IMap<int, int>^ m

= ref new PC::Map<int, int>{ { 1, 10 }, { 2, 20 }, { 3, 30 }, { 4, 40 } };

WFC::IMap<int, int>^ m2

= ref new PC::UnorderedMap<int, int>{ { 1, 10 }, { 2, 20 }, { 3, 30 }, { 4, 40 } };

 

Valid WinRT types only:

We now check whether the type you want to store in your given data structure is a valid WinRT type. Currently if you have:

PC::Vector<ordinary_cpp_class>

You will get an odd compiler error. Now we check whether the types passed in are valid WinRT types. If this check fails, you will now get a much nicer compiler error that even includes the line in which you tried to create a collection with an invalid type.

Only the items that will appear in the Windows::Foundation::Collections interfaces need to be valid WinRT types. Predicates such as std::less, std::hash, etc. are not passed into the Windows::Foundation::Collections interfaces, so they are not affected by that restriction.

Valid WinRT types are:

  1. integers
  2. interface class ^
  3. public ref class^
  4. value struct
  5. public enum class

 

Thank you for taking the time and reading this post,

Brandon Jacobs

SDE Intern – Visual C++ Libraries

0 comments

Discussion is closed.

Feedback usabilla icon