NCBI C++ ToolKit
extension_impl.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: extension_impl.cpp 18080 2008-10-14 22:33:27Z yazhuk $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Andrey Yazhuk
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
35 
37 
38 
39 ///////////////////////////////////////////////////////////////////////////////
40 /// CExtensionPoint - default implementation of IExtensionPoint
41 CExtensionPoint::CExtensionPoint(const string& id, const string& label)
42 : m_Id(id),
43  m_Label(label)
44 {
45 }
46 
47 
49 {
50  CMutexGuard guard(m_Mutex);
51  return m_Id;
52 }
53 
54 
56 {
57  CMutexGuard guard(m_Mutex);
58  return m_Label;
59 }
60 
61 
63 {
64  CMutexGuard guard(m_Mutex);
65 
66  string id = extension.GetExtensionIdentifier();
68  if(it == m_IdToExt.end()) {
69  m_IdToExt[id] = TExtRef(&extension);
70  return true;
71  }
72  return false;
73 }
74 
75 
77 {
78  CMutexGuard guard(m_Mutex);
79 
81  if(it != m_IdToExt.end()) {
82  return it->second;
83  }
84  return TExtRef();
85 }
86 
87 
89 {
90  CMutexGuard guard(m_Mutex);
91 
93  extensions.push_back(it->second);
94  }
95 }
96 
97 
99 {
100  return RemoveExtension(extension.GetExtensionIdentifier());
101 }
102 
103 
104 bool CExtensionPoint::RemoveExtension(const string& ext_id)
105 {
106  CMutexGuard guard(m_Mutex);
107 
109  if(it->first == ext_id) {
110  m_IdToExt.erase(it);
111  return true;
112  }
113  }
114  return false;
115 }
116 
117 
118 void CExtensionPoint::SetLabel(const string& label)
119 {
120  CMutexGuard guard(m_Mutex);
121  if(m_Label.empty()) {
122  m_Label = label;
123  }
124 }
125 
126 
127 ///////////////////////////////////////////////////////////////////////////////
128 /// CExtensionRegistry
129 
131 
132 // static
134 {
136 }
137 
138 
140 {
141 }
142 
144 {
145  CMutexGuard guard(m_Mutex);
146 
147  string ext_point_id = ext_point.GetIdentifier();
148 
149  TExtPointRef point_ref = GetExtensionPoint(ext_point_id);
150  if( point_ref) {
151  ERR_POST("CExtensionRegistry::AddExtensionPoint() - point \"" << ext_point_id
152  << "\" already registered.");
153  return false;
154  }
155 
156  m_IdToPoint[ext_point_id] = CIRef<IExtensionPoint>(&ext_point);
157  return true;
158 }
159 
160 
161 
162 bool CExtensionRegistry::AddExtensionPoint(const string& ext_point_id, const string& ext_point_label)
163 {
164  CMutexGuard guard(m_Mutex);
165 
166  TExtPointRef point_ref = GetExtensionPoint(ext_point_id);
167  if( ! point_ref) {
168  x_CreateExtensionPoint(ext_point_id, ext_point_label);
169  return true;
170  } else {
171  // see if the point was created automatically
172  if(point_ref->GetLabel().empty()) {
173  CExtensionPoint* the_point =
174  dynamic_cast<CExtensionPoint*>(point_ref.GetPointer());
175  _ASSERT(the_point);
176 
177  the_point->SetLabel(ext_point_label); // only assign label
178  return true;
179  } else {
180  ERR_POST("CExtensionRegistry::AddExtensionPoint() - point \"" << ext_point_id
181  << "\" already registered.");
182  }
183  }
184  return false;
185 }
186 
187 
188 bool CExtensionRegistry::AddExtension(const string& ext_point_id, IExtension& extension)
189 {
190  CMutexGuard guard(m_Mutex);
191 
192  TExtPointRef point = GetExtensionPoint(ext_point_id);
193  if( ! point) {
194  // create the point automatically
195  point = x_CreateExtensionPoint(ext_point_id, "");
196  }
197  _ASSERT(point);
198  return point->AddExtension(extension);
199 }
200 
201 
203  CExtensionRegistry::x_CreateExtensionPoint(const string& ext_point_id,
204  const string& ext_point_label)
205 {
206  _ASSERT( ! ext_point_id.empty());
207 
208  CRef<CExtensionPoint> point(new CExtensionPoint(ext_point_id, ext_point_label));
209  m_IdToPoint[ext_point_id] = point;
210  return point;
211 }
212 
213 
215  CExtensionRegistry::GetExtension(const string& extension_id)
216 {
217  CMutexGuard guard(m_Mutex);
218 
219  TExtRef extension; // empty
220 
222  IExtensionPoint& point = *(it->second);
223  extension = point.GetExtension(extension_id);
224  if(extension) {
225  break;
226  }
227  }
228  return extension;
229 }
230 
231 
233  CExtensionRegistry::GetExtension(const string& ext_point_id,
234  const string& extension_id)
235 {
236  CMutexGuard guard(m_Mutex);
237 
238  TExtRef extension;
239  TExtPointRef point = GetExtensionPoint(ext_point_id);
240  if(point) {
241  extension = point->GetExtension(extension_id);
242  }
243  return extension;
244 }
245 
246 
248  CExtensionRegistry::GetExtensionPoint(const string& ext_point_id)
249 {
250  CMutexGuard guard(m_Mutex);
251 
252  TExtPointRef point;
253  TIdToPointMap::iterator it = m_IdToPoint.find(ext_point_id);
254  if(it != m_IdToPoint.end()) {
255  point = it->second;
256  }
257  return point;
258 }
259 
260 
261 bool CExtensionRegistry::GetExtensions(const string& ext_point_id,
262  TExtVec& extensions)
263 {
264  CMutexGuard guard(m_Mutex);
265 
266  TExtPointRef point;
267  TIdToPointMap::iterator it = m_IdToPoint.find(ext_point_id);
268  if(it != m_IdToPoint.end()) {
269  point = it->second;
270  point->GetExtensions(extensions);
271  return true;
272  }
273  return false;
274 }
275 
276 
278 {
279  CMutexGuard guard(m_Mutex);
280 
282  ext_points.push_back(it->second);
283  }
284 }
285 
286 
288 {
289  return RemoveExtension(extension.GetExtensionIdentifier());
290 }
291 
292 
293 bool CExtensionRegistry::RemoveExtension(const string& ext_id)
294 {
295  CMutexGuard guard(m_Mutex);
296 
297  bool ok = false;
299  IExtensionPoint& point = *it->second;
300  bool res = point.RemoveExtension(ext_id);
301  ok |= res;
302  }
303  return ok;
304 }
305 
306 
307 bool CExtensionRegistry::RemoveExtension(const string& ext_point_id, IExtension& extension)
308 {
309  TExtPointRef point = GetExtensionPoint(ext_point_id);
310  if(point) {
311  return point->RemoveExtension(extension);
312  }
313  return false;
314 }
315 
316 
317 bool CExtensionRegistry::RemoveExtension(const string& ext_point_id, const string& ext_id)
318 {
319  TExtPointRef point = GetExtensionPoint(ext_point_id);
320  if(point) {
321  return point->RemoveExtension(ext_id);
322  }
323  return false;
324 }
325 
326 
328 {
329  CMutexGuard guard(m_Mutex);
330 
332  if(it->second.GetPointer() == &ext_point) {
333  m_IdToPoint.erase(it);
334  return true;
335  }
336  }
337  return false;
338 }
339 
340 ///////////////////////////////////////////////////////////////////////////////
341 /// CExtensionPointDeclaration
343  const string& label)
344 {
346  reg->AddExtensionPoint(id, label);
347 }
348 
350 {
352  reg->AddExtensionPoint(ext_point);
353 }
354 
355 ///////////////////////////////////////////////////////////////////////////////
356 /// CExtensionDeclaration
358  IExtension* extension)
359 {
361  reg->AddExtension(ext_point_id, *extension);
362 }
363 
364 
CExtensionPoint - standard implementation of IExtensionPoint.
CRef –.
Definition: ncbiobj.hpp:618
T & Get(void)
Create the variable if not created yet, return the reference.
IExtensionPoint.
Definition: extension.hpp:85
IExtension IExtension interface represents an abstract pluggable component.
Definition: extension.hpp:57
void erase(iterator pos)
Definition: map.hpp:167
const_iterator end() const
Definition: map.hpp:152
const_iterator find(const key_type &key) const
Definition: map.hpp:153
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
virtual string GetIdentifier() const =0
TIdToExtMap m_IdToExt
virtual bool RemoveExtension(IExtension &extension)
removes the given Extension from all Extension Points in the Registry returns false if the Extension ...
virtual TExtRef GetExtension(const string &ext_id)
virtual TExtRef GetExtension(const string &extension_id)
finds and returns a reference to an Extension with the specified Id, search is performed across all P...
TIdToPointMap m_IdToPoint
CExtensionDeclaration(const string &ext_point_id, IExtension *extension)
CExtensionDeclaration.
virtual bool RemoveExtensionPoint(IExtensionPoint &ext_point)
removes the specified Extension Point from the Registry.
static CIRef< IExtensionRegistry > GetInstance()
provides access to the Singleton
virtual string GetExtensionIdentifier() const =0
returns the unique human-readable identifier for the extension the id should use lowercase letters se...
static CSafeStaticRef< CExtensionRegistry > sm_Instance
CExtensionRegistry.
CRef< CExtensionPoint > x_CreateExtensionPoint(const string &ext_point_id, const string &ext_point_label)
virtual void GetExtensions(TExtVec &extensions)
virtual bool AddExtension(IExtension &extension)
virtual TExtRef GetExtension(const string &ext_id)=0
virtual void SetLabel(const string &label)
virtual bool GetExtensions(const string &ext_point_id, TExtVec &extensions)
finds an Extension Point with the given Id and fills the given container with references to all Exten...
virtual void GetExtensionPoints(TExtPointVec &ext_points)
fills the given container with all Extension Points registered
vector< TExtRef > TExtVec
Definition: extension.hpp:88
CIRef< IExtension > TExtRef
Definition: extension.hpp:87
vector< TExtPointRef > TExtPointVec
Definition: extension.hpp:127
virtual TExtPointRef GetExtensionPoint(const string &ext_point_id)
returns a reference to an Extension Point with the given identifier, returns null if there the Id is ...
virtual bool AddExtensionPoint(IExtensionPoint &ext_point)
adds a new Extension Point, returns true if successful
virtual string GetIdentifier() const
CExtensionPointDeclaration(const string &id, const string &label)
CExtensionPointDeclaration.
CExtensionPoint(const string &id, const string &label)
CExtensionPoint - default implementation of IExtensionPoint.
vector< TExtRef > TExtVec
Definition: extension.hpp:125
virtual bool AddExtension(const string &ext_point_id, IExtension &extension)
adds a new Extension to an Extension Point, if the Point does not exists creates a new one,...
virtual bool RemoveExtension(IExtension &extension)
virtual bool RemoveExtension(IExtension &extension)=0
virtual string GetLabel() const
TObjectType * GetPointer(void) THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:998
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static const char label[]
#define _ASSERT
Modified on Fri Sep 20 14:57:10 2024 by modify_doxy.py rev. 669887