Homepage GitHub

Unsubscribe messages in lib


#1

It is clear how to subscribe to multicast messages. But is it possible to unsubscribe ? I have a scenario when nodes are coming and going to/from the bus and subscriptions are done during runtime. Of course I can reuse the subscriptions if the same node comes back, but it is not as cool :wink:.

Is it somehow possible with the generalsubscriber class?

I think the same question relates to registered services as well…

Kind regards


(Pavel Kirienko) #2

Just destroy the subscriber object. Same applies to services.


#3

Ok! Thanks.

Follow up question:
I am now deriving uavcan::Subscriber i my own class (MyClass). I.e.

class MyClass: public uavcan::Subscriber<MyDataType>
{
  public: 
  MyClass(NodePtr& node) : uavcan::Subscriber<MyDataType>(*node)
  {
    ...
    ...
  }
}

So far no problems. But since my system dynamically detects nodes, I have a

std::vector<MyClass>

that is collects all detected nodes. But this creates problems on the subscribers. I get Noncopyable errors etc…
I am probably doing this the wrong way…

Do you have a suggestion on how to store the classes in a proper way without using pointers?


(Pavel Kirienko) #4

Do you have a suggestion on how to store the classes in a proper way without using pointers?

As the name suggests, noncopyable objects can’t be copied. The implication is that you can’t use them with containers that implicitly relocate their storage, such as std::vector<>.

You can either find some container that does not move its items around (some form of non-contiguous containers such as linked lists or queues should work; however, I don’t think such containers exist in the STL), or you can add an extra level of indirection to create movable references to immovable (noncopyable) objects, such as std::shared_ptr<> (or some other kind of pointers).

If you’re keen to stick with vectors, use std::vector<std::shared_ptr<MyClass>>.