Back to index

lightning-sunbird  0.9+nobinonly
SupportsMixin.h
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the MRJ Carbon OJI Plugin.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corp.
00018  * Portions created by the Initial Developer are Copyright (C) 2001
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Patrick C. Beard <beard@netscape.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039        SupportsMixin.h
00040        
00041        Experimental way to implement nsISupports interface.
00042        
00043        Uses table-driven approach for fast lookup.
00044        
00045        Aggregation support isn't quite here yet, it requires a helper object that is referenced only by the outer object,
00046        whose QueryInterace, AddRef, and Release act only locally (i.e. they call queryInterface, addRef, and release).
00047        However, if we're not using aggregation (hardly ever are) it seems wasteful to define the helper object.
00048        
00049        by Patrick C. Beard.
00050  */
00051 
00052 #pragma once
00053 
00054 #include <Types.h>
00055 
00056 #include "nsISupports.h"
00057 
00058 // #define SUPPORT_AGGREGATION
00059 
00060 struct InterfaceInfo {
00061        nsID mIID;                  // the IID of this interface.
00062        UInt32 mOffset;             // the offset of this interface.
00063 };
00064 
00065 class SupportsMixin {
00066 public:
00067        // These act locally on the current object, and are meant to be called by sub-classes.
00068        nsresult queryInterface(const nsIID& aIID, void** aInstancePtr);
00069        nsrefcnt addRef(void);
00070        nsrefcnt release(void);
00071 
00072 protected:
00073        SupportsMixin(void* instance, const InterfaceInfo interfaces[], UInt32 interfaceCount, nsISupports* outer = NULL);
00074        virtual ~SupportsMixin();
00075 
00076 #ifdef SUPPORT_AGGREGATION
00077        NS_METHOD OuterQueryInterface(REFNSIID aIID, void** aInstancePtr);
00078        NS_METHOD_(nsrefcnt) OuterAddRef(void);
00079        NS_METHOD_(nsrefcnt) OuterRelease(void);
00080 #endif
00081 
00082 private:
00083        void* mInstance;
00084        nsrefcnt mRefCount;
00085        const InterfaceInfo* mInterfaces;
00086        UInt32 mInterfaceCount;
00087 
00088 #ifdef SUPPORT_AGGREGATION
00089        nsISupports* mOuter;
00090        
00091        class Inner : public nsISupports {
00092        public:
00093               NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) { return mSupports->queryInterface(aIID, aInstancePtr); }
00094               NS_IMETHOD_(nsrefcnt) AddRef(void) { return mSupports->addRef(); }
00095               NS_IMETHOD_(nsrefcnt) Release(void) { return mSupports->release(); }
00096               
00097               Inner(SupportsMixin* supports) : mSupports(supports) {}
00098               
00099        private:
00100               SupportsMixin* mSupports;
00101        };
00102 
00103        Inner* mInner;
00104 #endif
00105 };
00106 
00107 #ifdef SUPPORT_AGGREGATION
00108 
00109 #define DECL_SUPPORTS_MIXIN \
00110        NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) { return OuterQueryInterface(aIID, aInstancePtr); } \
00111        NS_IMETHOD_(nsrefcnt) AddRef(void) { return OuterAddRef(); } \
00112        NS_IMETHOD_(nsrefcnt) Release(void) { return OuterRelease(); }
00113 
00114 #else
00115 
00116 #define DECL_SUPPORTS_MIXIN \
00117        NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) { return queryInterface(aIID, aInstancePtr); } \
00118        NS_IMETHOD_(nsrefcnt) AddRef(void) { return addRef(); } \
00119        NS_IMETHOD_(nsrefcnt) Release(void) { return release(); }
00120 
00121 #endif
00122 
00123 #define INTERFACE_OFFSET(leafType, interfaceType) \
00124        UInt32((interfaceType*) ((leafType*)0))