fw4spl
Bundles/core/dataReg/src/dataReg/parser/Object.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 "dataReg/parser/Object.hpp"
8 
9 #include <fwServices/macros.hpp>
10 
11 #include <boost/foreach.hpp>
12 
13 fwServicesRegisterMacro( ::fwServices::IXMLParser, ::dataReg::parser::Object, ::fwData::Object );
14 
15 namespace dataReg
16 {
17 namespace parser
18 {
19 
20 //------------------------------------------------------------------------------
21 
22 Object::Object( )
23 {
24 }
25 
26 //------------------------------------------------------------------------------
27 
28 Object::~Object()
29 {
30 }
31 
32 //------------------------------------------------------------------------------
33 
34 bool Object::refObjectValidator( ::fwRuntime::ConfigurationElement::csptr _cfgElement )
35 {
36  bool isOk = true;
37 
38  for( ::fwRuntime::ConfigurationElement::csptr elem : _cfgElement->getElements() )
39  {
40  std::string subElementName = elem->getName();
41  if( subElementName != "service" &&
42  subElementName != "serviceList" )
43  {
44  OSLM_ERROR(
45  "xml subelement \""<< subElementName <<
46  "\" for element object is not supported for the moment when you use a reference on item composite.");
47  isOk = false;
48  }
49  }
50 
51  return isOk;
52 }
53 
54 //------------------------------------------------------------------------------
55 
57 {
58  SLM_FATAL("This method is deprecated, and thus, shouldn't be used.");
59 }
60 
61 //------------------------------------------------------------------------------
62 
63 void Object::createConfig( ::fwTools::Object::sptr _obj )
64 {
65  // Declaration of attributes values
66  const std::string OBJECT_BUILD_MODE = "src";
67  const std::string BUILD_OBJECT = "new";
68  const std::string GET_OBJECT = "ref";
69 
70  ::fwData::Object::sptr associatedObject = ::fwData::Object::dynamicCast(_obj);
71  SLM_ASSERT("associatedObject not instanced", associatedObject);
72 
73  for( ::fwRuntime::ConfigurationElement::csptr elem : m_cfg->getElements() )
74  {
75 
76  if( elem->getName() == "item" )
77  {
78  // Test build mode
79  std::string buildMode = BUILD_OBJECT;
80 
81  if ( elem->hasAttribute( OBJECT_BUILD_MODE ) )
82  {
83  buildMode = elem->getExistingAttributeValue( OBJECT_BUILD_MODE );
84  OSLM_ASSERT( "The buildMode \""<< buildMode <<"\" is not supported, it should either be BUILD_OBJECT "
85  "or GET_OBJECT.",
86  buildMode == BUILD_OBJECT || buildMode == GET_OBJECT );
87  }
88 
89  SLM_ASSERT( "The xml element \"item\" must have an attribute named \"key\" .",
90  elem->hasAttribute("key") );
91  std::string key = elem->getExistingAttributeValue("key");
92  SLM_ASSERT( "The xml element \"item\" must have an attribute named \"key\" whick is not empty.",
93  !key.empty() );
94  SLM_ASSERT( "The xml element \"item\" must have one (and only one) xml sub-element \"object\".",
95  elem->size() == 1 && (*(elem->getElements().begin()))->getName() == "object" );
96 
97  if( buildMode == BUILD_OBJECT )
98  {
99  // Test if key already exist in object
100  OSLM_ASSERT("The key "<< key <<" already exists in the object.", !associatedObject->getField(
101  key ) );
102 
103  // Create and manage object config
104  ::fwServices::IAppConfigManager::sptr ctm = ::fwServices::IAppConfigManager::New();
105  ctm->::fwServices::IAppConfigManager::setConfig( elem );
106 
107  m_ctmContainer.push_back( ctm );
108  ctm->create();
109  ::fwData::Object::sptr localObj = ctm->getConfigRoot();
110 
111  // Add object
112  associatedObject->setField(key, localObj);
113  }
114  else // if( buildMode == GET_OBJECT )
115  {
116  SLM_FATAL("ACH => Todo");
117  // ToDo
118  }
119  }
120  }
121 }
122 
123 //------------------------------------------------------------------------------
124 
125 void Object::startConfig()
126 {
127  for( ::fwServices::IAppConfigManager::sptr ctm : m_ctmContainer )
128  {
129  ctm->start();
130  }
131 }
132 
133 //------------------------------------------------------------------------------
134 
135 void Object::updateConfig()
136 {
137  for( ::fwServices::IAppConfigManager::sptr ctm : m_ctmContainer )
138  {
139  ctm->update();
140  }
141 }
142 
143 //------------------------------------------------------------------------------
144 
145 void Object::stopConfig()
146 {
147  BOOST_REVERSE_FOREACH( ::fwServices::IAppConfigManager::sptr ctm, m_ctmContainer )
148  {
149  ctm->stop();
150  }
151 }
152 
153 //------------------------------------------------------------------------------
154 
155 void Object::destroyConfig()
156 {
157  BOOST_REVERSE_FOREACH( ::fwServices::IAppConfigManager::sptr ctm, m_ctmContainer )
158  {
159  ctm->destroy();
160  }
161  m_ctmContainer.clear();
162 }
163 
164 //------------------------------------------------------------------------------
165 
166 } //namespace parser
167 } //namespace dataReg
168 
#define OSLM_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:310
Specific fwTools::Object parser. Does nothing as no IService method is specialized: IXMLParser base c...
The namespace dataReg (data registration) contains classes which allow to parse the xml configuration...
#define SLM_FATAL(message)
Definition: spyLog.hpp:283
#define OSLM_ERROR(message)
Definition: spyLog.hpp:274
void updating() override
Parse the XML configuration.
#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
Base class for each data object.
static FWSERVICES_API std::shared_ptr< IAppConfigManager > New()
Service type for the construction of an object and associated services from an XML-based description...
Definition: IXMLParser.hpp:36