WvStreams
include/xplc/utils.h
Go to the documentation of this file.
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-2003, Pierre Phaneuf
5 * Copyright (C) 2001, 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 * As a special exception, you may use this file as part of a free
24 * software library without restriction. Specifically, if other files
25 * instantiate templates or use macros or inline functions from this
26 * file, or you compile this file and link it with other files to
27 * produce an executable, this file does not by itself cause the
28 * resulting executable to be covered by the GNU Lesser General Public
29 * License. This exception does not however invalidate any other
30 * reasons why the executable file might be covered by the GNU Lesser
31 * General Public License.
32 */
33
34#ifndef __XPLC_UTILS_H__
35#define __XPLC_UTILS_H__
36
37#if defined(__GNUC__) && __GNUC__ > 3
38# pragma GCC system_header
39#endif
40
46#include <stddef.h>
47#include <xplc/core.h>
48#include <xplc/IWeakRef.h>
49
53struct UUID_Info {
55 const UUID* iid;
56 ptrdiff_t delta;
58};
59
63#define UUID_MAP_BEGIN(component) const UUID_Info component::xplc_iobject_uuids[] = {
64
68#define UUID_MAP_ENTRY(iface) { &iface##_IID, reinterpret_cast<ptrdiff_t>(static_cast<iface*>(reinterpret_cast<ThisXPLCComponent*>(1))) - 1 },
69
75#define UUID_MAP_ENTRY_2(iface, iface2) { &iface##_IID, reinterpret_cast<ptrdiff_t>(static_cast<iface2*>(reinterpret_cast<ThisXPLCComponent*>(1))) - 1 },
76
80#define UUID_MAP_END { 0, 0 } };
81
82class WeakRef;
83
91 unsigned int refcount;
99 }
103 IObject* getInterface(void* self, const UUID& uuid,
104 const UUID_Info* uuidlist);
105};
106
107#ifndef xplcdelete
112#define xplcdelete delete
113#endif
114
123#define IMPLEMENT_IOBJECT(component) \
124private: \
125 IObjectImplInternal xplc_iobject_internal; \
126 static const UUID_Info xplc_iobject_uuids[]; \
127 typedef component ThisXPLCComponent; \
128public: \
129 virtual unsigned int addRef() { \
130 return ++xplc_iobject_internal.refcount; \
131 } \
132 virtual unsigned int release() { \
133 if(--xplc_iobject_internal.refcount) \
134 return xplc_iobject_internal.refcount; \
135 /* protect against re-entering the destructor */ \
136 xplc_iobject_internal.refcount = 1; \
137 if(xplc_iobject_internal.weakref) { \
138 xplc_iobject_internal.weakref->release(); \
139 xplc_iobject_internal.weakref->object = 0; \
140 } \
141 xplcdelete this; \
142 return 0; \
143 } \
144 virtual IObject* getInterface(const UUID& uuid) { \
145 return xplc_iobject_internal.getInterface(this, uuid, xplc_iobject_uuids); \
146 } \
147 virtual IWeakRef* getWeakRef() { \
148 if(!xplc_iobject_internal.weakref) \
149 xplc_iobject_internal.weakref = new WeakRef(reinterpret_cast<IObject*>(reinterpret_cast<ptrdiff_t>(this) + xplc_iobject_uuids->delta)); \
150 xplc_iobject_internal.weakref->addRef(); \
151 return xplc_iobject_internal.weakref; \
152 }
153
158class WeakRef: public IWeakRef {
160public:
163 virtual IObject* getObject() {
164 if(object)
165 object->addRef();
166
167 return object;
168 }
173 object(aObj) {
174 }
175};
176
183template<class Interface>
184Interface* get(IObject* aObj) {
185 if(!aObj)
186 return 0;
187
188 return static_cast<Interface*>(aObj->getInterface(XPLC_IID<Interface>::get()));
189}
190
197template<class Interface>
198Interface* mutate(IObject* aObj) {
199 Interface* rv;
200
201 if(!aObj)
202 return 0;
203
204 rv = static_cast<Interface*>(aObj->getInterface(XPLC_IID<Interface>::get()));
205
206 aObj->release();
207
208 return rv;
209}
210
211#endif /* __XPLC_UTILS_H__ */
The basic interface which is included by all other XPLC interfaces and objects.
virtual unsigned int addRef()=0
Indicate you are using this object.
virtual IObject * getInterface(const UUID &)=0
Returns the requested XPLC interface.
virtual unsigned int release()=0
Indicate that you are finished using this object.
Represents a weak reference to another object.
Common implementation of a weak reference.
virtual IObject * getObject()
Obtains an addRef()'d strong reference to the referenced object.
WeakRef(IObject *aObj)
Initialize a weak reference.
IObject * object
The object that the weak reference is pointing at.
#define IMPLEMENT_IOBJECT(component)
Helper macro to implement the IObject methods automatically.
Interface * mutate(IObject *aObj)
A version of get() that releases its parameter.
Interface * get(IObject *aObj)
XPLC equivalent to dynamic_cast.
IObject * getInterface(void *self, const UUID &uuid, const UUID_Info *uuidlist)
Used to implement IObject::getInterface().
WeakRef * weakref
Pointer to a weak reference object.
Utility structure used for the interface map.