fw4spl
ConfigurationElement.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 "fwRuntime/ConfigurationElement.hpp"
8 
9 #include "fwRuntime/Bundle.hpp"
10 #include "fwRuntime/IExecutable.hpp"
11 #include "fwRuntime/RuntimeException.hpp"
12 
13 #include <fwCore/base.hpp>
14 
15 namespace fwRuntime
16 {
17 
18 //------------------------------------------------------------------------------
19 
20 std::ostream& operator<<(std::ostream& _sstream, ConfigurationElement& _configurationElement)
21 {
22  _sstream << "Configuration element " << _configurationElement.getName() << " value = " <<
23  _configurationElement.getValue() << std::endl;
24  for(ConfigurationElement::AttributeContainer::iterator iter = _configurationElement.m_attributes.begin();
25  iter != _configurationElement.m_attributes.end(); ++iter )
26  {
27  _sstream << "Id = " << iter->first << " with value " << iter->second << std::endl;
28  }
29  _sstream << "Subelement : " << std::endl;
30  for(ConfigurationElementContainer::Container::iterator iter = _configurationElement.begin();
31  iter != _configurationElement.end(); ++iter )
32  {
33 
34  _sstream << std::endl << *(*iter) << std::endl;
35  }
36 
37  return _sstream;
38 }
39 
40 //------------------------------------------------------------------------------
41 
42 ConfigurationElement::ConfigurationElement( const std::shared_ptr< Bundle > bundle, const std::string& name ) :
43  m_bundle( bundle ),
44  m_name( name ),
45  m_value( std::string("") )
46 {
47 }
48 
49 //------------------------------------------------------------------------------
50 
51 const std::shared_ptr<Bundle> ConfigurationElement::getBundle() const noexcept
52 {
53  return m_bundle.lock();
54 }
55 
56 //------------------------------------------------------------------------------
57 
58 const std::string ConfigurationElement::getAttributeValue(const std::string& name) const noexcept
59 {
60  AttributeContainer::const_iterator foundPos = m_attributes.find(name);
61  return foundPos == m_attributes.end() ? std::string() : foundPos->second;
62 }
63 
64 //------------------------------------------------------------------------------
65 
66 const std::string ConfigurationElement::getExistingAttributeValue(const std::string& name) const
67 {
68  AttributeContainer::const_iterator foundPos = m_attributes.find(name);
69  if(foundPos == m_attributes.end())
70  {
71  FW_RAISE_EXCEPTION(NoSuchAttribute(name));
72  }
73  return foundPos->second;
74 }
75 
76 //------------------------------------------------------------------------------
77 
79 noexcept
80 {
81  AttributeContainer::const_iterator foundPos = m_attributes.find(name);
82  if(foundPos == m_attributes.end())
83  {
84  return AttributePair(false, std::string());
85  }
86  else
87  {
88  return AttributePair(true, foundPos->second);
89  }
90 }
91 
92 //------------------------------------------------------------------------------
93 
94 const std::string ConfigurationElement::getName() const noexcept
95 {
96  return m_name;
97 }
98 
99 //------------------------------------------------------------------------------
100 
101 const std::string ConfigurationElement::getValue() const noexcept
102 {
103  return m_value;
104 }
105 
106 //------------------------------------------------------------------------------
107 
108 bool ConfigurationElement::hasAttribute(const std::string& name) const noexcept
109 {
110  AttributeContainer::const_iterator foundPos = m_attributes.find(name);
111  return foundPos != m_attributes.end();
112 }
113 
114 //------------------------------------------------------------------------------
115 
116 const std::map<std::string, std::string> ConfigurationElement::getAttributes() const noexcept
117 {
118  return m_attributes;
119 }
120 
121 //------------------------------------------------------------------------------
122 
123 void ConfigurationElement::setAttributeValue(const std::string& name, const std::string& value) noexcept
124 {
125  m_attributes[name] = value;
126 }
127 
128 //------------------------------------------------------------------------------
129 
130 void ConfigurationElement::setValue(const std::string& value) noexcept
131 {
132  m_value = value;
133 }
134 
135 //------------------------------------------------------------------------------
136 
137 void ConfigurationElement::operator=(const ConfigurationElement&) noexcept
138 {
139 }
140 
141 //------------------------------------------------------------------------------
142 
143 std::vector < ConfigurationElement::sptr > ConfigurationElement::find(
144  std::string name,
145  std::string attribute,
146  std::string attributeValue,
147  int depth
148  )
149 {
150  typedef std::vector < ConfigurationElement::sptr > ConfVector;
151  ConfVector result;
152 
153  bool nameOk = (name.empty() || this->getName() == name);
154  bool attributeOk = (attribute.empty() || this->hasAttribute(attribute));
155  bool attributeValueOk =
156  (attributeValue.empty() ||
157  (this->hasAttribute(attribute) && this->getAttributeValue(attribute) == attributeValue));
158  if (nameOk && attributeOk && attributeValueOk)
159  {
160  result.push_back(this->shared_from_this());
161  }
162 
163  if (depth != 0)
164  {
166  for (iter = this->begin(); iter != this->end(); ++iter)
167  {
168  ConfVector deepConf;
169  deepConf = (*iter)->find(name, attribute, attributeValue, depth-1);
170  result.insert(result.end(), deepConf.begin(), deepConf.end());
171  }
172  }
173 
174  return result;
175 }
176 
177 //-----------------------------------------------------------------------------
178 
180 {
181 }
182 
183 //-----------------------------------------------------------------------------
184 
185 } // namespace fwRuntime
FWRUNTIME_API const std::map< std::string, std::string > getAttributes() const noexcept
Return the map with attributes.
FWRUNTIME_API Iterator begin()
Retrieves the iterator on the first managed configuration element.
FWRUNTIME_API void setValue(const std::string &value) noexcept
Sets the value of the configuration element it-self.
virtual FWRUNTIME_API ~ConfigurationElement()
Used only because this class inherit from enable_shared_from_this.
STL namespace.
FWRUNTIME_API bool hasAttribute(const std::string &name) const noexcept
Tells if the specified attributes exists.
std::pair< bool, std::string > AttributePair
Defines the attribute pair type.
Defines the configuration element class.
FWRUNTIME_API std::vector< ConfigurationElement::sptr > find(std::string name="", std::string attribute="", std::string attributeValue="", int depth=1)
Find recursively all the corresponding configuration elements.
FWRUNTIME_API const std::string getExistingAttributeValue(const std::string &name) const
Retrieves the value of an attribute for the specified name.
FWRUNTIME_API const std::string getAttributeValue(const std::string &name) const noexcept
Retrieves the value of an attribute for the specified name.
FWRUNTIME_API const std::string getName() const noexcept
Retrieves the name of the configuration element.
The namespace fwRuntime contains classes to manage bundle, configuration element, extension point in ...
FWRUNTIME_API const AttributePair getSafeAttributeValue(const std::string &name) const noexcept
Retrieves the value of an existing attribute for the specified name.
FWRUNTIME_API void setAttributeValue(const std::string &name, const std::string &value) noexcept
Sets an attribute with the specified name and value.
FWRUNTIME_API ConfigurationElement(const std::shared_ptr< Bundle > bundle, const std::string &name)
Constructor.
FWRUNTIME_API const std::shared_ptr< Bundle > getBundle() const noexcept
Retrieves the bundle the configuration element is attached to.
FWRUNTIME_API Iterator end()
Retrieves the iterator on the end of the configuration element container.
Container::iterator Iterator
Defines the configuration element container type.
FWRUNTIME_API const std::string getValue() const noexcept
Retrieves the configuration element value.
Defines the a class for attributes exceptions.