Back to index

lightning-sunbird  0.9+nobinonly
nsAbUtils.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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  * Sun Microsystems, Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Csaba Borbola <csaba.borbola@sun.com>
00024  *   Paul Sandoz   <paul.sandoz@sun.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or 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 nsAbUtils_h__
00041 #define nsAbUtils_h__
00042 
00043 #include "nsMemory.h"
00044 #include "nsHashtable.h"
00045 
00046 /*
00047  * Wrapper class to automatically free an array of
00048  * char* when class goes out of scope
00049  */
00050 class CharPtrArrayGuard
00051 {
00052 public:
00053     CharPtrArrayGuard (PRBool freeElements = PR_TRUE) :
00054         mFreeElements (freeElements),
00055         mArray (0),
00056         mSize (0)
00057     {
00058     }
00059 
00060     ~CharPtrArrayGuard ()
00061     {
00062         Free ();
00063     }
00064 
00065     char* operator[](int i)
00066     {
00067         return mArray[i];
00068     }
00069 
00070     PRUint32* GetSizeAddr(void)
00071     {
00072         return &mSize;
00073     }
00074 
00075     PRUint32 GetSize(void)
00076     {
00077         return mSize;
00078     }
00079 
00080     char*** GetArrayAddr(void)
00081     {
00082         return &mArray;
00083     }
00084 
00085     const char** GetArray(void)
00086     {
00087         return (const char** ) mArray;
00088     }
00089 
00090 public:
00091 
00092 private:
00093     PRBool mFreeElements;
00094     char **mArray;
00095     PRUint32 mSize;
00096 
00097     void Free ()
00098     {
00099         if (!mArray)
00100             return;
00101 
00102         if (mFreeElements)
00103             NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(mSize, mArray);
00104         else
00105         {
00106           nsMemory::Free(mArray);
00107         }
00108     }
00109 };
00110 
00111 /*
00112  * Wrapper class to automatically free an array of
00113  * PRUnichar* when class goes out of scope
00114  */
00115 class PRUnicharPtrArrayGuard
00116 {
00117 public:
00118     PRUnicharPtrArrayGuard (PRBool freeElements = PR_TRUE) :
00119         mFreeElements (freeElements),
00120         mArray (0),
00121         mSize (0)
00122     {
00123     }
00124 
00125     ~PRUnicharPtrArrayGuard ()
00126     {
00127         Free ();
00128     }
00129 
00130     PRUnichar* operator[](int i)
00131     {
00132         return mArray[i];
00133     }
00134 
00135     PRUint32* GetSizeAddr(void)
00136     {
00137         return &mSize;
00138     }
00139 
00140     PRUint32 GetSize(void)
00141     {
00142         return mSize;
00143     }
00144 
00145     PRUnichar*** GetArrayAddr(void)
00146     {
00147         return &mArray;
00148     }
00149 
00150     const PRUnichar** GetArray(void)
00151     {
00152         return (const PRUnichar** ) mArray;
00153     }
00154 
00155 public:
00156 
00157 private:
00158     PRBool mFreeElements;
00159     PRUnichar **mArray;
00160     PRUint32 mSize;
00161     void Free ()
00162     {
00163         if (!mArray)
00164             return;
00165 
00166         if (mFreeElements)
00167           NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(mSize, mArray);
00168         else
00169         {
00170           nsMemory::Free(mArray);
00171         }
00172     }
00173 };
00174 
00175 #endif  /* nsAbUtils_h__ */