PickNikRobotics / generate_parameter_library

Declarative ROS 2 Parameters
BSD 3-Clause "New" or "Revised" License
233 stars 44 forks source link

Register async callback for updates #6

Open tylerjw opened 2 years ago

tylerjw commented 2 years ago

Provide an interface for registering async callbacks for parameters being updated. Be careful that the mutex is not locked when these functions are called so if they call get() it doesn't create a deadlock.

ParameterListener::update()
{
  {
    std::lock_guard lock(mutex_);
    // modify params_ variable
  }
  std::lock_guard lock(update_callbacks_);
  for (auto const& f : update_callbacks_) f(params_);
}

class ParameterListener {
 public:
  void register_callback(std::function<void(Params const&)> cb) {
    std::lock_guard lock(update_callbacks_);
    update_callbacks_.push_back(cb);
  }
 private:
  std::mutex callbacks_mutex_;
  std::vecotr<std::function<void(Params const&)>> update_callbacks_;

Warn users that the lifetime of the callback function has to be at least as long as the lifetime of the ParameterListener object.

Fellfalla commented 1 year ago

I would love to see this being merged into the mainline.