fw4spl
SemanticPatchDB.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 "fwAtomsPatch/SemanticPatchDB.hpp"
9 #include "fwAtomsPatch/ISemanticPatch.hpp"
10 
11 namespace fwAtomsPatch
12 {
13 
14 
15 SemanticPatchDB::sptr SemanticPatchDB::s_default = std::make_shared<SemanticPatchDB>();
16 
17 // ----------------------------------------------------------------------------
18 
20 {
21 }
22 
23 // ----------------------------------------------------------------------------
24 
26 {
27 }
28 
29 // ----------------------------------------------------------------------------
30 
32 {
33 }
34 
35 // ----------------------------------------------------------------------------
36 
37 void SemanticPatchDB::registerPatch(::fwAtomsPatch::ISemanticPatch::sptr patch)
38 {
39  VersionIDType origin = std::make_pair(patch->getOriginClassname(), patch->getOriginVersion());
40  ::fwCore::mt::WriteLock lock(m_mutex);
41  m_patches[origin].push_back(patch);
42 }
43 
44 // ----------------------------------------------------------------------------
45 
46 ::fwAtomsPatch::ISemanticPatch::sptr SemanticPatchDB::getPatch(
47  const std::string& context,
48  const std::string& originVersion,
49  const std::string& targetVersion,
50  const std::string& objOriginClassname,
51  const std::string& objOriginVersion) const
52 {
53  VersionIDType origin = std::make_pair(objOriginClassname, objOriginVersion);
54 
55  ::fwCore::mt::ReadLock lock(m_mutex);
56  PatchesType::const_iterator it = m_patches.find(origin);
57  if(it != m_patches.end())
58  {
59  PatchVectorType::const_iterator itVec;
60  for(itVec = it->second.begin(); itVec != it->second.end(); ++itVec)
61  {
62  if((*itVec)->isApplicable(context, originVersion, targetVersion))
63  {
64  return *itVec;
65  }
66  }
67  }
68 
69  return ::fwAtomsPatch::ISemanticPatch::sptr();
70 }
71 
72 // ----------------------------------------------------------------------------
73 
74 size_t SemanticPatchDB::size() const
75 {
76  ::fwCore::mt::ReadLock lock(m_mutex);
77  return m_patches.size();
78 }
79 
80 // ----------------------------------------------------------------------------
81 
82 SemanticPatchDB::sptr SemanticPatchDB::getDefault()
83 {
84  return s_default;
85 }
86 
87 
88 } //fwAtomsPatch
FWATOMSPATCH_API void registerPatch(std::shared_ptr< ::fwAtomsPatch::ISemanticPatch > patch)
Registers a new patch.
Contextual patch database. All contextual patches are stored in this database.
Contains base functionalities used to transform objects from a version to another.
Definition: Abstract.hpp:16
::boost::unique_lock< ReadWriteMutex > WriteLock
Defines a lock of write type for read/write mutex.
std::pair< std::string, std::string > VersionIDType
Typedef used to store the pair type/version of a data structure.
::boost::shared_lock< ReadWriteMutex > ReadLock
Defines a lock of read type for read/write mutex.
FWATOMSPATCH_API std::shared_ptr< ::fwAtomsPatch::ISemanticPatch > getPatch(const std::string &context, const std::string &originVersion, const std::string &targetVersion, const std::string &objOriginClassname, const std::string &objOriginVersion) const
Retrieves a patch allowing to go to specified data type/version target.
FWATOMSPATCH_API SemanticPatchDB()
Constructor. Does nothing.
virtual FWATOMSPATCH_API ~SemanticPatchDB()
Destructor. Does nothing.
static FWATOMSPATCH_API SemanticPatchDB::sptr getDefault()
Return default instance of SemanticPatchDB.
FWATOMSPATCH_API size_t size() const
Returns the number of patches.