Changeset 23:3c9cac2a0cc1 in finroc_plugins_composite_ports


Ignore:
Timestamp:
20.03.2021 11:51:25 (5 weeks ago)
Author:
Max Reichardt <mreichardt@…>
Branch:
default
Phase:
public
Message:

Adds 'interface modifiers' for customization of port composite interface creation (as there are diverse use cases). Refactors and tidies partial interface creation with this new feature.

Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • internal/tGenericPortType.h

    r17 r23  
    128128    assert(creation_info.data_type); 
    129129 
    130     data_ports::tGenericPort port(creation_info); 
    131     this->SetWrapped(port.GetWrapped()); 
     130    this->SetWrapped(parent_backend->CreatePort(creation_info, Tprimary_interface_relations)); 
    132131  } 
    133132 
     
    166165    // Create port instance via factory 
    167166    core::tFrameworkElementFlags extra_flags = parent_backend->CreateOutputPort(Tprimary_interface_relations) ? (core::tFrameworkElementFlag::OUTPUT_PORT | core::tFrameworkElementFlag::EMITS_DATA) : (core::tFrameworkElementFlag::ACCEPTS_DATA | core::tFrameworkElementFlag::PORT); 
    168     this->SetWrapped(core::tPortFactory::CreatePort(name, *parent_backend, rrlib::rtti::tType::FindTypeByRtti(typeid(T).name()), parent_backend->GetDefaultPortFlags(true) | extra_flags)); 
     167    core::tAbstractPortCreationInfo creation_info; 
     168    creation_info.name = name; 
     169    creation_info.parent = parent_backend; 
     170    creation_info.data_type = rrlib::rtti::tType::FindTypeByRtti(typeid(T).name()); 
     171    creation_info.flags = parent_backend->GetDefaultPortFlags(true) | extra_flags; 
     172    this->SetWrapped(parent_backend->CreatePort(creation_info, Tprimary_interface_relations)); 
    169173  } 
    170174 
  • internal/tInterfaceTypeInfo.cpp

    r17 r23  
    3939// Internal includes with "" 
    4040//---------------------------------------------------------------------- 
     41#include "plugins/composite_ports/interface_types.h" 
    4142 
    4243//---------------------------------------------------------------------- 
  • internal/tInterfaceTypeInfo.h

    r17 r23  
    111111    auto name = GenerateName(typeid(TInterface).name()); 
    112112    rrlib::rtti::tType type = rrlib::rtti::tType::FindType(name.Get()); 
    113     return type && IsInterfaceType(type) ? type : rrlib::rtti::tType(); 
     113    return type.GetTypeClassification() == rrlib::rtti::tTypeClassification::PORT_COMPOSITE_INTERFACE ? type : rrlib::rtti::tType(); 
    114114  } 
    115115 
     
    141141  rrlib::rtti::detail::tTypeInfo* type_info_partial = nullptr; 
    142142 
    143   /*! Pointer to full type info this interface type has a partial type info */ 
     143  /*! Pointer to full type info (possibly relevant if this interface type has a partial type info) */ 
    144144  const rrlib::rtti::detail::tTypeInfo* type_info_full = nullptr; 
    145145 
  • internal/type_traits.h

    r22 r23  
    9393  ePTR_COUNTER_PART_PARAMETER_IF_INPUT, 
    9494  ePTR_SERVER_IF_INPUT, 
    95   ePTR_CLIENT_IF_INPUT 
     95  ePTR_CLIENT_IF_INPUT, 
     96  ePTR_DIMENSION 
    9697}; 
    9798 
  • tInterfaceBase.cpp

    r22 r23  
    4343#include "plugins/composite_ports/internal/tInterfaceTypeInfo.h" 
    4444#include "plugins/composite_ports/internal/type_traits.h" 
     45#include "plugins/composite_ports/tInterfaceModifier.h" 
    4546 
    4647//---------------------------------------------------------------------- 
     
    7778namespace 
    7879{ 
     80 
     81const tInterfaceModifier::tImplementation cMODIFIER_DEFAULT_IMPLEMENTATION; 
    7982 
    8083const core::tFrameworkElement* GetAggregator(const core::tFrameworkElement& element, core::tFrameworkElementFlags* extend_with_default_ports_flags = nullptr) 
     
    113116  } 
    114117  return element.GetParent() ? GetAggregator(*element.GetParent(), extend_with_default_ports_flags) : nullptr; 
     118} 
     119 
     120tInterfaceBase::tPrimaryPortType ModifierDefinesPartialInterface(const core::tFrameworkElement& parent_interface, const rrlib::rtti::tType& interface_type) 
     121{ 
     122  if ((!parent_interface.IsPort()) && parent_interface.GetFlag(tFlag::INTERFACE) && internal::IsComponent(parent_interface.GetParent())) 
     123  { 
     124    tInterfaceModifier* interface_modifier = parent_interface.GetParent()->GetAnnotation<tInterfaceModifier>(); 
     125    if (interface_modifier) 
     126    { 
     127      return interface_modifier->Implementation().PartialInterface(interface_type); 
     128    } 
     129  } 
     130  return tInterfaceBase::tPrimaryPortType::ANY; 
     131} 
     132 
     133tInterfaceBase::tPrimaryPortType ModifierDefinesPartialInterface(const core::tFrameworkElement* any_element, const rrlib::rtti::tType& interface_type) 
     134{ 
     135  if (any_element) 
     136  { 
     137    auto aggregator = GetAggregator(*any_element); 
     138    return ModifierDefinesPartialInterface(*aggregator, interface_type); 
     139  } 
     140  return tInterfaceBase::tPrimaryPortType::ANY; 
    115141} 
    116142 
     
    132158} 
    133159 
    134 //void ReplaceAny(std::string& s, const std::string& seach_string, const std::string& replace_with)  // TODO 
    135 //{ 
    136 //  auto index = s.find(seach_string); 
    137 //  while (index != std::string::npos) 
    138 //  { 
    139 //    s.replace(index, seach_string.length(), replace_with); 
    140 //    index = s.find(seach_string); 
    141 //  } 
    142 //} 
    143  
    144160void FindInterfacesBelowImplementation(std::vector<tInterfaceBase>& result, core::tFrameworkElement& parent, const rrlib::rtti::tType& type, bool skip_derived_interfaces) 
    145161{ 
     
    277293 
    278294tInterfaceBase::tBackend::tBackend(const rrlib::rtti::tType& interface_type, core::tFrameworkElement* parent_component, core::tFrameworkElement* parent, int primary_relation_id, const std::string& name, bool convenience_port_type, const tConnectFunction& custom_connect_function, tCreateMissingComponentInterfaceFunction create_missing_component_interface_function, tPrimaryPortType primary_port_type, bool partial_interface) : 
    279   tAbstractPort(MakeCreationInfo(interface_type, name, *parent, partial_interface || tScopedExtraConstructorParameters::Get()->partial_interface)), 
     295  tAbstractPort(MakeCreationInfo(interface_type, name, *parent, partial_interface || tScopedExtraConstructorParameters::Get()->partial_interface || ModifierDefinesPartialInterface(parent, interface_type))), 
    280296  parent_component(parent_component), 
    281297  primary_backend(*this), 
     
    285301  create_missing_component_interface_function(create_missing_component_interface_function), 
    286302  primary_port_type(primary_port_type), 
    287   partial_interface(static_cast<const internal::tInterfaceTypeInfo&>(GetDataType().SharedTypeInfo()).GetFullType() != GetDataType()) 
     303  partial_interface(static_cast<const internal::tInterfaceTypeInfo&>(GetDataType().SharedTypeInfo()).GetFullType() != GetDataType()), 
     304  partial_interface_primary_port_type(true) 
    288305{ 
    289306  all_backends.emplace_back(this); 
     
    292309  { 
    293310    auto aggregator = GetAggregator(*this); 
    294     bool sensor_parent = (aggregator && aggregator->GetFlag(tFlag::SENSOR_DATA)); 
    295     bool controller_parent = (aggregator && aggregator->GetFlag(tFlag::CONTROLLER_DATA)); 
    296     if ((sensor_parent && primary_port_type == tPrimaryPortType::CONTROLLER_PORT) || (controller_parent && primary_port_type == tPrimaryPortType::SENSOR_PORT)) 
     311    auto partial_definition = ModifierDefinesPartialInterface(*aggregator, interface_type); 
     312    if (partial_definition != tPrimaryPortType::ANY) 
     313    { 
     314      this->partial_interface_primary_port_type = partial_definition == primary_port_type; 
     315    } 
     316    else 
     317    { 
     318      bool sensor_parent = (aggregator && (aggregator->GetFlag(tFlag::SENSOR_DATA) || partial_definition == tInterfaceBase::tPrimaryPortType::SENSOR_PORT)); 
     319      bool controller_parent = (aggregator && (aggregator->GetFlag(tFlag::CONTROLLER_DATA) || partial_definition == tInterfaceBase::tPrimaryPortType::CONTROLLER_PORT)); 
     320      if ((sensor_parent && primary_port_type == tPrimaryPortType::CONTROLLER_PORT) || (controller_parent && primary_port_type == tPrimaryPortType::SENSOR_PORT)) 
     321      { 
     322        this->partial_interface_primary_port_type = false; 
     323      } 
     324    } 
     325    if (!this->partial_interface_primary_port_type) 
    297326    { 
    298327      primary_relation_backend = nullptr; 
     
    309338  create_missing_component_interface_function(nullptr), 
    310339  primary_port_type(primary.primary_port_type), 
    311   partial_interface(primary.partial_interface) 
     340  partial_interface(primary.partial_interface), 
     341  partial_interface_primary_port_type(primary.partial_interface_primary_port_type) 
    312342{ 
    313343} 
     
    369399  } 
    370400  throw std::runtime_error("Unspecified port direction"); 
     401} 
     402 
     403core::tAbstractPort* tInterfaceBase::tBackend::CreatePort(const core::tAbstractPortCreationInfo& creation_info, int primary_relation_id) const 
     404{ 
     405  return cMODIFIER_DEFAULT_IMPLEMENTATION.CreatePort(creation_info, primary_relation_id); 
    371406} 
    372407 
     
    408443    parent = this->GetParent(); 
    409444  } 
     445  else if (this->partial_interface && (!IsPartOfPartialInstantiation(primary_relation_id, this->partial_interface_primary_port_type))) 
     446  { 
     447    parent = nullptr; // Skip 
     448  } 
    410449  else 
    411450  { 
     
    421460 
    422461    const core::tFrameworkElementFlags relevant_flags = tFlag::SENSOR_DATA | tFlag::CONTROLLER_DATA | tFlag::INTERFACE_FOR_INPUTS | tFlag::INTERFACE_FOR_OUTPUTS | tFlag::PROXY_INTERFACE | tFlag::PARAMETER_INTERFACE; 
     462    tInterfaceModifier* interface_modifier = component_interface->GetParent()->GetAnnotation<tInterfaceModifier>(); 
    423463 
    424464    // Compute opcode sequence 
     
    442482    } 
    443483 
     484    int remaining_relations = relation_list.size(); 
    444485    for (int opcode : relation_list) 
    445486    { 
     487      remaining_relations--; 
    446488      core::tFrameworkElementFlags original_flags = use_these_original_flags.Raw() ? use_these_original_flags : parent_candidate->GetAllFlags(); 
    447489      use_these_original_flags = core::tFrameworkElementFlags(); 
     
    534576      } 
    535577 
    536       if ((!new_candidate) && this->IsPartialInterface() && ((partial_interface_direction_is_output && (!target_set_flags.Get(tFlag::INTERFACE_FOR_OUTPUTS))) || ((!partial_interface_direction_is_output) && target_set_flags.Get(tFlag::INTERFACE_FOR_OUTPUTS)) || target_set_flags.Get(tFlag::INTERFACE_FOR_RPC_PORTS))) 
     578      if ((!new_candidate) && ((this->IsPartialInterface() && remaining_relations) || interface_modifier)) 
    537579      { 
    538580        use_these_original_flags = target_set_flags; 
     
    560602      } 
    561603    } 
     604    assert(remaining_relations == 0); 
    562605 
    563606    parent = parent_candidate; 
    564   } 
    565  
    566   if (parent && partial_interface) 
    567   { 
    568     auto aggregator = GetAggregator(*parent); 
    569     if ((partial_interface_direction_is_output && aggregator->GetFlag(tFlag::INTERFACE_FOR_INPUTS)) || ((!partial_interface_direction_is_output) && aggregator->GetFlag(tFlag::INTERFACE_FOR_OUTPUTS))) 
    570     { 
    571       parent = nullptr; 
     607    if (interface_modifier) 
     608    { 
     609      parent = interface_modifier->Implementation().GetComponentInterface(parent_candidate && parent_candidate->GetFlag(tFlag::INTERFACE) ? static_cast<core::tPortGroup*>(parent_candidate) : nullptr, primary_relation_id); 
    572610    } 
    573611  } 
     
    625663} 
    626664 
     665bool tInterfaceBase::tBackend::IsPartOfPartialInstantiation(int primary_relation_id, bool partial_instantiation_variant_primary_port_type) 
     666{ 
     667  typedef internal::tPortTypeRelation tOpCode; 
     668  int switch_count = 0; 
     669  while (primary_relation_id) 
     670  { 
     671    int opcode = primary_relation_id & 0xFF; 
     672    switch_count += ((opcode == tOpCode::ePTR_COUNTER_PART || opcode == tOpCode::ePTR_COUNTER_PART_PARAMETER_IF_INPUT) ? 1 : 0); 
     673    primary_relation_id = primary_relation_id >> 8; 
     674  } 
     675  bool primary_port_type_relation_id = (switch_count % 2) == 0; 
     676  return primary_port_type_relation_id == partial_instantiation_variant_primary_port_type; 
     677} 
     678 
    627679void tInterfaceBase::tBackend::OnConnect(tAbstractPort& partner, bool partner_is_destination) 
    628680{ 
  • tInterfaceBase.h

    r22 r23  
    7575extern const std::string cINTERFACE_TAG_SINGLE_BACKEND_INSTANTIATION; 
    7676 
    77 /*! 
    78  * \param type Data type to check 
    79  * \return Is specified data type a port composite interface type? 
    80  */ 
    81 inline bool IsInterfaceType(const rrlib::rtti::tType& type) 
    82 { 
    83   return type.GetTypeClassification() == rrlib::rtti::tTypeClassification::PORT_COMPOSITE_INTERFACE; 
    84 } 
    85  
    8677 
    8778//---------------------------------------------------------------------- 
     
    10293  /*! 
    10394   * Allows to specify type of primary port 
    104    * Is set, this allows to instantiate interfaces partially 
     95   * If set, this allows to instantiate interfaces partially 
    10596   */ 
    10697  enum tPrimaryPortType 
     
    333324    tPrimaryPortType primary_port_type; 
    334325 
    335     /*! Whether this interface is a partial interface */ 
    336     bool partial_interface; 
     326    /*! Whether this interface is a partial interface (and if yes, whether partial instantiation should instantiate the primary port type ports (instead of the secondary))  */ 
     327    bool partial_interface, partial_interface_primary_port_type; 
    337328 
    338329    /*! 
     
    349340 
    350341    /*! 
     342     * Creates port (generic instantiation only) 
     343     * Called for all data and RPC ports to be created below port composite interface root. 
     344     * (Behavior of this function may be altered by modifier) 
     345     * 
     346     * \param creation_info Creation info to use. 
     347     * \param primary_relation_id Primary relation ID, as used in interface classes (zero is primary port type) 
     348     */ 
     349    core::tAbstractPort* CreatePort(const core::tAbstractPortCreationInfo& creation_info, int primary_relation_id) const; 
     350 
     351    /*! 
    351352     * (called by tGenericPortType) 
    352353     * \param primary_relation_id Relation id to primary port type 
     
    354355     */ 
    355356    tBackend* GetBackend(int primary_relation_id); 
     357 
     358    /*! 
     359     * Defines which ports are part of partial instantiations. 
     360     * Partial instantiations either contain all ports of the primary type (+ all port types in the same direction) - 
     361     * or the secondary. 
     362     * 
     363     * \param primary_relation_id Relation id to primary port type 
     364     * \param partial_interface_variant_primary_port_type Whether partial instantiation should instantiate the primary port type ports (instead of the secondary) 
     365     * \return Whether ports with relation id are part of the specified variant 
     366     */ 
     367    static bool IsPartOfPartialInstantiation(int primary_relation_id, bool partial_interface_variant_primary_port_type); 
    356368  }; 
    357369 
Note: See TracChangeset for help on using the changeset viewer.