I have a graph with an edge-class. I want edges to be copyable, but the problem is that the edge contains a std::map of std::unique_ptr of polymorphic content.
I have a base-class for edge-information. If an edge needs some extra-information, i can make a subclass of this information class and add it to the edge. This information is then stored in the map with the type of the information as key.
class Edge {
private:
std::size_t from;
std::size_t to;
std::map<std::type_index, std::unique_ptr<EdgeInformation>> info;
public:
Edge(std::size_t from, std::size_t to) : from(from), to(to) {
}
std::size_t start() const {
return from;
}
std::size_t end() const {
return to;
}
template <typename T, typename... Args>
void addInfo(Args&&... args) {
info[typeid(T)] = std::make_unique<T>(std::forward<Args>(args)...);
}
template <typename T>
bool hasInfo() {
return info.find(typeid(T)) != info.end();
}
template <typename T>
T getInfo() {
return info[typeid(T)];
}
template <typename T>
void removeInfo() {
info.erase(std::remove(info.begin(), info.end(), typeid(T)), info.end());
}
Edge flipped() const {
auto flipped_edge = Edge{to, from};
for(auto &entry : info) {
// add info to flipped edge here!
}
return flipped_edge;
}
};
At the bottom, you see the function flipped() which should return the edge with start and end reversed. My current problem is that i don't know how to copy the info-map of my edge.
My EdgeInformation is just a base-class without any fields or methods.
As @Jarod42 stated right, i just had to add a clone method to my EdgeInformation class. So my flipped() method can be now written as:
Edge flipped() const {
auto flipped_edge = Edge{to, from};
for(auto &entry : info) {
flipped_edge.info[entry.first] = entry.second->clone();
}
return flipped_edge;
}
Thank you alot.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With