How do I make a deep copy of a vector in C++/WinRT?

221 views Asked by At

I have a private IObservableVector of UIElements implemented through a single_threaded_observable_vector in my C++/WinRT WinUI3 project. Let's call it m_precious_vector. I have a getter function of PreciousVector that I want to return a deep copy of m_precious_vector from so no client could modify m_precious_vector without going through my separate setter function, but I can't seem to figure out how to create anything other than a shallow copy.

Please leave a comment if I should be leaving any more info :)

Edit: More info as promised, thank you commenters!

I first tried to see if the getter would return by reference or value. I was able to edit the vector itself from the getter, so I assumed it was a reference (I have a code snippet of what I did there further down the question). I tried making a deep copy by doing below in ProjContent::PreciousVector():

# ProjContent.h
# Some header files... 

namespace winrt::Proj::implementation
{
    struct ProjContent : ProjContentT<ProjContent>
    {
    public:
        # ... other code ...

        winrt::Windows::Foundation::Collections::IObservableVector<Microsoft::UI::Xaml::Controls::NavigationViewItem> PreciousVector();
    
    private:
        # ... other code ...

        winrt::Windows::Foundation::Collections::IObservableVector<Microsoft::UI::Xaml::Controls::NavigationViewItem> m_preciousVector;
    };
}


# ProjContent.cpp
# some header files...

namespace muxc
{
    using namespace winrt::Microsoft::UI::Xaml::Controls;
};

# ... more other code ...

winrt::Windows::Foundation::Collections::IObservableVector<muxc::NavigationViewItem> ProjContent::PreciousVector()
{
    return winrt::Windows::Foundation::Collections::IObservableVector<muxc::NavigationViewItem> {m_preciousVector};
}

The naming is not the best, thank you for bearing with me.

I believed this would create a shallow copy because I tested using this getter from another class that is in the same namespace and edited the vector from the returned value directly and observed the size change from another call of PreciousVector().Size().

# ProjViewModel.h
# Some header files... 

namespace winrt::Proj::implementation
{
    struct ProjViewModel : ProjViewModelT<ProjViewModel>
    {
    public:
        # ... other code ...

    private:
        # ... other code ...
        Proj::ProjContent m_projContent{ nullptr };
    };
}


# ProjViewModel.cpp
# Some header files
using namespace winrt;

namespace muxc
{
    using namespace winrt::Microsoft::UI::Xaml::Controls;
};

namespace winrt::Proj::implementation
{
    # ... other code ...

    winrt::Windows::Foundation::IAsyncAction ProjViewModel::AddContent()
    {
        co_await winrt::resume_foreground(m_dispatcher);  # m_dispatcher is tied to the UI thread
        # using the debugger, size is 0 here.
        int size = m_proj_content.PreciousVector().Size();
        muxc::WebView2 worldWV;
        m_proj_content.PreciousVector().Append(worldWV);
        # using the debugger, size is 1 here.
        size = m_proj_content.PreciousVector().Size();
    }

I wanted to try the "new" keyword but that is now deprecated and it would not let me.

0

There are 0 answers