fw4spl
SrcLib/core/fwAtomsPatch/src/fwAtomsPatch/helper/Object.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
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 "fwAtomsPatch/infos/log.hpp"
8 #include "fwAtomsPatch/helper/functions.hpp"
9 #include "fwAtomsPatch/helper/Object.hpp"
10 
11 namespace fwAtomsPatch
12 {
13 namespace helper
14 {
15 
16 Object::Object(::fwAtoms::Object::sptr metaObject) : m_object(metaObject)
17 {
18 }
19 
20 // ----------------------------------------------------------------------------
21 
23 {
24 }
25 
26 // ----------------------------------------------------------------------------
27 
28 void Object::addAttribute(const std::string& name,
29  ::fwAtoms::Base::sptr value,
30  conditions::Abstract::sptr condition)
31 {
32  if(m_object->getAttributes().find(name) == m_object->getAttributes().end())
33  {
34  if(condition->test(value))
35  {
36  m_object->setAttribute(name, value);
37  fwAtomsPatchAddAttributeLogMacro("'"
38  + ::fwAtomsPatch::helper::getClassname(m_object) + "|"
39  + ::fwAtomsPatch::helper::getVersion(m_object) + "' : '" + name + "'");
40  }
41  }
42  else
43  {
44  fwAtomsPatchErrorLogMacro("Attribute '" + name + "' already exists");
45  }
46 }
47 
48 // ----------------------------------------------------------------------------
49 
50 void Object::addOrReplaceAttribute(const std::string& name,
51  ::fwAtoms::Base::sptr value,
52  conditions::Abstract::sptr condition)
53 {
54  if(m_object->getAttributes().find(name) != m_object->getAttributes().end())
55  {
56  m_object->eraseAttribute(name);
57  fwAtomsPatchEraseAttributeLogMacro("'"
58  + ::fwAtomsPatch::helper::getClassname(m_object) + "|"
59  + ::fwAtomsPatch::helper::getVersion(m_object) + "' : '" + name + "'");
60  }
61 
62  if(condition->test(value))
63  {
64  m_object->setAttribute(name, value);
65  fwAtomsPatchAddAttributeLogMacro("'"
66  + ::fwAtomsPatch::helper::getClassname(m_object) + "|"
67  + ::fwAtomsPatch::helper::getVersion(m_object) + "' : '" + name + "'");
68  }
69 }
70 
71 // ----------------------------------------------------------------------------
72 
73 void Object::removeAttribute(const std::string& name,
74  conditions::Abstract::sptr condition)
75 {
76  ::fwAtoms::Object::AttributesType::const_iterator it = m_object->getAttributes().find(name);
77 
78  if(it != m_object->getAttributes().end())
79  {
80  if(condition->test(it->second))
81  {
82  m_object->eraseAttribute(name);
83  fwAtomsPatchEraseAttributeLogMacro("'"
84  + ::fwAtomsPatch::helper::getClassname(m_object) + "|"
85  + ::fwAtomsPatch::helper::getVersion(m_object) + "' : '" + name + "'");
86  }
87  }
88  else
89  {
90  fwAtomsPatchErrorLogMacro("Missing attribute '" + name + "'");
91  }
92 }
93 
94 // ----------------------------------------------------------------------------
95 
96 void Object::replaceAttribute(const std::string& name,
97  ::fwAtoms::Base::sptr newValue,
98  conditions::Abstract::sptr condition)
99 {
100  ::fwAtoms::Object::AttributesType::const_iterator cIt = m_object->getAttributes().find(name);
101 
102  if(cIt != m_object->getAttributes().end())
103  {
104  fwAtomsPatchReplaceAttributeLogMacro("'"
105  + ::fwAtomsPatch::helper::getClassname(m_object) + "|"
106  + ::fwAtomsPatch::helper::getVersion(m_object) + "' : '" + name + "'");
107 
108  if(condition->test(cIt->second, newValue))
109  {
110  m_object->setAttribute(name, newValue);
111  }
112  else
113  {
114  m_object->setAttribute(name, condition->getDefaultValue());
115  fwAtomsPatchOutOfRangeLogMacro("Value '" + name
116  + "' is out of range, using default value : '"
117  + condition->getDefaultValue()->getString() + "'");
118  }
119  }
120  else
121  {
122  fwAtomsPatchErrorLogMacro("Missing attribute '" + name + "'");
123  }
124 }
125 
126 // ----------------------------------------------------------------------------
127 
128 void Object::renameAttribute(const std::string& name, const std::string& newName,
129  conditions::Abstract::sptr condition)
130 {
131  ::fwAtoms::Object::AttributesType::const_iterator it = m_object->getAttributes().find(name);
132 
133  if(it != m_object->getAttributes().end())
134  {
135  if(condition->test(it->second))
136  {
137  ::fwAtoms::Base::sptr base = m_object->getAttribute(name);
138  fwAtomsPatchEraseAttributeLogMacro("'"
139  + ::fwAtomsPatch::helper::getClassname(m_object) + "|"
140  + ::fwAtomsPatch::helper::getVersion(m_object) + "' : '" + name + "'");
141  fwAtomsPatchAddAttributeLogMacro("'"
142  + ::fwAtomsPatch::helper::getClassname(m_object) + "|"
143  + ::fwAtomsPatch::helper::getVersion(m_object) + "' : '" + newName + "'");
144  m_object->setAttribute(newName, base);
145  m_object->eraseAttribute(name);
146  }
147  }
148  else
149  {
150  fwAtomsPatchErrorLogMacro("Missing attribute '" + name + "'");
151  }
152 }
153 
154 // ----------------------------------------------------------------------------
155 
156 ::fwAtoms::Object::sptr Object::getObject() const
157 {
158  return m_object;
159 }
160 
161 } //helper
162 } //fwAtomHelper
FWATOMSPATCH_API std::string getClassname(const ::fwAtoms::Object::sptr &obj)
Get classname of an object.
FWATOMSPATCH_API void renameAttribute(const std::string &name, const std::string &newName, conditions::Abstract::sptr condition=conditions::Abstract::New())
Renames an attribute.
FWATOMSPATCH_API void removeAttribute(const std::string &name, conditions::Abstract::sptr condition=conditions::Abstract::New())
Removes an attribute.
FWATOMSPATCH_API Object(::fwAtoms::Object::sptr metaObject)
Constructor.
Contains base functionalities used to transform objects from a version to another.
Definition: Abstract.hpp:16
FWATOMSPATCH_API void replaceAttribute(const std::string &name,::fwAtoms::Base::sptr newValue, conditions::Abstract::sptr condition=conditions::Abstract::New())
Replaces an attribute.
FWATOMSPATCH_API::fwAtoms::Object::sptr getObject() const
Atom::Object getter.
FWATOMSPATCH_API void addAttribute(const std::string &name,::fwAtoms::Base::sptr value, conditions::Abstract::sptr condition=conditions::Abstract::New())
Adds a new attribute in the current object.
FWATOMSPATCH_API std::string getVersion(const ::fwAtoms::Object::sptr &obj)
Get version of an object.
FWATOMSPATCH_API void addOrReplaceAttribute(const std::string &name,::fwAtoms::Base::sptr value, conditions::Abstract::sptr condition=conditions::Abstract::New())
Adds or replaces an attribute in the current object.