Changeset 125:caccc7482091 in finroc_plugins_data_ports


Ignore:
Timestamp:
07.09.2017 10:59:57 (2 years ago)
Author:
Tobias Föhst <foehst@…>
Branch:
17.03
Parents:
123:0d33b676b2a4 (diff), 116:a842f5afb76b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Phase:
public
Message:

Merge with 14.08

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tPortPack.h

    r116 r125  
    7777{ 
    7878 
     79  template <bool value, typename> 
     80  using CheckIterators = std::integral_constant<bool, value>; 
     81 
    7982//---------------------------------------------------------------------- 
    8083// Public methods and typedefs 
     
    8992  } 
    9093 
    91   template <typename TIterator> 
     94  template <typename TIterator, typename std::enable_if<CheckIterators<(Tsize> 1), TIterator>::value, int>::type = 0 > 
     95      inline tPortPack(core::tFrameworkElement *parent, TIterator names_begin, TIterator names_end) : 
     96        tPortPack < TPort, TTypeList, Tsize - 1 > (parent, names_begin, names_end - 1), 
     97        port(*(names_end - 1), parent) 
     98  { 
     99    this->port.Init(); 
     100  } 
     101 
     102  template <typename TIterator, typename std::enable_if<CheckIterators<(Tsize == 1), TIterator>::value, int>::type = 0> 
    92103  inline tPortPack(core::tFrameworkElement *parent, TIterator names_begin, TIterator names_end) : 
    93     tPortPack < TPort, TTypeList, Tsize - 1 > (parent, names_begin, names_end - 1), 
    94     port(names_end - 1, parent) 
     104    tPortPack < TPort, TTypeList, Tsize - 1 > (parent, *names_begin), 
     105    port(*names_begin, parent) 
    95106  { 
    96107    this->port.Init(); 
     
    110121    } 
    111122    return tPortPack < TPort, TTypeList, Tsize - 1 >::GetPort(index); 
     123  } 
     124 
     125  inline bool HasChanged(size_t index) 
     126  { 
     127    assert(index < this->NumberOfPorts()); 
     128    if (index == Tsize - 1) 
     129    { 
     130      return this->port.HasChanged(); 
     131    } 
     132    return tPortPack < TPort, TTypeList, Tsize - 1 >::HasChanged(index); 
    112133  } 
    113134 
     
    134155  inline tPortPack(core::tFrameworkElement *parent, const std::string &name_prefix) 
    135156  {} 
     157 
     158  template <typename TIterator> 
     159  inline tPortPack(core::tFrameworkElement *parent, TIterator names_begin, TIterator names_end) 
     160  {} 
     161 
    136162  inline core::tPortWrapperBase &GetPort(size_t index) 
    137163  { 
    138164    return *reinterpret_cast<core::tPortWrapperBase *>(0); 
    139165  }; 
     166 
     167  inline bool HasChanged(size_t index) 
     168  { 
     169    return false; 
     170  }; 
     171 
    140172  inline void ManagedDelete() 
    141173  {} 
  • tests/test_collection.cpp

    r116 r125  
    4444#include "plugins/data_ports/tProxyPort.h" 
    4545#include "plugins/data_ports/tThreadLocalBufferManagement.h" 
     46#include "plugins/data_ports/tPortPack.h" 
    4647 
    4748//---------------------------------------------------------------------- 
     
    195196    void OnPortChange(const rrlib::rtti::tGenericObject& value, tChangeContext& change_context) 
    196197    { 
    197       FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Port Changed Generic: ", value); 
     198      rrlib::serialization::tStringOutputStream stream; 
     199      value.Serialize(stream); 
     200      FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Port Changed Generic: ", stream.ToString()); 
    198201      this->calls++; 
    199202    } 
    200203    void OnPortChange(tPortDataPointer<const rrlib::rtti::tGenericObject>& value, tChangeContext& change_context) 
    201204    { 
    202       FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Port Changed Generic (tPortDataPointer): ", *value); 
     205      rrlib::serialization::tStringOutputStream stream; 
     206      value->Serialize(stream); 
     207      FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Port Changed Generic (tPortDataPointer): ", stream.ToString()); 
    203208      this->calls++; 
    204209    } 
     
    238243} 
    239244 
     245template <typename T> 
     246void TestNetworkConnectionLoss(const T& default_value, const T& publish_value) 
     247{ 
     248  core::tFrameworkElement* parent = new core::tFrameworkElement(&core::tRuntimeEnvironment::GetInstance(), "TestNetworkConnectionLoss"); 
     249 
     250  tOutputPort<T> output_port("Output Port", parent); 
     251  tInputPort<T> input_port_no_explicit_default("Input Port No Explicit Default", parent, core::tFrameworkElement::tFlag::DEFAULT_ON_DISCONNECT); 
     252  tInputPort<T> input_port_explicit_default("Input Port Explicit Default", parent, core::tFrameworkElement::tFlag::DEFAULT_ON_DISCONNECT, default_value); 
     253  tInputPort<T> input_port_deferred_default("Input Port Deferred Default", parent, core::tFrameworkElement::tFlag::DEFAULT_ON_DISCONNECT); 
     254  input_port_deferred_default.SetDefault(default_value); 
     255  output_port.ConnectTo(input_port_no_explicit_default); 
     256  output_port.ConnectTo(input_port_explicit_default); 
     257  output_port.ConnectTo(input_port_deferred_default); 
     258  parent->Init(); 
     259 
     260  output_port.Publish(publish_value); 
     261  RRLIB_UNIT_TESTS_EQUALITY(publish_value, *input_port_no_explicit_default.GetPointer()); 
     262  RRLIB_UNIT_TESTS_EQUALITY(publish_value, *input_port_explicit_default.GetPointer()); 
     263  RRLIB_UNIT_TESTS_EQUALITY(publish_value, *input_port_deferred_default.GetPointer()); 
     264  input_port_no_explicit_default.GetWrapped()->NotifyOfNetworkConnectionLoss(); 
     265  input_port_explicit_default.GetWrapped()->NotifyOfNetworkConnectionLoss(); 
     266  input_port_deferred_default.GetWrapped()->NotifyOfNetworkConnectionLoss(); 
     267  RRLIB_UNIT_TESTS_EQUALITY(T(), *input_port_no_explicit_default.GetPointer()); 
     268  RRLIB_UNIT_TESTS_EQUALITY(default_value, *input_port_explicit_default.GetPointer()); 
     269  RRLIB_UNIT_TESTS_EQUALITY(default_value, *input_port_deferred_default.GetPointer()); 
     270  output_port.Publish(publish_value); 
     271  RRLIB_UNIT_TESTS_EQUALITY(publish_value, *input_port_no_explicit_default.GetPointer()); 
     272  RRLIB_UNIT_TESTS_EQUALITY(publish_value, *input_port_explicit_default.GetPointer()); 
     273  RRLIB_UNIT_TESTS_EQUALITY(publish_value, *input_port_deferred_default.GetPointer()); 
     274  output_port.DisconnectAll(); 
     275  RRLIB_UNIT_TESTS_EQUALITY(T(), *input_port_no_explicit_default.GetPointer()); 
     276  RRLIB_UNIT_TESTS_EQUALITY(default_value, *input_port_explicit_default.GetPointer()); 
     277  RRLIB_UNIT_TESTS_EQUALITY(default_value, *input_port_deferred_default.GetPointer()); 
     278 
     279  parent->ManagedDelete(); 
     280} 
     281 
     282void TestOutOfBoundsPublish() 
     283{ 
     284  core::tFrameworkElement* parent = new core::tFrameworkElement(&core::tRuntimeEnvironment::GetInstance(), "TestOutOfBoundsPublish"); 
     285 
     286  tOutputPort<int> output_port("Output Port", parent, tBounds<int>(0, 2, tOutOfBoundsAction::DISCARD)); 
     287  tInputPort<int> input_port("Input Port", parent, tBounds<int>(0, 1)); 
     288  output_port.ConnectTo(input_port); 
     289  parent->Init(); 
     290 
     291  output_port.Publish(3); 
     292  RRLIB_UNIT_TESTS_EQUALITY(0, input_port.Get()); 
     293  output_port.Publish(2); 
     294  RRLIB_UNIT_TESTS_EQUALITY(1, input_port.Get()); 
     295 
     296  parent->ManagedDelete(); 
     297} 
     298 
     299template <typename T> 
     300void TestHijackedPublishing(const T& value_to_publish) 
     301{ 
     302  core::tFrameworkElement* parent = new core::tFrameworkElement(&core::tRuntimeEnvironment::GetInstance(), "TestHijackedPublishing"); 
     303  T default_value = T(); 
     304 
     305  tOutputPort<T> output_port("Output Port", parent); 
     306  tProxyPort<T, true> proxy_port("Proxy Port", parent, core::tFrameworkElementFlag::PUSH_STRATEGY); 
     307  tInputPort<T> input_port("Input Port", parent); 
     308  output_port.ConnectTo(proxy_port); 
     309  proxy_port.ConnectTo(input_port); 
     310  parent->Init(); 
     311 
     312  output_port.Publish(value_to_publish); 
     313  RRLIB_UNIT_TESTS_EQUALITY(value_to_publish, *proxy_port.GetPointer()); 
     314  RRLIB_UNIT_TESTS_EQUALITY(value_to_publish, *input_port.GetPointer()); 
     315  input_port.GetWrapped()->SetHijacked(true); 
     316  RRLIB_UNIT_TESTS_EQUALITY(value_to_publish, *proxy_port.GetPointer()); 
     317  RRLIB_UNIT_TESTS_EQUALITY(value_to_publish, *input_port.GetPointer()); 
     318  output_port.Publish(default_value); 
     319  RRLIB_UNIT_TESTS_EQUALITY(default_value, *proxy_port.GetPointer()); 
     320  RRLIB_UNIT_TESTS_EQUALITY(value_to_publish, *input_port.GetPointer()); 
     321  proxy_port.GetWrapped()->SetHijacked(true); 
     322  input_port.GetWrapped()->SetHijacked(false); 
     323  output_port.Publish(value_to_publish); 
     324  RRLIB_UNIT_TESTS_EQUALITY(default_value, *proxy_port.GetPointer()); 
     325  RRLIB_UNIT_TESTS_EQUALITY(value_to_publish, *input_port.GetPointer()); 
     326  output_port.GetWrapped()->SetHijacked(true); 
     327  output_port.Publish(value_to_publish); 
     328 
     329  parent->ManagedDelete(); 
     330} 
     331 
     332template <typename T> 
     333void TestGenericPorts(T value_to_publish, T another_value) 
     334{ 
     335  core::tFrameworkElement* parent = new core::tFrameworkElement(&core::tRuntimeEnvironment::GetInstance(), "TestGenericPorts"); 
     336  T default_value = T(); 
     337  rrlib::rtti::tGenericObjectWrapper<T> default_buffer(default_value); 
     338  rrlib::rtti::tGenericObjectWrapper<T> value_buffer(value_to_publish); 
     339  rrlib::rtti::tGenericObjectWrapper<T> another_value_buffer(another_value); 
     340 
     341  tGenericPort output_port("Output Port", rrlib::rtti::tDataType<T>(), parent, core::tFrameworkElement::tFlag::EMITS_DATA | core::tFrameworkElement::tFlag::OUTPUT_PORT); 
     342  tGenericPort proxy_port("Proxy Port", rrlib::rtti::tDataType<T>(), parent, core::tFrameworkElement::tFlag::ACCEPTS_DATA | core::tFrameworkElement::tFlag::PUSH_STRATEGY | core::tFrameworkElement::tFlag::EMITS_DATA); 
     343  tGenericPort input_port("Input Port", rrlib::rtti::tDataType<T>(), parent, core::tFrameworkElement::tFlag::ACCEPTS_DATA | core::tFrameworkElement::tFlag::PUSH_STRATEGY); 
     344  output_port.ConnectTo(proxy_port); 
     345  proxy_port.ConnectTo(input_port); 
     346  parent->Init(); 
     347 
     348  // Publish by value 
     349  T get_value = T(); 
     350  rrlib::rtti::tGenericObjectWrapper<T> get_buffer(get_value); 
     351  input_port.Get(get_buffer); 
     352  RRLIB_UNIT_TESTS_EQUALITY(default_value, get_value); 
     353  output_port.Publish(value_buffer); 
     354  proxy_port.Get(get_buffer); 
     355  RRLIB_UNIT_TESTS_EQUALITY(value_to_publish, get_value); 
     356  input_port.Get(get_buffer); 
     357  RRLIB_UNIT_TESTS_EQUALITY(value_to_publish, get_value); 
     358 
     359  // Publish via buffer 
     360  auto unused_buffer = output_port.GetUnusedBuffer(); 
     361  unused_buffer->GetData<T>() = another_value; 
     362  output_port.Publish(unused_buffer); 
     363  proxy_port.Get(get_buffer); 
     364  RRLIB_UNIT_TESTS_EQUALITY(another_value, get_value); 
     365  input_port.Get(get_buffer); 
     366  RRLIB_UNIT_TESTS_EQUALITY(another_value, get_value); 
     367 
     368  parent->ManagedDelete(); 
     369} 
     370 
    240371class DataPortsTestCollection : public rrlib::util::tUnitTestSuite 
    241372{ 
    242373  RRLIB_UNIT_TESTS_BEGIN_SUITE(DataPortsTestCollection); 
    243374  RRLIB_UNIT_TESTS_ADD_TEST(Test); 
     375  RRLIB_UNIT_TESTS_ADD_TEST(PortPack); 
    244376  RRLIB_UNIT_TESTS_END_SUITE; 
    245377 
     
    251383    TestPortListeners<int>(1); 
    252384    TestPortListeners<std::string>("test"); 
     385    TestNetworkConnectionLoss<int>(4, 7); 
     386    TestNetworkConnectionLoss<std::string>("default_value", "published_value"); 
     387    TestOutOfBoundsPublish(); 
     388    TestHijackedPublishing<int>(42); 
     389    TestHijackedPublishing<std::string>("test"); 
     390    TestGenericPorts<bool>(true, false); 
     391    TestGenericPorts<std::string>("123", "45"); 
    253392 
    254393    tThreadLocalBufferManagement local_buffers; 
     
    256395    TestPortQueues<int>(1, 2, 3); 
    257396    TestPortListeners<int>(1); 
     397    TestNetworkConnectionLoss<int>(4, 7); 
     398    TestOutOfBoundsPublish(); 
     399    TestHijackedPublishing<int>(42); 
     400    TestGenericPorts<bool>(true, false); 
     401  } 
     402 
     403  void PortPack() 
     404  { 
     405    auto parent = new core::tFrameworkElement(&core::tRuntimeEnvironment::GetInstance(), "TestPortPack"); 
     406 
     407    using tTypeList = rrlib::util::tTypeList<int, double, std::string, bool>; 
     408    data_ports::tPortPack<tInputPort, tTypeList> ports(parent, "X"); 
     409 
     410    RRLIB_UNIT_TESTS_EQUALITY(tTypeList::cSIZE, ports.NumberOfPorts()); 
     411 
     412    for (size_t i = 0; i < tTypeList::cSIZE; ++i) 
     413    { 
     414      RRLIB_UNIT_TESTS_EQUALITY("X" + std::to_string(i + 1), ports.GetPort(i).GetName()); 
     415    } 
     416 
     417    std::array<std::string, tTypeList::cSIZE> names {"foo", "bar", "baz", "fnord"}; 
     418    data_ports::tPortPack<tInputPort, tTypeList> named_ports(parent, names.begin(), names.end()); 
     419 
     420    for (size_t i = 0; i < tTypeList::cSIZE; ++i) 
     421    { 
     422      RRLIB_UNIT_TESTS_EQUALITY(names[i], named_ports.GetPort(i).GetName()); 
     423    } 
    258424  } 
    259425}; 
Note: See TracChangeset for help on using the changeset viewer.