fw4spl
fwID.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
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 #ifndef __FWTOOLS_FWID_HPP__
8 #define __FWTOOLS_FWID_HPP__
9 
10 #include "fwTools/config.hpp"
11 
12 #include <fwCore/base.hpp>
13 #include <fwCore/mt/types.hpp>
14 
15 #include <cstdint>
16 #include <string>
17 #include <unordered_map>
18 
19 namespace fwTools
20 {
21 
22 class Object;
26 class FWTOOLS_CLASS_API fwID
27 {
28 public:
29 
31 
32  typedef std::string IDType;
33 
34  typedef enum
35  {
36  EMPTY = 1,
38  MUST_EXIST
39  } Policy;
40 
47  FWTOOLS_API static bool exist( IDType _id);
48 
53  FWTOOLS_API static SPTR(::fwTools::Object ) getObject( IDType requestID );
54 
55  FWTOOLS_API virtual ~fwID();
56 
57 protected:
58 
59  // API to expose in fwTools::Object
64  FWTOOLS_API bool hasID() const;
65 
75  FWTOOLS_API IDType getID( Policy policy = GENERATE ) const;
76 
82  FWTOOLS_API virtual void setID( IDType newID ); // cannot set a empty one
83 
88  FWTOOLS_API void resetID();
89 
93  fwID()
94  {
95  } // cannot be instantiated
96 
97 private:
98 
103  IDType generate() const;
104 
109  static void removeIDfromDictionary(IDType _id );
110 
115  static bool isIdFound( IDType _id);
116 
121  void addIDInDictionary( IDType newID );
122 
123  IDType m_id;
124 
125  typedef std::unordered_map< IDType, WPTR(::fwTools::Object ) > Dictionary;
126  typedef std::unordered_map< std::string, std::uint32_t > CategorizedCounter;
127 
128  static Dictionary m_dictionary;
129  static CategorizedCounter m_CategorizedCounter;
130 
132  static ::fwCore::mt::ReadWriteMutex s_dictionaryMutex;
133 
135  static ::fwCore::mt::Mutex s_mutexCounter;
136 
138  mutable ::fwCore::mt::ReadWriteMutex m_idMutex;
139 };
140 
141 }
142 
143 #endif /* __FWTOOLS_FWID_HPP__ */
#define SPTR(_cls_)
The namespace fwTools contains several tools like UUID, factory, dispatche, stringizer, macros, helper.
Define Base class for FW4SPL objects and services.
Defines ID for fwTools::Object. It is used to associate ID with object.
Definition: fwID.hpp:26
#define fwCoreBaseClassDefinitionsMacro(_classinfo_)
Generate common code for a base class (Interfaces, Abstract classes, ...)
generate a new id if necessary
Definition: fwID.hpp:37