fw4spl
convert.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 <fwAtoms/Object.hpp>
8 
9 #include <fwData/Object.hpp>
10 
11 #include "fwAtomConversion/convert.hpp"
12 #include "fwAtomConversion/DataVisitor.hpp"
13 #include "fwAtomConversion/AtomVisitor.hpp"
14 #include "fwAtomConversion/mapper/Base.hpp"
15 
16 namespace fwAtomConversion
17 {
18 
19 ::fwAtoms::Object::sptr convert(const ::fwData::Object::sptr &data )
20 {
21  DataVisitor::AtomCacheType cache;
22  return convert( data, cache );
23 }
24 
25 //-----------------------------------------------------------------------------
26 
27 ::fwAtoms::Object::sptr convert(const ::fwData::Object::sptr &dataObj, DataVisitor::AtomCacheType & cache )
28 {
29  ::fwAtoms::Object::sptr atom;
30 
31  DataVisitor::AtomCacheType::iterator elem = cache.find( ::fwTools::UUID::get( dataObj ) );
32 
33  if ( elem == cache.end() )
34  {
35  SPTR(mapper::Base) mapper = mapper::factory::New( dataObj->getClassname() );
36  if ( mapper )
37  {
38  atom = mapper->convert( dataObj, cache );
39  }
40  else
41  {
42  const camp::Class& metaclass = ::camp::classByName( dataObj->getClassname() );
43  ::fwAtomConversion::DataVisitor visitor ( dataObj, cache );
44  metaclass.visit(visitor);
45  atom = visitor.getAtomObject();
46  }
47  }
48  else // already analysed
49  {
50  atom = elem->second;
51  }
52 
53  return atom;
54 }
55 
56 //-----------------------------------------------------------------------------
57 
58 ::fwData::Object::sptr convert( const ::fwAtoms::Object::sptr &atom,
59  const AtomVisitor::IReadPolicy &uuidPolicy
60  )
61 {
62  AtomVisitor::DataCacheType cache;
63  return convert( atom, cache, uuidPolicy );
64 }
65 
66 //-----------------------------------------------------------------------------
67 
68 ::fwData::Object::sptr convert( const ::fwAtoms::Object::sptr &atomObj, AtomVisitor::DataCacheType & cache,
69  const AtomVisitor::IReadPolicy &uuidPolicy
70  )
71 {
72  ::fwData::Object::sptr data;
73 
74  AtomVisitor::DataCacheType::iterator elem = cache.find( atomObj->getMetaInfo( DataVisitor::ID_METAINFO ) );
75 
76  if ( elem == cache.end() )
77  {
78  SPTR(mapper::Base) mapper = mapper::factory::New( atomObj->getMetaInfo( DataVisitor::CLASSNAME_METAINFO ) );
79  if ( mapper )
80  {
81  data = mapper->convert( atomObj, cache, uuidPolicy );
82  }
83  else
84  {
85  ::fwAtomConversion::AtomVisitor visitor ( atomObj, cache, uuidPolicy );
86  visitor.visit();
87  data = visitor.getDataObject();
88  }
89  }
90  else // already analyzed
91  {
92  data = elem->second;
93  }
94 
95  return data;
96 }
97 
98 }
#define SPTR(_cls_)
This namespace contains the necessary class for fwData <-> fwAtoms conversion.
Contains fwAtomsFilter::factory utilities.
FWATOMCONVERSION_API std::shared_ptr< ::fwAtoms::Object > convert(const std::shared_ptr< ::fwData::Object > &data)
Helper to convert a fwData::Object to a fwAtoms::Object.
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
Visitor used to convert a fwData to a fwAtoms. fwData camp property names are used like key to store ...
Definition: DataVisitor.hpp:38
This class is used to convert a fwAtoms to a fwData.
Definition: AtomVisitor.hpp:31