fw4spl
SExtractObj.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 "ctrlCamp/SExtractObj.hpp"
8 
9 #include <fwData/mt/ObjectWriteLock.hpp>
10 
11 #include <fwDataCamp/exception/NullPointer.hpp>
12 #include <fwDataCamp/exception/ObjectNotFound.hpp>
13 #include <fwDataCamp/getObject.hpp>
14 
15 #include <fwServices/registry/ObjectService.hpp>
16 
17 namespace ctrlCamp
18 {
19 
20 fwServicesRegisterMacro(::ctrlCamp::ICamp, ::ctrlCamp::SExtractObj);
21 
22 //-----------------------------------------------------------------------------
23 
25 {
26 
27 }
28 
29 //-----------------------------------------------------------------------------
30 
32 {
33 }
34 
35 //-----------------------------------------------------------------------------
36 
38 {
39  typedef ::fwRuntime::ConfigurationElement::sptr ConfigurationType;
40 
41  const ConfigurationType inoutCfg = m_configuration->findConfigurationElement("inout");
42  SLM_ASSERT("At one 'inout' tag is required.", inoutCfg);
43 
44  const std::vector< ConfigurationType > extractCfg = inoutCfg->find("extract");
45  SLM_ASSERT("At least one 'extract' tag is required.", !extractCfg.empty());
46 
47  const ConfigurationType outCfg = m_configuration->findConfigurationElement("out");
48 
49  const std::vector< ConfigurationType > outKeyCfg = outCfg->find("key");
50  SLM_ASSERT("You must have as many 'from' tags as 'out' keys.", extractCfg.size() == outKeyCfg.size());
51 
52  for(ConfigurationType cfg : extractCfg)
53  {
54  SLM_ASSERT("Missing attribute 'from'.", cfg->hasAttribute("from"));
55  const std::string from = cfg->getAttributeValue("from");
56 
57  m_sourcePaths.push_back(from);
58  }
59 }
60 
61 //-----------------------------------------------------------------------------
62 
64 {
65 }
66 
67 //-----------------------------------------------------------------------------
68 
70 {
71  auto sourceObject = this->getInOut< ::fwData::Object >("source");
72 
73  size_t index = 0;
74  for(auto path : m_sourcePaths)
75  {
76  const std::string from = path;
77 
78  ::fwData::Object::sptr object;
79  try
80  {
81  object = ::fwDataCamp::getObject( sourceObject, from, true );
82  }
84  {
85  this->setOutput("target", nullptr, index);
86  }
88  {
89  SLM_WARN("Object from '"+ from +"' not found");
90  }
91  catch(std::exception& e)
92  {
93  OSLM_FATAL("Unhandled exception: " << e.what());
94  }
95 
96  SLM_WARN_IF("Object from '"+ from +"' not found", !object);
97  if(object)
98  {
99  this->setOutput("target", object, index);
100  }
101  ++index;
102  }
103 }
104 
105 //-----------------------------------------------------------------------------
106 
108 {
109  // Unregister outputs
110  for (size_t i = 0; i < this->getKeyGroupSize("target"); ++i)
111  {
112  this->setOutput("target", nullptr, i);
113  }
114 }
115 
116 //-----------------------------------------------------------------------------
117 
118 } // namespace ctrlCamp
CTRLCAMP_API SExtractObj()
Constructor.
The namespace ctrlCamp contains services using camp.
Definition: ICamp.hpp:18
#define SLM_WARN(message)
Definition: spyLog.hpp:261
virtual CTRLCAMP_API void updating() override
Extract the object(s)
Definition: SExtractObj.cpp:69
This service get objects from a source object and expose them as new objects.
Definition: SExtractObj.hpp:50
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
virtual CTRLCAMP_API void starting() override
Does nothing.
Definition: SExtractObj.cpp:63
virtual CTRLCAMP_API void configuring() override
Configure the service.
Definition: SExtractObj.cpp:37
#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
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
size_t getKeyGroupSize(const KeyType &keybase) const
Return the number of key in a group of keys.
Definition: IService.hxx:119
virtual CTRLCAMP_API void stopping() override
Does nothing.
#define OSLM_FATAL(message)
Definition: spyLog.hpp:285
CTRLCAMP_API ~SExtractObj()
Destructor.
Definition: SExtractObj.cpp:31
#define SLM_WARN_IF(message, cond)
Definition: spyLog.hpp:265