Managing Parameters Through Interfaces

If you use YAML files to store system configuration parameters, you need to use the CfgMgr interface provided by the configuration management module to manage parameters and implement functions such as parameter obtaining, listening, snapshotting, and reloading.

This section uses the param.yaml parameter file as an example to describe the functions and usage of the CfgMgr interfaces.

param1:
  a: 12
  b: true
  c: "node"
param2: 234
The sample code for including dependent header files is as follows:
#include "cfgmgr/param.h"
#include <map>
#include <string>
  • Use the CfgMgr::Get interface to obtain parameter values. The sample code is as follows:
    int aValue;
    CfgMgr::Get("/param1/a", aValue);                // Obtain the value of a in param1. In this example, aValue is 12.
    bool bValue;
    CfgMgr::Get("/param1/b", bValue);               // Obtain the value of b in param1. In this example, bValue is true.
    std::map<std::string, std::string> param1Value;
    CfgMgr::Get("/param1", param1Value);            // Obtain all parameters in param1. In this example, param1Value is {{a, 12}, {b, true}, {c, "node"}}.
  • Use the CfgMgr::Parse interface to parse parameter values. Generally, this interface is used together with CfgMgr::Get. The sample code is as follows:
    std::map<std::string, std::string> param1Value;
    CfgMgr::Get("/param1", param1Value);              // Obtain all parameters in param1. In this example, param1Value is {{a, 12}, {b, true}, {c, "node"}}.
    int aValue;
    CfgMgr::Parse("a", param1Value, aValue);        // The value of aValue is 12.
  • Use the CfgMgr::Set interface to set parameter values. The sample code is as follows:
    int aValue = 24;
    CfgMgr::Set("/param1/a", aValue);             // Set the value of a in param1 to 24.
  • Use the CfgMgr::Monitor interface to set listening parameters and the CfgMgr::CancelMonitor interface to cancel listening parameters. The sample code is as follows:
    void printA(int a)
    {
    std::cout<<"/param1/a is : "<<a<<std::endl;
    }
    CfgMgr::Monitor("/param1/a", printA, false);   // Listen parameter /param1/a. When this parameter is set by another node, the printA callback function is executed.
    CfgMgr::CancelMonitor("/param1/a");         // Cancel the listening on parameter /param1/a.
  • Use the CfgMgr::CreateSnapshot interface to create a parameter snapshot and record all parameter settings. Then, invoke the CfgMgr::RevertSnapshot interface to restore the parameter snapshot. To delete a parameter snapshot, use the CfgMgr::DeleteSnapshot interface. The sample code is as follows:
    std::string filename = "demoParam";            // Snapshot file name, which cannot contain the path.
    CfgMgr::CreateSnapshot(filename);          // Create a snapshot. If the snapshot file has the same name, the new file will overwrite the old file.
    CfgMgr::RevertSnapshot(filename);         // Restore a snapshot.
    CfgMgr::DeleteSnapshot(filename);         // Delete a snapshot.
  • Use the CfgMgr::Reload interface to reload the YAML file. When the YAML file to be loaded changes, you can use this interface to reload the YAML file and generate a new configuration file. The sample code is as follows:
    CfgMgr::Reload();