fw4spl
fwAtomsPatch/src/fwAtomsPatch/helper/functions.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/helper/functions.hpp"
8 #include "fwAtomsPatch/types.hpp"
9 
10 #include <fwTools/UUID.hpp>
11 
12 namespace fwAtomsPatch
13 {
14 namespace helper
15 {
16 
17 //-----------------------------------------------------------------------------
18 
19 std::string getClassname( const ::fwAtoms::Object::sptr & obj )
20 {
21  return obj->getMetaInfo( ::fwAtomsPatch::s_OBJ_CLASSNAME );
22 }
23 
24 //-----------------------------------------------------------------------------
25 
26 void setClassname( const ::fwAtoms::Object::sptr & obj, const std::string & newClassname )
27 {
28  obj->setMetaInfo( ::fwAtomsPatch::s_OBJ_CLASSNAME, newClassname );
29 }
30 
31 //-----------------------------------------------------------------------------
32 
33 std::string getVersion( const ::fwAtoms::Object::sptr & obj )
34 {
35  return obj->getMetaInfo( ::fwAtomsPatch::s_OBJ_VERSION );
36 }
37 
38 //-----------------------------------------------------------------------------
39 
40 void setVersion( const ::fwAtoms::Object::sptr & obj, const std::string & newVersion )
41 {
42  obj->setMetaInfo( ::fwAtomsPatch::s_OBJ_VERSION, newVersion );
43 }
44 
45 //-----------------------------------------------------------------------------
46 
47 void generateID( const ::fwAtoms::Object::sptr & obj )
48 {
49  obj->setMetaInfo( fwAtomsPatch::s_OBJ_ID, ::fwTools::UUID::generateUUID() );
50 }
51 
52 //-----------------------------------------------------------------------------
53 
54 void cleanFields( const ::fwAtoms::Object::sptr & obj )
55 {
56  obj->setAttribute("fields", ::fwAtoms::Map::New());
57 }
58 
59 //-----------------------------------------------------------------------------
60 
61 void changeUID( const ::fwAtoms::Object::sptr & obj )
62 {
63  for( const ::fwAtoms::Object::AttributesType::value_type& elem : obj->getAttributes() )
64  {
65  if ( elem.second )
66  {
67  switch ( elem.second->type() )
68  {
69  case ::fwAtoms::Base::SEQUENCE:
70  {
71  changeSeqUID( ::fwAtoms::Sequence::dynamicCast( elem.second ) );
72  break;
73  }
74  case ::fwAtoms::Base::MAP:
75  {
76  changeMapUID( ::fwAtoms::Map::dynamicCast( elem.second ) );
77  break;
78  }
79  case ::fwAtoms::Base::OBJECT:
80  {
81  changeUID( ::fwAtoms::Object::dynamicCast( elem.second ) );
82  break;
83  }
84  default:
85  break;
86  }
87  }
88  }
89 
90  ::fwAtomsPatch::helper::generateID( obj );
91 }
92 
93 //-----------------------------------------------------------------------------
94 
95 void changeMapUID( const ::fwAtoms::Map::sptr & map )
96 {
97  for( ::fwAtoms::Map::ValueType elem : map->getValue() )
98  {
99  if ( elem.second )
100  {
101  switch ( elem.second->type() )
102  {
103  case ::fwAtoms::Base::SEQUENCE:
104  {
105  changeSeqUID( ::fwAtoms::Sequence::dynamicCast( elem.second ) );
106  break;
107  }
108  case ::fwAtoms::Base::MAP:
109  {
110  changeMapUID( ::fwAtoms::Map::dynamicCast( elem.second ) );
111  break;
112  }
113  case ::fwAtoms::Base::OBJECT:
114  {
115  changeUID( ::fwAtoms::Object::dynamicCast( elem.second ) );
116  break;
117  }
118  default:
119  break;
120  }
121  }
122  }
123 }
124 
125 //-----------------------------------------------------------------------------
126 
127 void changeSeqUID( const ::fwAtoms::Sequence::sptr & seq )
128 {
129  for( ::fwAtoms::Base::sptr elem : seq->getValue() )
130  {
131  if ( elem )
132  {
133  switch ( elem->type() )
134  {
135  case ::fwAtoms::Base::SEQUENCE:
136  {
137  changeSeqUID( ::fwAtoms::Sequence::dynamicCast( elem ) );
138  break;
139  }
140  case ::fwAtoms::Base::MAP:
141  {
142  changeMapUID( ::fwAtoms::Map::dynamicCast( elem ) );
143  break;
144  }
145  case ::fwAtoms::Base::OBJECT:
146  {
147  changeUID( ::fwAtoms::Object::dynamicCast( elem ) );
148  break;
149  }
150  default:
151  break;
152  }
153  }
154  }
155 }
156 
157 //-----------------------------------------------------------------------------
158 
159 } //helper
160 } //fwAtomHelper
161 
FWATOMSPATCH_API void setVersion(const ::fwAtoms::Object::sptr &obj, const std::string &newVersion)
Set version of an object.
FWATOMSPATCH_API std::string getClassname(const ::fwAtoms::Object::sptr &obj)
Get classname of an object.
FWATOMSPATCH_API void cleanFields(const ::fwAtoms::Object::sptr &obj)
Cleans object fields (also creates them if they are missing)
FWATOMSPATCH_API void changeSeqUID(const ::fwAtoms::Sequence::sptr &seq)
Generates new UUID for seq objects and child objects.
FWATOMSPATCH_API void generateID(const ::fwAtoms::Object::sptr &obj)
Generates new id for the object (also creates it if it is missing )
Contains base functionalities used to transform objects from a version to another.
Definition: Abstract.hpp:16
FWATOMSPATCH_API void changeMapUID(const ::fwAtoms::Map::sptr &map)
Generates new UUID for map objects and child objects.
FWATOMSPATCH_API void changeUID(const ::fwAtoms::Object::sptr &obj)
Generates new UUID for object and child objects.
FWATOMSPATCH_API std::string getVersion(const ::fwAtoms::Object::sptr &obj)
Get version of an object.
FWATOMSPATCH_API void setClassname(const ::fwAtoms::Object::sptr &obj, const std::string &newClassname)
Set classname of an object.
static FWTOOLS_API UUIDType generateUUID()
Return a new extended UUID;.
Definition: UUID.cpp:114