Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

call boost program option notifiers before plugin initialize #9207

Merged
merged 2 commits into from
Jun 19, 2020

Conversation

spoonincode
Copy link
Contributor

@spoonincode spoonincode commented Jun 14, 2020

Change Description

This PR changes boost program option notifiers to fire before plugin initialization instead of after initialization but before startup. Current behavior has bitten me an embarrassing number of times and I’m not immediately sure of any drawback moving the notifiers earlier given our typical plugin pattern.

Using notifiers, IMO, cleans up option handling. For example, without using the notifiers our typical pattern is something like

struct my_plugin_impl {
   std::string server_address;
}

void my_plugin::set_program_options(options_description&, options_description& cfg) {
   cfg.add_options()("https-server-address", bpo::value<string>(), "set the address");
}

void my_plugin::plugin_initialize(const variables_map& options) {
   if( options.count( "https-server-address" )
     my->server_address = options.at("https-server-address").as<string>();
   if(my->server_address.size())
      dothedew();
   //...

But using the notifiers it can be more like

struct my_plugin_impl {
   std::string server_address;
}

void my_plugin::set_program_options(options_description&, options_description& cfg) {
   cfg.add_options()("https-server-address", bpo::value<string>()->notifier([this](const string& a) {
      my->server_address = a;
   }), "set the address");
}

void my_plugin::plugin_initialize(const variables_map& options) {
   if(my->server_address.size())
      dothedew();
   //...

In particular notice that the https-server-address string doesn't have to be copy pastaed around, and that there is no need for the options.at("https-server-address").as<string>(); mouthful.

But the code above won't work today because the notifiers are fired after plugin_initialize() (before startup()). This PR moves the notifiers to occur before plugin_initialize() allowing plugin_initialize() to make use of values set in notifiers.

Change Type

Select ONE

  • Documentation
  • Stability bug fix
  • Other
  • Other - special case

Consensus Changes

  • Consensus Changes

API Changes

  • API Changes

Documentation Additions

  • Documentation Additions

Copy link
Contributor

@heifner heifner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@kj4ezj kj4ezj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me

Copy link
Contributor

@johndebord johndebord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Copy link
Contributor

@revl revl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

@spoonincode spoonincode merged commit b32e377 into develop Jun 19, 2020
@spoonincode spoonincode deleted the notify_early branch June 19, 2020 20:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants