fw4spl
SIncrementArray.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "Tuto16MultithreadConsole/SIncrementArray.hpp"
8 
9 #include <fwCom/Signal.hxx>
10 #include <fwCom/Slots.hxx>
11 
12 #include <fwData/Array.hpp>
13 #include <fwData/mt/ObjectWriteLock.hpp>
14 
15 #include <fwDataTools/helper/Array.hpp>
16 
17 #include <fwServices/macros.hpp>
18 
19 #include <fwThread/Timer.hpp>
20 
21 #include <functional>
22 
24 
26 {
27 
28 static const ::fwCom::Slots::SlotKeyType s_START_TIMER_SLOT = "startTimer";
29 
30 //------------------------------------------------------------------------------
31 
32 SIncrementArray::SIncrementArray() noexcept :
33  m_periodInMillisec(500)
34 {
35  newSlot(s_START_TIMER_SLOT, &SIncrementArray::startTimer, this);
36 }
37 
38 //------------------------------------------------------------------------------
39 
40 SIncrementArray::~SIncrementArray() noexcept
41 {
42 }
43 
44 //------------------------------------------------------------------------------
45 
47 {
48  m_timer = m_associatedWorker->createTimer();
49  m_timer->setFunction( std::bind(&SIncrementArray::updating, this) );
50  m_timer->setDuration( std::chrono::milliseconds(m_periodInMillisec) );
51 }
52 
53 //------------------------------------------------------------------------------
54 
56 {
57  if (m_timer->isRunning())
58  {
59  m_timer->stop();
60  }
61  m_timer.reset();
62 }
63 
64 //------------------------------------------------------------------------------
65 
67 {
68  ::fwData::Array::sptr array = this->getInOut< ::fwData::Array >("array");
69  ::fwData::mt::ObjectWriteLock writeLock(array);
70 
71  SLM_ASSERT("No array.", array);
72  SLM_ASSERT("Array : bad number of dimensions.", array->getNumberOfDimensions() == 1 );
73 
74  const size_t arraySize = array->getSize()[0];
75 
76  ::fwDataTools::helper::Array arrayHelper(array);
77 
78  unsigned int* buffer = static_cast< unsigned int* >( arrayHelper.getBuffer() );
79 
80  // Increment the array values
81  for (size_t i = 0; i < arraySize; i++)
82  {
83  ++buffer[i];
84  }
85 
86  // Notify that the array is modified
87  ::fwData::Object::ModifiedSignalType::sptr sig
89  {
90  ::fwCom::Connection::Blocker blockUpdate(sig->getConnection(m_slotUpdate));
91  ::fwCom::Connection::Blocker blockTimer(sig->getConnection(this->slot(s_START_TIMER_SLOT)));
92  sig->asyncEmit();
93  }
94 
95 }
96 
97 //------------------------------------------------------------------------------
98 
100 {
101 
102 }
103 
104 //------------------------------------------------------------------------------
105 
106 void SIncrementArray::startTimer()
107 {
108  m_timer->start();
109 }
110 
111 //------------------------------------------------------------------------------
112 
113 } // namespace Tuto16MultithreadConsole
Class allowing to block a Connection.
Definition: Connection.hpp:20
virtual FWDATATOOLS_API void * getBuffer()
Getter for the array buffer.
A helper to lock object on exclusive mode.
virtual void configuring() override
Do nothing.
virtual void starting() override
Initialize the timer.
UpdateSlotType::sptr m_slotUpdate
Slot to call update method.
Definition: IService.hpp:690
virtual void updating() override
Increment all the values of the current array.
Provides a way to manage a view on a multidimentionnal array.
This interface defines control service API. Does nothing particularly, can be considered as a default...
Definition: IController.hpp:23
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
This service increments all the values of a fwData::Array.
virtual void stopping() override
Stop and reset the timer.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
std::shared_ptr< ::fwThread::Worker > m_associatedWorker
Associated worker.
Definition: IService.hpp:699
The namespace Tuto16MultithreadConsole contains services to run the Tuto16MultithreadConsole example...
Helper to manage array buffer. Lock the buffer before to modify it.