Back to index

lightning-sunbird  0.9+nobinonly
nsIBaseDOMException.h
Go to the documentation of this file.
00001 /* -*- Mode: IDL; 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.org 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) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Peter Van der Beken <peterv@netscape.com> (original author)
00024  *
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef nsIBaseDOMException_h___
00041 #define nsIBaseDOMException_h___
00042 
00043 #include "nsIDOMClassInfo.h"
00044 
00045 // {1f13b201-39fa-11d6-a7f2-df501ff820dc}
00046 #define NS_BASE_DOM_EXCEPTION_CID  \
00047 { 0x1f13b201, 0x39fa, 0x11d6, \
00048 { 0xa7, 0xf2, 0xdf, 0x50, 0x1f, 0xf8, 0x20, 0xdc } }
00049 
00050 // {731d9701-39f8-11d6-a7f2-b39073384c9c}
00051 #define NS_IBASEDOMEXCEPTION_IID  \
00052 { 0x731d9701, 0x39f8, 0x11d6, \
00053 { 0xa7, 0xf2, 0xb3, 0x90, 0x73, 0x38, 0x4c, 0x9c } }
00054 
00055 class nsIBaseDOMException : public nsISupports {
00056 public:  
00057   NS_DEFINE_STATIC_IID_ACCESSOR(NS_IBASEDOMEXCEPTION_IID)
00058 
00059   NS_IMETHOD Init(nsresult aNSResult, const char* aName,
00060                   const char* aMessage,
00061                   nsIException* aDefaultException) = 0;
00062 };
00063 
00064 
00065 #define IMPL_DOM_EXCEPTION_HEAD(classname, ifname)                           \
00066 class classname : public nsIException,                                       \
00067                   public ifname                                              \
00068 {                                                                            \
00069 public:                                                                      \
00070   classname(nsIException* aInner);                                           \
00071   virtual ~classname();                                                      \
00072                                                                              \
00073   NS_DECL_ISUPPORTS                                                          \
00074   NS_FORWARD_NSIEXCEPTION(mBase->)
00075 
00076 // Note: the exception implemented by this macro doesn't free the pointers
00077 //       it gets from the mapping_function and assumes they will be valid
00078 //       as long as the exception object is alive.
00079 
00080 #define IMPL_DOM_EXCEPTION_TAIL(classname, ifname, domname, module,          \
00081                                 mapping_function)                            \
00082 private:                                                                     \
00083   nsCOMPtr<nsIException> mBase;                                              \
00084 };                                                                           \
00085                                                                              \
00086 classname::classname(nsIException* aInner) : mBase(aInner)                   \
00087 {                                                                            \
00088 }                                                                            \
00089 classname::~classname() {}                                                   \
00090                                                                              \
00091 NS_IMPL_ADDREF(classname)                                                    \
00092 NS_IMPL_RELEASE(classname)                                                   \
00093 NS_INTERFACE_MAP_BEGIN(classname)                                            \
00094   NS_INTERFACE_MAP_ENTRY(nsIException)                                       \
00095   NS_INTERFACE_MAP_ENTRY(ifname)                                             \
00096   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIException)                \
00097   NS_INTERFACE_MAP_ENTRY_EXTERNAL_DOM_CLASSINFO(domname)                     \
00098 NS_INTERFACE_MAP_END                                                         \
00099                                                                              \
00100 NS_DEFINE_CID(kBaseDOMException_CID_##domname, NS_BASE_DOM_EXCEPTION_CID);   \
00101                                                                              \
00102 nsresult                                                                     \
00103 NS_New##domname(nsresult aNSResult, nsIException* aDefaultException,         \
00104                 nsIException** aException)                                   \
00105 {                                                                            \
00106   if (!(NS_ERROR_GET_MODULE(aNSResult) == module)) {                         \
00107     NS_WARNING("Trying to create an exception for the wrong error module."); \
00108     return NS_ERROR_FAILURE;                                                 \
00109   }                                                                          \
00110   const char* name;                                                          \
00111   const char* message;                                                       \
00112   mapping_function(aNSResult, &name, &message);                              \
00113   nsCOMPtr<nsIBaseDOMException> baseException =                              \
00114     do_CreateInstance(kBaseDOMException_CID_##domname);                      \
00115   NS_ENSURE_TRUE(baseException, NS_ERROR_OUT_OF_MEMORY);                     \
00116   baseException->Init(aNSResult, name, message, aDefaultException);          \
00117   nsCOMPtr<nsIException> inner = do_QueryInterface(baseException);           \
00118   *aException = new classname(inner);                                        \
00119   NS_ENSURE_TRUE(*aException, NS_ERROR_OUT_OF_MEMORY);                       \
00120   NS_ADDREF(*aException);                                                    \
00121   return NS_OK;                                                              \
00122 }
00123 
00124 #endif /* nsIBaseDOMException_h___ */