I have a boost graph with custom properties. I want to make a copy of it. I tried it by following way but got many compilation error.
Here is what I did :
using BGType = boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS,
// Vertex Properties...
vertexProps,
// Edge Propereties...
edgeProps,
// Graph Properties
graphProps>;
vertexProps.h
class vertexProps {
public:
explicit vertexProps(const std::string *moduleName = nullptr, const std::string *name = nullptr,
long refPtr = 0 )
: _refPtr(refPtr),
{
_moduleName = moduleName ? *moduleName : "";
_name = name ? *name : "";
};
struct CustomVertexCopy {
void operator()(const vertexProps& source_vertex, vertexProps& target_vertex) const {
target_vertex._refPtr = source_vertex._refPtr;
target_vertex._moduleName = source_vertex._moduleName;
target_vertex._name = source_vertex._name;
}
edgeProps.h
class edgeProps {
public:
explicit edgeProps(std::string name = "")
: _name(name){};
std::string _name;
};
struct CustomEdgeCopy {
void operator()(const edgeProps& source_edge, edgeProps& target_edge) const {
target_edge._name = source_edge._name;
}
};
someFunction.cpp
OnClick(BGType* bGraph)
{
// some code
BGType* oldBg = new BGType;
boost::copy_graph(bGraph, oldBg, boost::vertex_copy(CustomVertexCopy()));
boost::copy_graph(bGraph, oldBg, boost::edge_copy(CustomEdgeCopy()));
// some code
}
Where am I wrong ?
I have a one more doubt.
Will such deep copying impact on performance if grpah is big ?
If yes, is there any way to avoid it ?
@sehe : I tried your answer, but I was getting compilation error. So I tried to change code little bit and now there is no compilation error. But please look into my changes and suggest me whther changes are right or wrong.
OnClick(BGType* bGraph)
{
// some code
BGType* oldBg = new BGType;
boost::copy_graph(*bGraph, *oldBg,
boost::vertex_copy(CustomVertexCopy{*bGraph,*oldBg})
.edge_copy(CustomEdgeCopy{*bGraph, *oldBg}));
// some code
}
@sehe: I have graph properties also along with vertex and edge property. How to copy that properties also ?
class graphProps {
public:
explicit graphProps(std::string *name = nullptr) { _name = name ? *name : ""; };
std::string _name;
std::map<std::string, std::tuple<std::vector<schPinInfo *>, // input Pins
std::vector<schPinInfo *>, // inout pins
std::vector<schPinInfo *>> // output pins
>
_modInfo;
std::map<std::string, std::vector<std::string>> _altNames;
std::map<std::string, schSymbol> _modSymbol;
}
Named parameters are chained so you can pass arbitrary number of arguments in one place:
Note that
.edge_copyis chained on thevertex_copy()named parameter object.Then, still things wouldn't compiler because the custom copiers should take descriptors, not bundle references:
Now it will all work:
Live On Coliru
Prints
BONUS
Simplify! Just make the properties copyable, and you're done, with 20 fewer lines of code, which is roughly 30% less room for error/pessimizations:
Live On Coliru
Still printing