Changeset 89:ac4d46e44550 in finroc_plugins_parameters
- Timestamp:
- 27.04.2020 05:53:49 (12 months ago)
- Branch:
- 17.03
- Phase:
- public
- Location:
- internal
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
internal/tParameterInfo.cpp
r66 r89 75 75 static rrlib::rtti::tDataType<tParameterInfo> cTYPE; 76 76 77 namespace 78 { 79 80 const std::string cEMPTY_STRING; 81 82 } 83 77 84 //---------------------------------------------------------------------- 78 85 // Implementation 79 86 //---------------------------------------------------------------------- 80 87 81 tParameterInfo::tParameterInfo() : 82 config_entry(), 83 entry_set_from_finstruct(false), 84 command_line_option(), 85 finstruct_default() 88 tParameterInfo::tParameterInfo() 86 89 {} 87 90 88 91 #ifdef _LIB_RRLIB_XML_PRESENT_ 89 void tParameterInfo::Deserialize(const rrlib::xml::tNode& node, bool finstruct_context, bool include_commmand_line) 90 { 92 void tParameterInfo::Deserialize(const rrlib::xml::tNode& node, bool high_precedence, bool include_commmand_line, tOrigin* origin) 93 { 94 std::string config_entry = ""; 91 95 if (node.HasAttribute("config")) 92 96 { 93 97 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 100 101 if (include_commmand_line) 101 102 { 103 std::string command_line_option = ""; 102 104 if (node.HasAttribute("cmdline")) 103 105 { 104 106 command_line_option = node.GetStringAttribute("cmdline"); 105 107 } 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 = ""; 111 111 if (node.HasAttribute("default")) 112 112 { 113 113 finstruct_default = node.GetStringAttribute("default"); 114 114 } 115 else 116 { 117 finstruct_default = ""; 118 } 115 Set(tSettingType::FINSTRUCT_DEFAULT, origin, high_precedence, finstruct_default, false); 119 116 } 120 117 #endif 118 119 const 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 133 const 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 145 bool 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 } 121 156 122 157 bool tParameterInfo::IsFinstructableGroupResponsibleForConfigFileConnections(const core::tFrameworkElement& finstructable_group, const core::tFrameworkElement& ap) … … 134 169 { 135 170 // command line option 171 auto& command_line_option = GetCommandLineOption(); 136 172 if (command_line_option.length() > 0) 137 173 { … … 168 204 // config file entry 169 205 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) 171 208 { 172 209 std::string full_config_entry = tConfigNode::GetFullConfigEntry(*ann, config_entry); … … 204 241 205 242 // finstruct default 243 auto& finstruct_default = GetFinstructDefault(); 206 244 if (finstruct_default.length() > 0) 207 245 { … … 251 289 { 252 290 core::tAbstractPort* ann = GetAnnotated<core::tAbstractPort>(); 253 if ( ann == NULL|| (!ann->IsReady()))291 if ((!ann) || (!ann->IsReady())) 254 292 { 255 293 return; 256 294 } 257 295 tConfigFile* cf = tConfigFile::Find(*ann); 296 auto& config_entry = GetConfigEntry(); 258 297 bool has_entry = cf->HasEntry(config_entry); 259 298 if (data_ports::IsDataFlowType(ann->GetDataType())) … … 277 316 278 317 #ifdef _LIB_RRLIB_XML_PRESENT_ 279 void tParameterInfo::Serialize(rrlib::xml::tNode & node, bool finstruct_context, bool include_command_line) const318 void tParameterInfo::Serialize(rrlib::xml::tNode& node, bool high_precedence, bool include_command_line, tOrigin* origin) const 280 319 { 281 320 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()) 283 323 { 284 324 node.SetAttribute("config", config_entry); … … 286 326 if (include_command_line) 287 327 { 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()) 289 330 { 290 331 node.SetAttribute("cmdline", command_line_option); 291 332 } 292 333 } 334 auto finstruct_default = GetSetting(tSettingType::FINSTRUCT_DEFAULT, high_precedence, origin); 293 335 if (finstruct_default.length() > 0) 294 336 { … … 298 340 #endif 299 341 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; 342 bool 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 { 306 378 try 307 379 { … … 313 385 } 314 386 } 387 388 return changed; 315 389 } 316 390 … … 327 401 rrlib::serialization::tInputStream& operator >> (rrlib::serialization::tInputStream & stream, tParameterInfo & parameter_info) 328 402 { 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) 341 412 { 342 413 try -
internal/tParameterInfo.h
r53 r89 78 78 public: 79 79 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 80 98 tParameterInfo(); 81 99 82 100 #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); 84 102 #endif 85 103 … … 88 106 * (set by outer-most finstructable group) 89 107 */ 90 std::stringGetCommandLineOption() const91 { 92 return command_line_option;108 inline const std::string& GetCommandLineOption() const 109 { 110 return GetSetting(tSettingType::COMMAND_LINE_OPTION); 93 111 } 94 112 … … 96 114 * \return Place in Configuration tree, this parameter is configured from (nodes are separated with dots) 97 115 */ 98 inline std::stringGetConfigEntry() const99 { 100 return config_entry;116 inline const std::string& GetConfigEntry() const 117 { 118 return GetSetting(tSettingType::CONFIG_ENTRY); 101 119 } 102 120 … … 105 123 * (set by finstructable group responsible for connecting this parameter to attribute tree) 106 124 */ 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); 118 128 } 119 129 … … 121 131 * \return Is config entry set from finstruct/xml? 122 132 */ 123 bool IsConfigEntrySetFromFinstruct() const 124 { 125 return entry_set_from_finstruct; 126 } 133 bool IsConfigEntrySetFromFinstruct() const; 127 134 128 135 /*! … … 137 144 /*! 138 145 * load value from configuration file 139 */140 inline void LoadValue()141 {142 LoadValue(false);143 }144 145 /*!146 * load value from configuration file147 146 * 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); 151 150 152 151 /*! … … 157 156 158 157 #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; 160 159 #endif 161 160 162 161 /*! 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 164 165 * (set by outer-most finstructable group) 165 166 */ 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) 173 174 * 174 175 * \param config_entry New Place in Configuration tree, this parameter is configured from (nodes are separated with dots) 175 176 * \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 181 188 * (set by finstructable group responsible for connecting this parameter to attribute tree) 182 189 */ 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; 186 201 } 187 202 … … 194 209 friend rrlib::serialization::tInputStream& operator >> (rrlib::serialization::tInputStream& stream, tParameterInfo& parameter_info); 195 210 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; 218 215 virtual void OnInitialization() override; 216 bool Set(tSettingType type, tOrigin* origin, bool high_precedence, const std::string& value, bool load_on_change); 219 217 }; 220 218
Note: See TracChangeset
for help on using the changeset viewer.