fw4spl
UUID.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 <boost/uuid/random_generator.hpp>
8 #include <boost/uuid/uuid_io.hpp>
9 
10 #include "fwTools/UUID.hpp"
11 
12 namespace fwTools
13 {
14 
15 UUID::UUIDContainer UUID::s_uuidMap;
16 
17 ::fwCore::mt::ReadWriteMutex UUID::s_uuidMapMutex;
18 ::fwCore::mt::Mutex UUID::s_generateUUIDMutex;
19 
20 //-----------------------------------------------------------------------------
21 
22 UUID::UUID() : m_uuid("")
23 {
24 }
25 
26 //-----------------------------------------------------------------------------
27 
29 {
31  ::fwCore::mt::ReadToWriteLock lock(s_uuidMapMutex);
32  UUID::UUIDContainer::iterator iter = UUID::s_uuidMap.find(m_uuid);
33  if( iter != UUID::s_uuidMap.end())
34  {
35  ::fwCore::mt::UpgradeToWriteLock writeLock(lock);
36  UUID::s_uuidMap.erase(iter);
37  }
38 }
39 
40 //-----------------------------------------------------------------------------
41 
42 bool UUID::exist( const UUID::UUIDType & uuid)
43 {
44  ::fwCore::mt::ReadLock lock(s_uuidMapMutex);
45  return ( UUID::s_uuidMap.find(uuid) != UUID::s_uuidMap.end() );
46 }
47 
48 //-----------------------------------------------------------------------------
49 
50 const UUID::UUIDType& UUID::get(::fwTools::Object::sptr object)
51 {
52  SLM_ASSERT("Object expired", object);
53 
54  UUID::sptr uuidObject = object->m_uuid;
55  ::fwCore::mt::ReadToWriteLock uuidLock(uuidObject->m_uuidMutex);
56  if(uuidObject->m_uuid.empty())
57  {
58  UUIDType uuid = UUID::generateUUID();
59 
60  {
61  ::fwCore::mt::UpgradeToWriteLock writeLock(uuidLock);
62  uuidObject->m_uuid = uuid;
63  }
64  {
65  ::fwCore::mt::WriteLock lock(s_uuidMapMutex);
66  UUID::s_uuidMap.insert(UUID::UUIDContainer::value_type(uuid, object));
67  }
68  }
69  return uuidObject->m_uuid;
70 }
71 
72 //-----------------------------------------------------------------------------
73 
74 ::fwTools::Object::sptr UUID::get( const UUID::UUIDType & uuid )
75 {
76  ::fwCore::mt::ReadLock lock(s_uuidMapMutex);
77  ::fwTools::Object::sptr obj;
78  UUID::UUIDContainer::iterator iter = UUID::s_uuidMap.find(uuid);
79  if( iter != UUID::s_uuidMap.end() )
80  {
81  obj = iter->second.lock();
82  }
83  return obj;
84 }
85 
86 //-----------------------------------------------------------------------------
87 
88 bool UUID::set(::fwTools::Object::sptr object, const UUID::UUIDType & uuid )
89 {
90  ::fwCore::mt::ReadToWriteLock lock(s_uuidMapMutex);
91 
92  bool isSet = false;
93 
94  if(UUID::s_uuidMap.find(uuid) == UUID::s_uuidMap.end())
95  {
96  UUID::sptr uuidObject = object->m_uuid;
97 
98  {
99  ::fwCore::mt::WriteLock uuidLock(uuidObject->m_uuidMutex);
100  uuidObject->m_uuid = uuid;
101  }
102  {
103  ::fwCore::mt::UpgradeToWriteLock writeLock(lock);
104  UUID::s_uuidMap.insert(UUID::UUIDContainer::value_type(uuid, object));
105  }
106  isSet = true;
107  }
108 
109  return isSet;
110 }
111 
112 //-----------------------------------------------------------------------------
113 
114 UUID::UUIDType UUID::generateUUID()
115 {
116  static boost::uuids::random_generator gen;
117  ::fwCore::mt::ScopedLock lock(s_generateUUIDMutex);
118  ::boost::uuids::uuid uuid = gen();
119  return ::boost::uuids::to_string(uuid);
120 }
121 
122 //-----------------------------------------------------------------------------
123 
124 }
::boost::upgrade_lock< ReadWriteMutex > ReadToWriteLock
Defines an upgradable lock type for read/write mutex.
::boost::upgrade_to_unique_lock< ReadWriteMutex > UpgradeToWriteLock
Defines a write lock upgraded from ReadToWriteLock.
std::map< UUIDType,::fwTools::Object::wptr > UUIDContainer
Store association std::weak_ptr <–> uuid as a string.
Definition: UUID.hpp:71
The namespace fwTools contains several tools like UUID, factory, dispatche, stringizer, macros, helper.
::boost::shared_mutex ReadWriteMutex
Defines a single writer, multiple readers mutex.
::boost::unique_lock< ReadWriteMutex > WriteLock
Defines a lock of write type for read/write mutex.
::fwCore::mt::ReadWriteMutex m_uuidMutex
Mutex used to lock uuid object access.
Definition: UUID.hpp:81
static FWTOOLS_API bool exist(const UUIDType &uuid)
Return true iff the given uuid is used.
Definition: UUID.cpp:42
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
#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
virtual FWTOOLS_API ~UUID()
Destructor : does nothing.
Definition: UUID.cpp:28
::boost::shared_lock< ReadWriteMutex > ReadLock
Defines a lock of read type for read/write mutex.
static FWTOOLS_API bool set(::fwTools::Object::sptr object, const UUID::UUIDType &uuid)
Attempt to set an UUID. If uuid already exists, do nothing.
Definition: UUID.cpp:88
FWTOOLS_API UUID()
Default constructor : does nothing.
Definition: UUID.cpp:22
static FWTOOLS_API UUIDType generateUUID()
Return a new extended UUID;.
Definition: UUID.cpp:114