RefVectorOf.c
上传用户:huihehuasu
上传日期:2007-01-10
资源大小:6948k
文件大小:11k
源码类别:

xml/soap/webservice

开发平台:

C/C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  * 
  4.  * Copyright (c) 1999-2000 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  * 
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer. 
  13.  * 
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  * 
  19.  * 3. The end-user documentation included with the redistribution,
  20.  *    if any, must include the following acknowledgment:  
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowledgment may appear in the software itself,
  24.  *    if and wherever such third-party acknowledgments normally appear.
  25.  * 
  26.  * 4. The names "Xerces" and "Apache Software Foundation" must
  27.  *    not be used to endorse or promote products derived from this
  28.  *    software without prior written permission. For written 
  29.  *    permission, please contact apache@apache.org.
  30.  * 
  31.  * 5. Products derived from this software may not be called "Apache",
  32.  *    nor may "Apache" appear in their name, without prior written
  33.  *    permission of the Apache Software Foundation.
  34.  * 
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  * 
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation, and was
  51.  * originally based on software copyright (c) 1999, International
  52.  * Business Machines, Inc., http://www.ibm.com .  For more information
  53.  * on the Apache Software Foundation, please see
  54.  * <http://www.apache.org/>.
  55.  */
  56. /**
  57.  * $Log: RefVectorOf.c,v $
  58.  * Revision 1.5  2001/06/25 13:01:49  knoaman
  59.  * Add constraint checking on elements in complex types to prevent same
  60.  * element names from having different definitions - use substitueGroups.
  61.  *
  62.  * Revision 1.4  2000/07/31 19:18:25  jpolast
  63.  * bug fix in removeAll() to zero out all the pointers.
  64.  *
  65.  * Revision 1.3  2000/03/02 19:54:45  roddey
  66.  * This checkin includes many changes done while waiting for the
  67.  * 1.1.0 code to be finished. I can't list them all here, but a list is
  68.  * available elsewhere.
  69.  *
  70.  * Revision 1.2  2000/02/06 07:48:03  rahulj
  71.  * Year 2K copyright swat.
  72.  *
  73.  * Revision 1.1.1.1  1999/11/09 01:05:04  twl
  74.  * Initial checkin
  75.  *
  76.  * Revision 1.2  1999/11/08 20:45:13  rahul
  77.  * Swat for adding in Product name and CVS comment log variable.
  78.  *
  79.  */
  80. // ---------------------------------------------------------------------------
  81. //  Includes
  82. // ---------------------------------------------------------------------------
  83. #if defined(XERCES_TMPLSINC)
  84. #include <util/RefVectorOf.hpp>
  85. #endif
  86. // ---------------------------------------------------------------------------
  87. //  RefVectorOf: Constructors and Destructor
  88. // ---------------------------------------------------------------------------
  89. template <class TElem> RefVectorOf<TElem>::
  90. RefVectorOf(const unsigned int maxElems, const bool adoptElems) :
  91.     fAdoptedElems(adoptElems)
  92.     , fCurCount(0)
  93.     , fMaxCount(maxElems)
  94.     , fElemList(0)
  95. {
  96.     // Allocate and initialize the array
  97.     fElemList = new TElem*[maxElems];
  98.     for (unsigned int index = 0; index < maxElems; index++)
  99.         fElemList[index] = 0;
  100. }
  101. template <class TElem> RefVectorOf<TElem>::~RefVectorOf()
  102. {
  103.     if (fAdoptedElems)
  104.     {
  105.         for (unsigned int index = 0; index < fCurCount; index++)
  106.             delete fElemList[index];
  107.     }
  108.     delete [] fElemList;
  109. }
  110. // ---------------------------------------------------------------------------
  111. //  RefVectorOf: Element management
  112. // ---------------------------------------------------------------------------
  113. template <class TElem> void RefVectorOf<TElem>::addElement(TElem* const toAdd)
  114. {
  115.     ensureExtraCapacity(1);
  116.     fElemList[fCurCount] = toAdd;
  117.     fCurCount++;
  118. }
  119. template <class TElem> void
  120. RefVectorOf<TElem>::setElementAt(TElem* const toSet, const unsigned int setAt)
  121. {
  122.     if (setAt >= fCurCount)
  123.         ThrowXML(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex);
  124.     if (fAdoptedElems)
  125.         delete fElemList[setAt];
  126.     fElemList[setAt] = toSet;
  127. }
  128. template <class TElem> void RefVectorOf<TElem>::
  129. insertElementAt(TElem* const toInsert, const unsigned int insertAt)
  130. {
  131.     if (insertAt == fCurCount)
  132.     {
  133.         addElement(toInsert);
  134.         return;
  135.     }
  136.     if (insertAt > fCurCount)
  137.         ThrowXML(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex);
  138.     ensureExtraCapacity(1);
  139.     // Make room for the newbie
  140.     for (unsigned int index = fCurCount; index > insertAt; index--)
  141.         fElemList[index] = fElemList[index-1];
  142.     // And stick it in and bump the count
  143.     fElemList[insertAt] = toInsert;
  144.     fCurCount++;
  145. }
  146. template <class TElem> TElem* RefVectorOf<TElem>::
  147. orphanElementAt(const unsigned int orphanAt)
  148. {
  149.     if (orphanAt >= fCurCount)
  150.         ThrowXML(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex);
  151.     // Get the element we are going to orphan
  152.     TElem* retVal = fElemList[orphanAt];
  153.     // Optimize if its the last element
  154.     if (orphanAt == fCurCount-1)
  155.     {
  156.         fElemList[orphanAt] = 0;
  157.         fCurCount--;
  158.         return retVal;
  159.     }
  160.     // Copy down every element above orphan point
  161.     for (unsigned int index = orphanAt; index < fCurCount-1; index++)
  162.         fElemList[index] = fElemList[index+1];
  163.     // Keep unused elements zero for sanity's sake
  164.     fElemList[fCurCount-1] = 0;
  165.     // And bump down count
  166.     fCurCount--;
  167.     return retVal;
  168. }
  169. template <class TElem> void RefVectorOf<TElem>::removeAllElements()
  170. {
  171.     for (unsigned int index = 0; index < fCurCount; index++)
  172.     {
  173.         if (fAdoptedElems)
  174.           delete fElemList[index];
  175.         // Keep unused elements zero for sanity's sake
  176.         fElemList[index] = 0;
  177.     }
  178.     fCurCount = 0;
  179. }
  180. template <class TElem> void RefVectorOf<TElem>::
  181. removeElementAt(const unsigned int removeAt)
  182. {
  183.     if (removeAt >= fCurCount)
  184.         ThrowXML(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex);
  185.     if (fAdoptedElems)
  186.         delete fElemList[removeAt];
  187.     // Optimize if its the last element
  188.     if (removeAt == fCurCount-1)
  189.     {
  190.         fElemList[removeAt] = 0;
  191.         fCurCount--;
  192.         return;
  193.     }
  194.     // Copy down every element above remove point
  195.     for (unsigned int index = removeAt; index < fCurCount-1; index++)
  196.         fElemList[index] = fElemList[index+1];
  197.     // Keep unused elements zero for sanity's sake
  198.     fElemList[fCurCount-1] = 0;
  199.     // And bump down count
  200.     fCurCount--;
  201. }
  202. template <class TElem> void RefVectorOf<TElem>::removeLastElement()
  203. {
  204.     if (!fCurCount)
  205.         return;
  206.     fCurCount--;
  207.     if (fAdoptedElems)
  208.         delete fElemList[fCurCount];
  209. }
  210. template <class TElem> 
  211. bool RefVectorOf<TElem>::containsElement(const TElem* const toCheck) {
  212.     for (unsigned int i = 0; i < fCurCount; i++) {
  213.         if (fElemList[i] == toCheck) {
  214.             return true;
  215.         }
  216.     }
  217.     return false;
  218. }
  219. // ---------------------------------------------------------------------------
  220. //  RefVectorOf: Getter methods
  221. // ---------------------------------------------------------------------------
  222. template <class TElem> unsigned int RefVectorOf<TElem>::curCapacity() const
  223. {
  224.     return fMaxCount;
  225. }
  226. template <class TElem> const TElem* RefVectorOf<TElem>::
  227. elementAt(const unsigned int getAt) const
  228. {
  229.     if (getAt >= fCurCount)
  230.         ThrowXML(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex);
  231.     return fElemList[getAt];
  232. }
  233. template <class TElem> TElem*
  234. RefVectorOf<TElem>::elementAt(const unsigned int getAt)
  235. {
  236.     if (getAt >= fCurCount)
  237.         ThrowXML(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex);
  238.     return fElemList[getAt];
  239. }
  240. template <class TElem> unsigned int RefVectorOf<TElem>::size() const
  241. {
  242.     return fCurCount;
  243. }
  244. // ---------------------------------------------------------------------------
  245. //  RefVectorOf: Miscellaneous
  246. // ---------------------------------------------------------------------------
  247. template <class TElem> void RefVectorOf<TElem>::
  248. ensureExtraCapacity(const unsigned int length)
  249. {
  250.     unsigned int newMax = fCurCount + length;
  251.     if (newMax < fMaxCount)
  252.         return;
  253.     // Avoid too many reallocations by providing a little more space
  254.     if (newMax < fMaxCount + 32)
  255.         newMax = fMaxCount + 32;
  256.     // Allocate the new array and copy over the existing stuff
  257.     TElem** newList = new TElem*[newMax];
  258.     unsigned int index = 0;
  259.     for (; index < fCurCount; index++)
  260.         newList[index] = fElemList[index];
  261.     // Zero out the rest of them
  262.     for (; index < newMax; index++)
  263.         newList[index] = 0;
  264.     // Clean up the old array and update our members
  265.     delete [] fElemList;
  266.     fElemList = newList;
  267.     fMaxCount = newMax;
  268. }
  269. // ---------------------------------------------------------------------------
  270. //  RefVectorEnumerator: Constructors and Destructor
  271. // ---------------------------------------------------------------------------
  272. template <class TElem> RefVectorEnumerator<TElem>::
  273. RefVectorEnumerator(        RefVectorOf<TElem>* const   toEnum
  274.                     , const bool                        adopt) :
  275.     fAdopted(adopt)
  276.     , fCurIndex(0)
  277.     , fToEnum(toEnum)
  278. {
  279. }
  280. template <class TElem> RefVectorEnumerator<TElem>::~RefVectorEnumerator()
  281. {
  282.     if (fAdopted)
  283.         delete fToEnum;
  284. }
  285. // ---------------------------------------------------------------------------
  286. //  RefVectorEnumerator: Enum interface
  287. // ---------------------------------------------------------------------------
  288. template <class TElem> bool RefVectorEnumerator<TElem>::hasMoreElements() const
  289. {
  290.     if (fCurIndex >= fToEnum->size())
  291.         return false;
  292.     return true;
  293. }
  294. template <class TElem> TElem& RefVectorEnumerator<TElem>::nextElement()
  295. {
  296.     return *(fToEnum->elementAt(fCurIndex++));
  297. }
  298. template <class TElem> void RefVectorEnumerator<TElem>::Reset()
  299. {
  300.     fCurIndex = 0;
  301. }