Impulse Graphics Library Polymorphic Properties

So, I am using a boost file of the following type:

typedef boost::adjacency_list<boost::listS, boost::vecS, boost:directedS, VertexT, EdgeT> GraphT 

VertexT and EdgeT are both classes for storing many of the properties I need. These are related properties. I'm not sure that some of the ways I want to use bgl are possible, so if you are familiar with them, help would be greatly appreciated.

VertexT and EdgeT are supposedly polymorphic base classes. I understand that bgl is not intended for pointers to these properties. How to work with polymorphic properties of vertex and edge with BGL? I was thinking about using shared pointers, but I would rather manage the memory myself. Additionally, this seems to prevent the problem when using boost :: get to create a location map to speed up layouts.

Right now, I hacked my way around this, just having a tip containing another pointer to a true polymorphic class. But that seems too complicated. Any suggestions?

+2
c ++ generics polymorphism boost boost-graph
source share
2 answers

In general implementations of the algorithms, it is preferable to use the semantics of the value: copying an object leads to the existence of two identical objects that are independent. This is a critical property when you need to duplicate objects. Dynamic polymorphism does not immediately work with the semantics of values, because to use dynamic polymorphism you need to deal with pointers or references: when using values, the static type and dynamic type of the object are the same, which does not allow dynamic polymorphism directly.

The only way to deal with dynamically polymorphic objects in this case is to give them an idea of ​​value. Effectively, this means that you need to encapsulate pointers to objects in an object that exposes the interface of the desired value (you can also encapsulate links if you insist, but I never found that this works well). The Boost Graph library does not really care about how various structures are represented internally if they have the necessary interface and implement the required semantics. From what you describe with a wrapper for pointers to polymorphic objects, is the right way. Regardless of whether you support the object through one of the standard smart pointers or in different ways, it does not matter, although I would suggest that using something like boost::shared_ptr<T> or std::shared_ptr<T> eliminates a number of unnecessary complications.

All this said, I would like to point out that I rarely come across useful examples of dynamically polymorphic objects in combination with algorithms! Yes, in most cases, using dynamic polymorphism helps solve the problem, not the solution (despite the fact that many people with only object-oriented techniques say, however, if the only tool you know is a hammer, each problem looks like nail).

+7
source share

I think that you basically solve the same problem as this question:

  • C ++ and the general graph construction algorithm

only for (related) properties. I suggest you make polymorphism by invoking free function templates.


For real powerful equipment:

See also this document: On the stretch between object-oriented and general C ++ programming ; this article describes erasing styles, which is a kind of all-all-all solution to unleash / minimize the need for runtime / static polymorphism. (_ Please note that libraries such as Boost Variant, Boost Any are much more convenient if you need to implement style erasure).

0
source share

All Articles