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

Go to the SVN repository for this file.

1 #ifndef GUI_FRAMEWORK___SERVICE__HPP
2 #define GUI_FRAMEWORK___SERVICE__HPP
3 
4 /* $Id: service.hpp 31428 2014-10-02 17:42:02Z 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: Andrey Yazhuk
30  *
31  * File Description:
32  *
33  */
34 
35 /** @addtogroup GUI_FRAMEWORK
36 *
37 * @{
38 */
39 
40 #include <corelib/ncbiobj.hpp>
41 
42 #include <gui/gui_export.h>
43 
45 
46 
47 ///////////////////////////////////////////////////////////////////////////////
48 /// IService an abstraction that represents an application component
49 /// providing specific functional capabilities via a special interface.
50 /// The interface can be produced by applying dynamic_cast<> to IService pointer.
51 /// The main intention of IService is to serve as a polymorphic base class and
52 /// to provide basic lifetime management. Classes implementing IService must be
53 /// CRef-compatible.
54 
56 {
57 public:
58  virtual void InitService() = 0;
59  virtual void ShutDownService() = 0;
60 
61  virtual ~IService() {};
62 };
63 
64 
65 ///////////////////////////////////////////////////////////////////////////////
66 /// IServiceLocator - an abstract mechanism for locating services.
67 /// IServiceLocator locates a service by its name. In most cases the name of
68 /// a service class or interface should be used as the name of the service.
69 
71 {
72 public:
73  /// returns true if the service exists within the scope of the locator
74  virtual bool HasService(const string& name) = 0;
75 
76  /// retrieves the service
77  virtual CIRef<IService> GetService(const string& name) = 0;
78 
79  virtual ~IServiceLocator() {}
80 
81  /// retrieves a typed reference to a service, the name of C++ type
82  /// is used as the name of the service.
83  template<class T> CIRef<T> GetServiceByType();
84 };
85 
86 
87 /// helper function that uses C++ class name of the service as its name
88 /// the function can be used like this:
89 /// CIRef<TheService> srv = srv_locator->GetServiceByType<TheService>();
90 ///
91 template<class T> inline CIRef<T> IServiceLocator::GetServiceByType()
92 {
93  string t_name(typeid(T).name());
94  CIRef<IService> srv = GetService(t_name);
95  CIRef<T> t_srv(dynamic_cast<T*>(srv.GetPointer()));
96  return t_srv;
97 }
98 
99 ///////////////////////////////////////////////////////////////////////////////
100 /// IServiceLocatorConsumer - classes that need IServiceLocator should
101 /// implement this interface.
103 {
104 public:
105  virtual void SetServiceLocator(IServiceLocator* locator) = 0;
106 
108 };
109 
110 
111 template<class T> void Consumer_SetLocator(T* obj, IServiceLocator* locator)
112 {
113  IServiceLocatorConsumer* consumer =
114  dynamic_cast<IServiceLocatorConsumer*>(obj);
115  if(consumer) {
116  consumer->SetServiceLocator(locator);
117  }
118 }
119 
120 
122 
123 /* @} */
124 
125 #endif // GUI_FRAMEWORK___SERVICE__HPP
126 
IServiceLocatorConsumer - classes that need IServiceLocator should implement this interface.
Definition: service.hpp:103
IServiceLocator - an abstract mechanism for locating services.
Definition: service.hpp:71
IService an abstraction that represents an application component providing specific functional capa...
Definition: service.hpp:56
#define T(s)
Definition: common.h:230
void Consumer_SetLocator(T *obj, IServiceLocator *locator)
Definition: service.hpp:111
CIRef< T > GetServiceByType()
retrieves a typed reference to a service, the name of C++ type is used as the name of the service.
Definition: service.hpp:91
virtual ~IService()
Definition: service.hpp:61
virtual bool HasService(const string &name)=0
returns true if the service exists within the scope of the locator
virtual ~IServiceLocatorConsumer()
Definition: service.hpp:107
virtual void SetServiceLocator(IServiceLocator *locator)=0
virtual void InitService()=0
virtual void ShutDownService()=0
virtual CIRef< IService > GetService(const string &name)=0
retrieves the service
virtual ~IServiceLocator()
Definition: service.hpp:79
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
#define NCBI_GUIFRAMEWORK_EXPORT
Definition: gui_export.h:509
Defines to provide correct exporting from DLLs in Windows.
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
Modified on Tue Apr 23 07:40:07 2024 by modify_doxy.py rev. 669887