Changeset 102:c325ed4d7e3e in finroc_plugins_data_ports


Ignore:
Timestamp:
09.12.2015 23:29:52 (4 years ago)
Author:
Max Reichardt <mreichardt@…>
Branch:
default
Phase:
public
Message:

Added publish operation for tPortDataPointers to tGenericPort class. Futhermore, added unit test for generic ports.

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • api/tGenericPortImplementation.h

    r69 r102  
    9494   * Gets Port's current value 
    9595   * 
    96    * (Note that numbers and "cheap copy" types also have a method: T GetValue();  (defined in tPortParent<T>)) 
    97    * 
     96   * \param port Wrapped port to operate on 
    9897   * \param result Buffer to (deep) copy port's current value to 
    99    * (Using this get()-variant is more efficient when using CC types, but can be extremely costly with large data types) 
     98   * \param timestamp Object to store timestamp in 
     99   * 
     100   * (Using this get()-variant is more efficient when using cheaply-copied types, but can be costly with large data types) 
    100101   */ 
    101102  virtual void Get(core::tAbstractPort& port, rrlib::rtti::tGenericObject& result, rrlib::time::tTimestamp& timestamp) = 0; 
     
    104105   * Gets Port's current value buffer 
    105106   * 
     107   * \param port Wrapped port to operate on 
    106108   * \param strategy Strategy to use for get operation 
    107109   * \return Buffer with port's current value with read lock. 
     
    110112 
    111113  /*! 
     114   * \param port Wrapped port to operate on 
    112115   * \return Port's default value (NULL if none has been set) 
    113116   */ 
     
    133136 
    134137  /*! 
     138   * \param port Wrapped port to operate on 
    135139   * \return Unused buffer. 
     140   * 
    136141   * Buffers to be published using this port should be acquired using this function. 
    137142   * The buffer might contain old data, so it should be cleared prior to using. 
     
    163168   * Should only be called on output ports. 
    164169   * 
     170   * \param port Wrapped port to operate on 
    165171   * \param data Data to publish. It will be deep-copied. 
    166    * This publish()-variant is efficient when using CC types, but can be extremely costly with large data types) 
     172   * \param timestamp Timestamp to attach to data 
     173   * 
     174   * This publish()-variant is efficient when using cheaply-copied types, but can be costly with large data types) 
    167175   */ 
    168176  virtual void Publish(core::tAbstractPort& port, const rrlib::rtti::tGenericObject& data, const rrlib::time::tTimestamp& timestamp) = 0; 
     177 
     178  /*! 
     179   * Publish Data Buffer. This data will be forwarded to any connected ports. 
     180   * Should only be called on output ports. 
     181   * 
     182   * \param port Wrapped port to operate on 
     183   * \param data_buffer Data to publish. 
     184   * 
     185   * This publish()-variant is efficient with all data types. 
     186   */ 
     187  inline void Publish(core::tAbstractPort& port, common::tReferenceCountingBufferManager* data_buffer) 
     188  { 
     189    if (IsCheaplyCopiedType(port.GetDataType())) 
     190    { 
     191      tCheapCopyPort& cc_port = static_cast<tCheapCopyPort&>(port); 
     192      if (optimized::tThreadLocalBufferPools::Get()) 
     193      { 
     194        common::tPublishOperation<optimized::tCheapCopyPort, typename optimized::tCheapCopyPort::tPublishingDataThreadLocalBuffer> publish_operation(static_cast<optimized::tThreadLocalBufferManager*>(data_buffer), true); 
     195        publish_operation.Execute<false, tChangeStatus::CHANGED, false, false>(cc_port); 
     196      } 
     197      else 
     198      { 
     199        optimized::tCheapCopyPort::tUnusedManagerPointer pointer(static_cast<optimized::tCheaplyCopiedBufferManager*>(data_buffer)); 
     200        common::tPublishOperation<optimized::tCheapCopyPort, typename optimized::tCheapCopyPort::tPublishingDataGlobalBuffer> publish_operation(pointer); 
     201        publish_operation.Execute<false, tChangeStatus::CHANGED, false, false>(cc_port); 
     202      } 
     203    } 
     204    else 
     205    { 
     206      standard::tStandardPort::tUnusedManagerPointer buffer(static_cast<standard::tPortBufferManager*>(data_buffer)); 
     207      assert(buffer->IsUnused()); 
     208      static_cast<standard::tStandardPort&>(port).Publish(buffer); 
     209    } 
     210  } 
    169211 
    170212  /*! 
     
    172214   * (This is not thread-safe and must only be done in "pause mode") 
    173215   * 
    174    * \param b New Bounds 
     216   * \param port Wrapped port to operate on 
     217   * \param min Minimum value 
     218   * \param max Maximum value 
    175219   */ 
    176220  virtual void SetBounds(core::tAbstractPort& port, const rrlib::rtti::tGenericObject& min, const rrlib::rtti::tGenericObject& max) = 0; 
    177221 
    178222  /*! 
     223   * \param port Wrapped port to operate on 
    179224   * \param pull_request_handler Object that handles any incoming pull requests - null if there is none (typical case) 
    180225   */ 
  • tGenericPort.h

    r101 r102  
    282282 
    283283  /*! 
     284   * Publish Data Buffer. This data will be forwarded to any connected ports. 
     285   * Should only be called on output ports. 
     286   * 
     287   * \param buffer Buffer with data to publish - typically acquired with GetUnuserBuffer() 
     288   */ 
     289  inline void Publish(tPortDataPointer<rrlib::rtti::tGenericObject>& buffer) 
     290  { 
     291    implementation->Publish(*GetWrapped(), buffer.implementation.Release()); 
     292  } 
     293 
     294  /*! 
    284295   * Set new bounds 
    285296   * (This is not thread-safe and must only be done in "pause mode") 
    286297   * 
    287    * \param b New Bounds 
     298   * \param min Minimum value 
     299   * \param max Maximum value 
    288300   */ 
    289301  inline void SetBounds(const rrlib::rtti::tGenericObject& min, const rrlib::rtti::tGenericObject& max) 
  • tests/test_collection.cpp

    r93 r102  
    325325} 
    326326 
     327template <typename T> 
     328void TestGenericPorts(T value_to_publish, T another_value) 
     329{ 
     330  core::tFrameworkElement* parent = new core::tFrameworkElement(&core::tRuntimeEnvironment::GetInstance(), "TestGenericPorts"); 
     331  T default_value = T(); 
     332  rrlib::rtti::tGenericObjectWrapper<T> default_buffer(default_value); 
     333  rrlib::rtti::tGenericObjectWrapper<T> value_buffer(value_to_publish); 
     334  rrlib::rtti::tGenericObjectWrapper<T> another_value_buffer(another_value); 
     335 
     336  tGenericPort output_port("Output Port", rrlib::rtti::tDataType<T>(), parent, core::tFrameworkElement::tFlag::EMITS_DATA | core::tFrameworkElement::tFlag::OUTPUT_PORT); 
     337  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); 
     338  tGenericPort input_port("Input Port", rrlib::rtti::tDataType<T>(), parent, core::tFrameworkElement::tFlag::ACCEPTS_DATA | core::tFrameworkElement::tFlag::PUSH_STRATEGY); 
     339  output_port.ConnectTo(proxy_port); 
     340  proxy_port.ConnectTo(input_port); 
     341  parent->Init(); 
     342 
     343  // Publish by value 
     344  T get_value = T(); 
     345  rrlib::rtti::tGenericObjectWrapper<T> get_buffer(get_value); 
     346  input_port.Get(get_buffer); 
     347  RRLIB_UNIT_TESTS_EQUALITY(default_value, get_value); 
     348  output_port.Publish(value_buffer); 
     349  proxy_port.Get(get_buffer); 
     350  RRLIB_UNIT_TESTS_EQUALITY(value_to_publish, get_value); 
     351  input_port.Get(get_buffer); 
     352  RRLIB_UNIT_TESTS_EQUALITY(value_to_publish, get_value); 
     353 
     354  // Publish via buffer 
     355  auto unused_buffer = output_port.GetUnusedBuffer(); 
     356  unused_buffer->GetData<T>() = another_value; 
     357  output_port.Publish(unused_buffer); 
     358  proxy_port.Get(get_buffer); 
     359  RRLIB_UNIT_TESTS_EQUALITY(another_value, get_value); 
     360  input_port.Get(get_buffer); 
     361  RRLIB_UNIT_TESTS_EQUALITY(another_value, get_value); 
     362 
     363  parent->ManagedDelete(); 
     364} 
     365 
    327366class DataPortsTestCollection : public rrlib::util::tUnitTestSuite 
    328367{ 
     
    343382    TestHijackedPublishing<int>(42); 
    344383    TestHijackedPublishing<std::string>("test"); 
     384    TestGenericPorts<bool>(true, false); 
     385    TestGenericPorts<std::string>("123", "45"); 
    345386 
    346387    tThreadLocalBufferManagement local_buffers; 
     
    351392    TestOutOfBoundsPublish(); 
    352393    TestHijackedPublishing<int>(42); 
     394    TestGenericPorts<bool>(true, false); 
    353395  } 
    354396}; 
Note: See TracChangeset for help on using the changeset viewer.