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

Go to the SVN repository for this file.

1 /* $Id: interface_registry.cpp 40531 2018-03-02 21:27:20Z katargir $
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: Roman Katargin
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <corelib/ncbimtx.hpp>
35 
37 
38 DEFINE_STATIC_MUTEX(s_ObjFactoryMutex);
39 
40 void CInterfaceRegistry::RegisterFactory(const string& interface_name, IInterfaceFactory* factory)
41 {
42  RegisterFactory(interface_name, NULL, factory);
43 }
44 
45 void CInterfaceRegistry::RegisterFactory(const string& interface_name, IObjectInterfaceFactory* factory)
46 {
47  RegisterFactory(interface_name, NULL, factory);
48 }
49 
50 void CInterfaceRegistry::RegisterFactory(const string& interface_name,
51  TTypeInfo info,
52  IInterfaceFactory* factory)
53 {
54  CMutexGuard LOCK(s_ObjFactoryMutex);
55  x_GetInstance().m_Interfaces[info][interface_name] = CRef<IInterfaceFactory>(factory);
56 }
57 
58 void CInterfaceRegistry::RegisterFactory(const string& interface_name,
60  IObjectInterfaceFactory* factory)
61 {
62  CMutexGuard LOCK(s_ObjFactoryMutex);
64 }
65 
67  const string& interface_name,
69  ICreateParams* params)
70 {
71  CMutexGuard LOCK(s_ObjFactoryMutex);
72 
73  CInterfaceRegistry& factory = x_GetInstance();
74 
76  if (it == factory.m_Interfaces.end()) // Search default factory
77  it = factory.m_Interfaces.find(NULL);
78  if (it == factory.m_Interfaces.end())
79  return NULL;
80 
81  TIfFactoryMap::const_iterator it2 = it->second.find(interface_name);
82  if (it2 != it->second.end())
83  return it2->second->CreateInterface(params);
84 
85  return NULL;
86 }
87 
89  const string& interface_name,
90  SConstScopedObject& object,
91  ICreateParams* params)
92 {
93  CMutexGuard LOCK(s_ObjFactoryMutex);
94 
95  const CSerialObject* cso = dynamic_cast<const CSerialObject*>(object.object.GetPointer());
96  if (!cso)
97  return nullptr;
98 
99  try {
100  TTypeInfo info = cso->GetThisTypeInfo();
101 
102  CInterfaceRegistry& factory = x_GetInstance();
103 
105  if (it != factory.m_ObjectInterfaces.end()) {
106  TIfObjectFactoryMap::const_iterator it2 = it->second.find(interface_name);
107  if (it2 != it->second.end())
108  return it2->second->CreateInterface(object, params);
109  }
110  it = factory.m_ObjectInterfaces.find(NULL);
111  if (it != factory.m_ObjectInterfaces.end()) {
112  TIfObjectFactoryMap::const_iterator it2 = it->second.find(interface_name);
113  if (it2 != it->second.end())
114  return it2->second->CreateInterface(object, params);
115  }
116 
117  return CreateInterface(interface_name, info, params);
118  }
119  catch (const exception&)
120  {
121  return nullptr;
122  }
123 }
124 
125 bool CInterfaceRegistry::ObjectHasInterface(const string& interface_name, SConstScopedObject& object)
126 {
127  CMutexGuard LOCK(s_ObjFactoryMutex);
128 
129  const CSerialObject* cso = dynamic_cast<const CSerialObject*>(object.object.GetPointer());
130  if (!cso)
131  return false;
132 
133  TTypeInfo info = cso->GetThisTypeInfo();
134 
135  CInterfaceRegistry& factory = x_GetInstance();
136 
138  if (it != factory.m_ObjectInterfaces.end()) {
139  TIfObjectFactoryMap::const_iterator it2 = it->second.find(interface_name);
140  if (it2 != it->second.end())
141  return true;
142  }
143  it = factory.m_ObjectInterfaces.find(NULL);
144  if (it != factory.m_ObjectInterfaces.end()) {
145  TIfObjectFactoryMap::const_iterator it2 = it->second.find(interface_name);
146  if (it2 != it->second.end())
147  return true;
148  }
149 
150  return false;
151 }
152 
154 {
155  static CInterfaceRegistry inst_;
156  return inst_;
157 }
158 
static CObject * CreateInterface(const string &interface_name, TTypeInfo info, ICreateParams *params)
TTypeIfFactoryMap m_Interfaces
static bool ObjectHasInterface(const string &interface_name, SConstScopedObject &object)
static void RegisterFactory(const string &interface_name, IInterfaceFactory *factory)
static CInterfaceRegistry & x_GetInstance()
TTypeIfObjectFactoryMap m_ObjectInterfaces
CObject –.
Definition: ncbiobj.hpp:180
CRef –.
Definition: ncbiobj.hpp:618
Base class for all serializable objects.
Definition: serialbase.hpp:150
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
const_iterator end() const
Definition: map.hpp:152
const_iterator find(const key_type &key) const
Definition: map.hpp:153
#define NULL
Definition: ncbistd.hpp:225
virtual const CTypeInfo * GetThisTypeInfo(void) const =0
#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_STATIC_MUTEX(s_ObjFactoryMutex)
static MDB_envinfo info
Definition: mdb_load.c:37
Multi-threading – mutexes; rw-locks; semaphore.
Modified on Mon Jun 24 05:25:07 2024 by modify_doxy.py rev. 669887