WvStreams
servmgr.cc
1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 *
3 * XPLC - Cross-Platform Lightweight Components
4 * Copyright (C) 2000-2004, Pierre Phaneuf
5 * Copyright (C) 2000, Stéphane Lajoie
6 * Copyright (C) 2002-2004, Net Integration Technologies, Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * USA
22 */
23
24#include <xplc/core.h>
25#include <xplc/utils.h>
26#include <xplc/factory.h>
27#include "servmgr.h"
28#include "catmgr.h"
29#include "statichandler.h"
30#include "moduleloader.h"
31#include "monikers.h"
32#include "new.h"
33#include "modulemgr.h"
34
39
40static ServiceManager* singleton;
41
42IServiceManager* XPLC_getServiceManager() {
43 if(singleton)
44 singleton->addRef();
45 else {
46 IStaticServiceHandler* handler;
47 IStaticServiceHandler* handler2;
48 IMonikerService* monikers;
49 IObject* obj;
50
51 singleton = new ServiceManager;
52
53 if(!singleton)
54 return 0;
55
56 handler = new StaticServiceHandler;
57 if(!handler) {
58 singleton->release();
59 return 0;
60 }
61
62 /*
63 * Populate the static service handler.
64 */
65
66 handler2 = new StaticServiceHandler;
67 if(handler2) {
68 handler->addObject(XPLC_staticServiceHandler, handler2);
69 singleton->addHandler(handler2);
70 handler2->release();
71 } else {
72 singleton->release();
73 return 0;
74 }
75
76 obj = new NewMoniker;
77 if(obj) {
78 handler->addObject(XPLC_newMoniker, obj);
79 obj->release();
80 }
81
82 obj = new CategoryManager;
83 if(obj) {
84 handler->addObject(XPLC_categoryManager, obj);
85 obj->release();
86 }
87
88 obj = new ModuleLoader;
89 if(obj) {
90 handler->addObject(XPLC_moduleLoader, obj);
91 obj->release();
92 }
93
94 obj = new ModuleManagerFactory;
95 if(obj) {
96 handler->addObject(XPLC_moduleManagerFactory, obj);
97 obj->release();
98 }
99
100 monikers = new MonikerService;
101 if(monikers) {
102 monikers->registerObject("new", XPLC_newMoniker);
103 handler->addObject(XPLC_monikers, monikers);
104 monikers->release();
105 }
106
107 singleton->addHandler(handler);
108
109 handler->release();
110 }
111
112 return singleton;
113}
114
115ServiceManager::~ServiceManager() {
116 HandlerNode* next;
117
118 while(handlers) {
119 next = handlers->next;
120 delete handlers;
121 handlers = next;
122 }
123
124 if(singleton == this)
125 singleton = 0;
126}
127
129 HandlerNode* node;
130 HandlerNode** ptr;
131
132 ptr = &handlers;
133 node = *ptr;
134 while(node) {
135 if(node->handler == aHandler)
136 break;
137
138 if(node->intercept) {
139 ptr = &node->next;
140 }
141 node = node->next;
142 }
143
144 /*
145 * The handler is already there.
146 */
147 if(node)
148 return;
149
150 node = new HandlerNode(aHandler, *ptr, false);
151 *ptr = node;
152}
153
155 HandlerNode* node;
156
157 node = handlers;
158 while(node) {
159 if(node->handler == aHandler)
160 break;
161
162 node = node->next;
163 }
164
165 /*
166 * The handler is already there.
167 */
168 if(node)
169 return;
170
171 node = new HandlerNode(aHandler, handlers, true);
172 handlers = node;
173}
174
176 HandlerNode* node;
177 HandlerNode** ptr;
178
179 ptr = &handlers;
180 node = *ptr;
181 while(node) {
182 if(node->handler == aHandler)
183 break;
184
185 ptr = &node->next;
186 node = *ptr;
187 }
188
189 /*
190 * The handler is already there.
191 */
192 if(node)
193 return;
194
195 node = new HandlerNode(aHandler, *ptr, false);
196 *ptr = node;
197}
198
200 HandlerNode* node;
201 HandlerNode** ptr;
202
203 node = handlers;
204 ptr = &handlers;
205 while(node) {
206 if(node->handler == aHandler) {
207 *ptr = node->next;
208 delete node;
209 break;
210 }
211
212 ptr = &node->next;
213 node = *ptr;
214 }
215}
216
218 IObject* obj;
219 HandlerNode* handler;
220
221 handler = handlers;
222 while(handler) {
223 obj = handler->handler->getObject(aUuid);
224
225 /*
226 * No need to addRef the object, the handler does it for us.
227 */
228 if(obj)
229 return obj;
230
231 handler = handler->next;
232 }
233
234 return 0;
235}
236
An interface for registering objects so they can be retrieved using a moniker.
virtual void registerObject(const char *prefix, const UUID &uuid)=0
Register an object to be retrieved with a moniker.
The basic interface which is included by all other XPLC interfaces and objects.
Definition: IObject.h:65
virtual unsigned int addRef()=0
Indicate you are using this object.
virtual unsigned int release()=0
Indicate that you are finished using this object.
Interface to an object which can be used to find other objects, given their UUIDs.
virtual IObject * getObject(const UUID &)=0
Get the object corresponding to the given UUID.
The XPLC service manager interface.
Service handler for statically linked components.
virtual void addObject(const UUID &, IObject *)=0
Adds an object to the static service handler.
Definition: new.h:28
virtual void removeHandler(IServiceHandler *)
Remove a handler from the list.
Definition: servmgr.cc:199
virtual IObject * getObject(const UUID &)
Get the object corresponding to the given UUID.
Definition: servmgr.cc:217
virtual void addLastHandler(IServiceHandler *)
Register a handler to be handled by this manager, explicitly adding it to the end of the list (lowest...
Definition: servmgr.cc:175
virtual void addHandler(IServiceHandler *)
Register a handler to be handled by this manager.
Definition: servmgr.cc:128
virtual void addFirstHandler(IServiceHandler *)
Register a handler to be handled by this manager, explicitly adding it to the beginning of the list (...
Definition: servmgr.cc:154
The structure underlying UUIDs.
Definition: uuid.h:94
Various utility functions, macros and templates.
#define UUID_MAP_END
Marks the end of an interface map.
Definition: utils.h:80
#define UUID_MAP_BEGIN(component)
Start the interface map for "component".
Definition: utils.h:63
#define UUID_MAP_ENTRY(iface)
Add an entry to an interface map.
Definition: utils.h:68