Back to index

lightning-sunbird  0.9+nobinonly
nsAEClassDispatcher.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Simon Fraser <sfraser@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 
00040 #include "nsAEUtils.h"
00041 
00042 #include "nsAEGenericClass.h"
00043 #include "nsAEClassDispatcher.h"
00044 
00045 
00046 /*----------------------------------------------------------------------------
00047        AEDispatchHandler 
00048        
00049        Ownership of the hanlder passes to this object
00050 ----------------------------------------------------------------------------*/
00051 
00052 AEDispatchHandler::AEDispatchHandler(DescType handlerClass, AEGenericClass* handler, Boolean deleteOnRemove /* = true*/ )
00053 :      mDeleteHandler(deleteOnRemove)
00054 ,      mHandlerClass(handlerClass)
00055 ,      mHandler(handler)
00056 {
00057        AE_ASSERT(mHandler, "No handler");
00058 }
00059 
00060 
00061 /*----------------------------------------------------------------------------
00062        AEDispatchHandler 
00063        
00064 ----------------------------------------------------------------------------*/
00065 AEDispatchHandler::~AEDispatchHandler()
00066 {
00067        if (mDeleteHandler)
00068               delete mHandler;
00069 }
00070 
00071 
00072 /*----------------------------------------------------------------------------
00073        DispatchEvent 
00074        
00075 ----------------------------------------------------------------------------*/
00076 void AEDispatchHandler::DispatchEvent(                         AEDesc *                    token,
00077                                                                              const AppleEvent *   appleEvent,
00078                                                                              AppleEvent *         reply)
00079 {
00080        AE_ASSERT(mHandler, "No handler");
00081        mHandler->DispatchEvent(token, appleEvent, reply);
00082 }
00083 
00084 /*----------------------------------------------------------------------------
00085        GetProperty 
00086        
00087 ----------------------------------------------------------------------------*/
00088 void AEDispatchHandler::GetProperty(                           DescType                    desiredClass,
00089                                                                              const AEDesc*        containerToken,
00090                                                                              DescType                    containerClass,
00091                                                                              DescType                    keyForm,
00092                                                                              const AEDesc*        keyData,
00093                                                                              AEDesc*                     resultToken)
00094 {
00095        AE_ASSERT(mHandler, "No handler");
00096        mHandler->GetProperty(desiredClass, containerToken, containerClass, keyForm, keyData, resultToken);
00097 }
00098 
00099 /*----------------------------------------------------------------------------
00100        GetDataFromListOrObject 
00101        
00102 ----------------------------------------------------------------------------*/
00103 void   AEDispatchHandler::GetDataFromListOrObject(             const AEDesc *              tokenOrTokenList,
00104                                                                              AEDesc *                    desiredTypes,
00105                                                                              AEDesc *                    data)
00106 {
00107        AE_ASSERT(mHandler, "No handler");
00108        mHandler->GetDataFromListOrObject(tokenOrTokenList, desiredTypes, data);
00109 }
00110 
00111 
00112 /*----------------------------------------------------------------------------
00113        GetItemFromContainer 
00114        
00115 ----------------------------------------------------------------------------*/
00116 void   AEDispatchHandler::GetItemFromContainer(                DescType                    desiredClass,
00117                                                                                     const AEDesc*        containerToken,
00118                                                                                     DescType                    containerClass, 
00119                                                                                     DescType                    keyForm,
00120                                                                                     const AEDesc*        keyData,
00121                                                                                     AEDesc*                     resultToken)
00122 {
00123        AE_ASSERT(mHandler, "No handler");
00124        mHandler->GetItemFromContainer(desiredClass, containerToken, containerClass, keyForm, keyData, resultToken);
00125 }
00126 
00127 
00128 /*----------------------------------------------------------------------------
00129        CompareObjects 
00130        
00131 ----------------------------------------------------------------------------*/
00132 void AEDispatchHandler::CompareObjects(                               DescType                    comparisonOperator,
00133                                                                                     const AEDesc *              object,
00134                                                                                     const AEDesc *              descriptorOrObject,
00135                                                                                     Boolean *                   result)
00136 {
00137        AE_ASSERT(mHandler, "No handler");
00138        mHandler->CompareObjects(comparisonOperator, object, descriptorOrObject, result);
00139 }
00140 
00141 
00142 
00143 /*----------------------------------------------------------------------------
00144        CountObjects 
00145        
00146 ----------------------------------------------------------------------------*/
00147 void AEDispatchHandler::CountObjects(                                 DescType                    desiredType,
00148                                                                                     DescType                    containerClass,
00149                                                                                     const AEDesc *              container,
00150                                                                                     long *               result)
00151 {
00152        AE_ASSERT(mHandler, "No handler");
00153        mHandler->CountObjects(desiredType, containerClass, container, result);
00154 }
00155 
00156 
00157 /*----------------------------------------------------------------------------
00158        CreateSelfSpecifier 
00159        
00160 ----------------------------------------------------------------------------*/
00161 void AEDispatchHandler::CreateSelfSpecifier(                          const AEDesc *              token,
00162                                                                                     AEDesc *                    outSpecifier)
00163 {
00164        AE_ASSERT(mHandler, "No handler");
00165        mHandler->CreateSelfSpecifier(token, outSpecifier);
00166 }
00167 
00168 #pragma mark -
00169 
00170 
00171 /*----------------------------------------------------------------------------
00172        AEDispatchTree 
00173        
00174 ----------------------------------------------------------------------------*/
00175 AEDispatchTree::AEDispatchTree()
00176 :      mTree(nil)
00177 {
00178        
00179        mTree = PatriciaInitTree(8 * sizeof(DescType));
00180        ThrowIfNil(mTree);
00181 }
00182 
00183 
00184 /*----------------------------------------------------------------------------
00185        ~AEDispatchTree 
00186        
00187 ----------------------------------------------------------------------------*/
00188 AEDispatchTree::~AEDispatchTree()
00189 {
00190        if (mTree)
00191               PatriciaFreeTree(mTree, FreeDispatchTreeNodeData, this);
00192 }
00193 
00194 
00195 /*----------------------------------------------------------------------------
00196        InsertHandler 
00197        
00198 ----------------------------------------------------------------------------*/
00199 void AEDispatchTree::InsertHandler(DescType handlerClass, AEGenericClass *handler, Boolean isDuplicate /* = false */)
00200 {
00201        AEDispatchHandler    *newHandler = new AEDispatchHandler(handlerClass, handler, !isDuplicate);
00202        unsigned char        key[5] = {0};                      
00203        int                         result;
00204        
00205        *(DescType *)key = handlerClass;
00206        
00207        result = PatriciaInsert(mTree, nil, key, newHandler, nil);
00208        if (result == kDuplicateKeyError || result == 1)
00209        {
00210               ThrowIfOSErr(kDuplicateKeyError);
00211        }
00212        else if (result != 0)
00213        {
00214               ThrowIfOSErr(result);
00215        }
00216 }
00217 
00218 
00219 /*----------------------------------------------------------------------------
00220        FindHandler 
00221        
00222 
00223 ----------------------------------------------------------------------------*/
00224 AEDispatchHandler* AEDispatchTree::FindHandler(DescType handlerClass)
00225 {
00226        AEDispatchHandler*   foundClass = nil;
00227        unsigned char        key[5] = {0};                      
00228        
00229        *(DescType *)key = handlerClass;
00230 
00231        (void)PatriciaSearch(mTree, key, (void**)&foundClass);
00232        
00233        return foundClass;
00234 }
00235 
00236 /*----------------------------------------------------------------------------
00237        ReplaceDispatchTreeNode 
00238        
00239        static
00240        
00241        if this ever gets called, it means we tried to insert a node for a duplicate class,
00242        which is an error. So return an error. We don't want to throw because the
00243        patricia code may not be exception-safe.
00244 ----------------------------------------------------------------------------*/
00245 int AEDispatchTree::ReplaceDispatchTreeNode(void *nodeData, unsigned char *key, void *replaceData)
00246 {
00247        return kDuplicateKeyError;
00248 }
00249 
00250 /*----------------------------------------------------------------------------
00251        FreeDispatchTreeNodeData 
00252        
00253        static
00254 ----------------------------------------------------------------------------*/
00255 int AEDispatchTree::FreeDispatchTreeNodeData(void *nodeData, unsigned char *key, void *refCon)
00256 {
00257        AEDispatchTree*      dispatchTree = reinterpret_cast<AEDispatchTree *>(refCon);
00258        AEDispatchHandler*   handler = reinterpret_cast<AEDispatchHandler *>(nodeData);
00259        
00260        delete handler;
00261        return 0;
00262 }
00263