Back to index

lightning-sunbird  0.9+nobinonly
PropertyList.h
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.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) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Adam Lock <adamlock@eircom.net>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * 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 #ifndef PROPERTYLIST_H
00039 #define PROPERTYLIST_H
00040 
00041 // A simple array class for managing name/value pairs typically fed to controls
00042 // during initialization by IPersistPropertyBag
00043 
00044 class PropertyList
00045 {
00046     struct Property {
00047         BSTR    bstrName;
00048         VARIANT vValue;
00049     } *mProperties;
00050     unsigned long mListSize;
00051     unsigned long mMaxListSize;
00052 
00053     bool EnsureMoreSpace()
00054     {
00055         // Ensure enough space exists to accomodate a new item
00056         const unsigned long kGrowBy = 10;
00057         if (!mProperties)
00058         {
00059             mProperties = (Property *) malloc(sizeof(Property) * kGrowBy);
00060             if (!mProperties)
00061                 return false;
00062             mMaxListSize = kGrowBy;
00063         }
00064         else if (mListSize == mMaxListSize)
00065         {
00066             Property *pNewProperties;
00067             pNewProperties = (Property *) realloc(mProperties, sizeof(Property) * (mMaxListSize + kGrowBy));
00068             if (!pNewProperties)
00069                 return false;
00070             mProperties = pNewProperties;
00071             mMaxListSize += kGrowBy;
00072         }
00073         return true;
00074     }
00075 
00076 public:
00077     PropertyList() :
00078       mProperties(NULL),
00079       mListSize(0),
00080       mMaxListSize(0)
00081     {
00082     }
00083     ~PropertyList()
00084     {
00085     }
00086     void Clear()
00087     {
00088         if (mProperties)
00089         {
00090             for (unsigned long i = 0; i < mListSize; i++)
00091             {
00092                 SysFreeString(mProperties[i].bstrName); // Safe even if NULL
00093                 VariantClear(&mProperties[i].vValue);
00094             }
00095             free(mProperties);
00096             mProperties = NULL;
00097         }
00098         mListSize = 0;
00099         mMaxListSize = 0;
00100     }
00101     unsigned long GetSize() const
00102     {
00103         return mListSize;
00104     }
00105     const BSTR GetNameOf(unsigned long nIndex) const
00106     {
00107         if (nIndex > mListSize)
00108         {
00109             return NULL;
00110         }
00111         return mProperties[nIndex].bstrName;
00112     }
00113     const VARIANT *GetValueOf(unsigned long nIndex) const
00114     {
00115         if (nIndex > mListSize)
00116         {
00117             return NULL;
00118         }
00119         return &mProperties[nIndex].vValue;
00120     }
00121     bool AddOrReplaceNamedProperty(const BSTR bstrName, const VARIANT &vValue)
00122     {
00123         if (!bstrName)
00124             return false;
00125         for (unsigned long i = 0; i < GetSize(); i++)
00126         {
00127             // Case insensitive
00128             if (wcsicmp(mProperties[i].bstrName, bstrName) == 0)
00129             {
00130                 return SUCCEEDED(
00131                     VariantCopy(&mProperties[i].vValue, const_cast<VARIANT *>(&vValue)));
00132             }
00133         }
00134         return AddNamedProperty(bstrName, vValue);
00135     }
00136     bool AddNamedProperty(const BSTR bstrName, const VARIANT &vValue)
00137     {
00138         if (!bstrName || !EnsureMoreSpace())
00139             return false;
00140         Property *pProp = &mProperties[mListSize];
00141         pProp->bstrName = ::SysAllocString(bstrName);
00142         if (!pProp->bstrName)
00143         {
00144             return false;
00145         }
00146         VariantInit(&pProp->vValue);
00147         if (FAILED(VariantCopy(&pProp->vValue, const_cast<VARIANT *>(&vValue))))
00148         {
00149             SysFreeString(pProp->bstrName);
00150             return false;
00151         }
00152         mListSize++;
00153         return true;
00154     }
00155 };
00156 
00157 #endif