fw4spl
src/fwData/StructureTraitsDictionary.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 
8 #include "fwData/registry/macros.hpp"
9 #include "fwData/Exception.hpp"
10 #include "fwData/StructureTraitsDictionary.hpp"
11 
12 #include <fwCore/base.hpp>
13 
14 #include <functional>
15 
16 fwDataRegisterMacro( ::fwData::StructureTraitsDictionary );
17 
18 namespace fwData
19 {
20 //------------------------------------------------------------------------------
21 
23 {
24 }
25 
26 //------------------------------------------------------------------------------
27 
29 {
30 }
31 
32 //------------------------------------------------------------------------------
33 
34 StructureTraits::sptr StructureTraitsDictionary::getStructure(std::string type)
35 {
36  SLM_ASSERT("Structure of type '" + type + "' not found",
37  m_structureTraitsMap.find(type) != m_structureTraitsMap.end());
38  return m_structureTraitsMap[type];
39 }
40 
41 //------------------------------------------------------------------------------
42 
43 void StructureTraitsDictionary::addStructure(StructureTraits::sptr structureTraits)
44 {
45  std::string type = structureTraits->getType();
46  StructureTraits::StructureClass structClass = structureTraits->getClass();
47  std::string attachment = structureTraits->getAttachmentType();
48 
49  FW_RAISE_IF("Structure of type '" << type << "' already exist",
50  m_structureTraitsMap.find(type) != m_structureTraitsMap.end());
51 
52  FW_RAISE_IF("Structure of class '" << structClass << "' can not have attachment",
53  !(attachment.empty() || structClass == StructureTraits::LESION || structClass ==
54  StructureTraits::FUNCTIONAL) );
55 
56  FW_RAISE_IF("Structure attachment '" << attachment << "' not found in dictionary",
57  !(attachment.empty() || m_structureTraitsMap.find(attachment) != m_structureTraitsMap.end() ) );
58 
59  FW_RAISE_IF("Structure attachment '" << attachment << "' must be of class ORGAN",
60  !(attachment.empty() || m_structureTraitsMap[attachment]->getClass() == StructureTraits::ORGAN ) );
61 
62  FW_RAISE_IF("Structure must have at least one category",
63  structureTraits->getCategories().empty());
64 
65  FW_RAISE_IF("Wrong structure type '" << type<< "', a type cannot contain space",
66  structureTraits->getType().find(" ") != std::string::npos );
67 
68  m_structureTraitsMap[type] = structureTraits;
69 }
70 
71 //------------------------------------------------------------------------------
72 
73 StructureTraitsDictionary::StructureTypeNameContainer StructureTraitsDictionary::getStructureTypeNames() const
74 {
75  StructureTypeNameContainer vectNames;
76  std::transform( m_structureTraitsMap.begin(), m_structureTraitsMap.end(),
77  std::back_inserter(vectNames),
78  std::bind(&StructureTraitsMapType::value_type::first, std::placeholders::_1) );
79  return vectNames;
80 }
81 
82 //------------------------------------------------------------------------------
83 
84 void StructureTraitsDictionary::shallowCopy(const Object::csptr &source )
85 {
86  StructureTraitsDictionary::csptr other = StructureTraitsDictionary::dynamicConstCast(source);
87  FW_RAISE_EXCEPTION_IF( ::fwData::Exception(
88  "Unable to copy" + (source ? source->getClassname() : std::string("<NULL>"))
89  + " to " + this->getClassname()), !bool(other) );
90  this->fieldShallowCopy( source );
91  m_structureTraitsMap = other->m_structureTraitsMap;
92 }
93 
94 //------------------------------------------------------------------------------
95 
96 void StructureTraitsDictionary::cachedDeepCopy(const Object::csptr &source, DeepCopyCacheType &cache)
97 {
98  StructureTraitsDictionary::csptr other = StructureTraitsDictionary::dynamicConstCast(source);
99  FW_RAISE_EXCEPTION_IF( ::fwData::Exception(
100  "Unable to copy" + (source ? source->getClassname() : std::string("<NULL>"))
101  + " to " + this->getClassname()), !bool(other) );
102  this->fieldDeepCopy( source, cache );
103  m_structureTraitsMap.clear();
104  for(const StructureTraitsMapType::value_type& elt : other->m_structureTraitsMap)
105  {
106  m_structureTraitsMap[elt.first] = ::fwData::Object::copy(elt.second, cache);
107  }
108 }
109 
110 //------------------------------------------------------------------------------
111 
112 
113 } // namespace fwData
114 
115 
FWDATA_API void shallowCopy(const Object::csptr &_source) override
Defines shallow copy.
virtual const std::string & getClassname() const override
return full object&#39;s classname with its namespace, i.e. fwCore::BaseObject
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
StructureClass
Defines structure class.
Implements data exception class.
FWDATA_API StructureTraitsDictionary(::fwData::Object::Key key)
Constructor.
This class defines a dictionary of structure traits.
FWDATA_API StructureTypeNameContainer getStructureTypeNames() const
Return all array names stock in the structureTraits-map.
FWDATA_API void fieldDeepCopy(const ::fwData::Object::csptr &source)
A deep copy of fields (objects in m_children)
FWDATA_API StructureTraits::sptr getStructure(std::string type)
Return the structure traits for given type.
#define SLM_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:308
static FWDATA_API::fwData::Object::sptr copy(const ::fwData::Object::csptr &source)
return a copy of the source. if source is a null pointer, return a null pointer.
virtual FWDATA_API ~StructureTraitsDictionary()
Destructor. Does nothing.
FWDATA_API void cachedDeepCopy(const Object::csptr &_source, DeepCopyCacheType &cache) override
Defines deep copy.
Contains the representation of the data objects used in the framework.
FWDATA_API void fieldShallowCopy(const ::fwData::Object::csptr &source)
A shallow copy of fields (objects in m_children)
FWDATA_API void addStructure(StructureTraits::sptr structureTraits)
Add a structure in dictionary.