fw4spl
StructuralCreatorDB.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/infos/log.hpp"
9 #include "fwAtomsPatch/IStructuralCreator.hpp"
10 #include "fwAtomsPatch/StructuralCreatorDB.hpp"
11 
12 namespace fwAtomsPatch
13 {
14 
15 StructuralCreatorDB::sptr StructuralCreatorDB::s_default = std::make_shared<StructuralCreatorDB>();
16 
17 void StructuralCreatorDB::registerCreator(::fwAtomsPatch::IStructuralCreator::sptr creator)
18 {
19  VersionIDType key = std::make_pair(creator->getObjectClassname(), creator->getObjectVersion());
20  ::fwCore::mt::WriteLock lock(m_mutex);
21  m_creators[key] = creator;
22 }
23 
24 // ----------------------------------------------------------------------------
25 
26 ::fwAtomsPatch::IStructuralCreator::sptr StructuralCreatorDB::getCreator(const std::string& classname,
27  const std::string& version)
28 {
29  VersionIDType key = std::make_pair(classname, version);
30 
31  ::fwCore::mt::ReadLock lock(m_mutex);
32  CreatorsType::const_iterator it = m_creators.find(key);
33  if(it != m_creators.end())
34  {
35  return it->second;
36  }
37  else
38  {
39  return ::fwAtomsPatch::IStructuralCreator::sptr();
40  }
41 }
42 
43 // ----------------------------------------------------------------------------
44 
45 ::fwAtoms::Object::sptr StructuralCreatorDB::create(const std::string& classname, const std::string& version)
46 {
47  ::fwAtomsPatch::IStructuralCreator::sptr creator = this->getCreator( classname, version );
48  OSLM_ASSERT( "object creator of type '"<< classname <<"' and version '"<< version <<"' not found", creator );
49  fwAtomsPatchInfoLogMacro("Create '" + classname + "|" + version + "'");
50  ::fwAtoms::Object::sptr obj = creator->create();
51  return obj;
52 }
53 
54 // ----------------------------------------------------------------------------
55 
57 {
58  ::fwCore::mt::ReadLock lock(m_mutex);
59  return m_creators.size();
60 }
61 
62 
63 
64 } //fwAtomsPatch
FWATOMSPATCH_API std::shared_ptr< ::fwAtomsPatch::IStructuralCreator > getCreator(const std::string &classname, const std::string &version)
Retrieves creator to create data revision.
#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
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.
FWATOMSPATCH_API void registerCreator(std::shared_ptr< ::fwAtomsPatch::IStructuralCreator > creator)
Registers a new creator.
::boost::shared_lock< ReadWriteMutex > ReadLock
Defines a lock of read type for read/write mutex.
FWATOMSPATCH_API size_t size() const
Returns the number of creators.
FWATOMSPATCH_API std::shared_ptr< ::fwAtoms::Object > create(const std::string &classname, const std::string &version)
Creates a new object instance with given classname and version.
std::pair< std::string, std::string > VersionIDType
Typedef used to store the pair classname/version of a data structure.