Boost图形库多形态捆绑属性 [英] Boost Graph Library Polymorphic Bundled Properties

查看:134
本文介绍了Boost图形库多形态捆绑属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用了以下类型的提升图:

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

VertexT和EdgeT都是保存我需要的许多属性的类。这些是捆绑属性。我不知道如果一些我想使用bgl的方式是可能的,所以如果你熟悉他们的帮助将非常感谢。



VertexT和EdgeT假设是多态基类。我的理解是bgl不是用于指向这些属性的指针。如何用BGL处理多边形顶点和边缘属性?我想到使用共享指针,但我宁愿自己管理内存。另外,这似乎防止了一个问题,当使用boost :: get生成位置图的升压布局。



现在我已经黑客我的方式顶点包含另一个指向真正多态类的指针。但这似乎太复杂。任何建议?

解决方案

在算法的通用实现中,首选使用值语义:复制对象会导致两个相同的对象存在是独立的。当需要复制对象时,这是重要的属性。动态多态不会立即与值语义一起使用,因为对于使用动态多态性,您需要处理指针或引用:当使用值时,对象的静态类型和动态类型一致,不允许直接动态多态性。 / p>

在这种情况下处理动态多态对象的唯一方法是给它们一个值的外观和感觉。实际上,这意味着你需要将指向你的对象的指针封装到暴露所需的值接口的对象中(如果你坚持的话,你也可以封装引用,但是我从来没有发现它的效果很好)。 Boost图形库并不真正关心各种结构如何在内部表示,只要它们具有所需的接口并实现所需的语义即可。从你所描述的使用指针到多态对象的包装器是正确的方法。无论你是通过一个标准的智能指针或不同的方式保持对象不重要,虽然我猜想使用 boost :: shared_ptr< T> std :: shared_ptr< T> 删除了一些不必要的并发症。



指出我很少找到动态多边形对象结合算法的有用的例子!是的,有一些,但大多数时候使用动态多态性是有助于问题,而不是解决方案(尽管许多人只有暴露面向对象技术说;但是,如果你知道唯一的工具是一个锤子,每个问题看起来像钉子)。


So I'm using a boost graph of the following type:

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

VertexT and EdgeT are both classes to keep many of the properties I need. These are bundled properties. I'm not sure if some of the ways I want to use bgl are possible so if you are familiar with them help would be much appreciated.

VertexT and EdgeT are suppose to be polymorphic base classes. My understanding is bgl is not meant to use for pointers to these properties. How does one work with polymorphic vertex and edge properties with BGL? I thought of using shared pointers but I'd prefer to manage the memory myself. Additionally this seems to prevent a problem when using boost::get to generate the position map for boost layouts.

Right now I've hacked my way around this by just having vertex contain another pointer to the true polymorphic class. But that seems too complex. Any suggestions?

解决方案

In generic implementations of algorithms it is preferred to use value semantics: copying an object causes two identical objects to exist which are independent. This is crucial property when it is necessary to duplicate objects. Dynamic polymorphism doesn't immediately work with value semantics because for the use of dynamic polymorphism you need to deal with pointers or reference: when using values, the static type and the dynamic type of object coincide which doesn't allow dynamic polymorphism directly.

The only way to deal with dynamically polymorph objects in this case is to give them a value look and feel. Effectively, this means that you need to encapsulate the pointers to your objects into object which expose the required value interface (you can also encapsulate references if you insist but I never found that this works well). The Boost Graph library doesn't really care about how the various structures are internally represented as long as they have the required interface and implement the required semantics. From what you describe using a wrapper for your pointers to the polymorphic objects is the right way to go. Whether you maintain the object via one of the standard smart pointers or differently doesn't really matter although I'd guess that using something like boost::shared_ptr<T> or std::shared_ptr<T> removes a number of unnecessary complications.

All this said, I'd like to point out that I rarely find useful examples of dynamically polymorph objects combined with algorithms! Yes, there are some but most of the time use of dynamic polymorphism is contributing to the problem, not to the solution (despite what many people having only exposure to object-oriented technique say; however, if the only tool you know is a hammer, every problem looks like a nail).

这篇关于Boost图形库多形态捆绑属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆