fw4spl
AtomVisitor.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 <fwData/Object.hpp>
8 
9 #include "fwAtomConversion/AtomVisitor.hpp"
10 #include "fwAtomConversion/DataVisitor.hpp"
11 #include "fwAtomConversion/AtomToDataMappingVisitor.hpp"
12 #include "fwAtomConversion/exception/DataFactoryNotFound.hpp"
13 #include "fwAtomConversion/exception/DuplicatedDataUUID.hpp"
14 #include "fwAtomConversion/exception/ClassnameMismatch.hpp"
15 
16 namespace fwAtomConversion
17 {
18 
19 ::fwData::Object::sptr AtomVisitor::ReusePolicy::operator()(const std::string &uuid, const std::string &classname) const
20 {
21  ::fwData::Object::sptr obj = ::fwData::Object::dynamicCast(::fwTools::UUID::get(uuid));
22 
23  FW_RAISE_EXCEPTION_IF(
24  exception::ClassnameMismatch("Loaded object classname (" + classname
25  + ") for UUID '" + uuid
26  + "' does not match existing classname (" + obj->classname() + ")" ),
27  classname != obj->getClassname());
28 
29  if (!obj)
30  {
31  ::fwData::Object::sptr obj = ::fwData::factory::New(classname);
32 
33 
34  FW_RAISE_EXCEPTION_IF(
35  exception::DataFactoryNotFound(
36  std::string("Unable to build '") + classname + "': the data factory may be missing.")
37  , !obj
38  );
39  bool uuidIsSet = ::fwTools::UUID::set(obj, uuid);
40 
41  OSLM_ASSERT( "UUID '" << uuid << "' should not exist", uuidIsSet );
42  }
43 
44  return obj;
45 }
46 
47 ::fwData::Object::sptr AtomVisitor::ChangePolicy::operator()(const std::string &uuid,
48  const std::string &classname) const
49 {
50  ::fwData::Object::sptr obj = ::fwData::factory::New(classname);
51  // uuid is set only if the given uuid is available
52 
53  FW_RAISE_EXCEPTION_IF(
54  exception::DataFactoryNotFound(
55  std::string("Unable to build '") + classname + "': the data factory may be missing.")
56  , !obj
57  );
58 
59  ::fwTools::UUID::set(obj, uuid);
60  return obj;
61 }
62 
63 ::fwData::Object::sptr AtomVisitor::StrictPolicy::operator()(const std::string &uuid,
64  const std::string &classname) const
65 {
66 
67  ::fwData::Object::sptr obj = ::fwData::factory::New(classname);
68 
69  FW_RAISE_EXCEPTION_IF(
70  exception::DataFactoryNotFound(
71  std::string("Unable to build '") + classname + "': the data factory may be missing.")
72  , !obj
73  );
74 
75  bool uuidIsSet = ::fwTools::UUID::set(obj, uuid);
76 
77  FW_RAISE_EXCEPTION_IF(
78  exception::DuplicatedDataUUID(
79  std::string( "Try to create new data object '") + classname + "' with uuid '"
80  + uuid + "' but this uuid is already used."
81  ), !uuidIsSet );
82 
83  return obj;
84 
85 }
86 
87 
88 
89 AtomVisitor::AtomVisitor(const ::fwAtoms::Object::sptr &atomObj, DataCacheType & cache, const IReadPolicy &uuidPolicy)
90  : m_atomObj ( atomObj ),
91  m_cache ( cache ),
92  m_uuidPolicy(uuidPolicy)
93 {
94 }
95 
97 {
98 }
99 
101 {
102  this->processMetaInfos( m_atomObj->getMetaInfos() );
103  this->processAttributes( m_atomObj->getAttributes() );
104 }
105 
106 void AtomVisitor::processMetaInfos( const ::fwAtoms::Object::MetaInfosType & metaInfos )
107 {
108  const DataVisitor::ClassnameType& classname = metaInfos.find( DataVisitor::CLASSNAME_METAINFO )->second;
109  const ::fwTools::UUID::UUIDType& uuid = metaInfos.find( DataVisitor::ID_METAINFO )->second;
110 
111  m_dataObj = m_uuidPolicy(uuid, classname);
112  m_cache[uuid] = m_dataObj;
113 }
114 
115 void AtomVisitor::processAttributes( const ::fwAtoms::Object::AttributesType & attributes )
116 {
117  const camp::Class& metaclass = ::camp::classByName( m_dataObj->getClassname() );
118  ::fwAtomConversion::AtomToDataMappingVisitor visitor ( m_dataObj, m_atomObj, m_cache, m_uuidPolicy );
119  metaclass.visit(visitor);
120 }
121 
122 ::fwData::Object::sptr AtomVisitor::getDataObject() const
123 {
124  return m_dataObj;
125 }
126 
127 } // end namespace fwAtomConversion
FWATOMCONVERSION_API void visit()
Visits the atom information to create the data object and store it in the cache.
#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
This namespace contains the necessary class for fwData <-> fwAtoms conversion.
Visitor UUID Management policies.
Definition: AtomVisitor.hpp:40
This visitor visits data object to fill it with associated atom object attributes. This class is used by AtomVisitor to convert an fwData::Object.
FWATOMCONVERSION_API std::shared_ptr< ::fwData::Object > getDataObject() const
Returns the data object. Calls this method after visit().
static FWATOMCONVERSION_API const std::string CLASSNAME_METAINFO
Key of the meta info to store data object classname.
Definition: DataVisitor.hpp:48
static FWTOOLS_API const UUIDType & get(::fwTools::Object::sptr object)
Return an uuid to the given object : if no one previously set then generate a new one...
Definition: UUID.cpp:50
static FWATOMCONVERSION_API const std::string ID_METAINFO
Key of the meta info to store data object ID.
Definition: DataVisitor.hpp:50
static FWTOOLS_API bool set(::fwTools::Object::sptr object, const UUID::UUIDType &uuid)
Attempt to set an UUID. If uuid already exists, do nothing.
Definition: UUID.cpp:88
FWATOMCONVERSION_API AtomVisitor(const ::fwAtoms::Object::sptr &atomObj, DataCacheType &cache, const IReadPolicy &uuidPolicy)
Constructors. Initializes parameters.
Definition: AtomVisitor.cpp:89
virtual FWATOMCONVERSION_API ~AtomVisitor()
Destructor. Does nothing.
Definition: AtomVisitor.cpp:96