fw4spl
SCopy.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
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 "ctrlCamp/SCopy.hpp"
8 
9 #include <fwCom/Signal.hpp>
10 #include <fwCom/Signal.hxx>
11 #include <fwCom/Signals.hpp>
12 
13 #include <fwData/mt/ObjectWriteLock.hpp>
14 
15 #include <fwDataCamp/exception/ObjectNotFound.hpp>
16 #include <fwDataCamp/getObject.hpp>
17 
18 namespace ctrlCamp
19 {
20 
21 fwServicesRegisterMacro(::ctrlCamp::ICamp, ::ctrlCamp::SCopy, ::fwData::Object);
22 
23 const ::fwServices::IService::KeyType s_SOURCE_INPUT = "source";
24 const ::fwServices::IService::KeyType s_TARGET_INOUT = "target";
25 
26 //-----------------------------------------------------------------------------
27 
29  m_mode(ModeType::UPDATE)
30 {
31 
32 }
33 
34 //-----------------------------------------------------------------------------
35 
37 {
38 }
39 
40 //-----------------------------------------------------------------------------
41 
43 {
44  typedef ::fwRuntime::ConfigurationElement::sptr ConfigurationType;
45 
46  m_hasExtractTag = false;
47  const ConfigurationType inCfg = m_configuration->findConfigurationElement("in");
48  SLM_ASSERT("One 'in' tag is required.", inCfg);
49 
50  const std::vector< ConfigurationType > inoutCfg = m_configuration->find("inout");
51  const std::vector< ConfigurationType > outCfg = m_configuration->find("out");
52  SLM_ASSERT("One 'inout' or one 'out' tag is required.", inoutCfg.size() + outCfg.size() == 1);
53 
54  const std::vector< ConfigurationType > extractCfg = inCfg->find("extract");
55  SLM_ASSERT("Only one 'extract' tag is authorized.", extractCfg.size() <= 1);
56  if (extractCfg.size() == 1)
57  {
58  ConfigurationType cfg = extractCfg[0];
59  SLM_ASSERT("Missing attribute 'from'.", cfg->hasAttribute("from"));
60  m_path = cfg->getAttributeValue("from");
61  m_hasExtractTag = true;
62  }
63 
64  const ConfigurationType modeConfig = m_configuration->findConfigurationElement("mode");
65  if (modeConfig)
66  {
67  auto mode = modeConfig->getValue();
68  if(mode == "copyOnStart")
69  {
70  m_mode = ModeType::START;
71  }
72  else if(mode == "copyOnUpdate")
73  {
74  m_mode = ModeType::UPDATE;
75  }
76  else
77  {
78  SLM_ERROR("Mode " + mode + " unknown. It should be either 'copyOnStart' or 'copyOnUpdate'");
79  }
80  }
81 }
82 
83 //-----------------------------------------------------------------------------
84 
86 {
87  if(m_mode == ModeType::START)
88  {
89  this->copy();
90  }
91 }
92 
93 //-----------------------------------------------------------------------------
94 
96 {
97  if(m_mode == ModeType::UPDATE)
98  {
99  this->copy();
100  }
101  else
102  {
103  SLM_ERROR("Object copy was request but the mode is to 'copyOnStart'");
104  }
105 }
106 
107 //-----------------------------------------------------------------------------
108 
110 {
111  // Unregister output
112  this->setOutput(s_TARGET_INOUT, nullptr);
113 }
114 
115 //-----------------------------------------------------------------------------
116 
117 void SCopy::copy()
118 {
119  bool create = false;
120  ::fwData::Object::sptr target;
121  ::fwData::Object::csptr source;
122  target = this->getInOut< ::fwData::Object >(s_TARGET_INOUT);
123  if(!target)
124  {
125  create = true;
126  }
127 
128  ::fwData::Object::csptr sourceObject = this->getInput< ::fwData::Object >(s_SOURCE_INPUT);
129  if (m_hasExtractTag)
130  {
131  ::fwData::Object::sptr object;
132  try
133  {
134  object = ::fwDataCamp::getObject( sourceObject, m_path, true );
135  }
137  {
138  SLM_WARN("Object from '"+ m_path +"' not found");
139  }
140  catch(std::exception& e)
141  {
142  OSLM_FATAL("Unhandled exception: " << e.what());
143  }
144 
145  SLM_WARN_IF("Object from '"+ m_path +"' not found", !object);
146  if(object)
147  {
148  source = object;
149  }
150  }
151  else
152  {
153  source = sourceObject;
154  }
155 
156  if(source)
157  {
158  if(create)
159  {
160  target = ::fwData::Object::copy(source);
161  this->setOutput(s_TARGET_INOUT, target);
162  }
163  else
164  {
165  ::fwData::mt::ObjectWriteLock lock(target);
166 
167  // copy the object
168  target->deepCopy(source);
169 
171  {
172  ::fwCom::Connection::Blocker block(sig->getConnection(m_slotUpdate));
173  sig->asyncEmit();
174  }
175  }
176  }
177 }
178 
179 //-----------------------------------------------------------------------------
180 
181 } // namespace ctrlCamp
virtual CTRLCAMP_API void starting() override
Does nothing.
Definition: SCopy.cpp:85
CTRLCAMP_API SCopy()
Constructor.
Definition: SCopy.cpp:28
Class allowing to block a Connection.
Definition: Connection.hpp:20
The namespace ctrlCamp contains services using camp.
Definition: ICamp.hpp:18
A helper to lock object on exclusive mode.
virtual CTRLCAMP_API void updating() override
Does nothing.
Definition: SCopy.cpp:95
#define SLM_WARN(message)
Definition: spyLog.hpp:261
UpdateSlotType::sptr m_slotUpdate
Slot to call update method.
Definition: IService.hpp:690
#define SLM_ERROR(message)
Definition: spyLog.hpp:272
FWSERVICES_API void setOutput(const ::fwServices::IService::KeyType &key, const ::fwData::Object::sptr &object, size_t index=0)
Register an output object at a given key in the OSR, replacing it if it already exists.
Definition: IService.cpp:80
This interface defines service API. It can be considered as a default type for services using fwCamp...
Definition: ICamp.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
static FWDATA_API::fwData::Object::sptr copy(const ::fwData::Object::csptr &source)
return a copy of the source. if source is a null pointer, return a null pointer.
Base class for each data object.
virtual CTRLCAMP_API void configuring() override
Configure the service before starting. Apply the configuration to service.
Definition: SCopy.cpp:42
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
#define OSLM_FATAL(message)
Definition: spyLog.hpp:285
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
This service copies an object and updates its own object or the defined target. It can either copy th...
Definition: SCopy.hpp:46
virtual CTRLCAMP_API void stopping() override
Does nothing.
Definition: SCopy.cpp:109
CTRLCAMP_API ~SCopy()
Destructor.
Definition: SCopy.cpp:36
#define SLM_WARN_IF(message, cond)
Definition: spyLog.hpp:265