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

Go to the SVN repository for this file.

1 #ifndef GUI_OBJUTILS___OBJECT_FACTORY__HPP
2 #define GUI_OBJUTILS___OBJECT_FACTORY__HPP
3 
4 /* $Id: interface_registry.hpp 32328 2015-02-05 19:03:30Z katargir $
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  * Authors: Roman Katargin
30  *
31  * File Description:
32  *
33  */
34 
35 #include <gui/gui_export.h>
36 #include <serial/typeinfo.hpp>
37 #include <serial/objectinfo.hpp>
38 
39 #include <gui/objutils/objects.hpp>
40 
42 
44 {
45 public:
46  virtual ~ICreateParams() {}
47 };
48 
49 class IInterfaceFactory : public CObject
50 {
51 public:
52  virtual ~IInterfaceFactory() {}
53  virtual CObject* CreateInterface(ICreateParams* params) const = 0;
54 };
55 
57 {
58 public:
60  virtual CObject* CreateInterface(SConstScopedObject& object, ICreateParams* params) const = 0;
61 };
62 
63 template <class T> class CTypeInterfaceFactory : public IInterfaceFactory
64 {
65 public:
66  virtual CObject* CreateInterface(ICreateParams* /*params*/) const { return new T(); }
67 };
68 
69 template <class T> class CObjectInterfaceFactory : public IObjectInterfaceFactory
70 {
71 public:
72  virtual CObject* CreateInterface(SConstScopedObject& object, ICreateParams* params) const
73  {
74  return T::CreateObject(object, params);
75  }
76 };
77 
79 {
80 public:
81  static void RegisterFactory(const string& interface_name, IInterfaceFactory* factory);
82  static void RegisterFactory(const string& interface_name, IObjectInterfaceFactory* factory);
83  static void RegisterFactory(const string& interface_name, TTypeInfo info, IInterfaceFactory* factory);
84  static void RegisterFactory(const string& interface_name, TTypeInfo info, IObjectInterfaceFactory* factory);
85 
86  static CObject* CreateInterface(const string& interface_name,
88  ICreateParams* params);
89 
90  static CObject* CreateInterface(const string& interface_name,
91  SConstScopedObject& object,
92  ICreateParams* params);
93 
94  static bool ObjectHasInterface(const string& interface_name, SConstScopedObject& object);
95 
96 private:
99 
102 
103  static CInterfaceRegistry& x_GetInstance();
105 
108 };
109 
110 template <typename T> T* CreateInterface(TTypeInfo type_info, ICreateParams* params = NULL)
111 {
112  return dynamic_cast<T*>
113  (CInterfaceRegistry::CreateInterface(typeid(T).name(), type_info, params));
114 }
115 
116 template <typename T> T* CreateObjectInterface(SConstScopedObject& object, ICreateParams* params)
117 {
118  return dynamic_cast<T*>
119  (CInterfaceRegistry::CreateInterface(typeid(T).name(), object, params));
120 }
121 
122 template <typename T> bool ObjectHasInterface(SConstScopedObject& object)
123 {
124  return CInterfaceRegistry::ObjectHasInterface(typeid(T).name(), object);
125 }
126 
128 
129 #endif // GUI_OBJUTILS___OBJECT_FACTORY__HPP
map< string, CRef< IInterfaceFactory > > TIfFactoryMap
map< string, CRef< IObjectInterfaceFactory > > TIfObjectFactoryMap
static CObject * CreateInterface(const string &interface_name, TTypeInfo info, ICreateParams *params)
TTypeIfFactoryMap m_Interfaces
static bool ObjectHasInterface(const string &interface_name, SConstScopedObject &object)
map< TTypeInfo, TIfFactoryMap > TTypeIfFactoryMap
map< TTypeInfo, TIfObjectFactoryMap > TTypeIfObjectFactoryMap
TTypeIfObjectFactoryMap m_ObjectInterfaces
virtual CObject * CreateInterface(SConstScopedObject &object, ICreateParams *params) const
CObject –.
Definition: ncbiobj.hpp:180
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
virtual CObject * CreateInterface(ICreateParams *) const
virtual ~ICreateParams()
virtual CObject * CreateInterface(ICreateParams *params) const =0
virtual CObject * CreateInterface(SConstScopedObject &object, ICreateParams *params) const =0
Definition: map.hpp:338
#define T(s)
Definition: common.h:230
#define NULL
Definition: ncbistd.hpp:225
#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_GUIOBJUTILS_EXPORT
Definition: gui_export.h:512
Defines to provide correct exporting from DLLs in Windows.
T * CreateInterface(TTypeInfo type_info, ICreateParams *params=NULL)
T * CreateObjectInterface(SConstScopedObject &object, ICreateParams *params)
bool ObjectHasInterface(SConstScopedObject &object)
static MDB_envinfo info
Definition: mdb_load.c:37
Modified on Mon Feb 26 04:01:54 2024 by modify_doxy.py rev. 669887