wvdbusserver: implement NameHasOwner request.
[wvstreams.git] / include / wvmoniker.h
blob71121b961fea3e1b999a24f31a78e0bf694f2dd4
1 /* -*- Mode: C++ -*-
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
5 * Support for monikers, which are strings that you can pass to a magic
6 * factory to get objects supporting a particular interface, without actually
7 * knowing anything about the constructor for those objects.
8 */
9 #ifndef __WVMONIKER_H
10 #define __WVMONIKER_H
12 #include "wvstring.h"
13 #include "wvxplc.h"
15 class WvMonikerRegistry;
17 typedef void *WvMonikerCreateFunc(WvStringParm parms, IObject *obj);
19 /**
20 * WvMonikerBase is an auto-registration class for putting things into
21 * a WvMonikerRegistry. When a WvMonikerBase instance is created, it
22 * registers a moniker prefix ("test:", "ssl:", "ini:", etc) and a factory
23 * function that can be used to create an IObject using that prefix.
25 * When the instance is destroyed, it auto-unregisters the moniker prefix
26 * from the registry.
28 * You can't actually create one of these, because it's not typesafe. See
29 * WvMoniker<T> instead.
31 class WvMonikerBase
33 protected:
34 WvMonikerBase(const UUID &iid, WvStringParm _id,
35 WvMonikerCreateFunc *func, const bool override = false);
36 ~WvMonikerBase();
38 public:
39 WvString id;
40 WvMonikerRegistry *reg;
44 /**
45 * A type-safe version of WvMonikerBase that lets you provide create functions
46 * for object types other than IObject. (The objects themselves have to
47 * be derived from IObject, however.)
49 * See WvMonikerBase for details.
51 * Example:
52 * static IWvStream *createfunc(WvStringParm s, IObject *obj,
53 * void *userdata)
54 * {
55 * return new WvStream;
56 * }
58 * static WvMoniker<IWvStream> registration("ssl", createfunc);
60 template <class T>
61 class WvMoniker : public WvMonikerBase
63 public:
64 typedef T *CreateFunc(WvStringParm parms, IObject *obj);
66 WvMoniker(WvStringParm _id, CreateFunc *_func, const bool override = false)
67 : WvMonikerBase(XPLC_IID<T>::get(), _id, (WvMonikerCreateFunc *)_func,
68 override)
70 // this looks pointless, but it ensures that T* can be safely,
71 // automatically downcast to IObject*. That means T is really derived
72 // from IObject, which is very important. The 'for' avoids a
73 // warning.
74 for(IObject *silly = (T *)NULL; silly; )
80 /**
81 * Create an object registered in a WvMonikerRegistry. The iid specifies
82 * which registry to look in, and s, obj, and userdata are the parameters to
83 * supply to the object's factory. Most factories need only 's', which is the
84 * moniker itself.
86 * Most people don't use this function. See the templated, type-safe version
87 * of wvcreate() below.
89 void *wvcreate(const UUID &iid, WvStringParm s, IObject *obj);
92 /**
93 * Create an object registered in a WvMonikerRegistry. Exactly which
94 * registry is determined by the template type T.
96 * s, obj, and userdata are the parameters to supply to the object's
97 * factory. Most factories need only 's', which is the moniker itself.
99 * Example:
100 * IWvStream *s = wvcreate<IWvStream>("tcp:localhost:25");
101 * IWvStream *s_ssl = wvcreate<IWvStream>("ssl:", s);
103 template <class T>
104 inline T *wvcreate(WvStringParm s, IObject *obj = 0)
106 return (T *)(wvcreate(XPLC_IID<T>::get(), s, obj));
110 #endif // __WVMONIKER_H