Hyp-ed/hyped-2022

Modernise STM to make use of const, references, optionals and unique pointers

Opened this issue · 0 comments

For example the current version

State *Idle::checkTransition(Logger &log)
{
  updateModuleData();

  bool emergency = checkEmergency(log, embrakes_data_, nav_data_, batteries_data_, telemetry_data_,
                                  sensors_data_, motors_data_);
  if (emergency) { return FailureStopped::getInstance(); }

  bool calibrate_command = checkCalibrateCommand(log, telemetry_data_);
  if (!calibrate_command) { return nullptr; }

  bool all_initialised = checkModulesInitialised(log, embrakes_data_, nav_data_, batteries_data_,
                                                 telemetry_data_, sensors_data_, motors_data_);
  if (all_initialised) { return Calibrating::getInstance(); }

  return nullptr;
}

should be rewritten to

const State *Idle::checkTransition(Logger &log)
{
  updateModuleData();

  const bool emergency = checkEmergency(log, embrakes_data_, nav_data_, batteries_data_, telemetry_data_,
                                  sensors_data_, motors_data_);
  if (emergency) { return FailureStopped::getInstance(); }

  const bool calibrate_command = checkCalibrateCommand(log, telemetry_data_);
  if (!calibrate_command) { return nullptr; }

  const bool all_initialised = checkModulesInitialised(log, embrakes_data_, nav_data_, batteries_data_,
                                                 telemetry_data_, sensors_data_, motors_data_);
  if (all_initialised) { return Calibrating::getInstance(); }

  return nullptr;
}

Alternatives like

std::optional<std::unique_ptr<State>> Idle::checkTransition(Logger &log)

also seem possible.
Similarly, all transition functions should only use const references to the data objects.