繁体   English   中英

C++:深拷贝菱形指针结构

[英]C++: Deep Copy Diamond Pointer Structure

在我的模拟软件中,我使用 pybind11 生成对象。 因此,所有对象都存储在 std::shared_ptr 中,编译时结构未知。 对于我的模拟的并行化,我需要使用不同的种子运行相同的配置。 我想在 C++ 端的一次调用中实现这些对象的复制。

按照一个最小的例子,我希望a2a的深拷贝,具有菱形结构。

// Type your code here, or load an example.
#include <memory>
#include <map>
#include <iostream>

class C{};
class B{
    public:
    B(std::shared_ptr<C> c):c(c){}
    std::shared_ptr<C> c;
};
class A{
    public:
    A(std::shared_ptr<B> b1, std::shared_ptr<B> b2):b1(b1), b2(b2){}
    std::shared_ptr<B> b1;
    std::shared_ptr<B> b2;
};

auto init(){
    auto c = std::make_shared<C>();
    auto b1 = std::make_shared<B>(c);
    auto b2 = std::make_shared<B>(c);
    auto a = std::make_shared<A>(b1,b2);
    return a;
}


int main(){
    auto a = init();
    auto a2 = a; //deepcopy of a, where b1 and b2 of the copy point to the same object C
}

我想出的唯一解决方案是传递一个 map<pointer,shared_ptr>。 如果 shared_ptr 已经被深度复制,这允许查找。 (这里我的打字有一些问题,因为我需要动态地回退类型。这感觉真的很难看,而且很容易出错。)

不确定我是否理解问题,但似乎您只需要A的复制构造函数:

A(const A& other){
    if (other.b1.get() == other.b2.get()) {
        b1 = std::make_shared<B>(std::make_shared<C>(*other.b1->c.get()));
        b2 = b1;
    } else {
        b1 = std::make_shared<B>(std::make_shared<C>(*other.b1->c.get()));
        b2 = std::make_shared<B>(std::make_shared<C>(*other.b2->c.get()));
    }    
}

请注意,这只检查other.b1other.b2是否是相同的对象。 当它们是不同的对象但指向同一个C时,它将被复制两次。 如果要检查other.b1->cother.b2->c是否相同,则需要相应地修改代码。

您可以使用std::shared_ptr<void>对所有共享指针进行类型擦除,使用std::static_pointer_cast您的实际类型。

using Seen = std::set<std::shared_ptr<void>>;

template <typename T>
std::shared_ptr<T> deep_copy(std::shared_ptr<T> source, Seen & seen) {
    if (auto it = seen.find(std::static_pointer_cast<void>(source)); it != seen.end()) {
        return std::static_pointer_cast<T>(*it);
    }
    auto dest = make(*source, seen);
    seen.insert(std::static_pointer_cast<void>(dest));
    return dest;
}

然后,您可以编写采用现有实例和seen映射的构造函数来深度复制成员,允许它们是私有的。

template <typename T>
std::shared_ptr<T> make(const T & source, Seen & seen) {
    return std::make_shared<T>(source, seen);
}    

class C{
    public:
    C(){}
    C(const C &, Seen &){}    
};    
class B{
    std::shared_ptr<C> c;
    public:
    B(std::shared_ptr<C> c):c(c){}
    B(const B & other, Seen & seen):c(deep_copy(other.c, seen)){}
};
class A{
    std::shared_ptr<B> b1;
    std::shared_ptr<B> b2;
    public:
    A(std::shared_ptr<B> b1, std::shared_ptr<B> b2):b1(b1), b2(b2){}
    A(const A & other, Seen & seen):b1(deep_copy(other.b1, seen)), b2(deep_copy(other.b2, seen)){}
};

int main(){
    auto a = init();
    Seen a2_seen;
    auto a2 = deep_copy(a, a2_seen);
}

或者,您可以为每种类型重载make ,如果成员是私有的,则make<T>需要被T加为好友。

std::shared_ptr<C> make(const C &, Seen &) {
    return std::make_shared<C>();
}

std::shared_ptr<B> make(const B & other, Seen & seen) {
    auto c = deep_copy(other.c, seen);
    return std::make_shared<B>(c);
}

std::shared_ptr<A> make(const A & other, Seen & seen) {
    auto b1 = deep_copy(other.b1, seen);
    auto b2 = deep_copy(other.b2, seen);
    return std::make_shared<A>(b1, b2);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM