C++ 可以在类似STL的容器中使用WinRT对象吗?

C++ 可以在类似STL的容器中使用WinRT对象吗?,c++,windows,stl,windows-runtime,directx,C++,Windows,Stl,Windows Runtime,Directx,我正在尝试为D3D应用程序创建一个简单的手势识别器。手势识别器的工作原理是将接收到的每个点存储到容量为3的boost::circular_缓冲区中,然后计算缓冲区中类似FrameID的数量,如下所示: UINT Trackball::CalculateGestureSize(Windows::UI::Input::PointerPoint ^ pPoint) { // shift the circular buffer queue one if it's full (common cas

我正在尝试为D3D应用程序创建一个简单的手势识别器。手势识别器的工作原理是将接收到的每个点存储到容量为3的boost::circular_缓冲区中,然后计算缓冲区中类似FrameID的数量,如下所示:

UINT Trackball::CalculateGestureSize(Windows::UI::Input::PointerPoint ^ pPoint)
{
    // shift the circular buffer queue one if it's full (common case)
    if (m_pointQueue.full())
    {
        m_pointQueue.pop_back();
    }

    // then store our point
    m_pointQueue.push_front(*pPoint);

    // now we need to see how many of the points in the
    // circular buffer match the frame Id
    UINT gestureLength = 0;
    for (UINT i = 0; i < MAX_GESTURE_SIZE; i += 1)
    {
        if (m_pointQueue[i].FrameId == pPoint->FrameId)
        {
            gestureLength += 1;
        }
    }

    assert(gestureLength != 0);

    return gestureLength;
}
// So WinRT objects (like Windows::UI::Input::PointerPoint) can't
// be used in STL-like containers (like boost::circular_buffer)
// because * and & operators cannot be used on them, so I'm copying 
// the necessary info into this struct and using that instead
typedef struct 
{
    UINT FrameId;
    Windows::Foundation::Point Position;
} LocalPoint;

// a queue of size 3 that helps determine what kind of gesture we're working with
boost::circular_buffer<LocalPoint> m_pointQueue;
由于该错误的级联效应,编译器的错误列表很快就会变长

现在,我的解决方案是将PointerPoint的必要信息复制到结构中,并将其用作boost::circular_缓冲区的类型名,如下所示:

UINT Trackball::CalculateGestureSize(Windows::UI::Input::PointerPoint ^ pPoint)
{
    // shift the circular buffer queue one if it's full (common case)
    if (m_pointQueue.full())
    {
        m_pointQueue.pop_back();
    }

    // then store our point
    m_pointQueue.push_front(*pPoint);

    // now we need to see how many of the points in the
    // circular buffer match the frame Id
    UINT gestureLength = 0;
    for (UINT i = 0; i < MAX_GESTURE_SIZE; i += 1)
    {
        if (m_pointQueue[i].FrameId == pPoint->FrameId)
        {
            gestureLength += 1;
        }
    }

    assert(gestureLength != 0);

    return gestureLength;
}
// So WinRT objects (like Windows::UI::Input::PointerPoint) can't
// be used in STL-like containers (like boost::circular_buffer)
// because * and & operators cannot be used on them, so I'm copying 
// the necessary info into this struct and using that instead
typedef struct 
{
    UINT FrameId;
    Windows::Foundation::Point Position;
} LocalPoint;

// a queue of size 3 that helps determine what kind of gesture we're working with
boost::circular_buffer<LocalPoint> m_pointQueue;
//因此WinRT对象(如Windows::UI::Input::PointerPoint)无法
//用于类似STL的容器(如boost::circular\u buffer)
//因为*和&运算符不能用于它们,所以我正在复制
//将必要的信息导入此结构并使用它
类型定义结构
{
UINT-FrameId;
Windows::Foundation::点位置;
}本地点;
//大小为3的队列有助于确定我们正在处理的手势类型
boost::循环缓冲区m_pointQueue;
这确实有效,但我想知道是否有更好的解决方案


感谢您的阅读和帮助。

我想我不小心在LocalPoint结构中使用了Windows::Foundation::Point对象,找到了一个可行的解决方案。只需用一个struct包装WinRT对象,然后操作符就可以正常工作,但会增加一些语法噪音


不过,我仍在寻找更好的解决方案,但我将把它留在这里,直到那时。

如果要在STL集合中放置引用类型,需要使用^form。因此,您将使用:
boost::circular\u buffer
而不是
boost::circular\u buffer
。Windows::Foundation::Point是一种值类型,因此可以直接在集合中使用。

是否有理由尝试存储
点点点
,而不是
点^
,我的直觉是,我宁愿复制内存,也不愿保留对它的引用以解决生存期问题,但我认为,由于^s是一个ref计数的智能指针,如果我用^pointer在其上保留一个句柄,则存储的PointerPoint不应超出范围。对吗?我问的原因是PointerPoint显然是一个托管对象,即其存储由CLR管理。试图通过Boost来管理它是根本原因。啊,你发帖的时候我在编辑我的评论,但结果证明这无关紧要。在这种情况下,由于我不控制PointerPoint的生存期,所以将内存复制到我自己的本地缓冲区是正确的解决方案。对吗?如果保留
指针点^
,则可以控制对象的生存期。所以我不确定你的结论。是的,就是这样。谢谢你的帮助!