locked
How to - Create a Map with Key/Value pairs containing List Values? (Visual C++ WinRT app)

    Question

  • I have a messaging system that needs to be efficient in delivering messages.  Messages have a source and a type, and listeners can either listen for generic messages of a particular type, or specific messages of a particular type and from a particular source.

    Basically, I am trying to implement this system and am trying to get this code to compile:

    MessageManager.h:

    // When a message is sent, we first check for subscriptions matching the message type.
    Platform::Collections::Map<Platform::String^, Platform::Collections::Vector<Gaia::Subscription^> >^ mSubscriptionsByMessageType;

    MessageManager.cpp:

    mSubscriptionsByMessageType = ref new Platform::Collections::Map<Platform::String^, Platform::Collections::Vector<Gaia::Subscription^> >();

    Error:

    Error 1 error C3225: generic type argument for 'V' cannot be 'Platform::Collections::Vector<T>', it must be a value type or a handle to a reference type c:\program files (x86)\microsoft visual studio 11.0\vc\include\collection.h 1016 1 Gaia

    The error is saying I can't have a container inside a container?  Or am I doing something wrong?  I need to have an efficient way to send messages to interested listeners, and I'm planning on doing this by have a map with key/value pairs of <string message type, list of listeners >.


    Land Of Simeria - http://www.stormsonggames.com/los

    • Moved by Jesse Jiang Tuesday, October 16, 2012 3:02 AM (From:Visual C++ General)
    Monday, October 15, 2012 11:08 PM

Answers

  • Hi,

    Any element to be stored in a Platform::Collections::Map must support less-than comparison with strict weak ordering, either implicitly or by using a custom stl::less comparator that you provide. Reference types support the comparison implicitly, as do scalar types. For non-scalar value types such as Windows::Foundation::DateTime, or for custom comparisons—for example, objA->UniqueID < objB->UniqueID—you must provide a custom comparator.

    From http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh700103.aspx

    I think the  Platform::Collections::Map does not support that, so the error throw. Maybe you can try the map in std, or just use one way with the index of a vector.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Jesse Jiang Monday, October 22, 2012 3:24 AM
    Tuesday, October 16, 2012 6:15 AM

All replies

  • Thinking about this more, I'd ideally want the following data structure:

    map< string, map< string, list< message listener > > >

    In other words:

    map< string MessageType, map < string MessageSource, list< MessageListener > > >

    When a message is broadcast, I can easily find the recipients using the structure above, provided the message type and source.

    If anybody can tell me how to define and initialize the data structure above, it would be most helpful.


    Land Of Simeria - http://www.stormsonggames.com/los

    Monday, October 15, 2012 11:13 PM
  • How can I make this work?

    .h:

    		// In order to efficiently find recipients for a given message, we need to be able to quickly access
    		// a list of recipients based off a given MessageType and MessageSource.  Generic Subscribers will have their MessageSource set to empty string. ("")
    		Platform::Collections::Map<									// Outter map M1.
    			Platform::String^,										// M1 Key: MessageType
    			Platform::Collections::Map<								// M1 Value: Inner map m2.
    				Platform::String^,									// M2 Key: MessageSource
    				Platform::Collections::Vector<Gaia::Subscription^>	// M2 Value: List of Subscriptions for a specific MessageType/MessageSource combination
    			>
    		>^ mSubscriptions;

    .cpp:

    		mSubscriptions = ref new Platform::Collections::Map<		// Outter map M1.
    			Platform::String^,										// M1 Key: MessageType
    			Platform::Collections::Map<								// M1 Value: Inner map m2.
    				Platform::String^,									// M2 Key: MessageSource
    				Platform::Collections::Vector<Gaia::Subscription^>	// M2 Value: List of Subscriptions for a specific MessageType/MessageSource combination
    			>
    		>();

    Errors:

    Error 1 error C3225: generic type argument for 'V' cannot be 'Platform::Collections::Map<K,V>', it must be a value type or a handle to a reference type c:\program files (x86)\microsoft visual studio 11.0\vc\include\collection.h 1016 1 Gaia
    Error 2 error C2955: 'Windows::Foundation::Collections::IObservableMap' : use of class generic requires generic argument list (MessageManager.cpp) c:\program files (x86)\microsoft visual studio 11.0\vc\include\collection.h 1016 1 Gaia


    Land Of Simeria - http://www.stormsonggames.com/los

    Monday, October 15, 2012 11:24 PM
  • Hi,

    I will move this thread to Building Metro style apps with C++ forum. This forum only talk about native codes.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, October 16, 2012 3:02 AM
  • Hi,

    Any element to be stored in a Platform::Collections::Map must support less-than comparison with strict weak ordering, either implicitly or by using a custom stl::less comparator that you provide. Reference types support the comparison implicitly, as do scalar types. For non-scalar value types such as Windows::Foundation::DateTime, or for custom comparisons—for example, objA->UniqueID < objB->UniqueID—you must provide a custom comparator.

    From http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh700103.aspx

    I think the  Platform::Collections::Map does not support that, so the error throw. Maybe you can try the map in std, or just use one way with the index of a vector.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Jesse Jiang Monday, October 22, 2012 3:24 AM
    Tuesday, October 16, 2012 6:15 AM