Changeset 89:ac4d46e44550 in finroc_plugins_parameters


Ignore:
Timestamp:
27.04.2020 05:53:49 (12 months ago)
Author:
Max Reichardt <mreichardt@…>
Branch:
17.03
Phase:
public
Message:

Revises implementation of tParameterInfo. It now supports managing and retaining parameter settings from multiple origins.

Location:
internal
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • internal/tParameterInfo.cpp

    r66 r89  
    7575static rrlib::rtti::tDataType<tParameterInfo> cTYPE; 
    7676 
     77namespace 
     78{ 
     79 
     80const std::string cEMPTY_STRING; 
     81 
     82} 
     83 
    7784//---------------------------------------------------------------------- 
    7885// Implementation 
    7986//---------------------------------------------------------------------- 
    8087 
    81 tParameterInfo::tParameterInfo() : 
    82   config_entry(), 
    83   entry_set_from_finstruct(false), 
    84   command_line_option(), 
    85   finstruct_default() 
     88tParameterInfo::tParameterInfo() 
    8689{} 
    8790 
    8891#ifdef _LIB_RRLIB_XML_PRESENT_ 
    89 void tParameterInfo::Deserialize(const rrlib::xml::tNode& node, bool finstruct_context, bool include_commmand_line) 
    90 { 
     92void tParameterInfo::Deserialize(const rrlib::xml::tNode& node, bool high_precedence, bool include_commmand_line, tOrigin* origin) 
     93{ 
     94  std::string config_entry = ""; 
    9195  if (node.HasAttribute("config")) 
    9296  { 
    9397    config_entry = node.GetStringAttribute("config"); 
    94     entry_set_from_finstruct = finstruct_context; 
    95   } 
    96   else 
    97   { 
    98     config_entry = ""; 
    99   } 
     98  } 
     99  Set(tSettingType::CONFIG_ENTRY, origin, high_precedence, config_entry, false); 
     100 
    100101  if (include_commmand_line) 
    101102  { 
     103    std::string command_line_option = ""; 
    102104    if (node.HasAttribute("cmdline")) 
    103105    { 
    104106      command_line_option = node.GetStringAttribute("cmdline"); 
    105107    } 
    106     else 
    107     { 
    108       command_line_option = ""; 
    109     } 
    110   } 
     108    Set(tSettingType::COMMAND_LINE_OPTION, origin, high_precedence, command_line_option, false); 
     109  } 
     110  std::string finstruct_default = ""; 
    111111  if (node.HasAttribute("default")) 
    112112  { 
    113113    finstruct_default = node.GetStringAttribute("default"); 
    114114  } 
    115   else 
    116   { 
    117     finstruct_default = ""; 
    118   } 
     115  Set(tSettingType::FINSTRUCT_DEFAULT, origin, high_precedence, finstruct_default, false); 
    119116} 
    120117#endif 
     118 
     119const std::string& tParameterInfo::GetSetting(tSettingType type) const 
     120{ 
     121  const tSetting* low_precedence_setting = nullptr; 
     122  const tSetting* high_precedence_setting = nullptr; 
     123  for (auto & setting : settings) 
     124  { 
     125    if (setting.type == type) 
     126    { 
     127      (setting.high_precedence ? high_precedence_setting : low_precedence_setting) = &setting; 
     128    } 
     129  } 
     130  return high_precedence_setting ? high_precedence_setting->value : (low_precedence_setting ? low_precedence_setting->value : cEMPTY_STRING); 
     131} 
     132 
     133const std::string& tParameterInfo::GetSetting(tSettingType type, bool high_precedence, tOrigin* origin) const 
     134{ 
     135  for (auto & setting : settings) 
     136  { 
     137    if (setting.type == type && setting.high_precedence == high_precedence && setting.origin == origin) 
     138    { 
     139      return setting.value; 
     140    } 
     141  } 
     142  return cEMPTY_STRING; 
     143} 
     144 
     145bool tParameterInfo::IsConfigEntrySetFromFinstruct() const 
     146{ 
     147  for (auto & setting : settings) 
     148  { 
     149    if (setting.type == tSettingType::CONFIG_ENTRY && setting.high_precedence) 
     150    { 
     151      return true; 
     152    } 
     153  } 
     154  return false; 
     155} 
    121156 
    122157bool tParameterInfo::IsFinstructableGroupResponsibleForConfigFileConnections(const core::tFrameworkElement& finstructable_group, const core::tFrameworkElement& ap) 
     
    134169    { 
    135170      // command line option 
     171      auto& command_line_option = GetCommandLineOption(); 
    136172      if (command_line_option.length() > 0) 
    137173      { 
     
    168204      // config file entry 
    169205      tConfigFile* cf = tConfigFile::Find(*ann); 
    170       if (cf != NULL && config_entry.length() > 0) 
     206      auto& config_entry = GetConfigEntry(); 
     207      if (cf && config_entry.length() > 0) 
    171208      { 
    172209        std::string full_config_entry = tConfigNode::GetFullConfigEntry(*ann, config_entry); 
     
    204241 
    205242      // finstruct default 
     243      auto& finstruct_default = GetFinstructDefault(); 
    206244      if (finstruct_default.length() > 0) 
    207245      { 
     
    251289{ 
    252290  core::tAbstractPort* ann = GetAnnotated<core::tAbstractPort>(); 
    253   if (ann == NULL || (!ann->IsReady())) 
     291  if ((!ann) || (!ann->IsReady())) 
    254292  { 
    255293    return; 
    256294  } 
    257295  tConfigFile* cf = tConfigFile::Find(*ann); 
     296  auto& config_entry = GetConfigEntry(); 
    258297  bool has_entry = cf->HasEntry(config_entry); 
    259298  if (data_ports::IsDataFlowType(ann->GetDataType())) 
     
    277316 
    278317#ifdef _LIB_RRLIB_XML_PRESENT_ 
    279 void tParameterInfo::Serialize(rrlib::xml::tNode & node, bool finstruct_context, bool include_command_line) const 
     318void tParameterInfo::Serialize(rrlib::xml::tNode& node, bool high_precedence, bool include_command_line, tOrigin* origin) const 
    280319{ 
    281320  assert(!(node.HasAttribute("default") || node.HasAttribute("cmdline") || node.HasAttribute("config"))); 
    282   if (config_entry.length() > 0 && (entry_set_from_finstruct || (!finstruct_context))) 
     321  auto config_entry = GetSetting(tSettingType::CONFIG_ENTRY, high_precedence, origin); 
     322  if (config_entry.length()) 
    283323  { 
    284324    node.SetAttribute("config", config_entry); 
     
    286326  if (include_command_line) 
    287327  { 
    288     if (command_line_option.length() > 0) 
     328    auto command_line_option = GetSetting(tSettingType::COMMAND_LINE_OPTION, high_precedence, origin); 
     329    if (command_line_option.length()) 
    289330    { 
    290331      node.SetAttribute("cmdline", command_line_option); 
    291332    } 
    292333  } 
     334  auto finstruct_default = GetSetting(tSettingType::FINSTRUCT_DEFAULT, high_precedence, origin); 
    293335  if (finstruct_default.length() > 0) 
    294336  { 
     
    298340#endif 
    299341 
    300 void tParameterInfo::SetConfigEntry(const std::string & config_entry, bool finstruct_set) 
    301 { 
    302   if (this->config_entry.compare(config_entry) != 0) 
    303   { 
    304     this->config_entry = config_entry; 
    305     this->entry_set_from_finstruct = finstruct_set; 
     342bool tParameterInfo::Set(tSettingType type, tOrigin* origin, bool high_precedence, const std::string& value, bool load_on_change) 
     343{ 
     344  auto existing_setting = settings.end(); 
     345  for (auto it = settings.begin(); it != settings.end(); ++it) 
     346  { 
     347    if (it->type == type && it->origin == origin && it->high_precedence == high_precedence) 
     348    { 
     349      existing_setting = it; 
     350      break; 
     351    } 
     352  } 
     353 
     354  bool reload = false; 
     355  bool changed = false; 
     356  if (existing_setting != settings.end()) 
     357  { 
     358    if (value.empty()) 
     359    { 
     360      settings.erase(existing_setting); 
     361      changed = true; 
     362    } 
     363    else if (existing_setting->value != value) 
     364    { 
     365      existing_setting->value = value; 
     366      reload = true; 
     367    } 
     368  } 
     369  else if (!value.empty()) 
     370  { 
     371    tSetting new_setting = { type, origin, high_precedence, value }; 
     372    settings.push_back(new_setting); 
     373  } 
     374  changed |= reload; 
     375 
     376  if (load_on_change && reload) 
     377  { 
    306378    try 
    307379    { 
     
    313385    } 
    314386  } 
     387 
     388  return changed; 
    315389} 
    316390 
     
    327401rrlib::serialization::tInputStream& operator >> (rrlib::serialization::tInputStream & stream, tParameterInfo & parameter_info) 
    328402{ 
    329   parameter_info.entry_set_from_finstruct = stream.ReadBoolean(); 
    330   std::string config_entry_tmp = stream.ReadString(); 
    331   std::string command_line_option_tmp = stream.ReadString(); 
    332   std::string finstruct_default_tmp = stream.ReadString(); 
    333   bool same = config_entry_tmp.compare(parameter_info.GetConfigEntry()) == 0 && 
    334               command_line_option_tmp.compare(parameter_info.GetCommandLineOption()) == 0 && 
    335               finstruct_default_tmp.compare(parameter_info.GetFinstructDefault()) == 0; 
    336   parameter_info.config_entry = config_entry_tmp; 
    337   parameter_info.command_line_option = command_line_option_tmp; 
    338   parameter_info.finstruct_default = finstruct_default_tmp; 
    339  
    340   if (!same) 
     403  bool entry_set_from_finstruct = stream.ReadBoolean(); 
     404  std::string config_entry = stream.ReadString(); 
     405  std::string command_line_option = stream.ReadString(); 
     406  std::string finstruct_default = stream.ReadString(); 
     407  bool changed = parameter_info.Set(tParameterInfo::tSettingType::CONFIG_ENTRY, nullptr, entry_set_from_finstruct, config_entry, false); 
     408  changed |= parameter_info.Set(tParameterInfo::tSettingType::COMMAND_LINE_OPTION, nullptr, true, command_line_option, false); 
     409  changed |= parameter_info.Set(tParameterInfo::tSettingType::FINSTRUCT_DEFAULT, nullptr, true, finstruct_default, false); 
     410 
     411  if (changed) 
    341412  { 
    342413    try 
  • internal/tParameterInfo.h

    r53 r89  
    7878public: 
    7979 
     80  typedef core::tFrameworkElement tOrigin; 
     81 
     82  enum class tSettingType : uint8_t 
     83  { 
     84    CONFIG_ENTRY,         // Place in Configuration tree, this parameter is configured from (nodes are separated with '/')  (starting with '/' => absolute link - otherwise relative) 
     85    COMMAND_LINE_OPTION,  // Command line option to set this parameter (set by outer-most finstructable group) 
     86    FINSTRUCT_DEFAULT     // Default value set in finstruct (optional) 
     87  }; 
     88 
     89  struct tSetting 
     90  { 
     91    tSettingType type; 
     92    tOrigin* origin; 
     93    bool high_precedence; // Entry with higher precedence than entries without this flag (typically true for finstructed entries) 
     94    std::string value; 
     95  }; 
     96 
     97 
    8098  tParameterInfo(); 
    8199 
    82100#ifdef _LIB_RRLIB_XML_PRESENT_ 
    83   void Deserialize(const rrlib::xml::tNode& node, bool finstruct_context, bool include_commmand_line); 
     101  void Deserialize(const rrlib::xml::tNode& node, bool high_precedence, bool include_commmand_line, tOrigin* origin); 
    84102#endif 
    85103 
     
    88106   * (set by outer-most finstructable group) 
    89107   */ 
    90   std::string GetCommandLineOption() const 
    91   { 
    92     return command_line_option; 
     108  inline const std::string& GetCommandLineOption() const 
     109  { 
     110    return GetSetting(tSettingType::COMMAND_LINE_OPTION); 
    93111  } 
    94112 
     
    96114   * \return Place in Configuration tree, this parameter is configured from (nodes are separated with dots) 
    97115   */ 
    98   inline std::string GetConfigEntry() const 
    99   { 
    100     return config_entry; 
     116  inline const std::string& GetConfigEntry() const 
     117  { 
     118    return GetSetting(tSettingType::CONFIG_ENTRY); 
    101119  } 
    102120 
     
    105123   * (set by finstructable group responsible for connecting this parameter to attribute tree) 
    106124   */ 
    107   std::string GetFinstructDefault() const 
    108   { 
    109     return finstruct_default; 
    110   } 
    111  
    112   /*! 
    113    * \return Does parameter have any non-default info relevant for finstructed group? 
    114    */ 
    115   bool HasNonDefaultFinstructInfo() 
    116   { 
    117     return (config_entry.length() > 0 && entry_set_from_finstruct) || command_line_option.length() > 0 || finstruct_default.length() > 0; 
     125  inline const std::string& GetFinstructDefault() const 
     126  { 
     127    return GetSetting(tSettingType::FINSTRUCT_DEFAULT); 
    118128  } 
    119129 
     
    121131   * \return Is config entry set from finstruct/xml? 
    122132   */ 
    123   bool IsConfigEntrySetFromFinstruct() const 
    124   { 
    125     return entry_set_from_finstruct; 
    126   } 
     133  bool IsConfigEntrySetFromFinstruct() const; 
    127134 
    128135  /*! 
     
    137144  /*! 
    138145   * load value from configuration file 
    139    */ 
    140   inline void LoadValue() 
    141   { 
    142     LoadValue(false); 
    143   } 
    144  
    145   /*! 
    146    * load value from configuration file 
    147146   * 
    148    * \param ignore ready flag? 
    149    */ 
    150   void LoadValue(bool ignore_ready); 
     147   * \param ignore_ready Ignore ready flag? 
     148   */ 
     149  void LoadValue(bool ignore_ready = false); 
    151150 
    152151  /*! 
     
    157156 
    158157#ifdef _LIB_RRLIB_XML_PRESENT_ 
    159   void Serialize(rrlib::xml::tNode& node, bool finstruct_context, bool include_command_line) const; 
     158  void Serialize(rrlib::xml::tNode& node, bool high_precedence, bool include_command_line, tOrigin* origin) const; 
    160159#endif 
    161160 
    162161  /*! 
    163    * \param commandLineOption Command line option to set this parameter 
     162   * \param command_line_option Command line option to set this parameter 
     163   * \param high_precedence Entry with higher precedence than entries without this flag 
     164   * \param origin Optional origin for entry 
    164165   * (set by outer-most finstructable group) 
    165166   */ 
    166   void SetCommandLineOption(const std::string& command_line_option) 
    167   { 
    168     this->command_line_option = command_line_option; 
    169   } 
    170  
    171   /*! 
    172    * (loads value from configuration file, if is exists 
     167  void SetCommandLineOption(const std::string& command_line_option, bool high_precedence = false, tOrigin* origin = nullptr) 
     168  { 
     169    Set(tSettingType::COMMAND_LINE_OPTION, origin, high_precedence, command_line_option, false); 
     170  } 
     171 
     172  /*! 
     173   * (loads value from configuration file, if is exists) 
    173174   * 
    174175   * \param config_entry New Place in Configuration tree, this parameter is configured from (nodes are separated with dots) 
    175176   * \param finstruct_set Is config entry set from finstruct? 
    176    */ 
    177   void SetConfigEntry(const std::string& config_entry, bool finstruct_set = false); 
    178  
    179   /*! 
    180    * \param finstructDefault Default value set in finstruct. 
     177   * \param origin Optional origin for entry 
     178   */ 
     179  void SetConfigEntry(const std::string& config_entry, bool finstruct_set = false, tOrigin* origin = nullptr) 
     180  { 
     181    Set(tSettingType::CONFIG_ENTRY, origin, finstruct_set, config_entry, true); 
     182  } 
     183 
     184  /*! 
     185   * \param finstruct_default Default value set in finstruct. 
     186   * \param high_precedence Entry with higher precedence than entries without this flag 
     187   * \param origin Optional origin for entry 
    181188   * (set by finstructable group responsible for connecting this parameter to attribute tree) 
    182189   */ 
    183   void SetFinstructDefault(const std::string& finstruct_default) 
    184   { 
    185     this->finstruct_default = finstruct_default; 
     190  void SetFinstructDefault(const std::string& finstruct_default, bool high_precedence = false, tOrigin* origin = nullptr) 
     191  { 
     192    Set(tSettingType::FINSTRUCT_DEFAULT, origin, high_precedence, finstruct_default, true); 
     193  } 
     194 
     195  /*! 
     196   * \return All current settings for this parameter 
     197   */ 
     198  const std::vector<tSetting>& Settings() const 
     199  { 
     200    return settings; 
    186201  } 
    187202 
     
    194209  friend rrlib::serialization::tInputStream& operator >> (rrlib::serialization::tInputStream& stream, tParameterInfo& parameter_info); 
    195210 
    196   /*! 
    197    * Place in Configuration tree, this parameter is configured from (nodes are separated with '/') 
    198    * (starting with '/' => absolute link - otherwise relative) 
    199    */ 
    200   std::string config_entry; 
    201  
    202   /*! Was config entry set from finstruct? */ 
    203   bool entry_set_from_finstruct; 
    204  
    205   /*! 
    206    * Command line option to set this parameter 
    207    * (set by outer-most finstructable group) 
    208    */ 
    209   std::string command_line_option; 
    210  
    211   /*! 
    212    * Default value set in finstruct (optional) 
    213    * (set by finstructable group responsible for connecting this parameter to attribute tree) 
    214    */ 
    215   std::string finstruct_default; 
    216  
    217  
     211  std::vector<tSetting> settings; 
     212 
     213  const std::string& GetSetting(tSettingType type) const; 
     214  const std::string& GetSetting(tSettingType type, bool high_precedence, tOrigin* origin) const; 
    218215  virtual void OnInitialization() override; 
     216  bool Set(tSettingType type, tOrigin* origin, bool high_precedence, const std::string& value, bool load_on_change); 
    219217}; 
    220218 
Note: See TracChangeset for help on using the changeset viewer.