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