2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
5 * Support for monikers, which are strings that you can pass to a magic
6 * factory to get objects supporting a particular interface. See wvmoniker.h.
8 #include "wvmonikerregistry.h"
14 # define DEBUGLOG(fmt, args...) fprintf(stderr, fmt, ## args)
17 # define DEBUGLOG(fmt, args...)
18 #else // MS Visual C++ doesn't support varags preproc macros
24 static unsigned WvHash(const UUID
&_uuid
)
27 unsigned int *uuid
= (unsigned int *)&_uuid
;
28 int max
= sizeof(UUID
)/sizeof(*uuid
);
30 for (int count
= 0; count
< max
; count
++)
37 DeclareWvScatterDict(WvMonikerRegistry
, UUID
, reg_iid
);
38 static WvMonikerRegistryDict
*regs
;
42 WvMonikerRegistry::WvMonikerRegistry(const UUID
&iid
)
43 : reg_iid(iid
), dict(10)
45 DEBUGLOG("WvMonikerRegistry creating.\n");
50 WvMonikerRegistry::~WvMonikerRegistry()
52 DEBUGLOG("WvMonikerRegistry destroying.\n");
56 void WvMonikerRegistry::add(WvStringParm id
, WvMonikerCreateFunc
*func
)
58 DEBUGLOG("WvMonikerRegistry register(%s).\n", id
.cstr());
59 wvassert(!dict
[id
], id
);
60 dict
.add(new Registration(id
, func
), true);
64 void WvMonikerRegistry::del(WvStringParm id
)
66 DEBUGLOG("WvMonikerRegistry unregister(%s).\n", id
.cstr());
68 dict
.remove(dict
[id
]);
72 void *WvMonikerRegistry::create(WvStringParm _s
, IObject
*obj
)
75 WvString
s(trim_string(t
.edit()));
77 char *cptr
= strchr(s
.edit(), ':');
83 DEBUGLOG("WvMonikerRegistry create object ('%s' '%s').\n", s
.cstr(), cptr
);
85 Registration
*r
= dict
[s
];
87 return r
->func(cptr
, obj
);
93 WvMonikerRegistry
*WvMonikerRegistry::find_reg(const UUID
&iid
)
95 DEBUGLOG("WvMonikerRegistry find_reg.\n");
98 regs
= new WvMonikerRegistryDict(10);
100 WvMonikerRegistry
*reg
= (*regs
)[iid
];
104 // we have to make one!
105 reg
= new WvMonikerRegistry(iid
);
106 regs
->add(reg
, true);
107 reg
->addRef(); // one reference for being in the list at all
115 IObject
*WvMonikerRegistry::getInterface(const UUID
&uuid
)
118 if (uuid
.equals(IObject_IID
))
125 // we don't really support any interfaces for now.
131 unsigned int WvMonikerRegistry::addRef()
133 DEBUGLOG("WvMonikerRegistry addRef.\n");
138 unsigned int WvMonikerRegistry::release()
140 DEBUGLOG("WvMonikerRegistry release.\n");
147 // the list has one reference to us, but it's no longer needed.
148 // Note: remove() will delete this object!
158 /* protect against re-entering the destructor */
165 WvMonikerBase::WvMonikerBase(const UUID
&iid
, WvStringParm _id
,
166 WvMonikerCreateFunc
*func
)
169 DEBUGLOG("WvMoniker creating(%s).\n", id
.cstr());
170 reg
= WvMonikerRegistry::find_reg(iid
);
176 WvMonikerBase::~WvMonikerBase()
178 DEBUGLOG("WvMoniker destroying(%s).\n", id
.cstr());
187 void *wvcreate(const UUID
&iid
, WvStringParm moniker
, IObject
*obj
)
189 assert(!moniker
.isnull());
190 // fprintf(stderr, "wvcreate: Looking for '%s'\n", moniker.cstr());
191 WvMonikerRegistry
*reg
= WvMonikerRegistry::find_reg(iid
);
194 void *ret
= reg
->create(moniker
, obj
);