NCBI C++ ToolKit
obj_store.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef UTIL__OBJECTSTORE__HPP
2 #define UTIL__OBJECTSTORE__HPP
3 
4 /* $Id: obj_store.hpp 33815 2007-05-04 17:18:18Z kazimird $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Anatoliy Kuznetsov
30  *
31  *
32  */
33 #include <corelib/ncbistd.hpp>
34 #include <corelib/ncbiobj.hpp>
35 #include <corelib/ncbimtx.hpp>
36 
38 
39 
40 /// Storage container for CObject derived classes
41 /// Objects are indexed by a key (usually string or integer).
42 /// Template makes sure objects are destroyed in the order reverse
43 /// to the order of insertion (similar to C++ object constr/destr rules).
44 /// First in last out.
45 ///
46 template<class TKey, class TObject>
48 {
49 public:
50  typedef TObject* TObjectPtr;
51 
52 public:
54  {
55  Clear();
56  }
57 
58  /// Clear all objects from the store
59  void Clear()
60  {
61  m_ObjMap.clear();
62  m_ObjList.erase(m_ObjList.begin(), m_ObjList.end());
63  }
64 
65  /// Retrieve a named object from the data store.
66  /// (Advised to use CRef<> to host the return value)
67  /// Method returns NULL if object cannot be found.
68  TObject* GetObject(const TKey& key)
69  {
71  return (it != m_ObjMap.end()) ? it->second : 0;
72  }
73 
74  /// Put an object in the store. This will return TRUE if the
75  /// operation succeded, FALSE if the object already exists in the store.
76  bool PutObject(const TKey& key, TObject* obj)
77  {
79  if (it == m_ObjMap.end()) {
80  m_ObjList.push_front(CRef<TObject>(obj));
81  m_ObjMap.insert(pair<string, TObjectPtr>(key, obj));
82  return true;
83  }
84  return false;
85  }
86 
87  /// Release an object from the data store
88  void ReleaseObject(const TKey& key)
89  {
90  typename TObjectMap::iterator it(m_ObjMap.find(key));
91  if (it != m_ObjMap.end()) {
92  TObject* obj = it->second;
93  m_ObjMap.erase(it);
94  NON_CONST_ITERATE(typename TObjectList, lit, m_ObjList) {
95  TObject* ptr = lit->GetPointer();
96  if (ptr == obj) {
97  m_ObjList.erase(lit);
98  break;
99  }
100  }
101  }
102  }
103 
104  /// Check to see if a named object exists
105  bool HasObject(const TKey& key)
106  {
107  return (m_ObjMap.find(key) != m_ObjMap.end());
108  }
109 
110  /// check to see if a given object is in the store
111  bool HasObject(const CObject* obj)
112  {
113  ITERATE(typename TObjectList, lit, m_ObjList) {
114  const CObject* ptr = lit->GetPointer();
115  if (ptr == obj) {
116  return true;
117  }
118  }
119  return false;
120  }
121 
122 protected:
124  typedef list<CRef<TObject> > TObjectList;
125 
126  TObjectMap m_ObjMap; //< String to object locator
127  TObjectList m_ObjList; //< Object storage (objects kept in the reverse order)
128 };
129 
130 /// Protected base class for CSingletonObjectStore
131 /// Holds a syncronization mutex.
132 ///
133 /// @note
134 /// Zero functionality base class is created to make sure
135 /// that mutex is initialized just only once
136 /// (in case of a template member this is not guaranteed)
137 ///
139 {
140 protected:
141  static SSystemFastMutex& GetMutex(void);
142 };
143 
144 /// System wide dumping ground for objects
145 ///
146 /// @note
147 /// Thread safe, synctonized singleton
148 ///
149 template<class TKey, class TObject>
151 {
152 public:
154 public:
156  {
157  Clear();
158  }
159 
160  /// Clear all objects from the store
161  static
162  void Clear()
163  {
164  CFastMutexGuard guard( GetMutex() );
165  GetObjStore().Clear();
166  }
167 
168  /// Retrieve a named object from the data store.
169  /// (Advised to use CRef<> to host the return value)
170  /// Method returns NULL if object cannot be found.
171  static
172  TObject* GetObject(const TKey& key)
173  {
174  CFastMutexGuard guard( GetMutex() );
175  return GetObjStore().GetObject(key);
176  }
177 
178  /// Put an object in the store. This will return TRUE if the
179  /// operation succeded, FALSE if the object already exists in the store.
180  static
181  bool PutObject(const TKey& key, TObject* obj)
182  {
183  CFastMutexGuard guard( GetMutex() );
184  return GetObjStore().PutObject(key, obj);
185  }
186 
187  /// Release an object from the data store
188  static
189  void ReleaseObject(const TKey& key)
190  {
191  CFastMutexGuard guard( GetMutex() );
193  }
194 
195  /// Check to see if a named object exists
196  static
197  bool HasObject(const TKey& key)
198  {
199  CFastMutexGuard guard( GetMutex() );
200  return GetObjStore().HasObject(key);
201  }
202 
203  /// check to see if a given object is in the store
204  static
205  bool HasObject(const CObject* obj)
206  {
207  CFastMutexGuard guard( GetMutex() );
208  return GetObjStore().HasObject(obj);
209  }
210 
211 protected:
212  static
214  {
215  static TReverseObjectStore s_obj_store;
216  return s_obj_store;
217  }
218 };
219 
220 
222 
223 #endif
Protected base class for CSingletonObjectStore Holds a syncronization mutex.
Definition: obj_store.hpp:139
static SSystemFastMutex & GetMutex(void)
Definition: obj_store.cpp:39
CObject –.
Definition: ncbiobj.hpp:180
Storage container for CObject derived classes Objects are indexed by a key (usually string or integer...
Definition: obj_store.hpp:48
bool HasObject(const TKey &key)
Check to see if a named object exists.
Definition: obj_store.hpp:105
TObjectList m_ObjList
Definition: obj_store.hpp:127
TObjectMap m_ObjMap
Definition: obj_store.hpp:126
TObject * GetObject(const TKey &key)
Retrieve a named object from the data store.
Definition: obj_store.hpp:68
map< TKey, TObjectPtr > TObjectMap
Definition: obj_store.hpp:123
TObject * TObjectPtr
Definition: obj_store.hpp:50
void ReleaseObject(const TKey &key)
Release an object from the data store.
Definition: obj_store.hpp:88
list< CRef< TObject > > TObjectList
Definition: obj_store.hpp:124
bool PutObject(const TKey &key, TObject *obj)
Put an object in the store.
Definition: obj_store.hpp:76
void Clear()
Clear all objects from the store.
Definition: obj_store.hpp:59
bool HasObject(const CObject *obj)
check to see if a given object is in the store
Definition: obj_store.hpp:111
System wide dumping ground for objects.
Definition: obj_store.hpp:151
static TObject * GetObject(const TKey &key)
Retrieve a named object from the data store.
Definition: obj_store.hpp:172
static TReverseObjectStore & GetObjStore(void)
Definition: obj_store.hpp:213
static void ReleaseObject(const TKey &key)
Release an object from the data store.
Definition: obj_store.hpp:189
CReverseObjectStore< TKey, TObject > TReverseObjectStore
Definition: obj_store.hpp:153
static bool HasObject(const CObject *obj)
check to see if a given object is in the store
Definition: obj_store.hpp:205
static void Clear()
Clear all objects from the store.
Definition: obj_store.hpp:162
static bool PutObject(const TKey &key, TObject *obj)
Put an object in the store.
Definition: obj_store.hpp:181
static bool HasObject(const TKey &key)
Check to see if a named object exists.
Definition: obj_store.hpp:197
void erase(iterator pos)
Definition: map.hpp:167
container_type::const_iterator const_iterator
Definition: map.hpp:53
const_iterator end() const
Definition: map.hpp:152
iterator_bool insert(const value_type &val)
Definition: map.hpp:165
void clear()
Definition: map.hpp:169
const_iterator find(const key_type &key) const
Definition: map.hpp:153
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define NCBI_XNCBI_EXPORT
Definition: ncbi_export.h:1283
const struct ncbi::grid::netcache::search::fields::KEY key
Multi-threading – mutexes; rw-locks; semaphore.
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
Modified on Fri May 24 14:56:07 2024 by modify_doxy.py rev. 669887