Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   File Members  

utils.h

Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * XPLC - Cross-Platform Lightweight Components
00004  * Copyright (C) 2000-2003, Pierre Phaneuf
00005  * Copyright (C) 2001, Stéphane Lajoie
00006  * Copyright (C) 2002-2004, Net Integration Technologies, Inc.
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation; either version 2.1 of
00011  * the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021  * USA
00022  *
00023  * As a special exception, you may use this file as part of a free
00024  * software library without restriction.  Specifically, if other files
00025  * instantiate templates or use macros or inline functions from this
00026  * file, or you compile this file and link it with other files to
00027  * produce an executable, this file does not by itself cause the
00028  * resulting executable to be covered by the GNU Lesser General Public
00029  * License.  This exception does not however invalidate any other
00030  * reasons why the executable file might be covered by the GNU Lesser
00031  * General Public License.
00032  */
00033 
00034 #ifndef __XPLC_UTILS_H__
00035 #define __XPLC_UTILS_H__
00036 
00042 #include <stddef.h>
00043 #include <xplc/core.h>
00044 #include <xplc/IWeakRef.h>
00045 
00049 struct UUID_Info {
00051   const UUID* iid;
00052   ptrdiff_t delta;
00054 };
00055 
00059 #define UUID_MAP_BEGIN(component) const UUID_Info component::xplc_iobject_uuids[] = {
00060 
00064 #define UUID_MAP_ENTRY(iface) { &iface##_IID, reinterpret_cast<ptrdiff_t>(static_cast<iface*>(reinterpret_cast<ThisXPLCComponent*>(1))) - 1 },
00065 
00071 #define UUID_MAP_ENTRY_2(iface, iface2) { &iface##_IID, reinterpret_cast<ptrdiff_t>(static_cast<iface2*>(reinterpret_cast<ThisXPLCComponent*>(1))) - 1 },
00072 
00076 #define UUID_MAP_END { 0, 0 } };
00077 
00078 class WeakRef;
00079 
00083 struct IObjectImplInternal {
00087   unsigned int refcount;
00093   WeakRef* weakref;
00094   IObjectImplInternal(): refcount(1), weakref(0) {
00095   }
00099   IObject* getInterface(void* self, const UUID& uuid,
00100                         const UUID_Info* uuidlist);
00101 };
00102 
00103 #ifndef xplcdelete
00104 
00108 #define xplcdelete delete
00109 #endif
00110 
00119 #define IMPLEMENT_IOBJECT(component) \
00120 private: \
00121   IObjectImplInternal xplc_iobject_internal; \
00122   static const UUID_Info xplc_iobject_uuids[]; \
00123   typedef component ThisXPLCComponent; \
00124 public: \
00125   virtual unsigned int addRef() { \
00126     return ++xplc_iobject_internal.refcount; \
00127   } \
00128   virtual unsigned int release() { \
00129     if(--xplc_iobject_internal.refcount) \
00130       return xplc_iobject_internal.refcount; \
00131     /* protect against re-entering the destructor */ \
00132     xplc_iobject_internal.refcount = 1; \
00133     if(xplc_iobject_internal.weakref) { \
00134       xplc_iobject_internal.weakref->release(); \
00135       xplc_iobject_internal.weakref->object = 0; \
00136     } \
00137     xplcdelete this; \
00138     return 0; \
00139   } \
00140   virtual IObject* getInterface(const UUID& uuid) { \
00141     return xplc_iobject_internal.getInterface(this, uuid, xplc_iobject_uuids); \
00142   } \
00143   virtual IWeakRef* getWeakRef() { \
00144     if(!xplc_iobject_internal.weakref) \
00145       xplc_iobject_internal.weakref = new WeakRef(reinterpret_cast<IObject*>(reinterpret_cast<ptrdiff_t>(this) + xplc_iobject_uuids->delta)); \
00146     xplc_iobject_internal.weakref->addRef(); \
00147     return xplc_iobject_internal.weakref; \
00148   }
00149 
00154 class WeakRef: public IWeakRef {
00155   IMPLEMENT_IOBJECT(WeakRef);
00156 public:
00158   IObject* object;
00159   virtual IObject* getObject() {
00160     if(object)
00161       object->addRef();
00162 
00163     return object;
00164   }
00168   WeakRef(IObject* aObj):
00169     object(aObj) {
00170   }
00171 };
00172 
00179 template<class Interface>
00180 Interface* get(IObject* aObj) {
00181   if(!aObj)
00182     return 0;
00183 
00184   return static_cast<Interface*>(aObj->getInterface(XPLC_IID<Interface>::get()));
00185 }
00186 
00193 template<class Interface>
00194 Interface* mutate(IObject* aObj) {
00195   Interface* rv;
00196 
00197   if(!aObj)
00198     return 0;
00199 
00200   rv = static_cast<Interface*>(aObj->getInterface(XPLC_IID<Interface>::get()));
00201 
00202   aObj->release();
00203 
00204   return rv;
00205 }
00206 
00207 #endif /* __XPLC_UTILS_H__ */

Generated on Sun Sep 26 04:01:25 2004 for XPLC by doxygen1.2.18