FDO API Reference Feature Data Objects

Collection.h

Go to the documentation of this file.
00001 #ifndef _COLLECTION_H_
00002 #define _COLLECTION_H_
00003 // 
00004 
00005 //
00006 // Copyright (C) 2004-2006  Autodesk, Inc.
00007 // 
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of version 2.1 of the GNU Lesser
00010 // General Public License as published by the Free Software Foundation.
00011 // 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 // Lesser General Public License for more details.
00016 // 
00017 // You should have received a copy of the GNU Lesser General Public
00018 // License along with this library; if not, write to the Free Software
00019 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 //
00021 
00022 #ifdef _WIN32
00023 #pragma once
00024 #endif
00025 
00026 /// \brief
00027 /// FdoCollection is an abstract template class for defining
00028 /// standard collection access for all collection classes.
00029 template <class OBJ, class EXC> class FdoCollection : public FdoIDisposable
00030 {
00031 protected:
00032     static const FdoInt32 INIT_CAPACITY = 10;
00033 
00034     FdoCollection()
00035     {
00036         m_capacity = INIT_CAPACITY;
00037         m_size = 0;
00038         m_list = new OBJ*[m_capacity];
00039     }
00040 
00041     virtual ~FdoCollection()
00042     {
00043         for(FdoInt32 i = 0; i < m_size; i++)
00044         {
00045             FDO_SAFE_RELEASE(m_list[i]);
00046         }
00047 
00048         delete[] m_list;
00049     }
00050 
00051 public:
00052     /// \brief
00053     /// Gets the number of items in the collection.
00054     /// 
00055     /// \return
00056     /// Returns number of items in the collection
00057     /// 
00058     virtual FdoInt32 GetCount() const
00059     {
00060         return m_size;
00061     }
00062 
00063     /// \brief
00064     /// Gets the item in the collection at the specified index. Throws an invalid argument exception if the index is out of range.
00065     /// 
00066     /// \param index 
00067     /// Input index
00068     /// 
00069     /// \return
00070     /// Returns the item in the collection at the specified index
00071     /// 
00072     virtual OBJ* GetItem(FdoInt32 index) const
00073     {
00074         if (index >= m_size || index < 0)
00075             throw EXC::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_5_INDEXOUTOFBOUNDS)));
00076         return FDO_SAFE_ADDREF(m_list[index]);
00077     }
00078 
00079     /// \brief
00080     /// Sets the item in the collection at the specified index to the specified value. Throws an invalid argument exception if the index is out of range.
00081     /// 
00082     /// \param index 
00083     /// Input index
00084     /// \param value 
00085     /// Input value
00086     /// 
00087     /// \return
00088     /// Returns nothing
00089     /// 
00090     virtual void SetItem(FdoInt32 index, OBJ* value)
00091     {
00092         if (index < m_size && index >= 0)
00093         {
00094             FDO_SAFE_RELEASE(m_list[index]);
00095             m_list[index] = FDO_SAFE_ADDREF(value);
00096         }
00097         else
00098             throw EXC::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_5_INDEXOUTOFBOUNDS)));
00099     }
00100 
00101     /// \brief
00102     /// Adds the specified item to the end of the collection. Returns the index of the newly added item.
00103     /// 
00104     /// \param value 
00105     /// Input value
00106     /// 
00107     /// \return
00108     /// Returns the index of the newly added item
00109     /// 
00110     virtual FdoInt32 Add(OBJ* value)
00111     {
00112         if (m_size == m_capacity)
00113             resize();
00114 
00115         m_list[m_size] = FDO_SAFE_ADDREF(value);
00116         return m_size++;
00117     }
00118 
00119     /// \brief
00120     /// Inserts the specified item at the specified index within the collection. 
00121     /// Items following the insertion point are moved down to accommodate the new item. 
00122     /// Throws an invalid argument exception if the specified index is out of range.
00123     /// 
00124     /// \param index 
00125     /// Input index
00126     /// \param value 
00127     /// Input value
00128     /// 
00129     /// \return
00130     /// Returns nothing
00131     /// 
00132     virtual void Insert(FdoInt32 index, OBJ* value)
00133     {
00134         FdoInt32    i;
00135         if (m_size == m_capacity) 
00136             resize();
00137         if (index <= m_size && index >= 0)
00138         {
00139             for (i = m_size; i > index; i--) 
00140                 m_list[i] = m_list[i-1];
00141 
00142             m_list[index] = FDO_SAFE_ADDREF(value);
00143             m_size++;
00144         }
00145         else
00146             throw EXC::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_5_INDEXOUTOFBOUNDS)));
00147     }
00148 
00149     /// \brief
00150     /// Removes all items from the collection.
00151     /// 
00152     /// \return
00153     /// Returns nothing
00154     /// 
00155     virtual void Clear()
00156     {
00157         FdoInt32    i;
00158         for (i = 0; i < m_size; i++) 
00159         {
00160             FDO_SAFE_RELEASE(m_list[i]);
00161             m_list[i] = NULL;
00162         }
00163 
00164         m_size = 0;
00165     }
00166 
00167     /// \brief
00168     /// Removes the specified item from the collection. Throws an invalid argument exception if the item does not exist within the collection.
00169     /// 
00170     /// \param value 
00171     /// Input value
00172     /// 
00173     /// \return
00174     /// Returns nothing
00175     /// 
00176     virtual void Remove(const OBJ* value)
00177     {
00178         FdoInt32    i;
00179         for (i = 0; i < m_size; i++) 
00180         {
00181             if (m_list[i] == value)
00182                 break;
00183         }
00184 
00185         FDO_SAFE_RELEASE(m_list[i]);
00186 
00187         if (i == m_size)
00188             throw EXC::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_6_OBJECTNOTFOUND)));
00189         
00190         while (i < m_size - 1) 
00191         {
00192             m_list[i] = m_list[i+1];
00193             i++;
00194         }
00195 
00196         m_list[--m_size] = NULL;
00197     }
00198 
00199     /// \brief
00200     /// Removes the specified item from the collection. Throws an invalid argument exception if the item does not exist within the collection.
00201     /// 
00202     /// \param index 
00203     /// Input index
00204     /// 
00205     /// \return
00206     /// Returns nothing
00207     /// 
00208     virtual void RemoveAt(FdoInt32 index)
00209     {
00210         if (index < m_size && index >= 0) 
00211         {
00212             FdoInt32    i;
00213 
00214             FDO_SAFE_RELEASE(m_list[index]);    // also NULLs out m_list[index]
00215 
00216             for (i = index; i < m_size-1; i++)
00217                 m_list[i] = m_list[i+1];
00218 
00219             m_list[--m_size] = NULL;
00220         }
00221         else
00222             throw EXC::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_5_INDEXOUTOFBOUNDS)));
00223     }
00224 
00225     /// \brief
00226     /// Returns true if the collection contains the specified item, false otherwise.
00227     /// 
00228     /// \param value 
00229     /// Input value
00230     /// 
00231     /// \return
00232     /// Returns true if the collection contains the specified item, false otherwise
00233     /// 
00234     virtual bool Contains(const OBJ* value) const
00235     {
00236         FdoInt32    i;
00237         bool ret = false;
00238         for (i = 0; i < m_size; i++) {
00239             if (m_list[i] == value)
00240             {
00241                 ret = true;
00242                 break;
00243             }
00244         }
00245         return ret;
00246     }
00247 
00248     /// \brief
00249     /// Returns the index of the specified item in the collection or -1 if the item does not exist.
00250     /// 
00251     /// \param value 
00252     /// Input value
00253     /// 
00254     /// \return
00255     /// Returns the index of the specified item in the collection or -1 if the item does not exist
00256     /// 
00257     virtual FdoInt32 IndexOf(const OBJ* value) const
00258     {
00259         FdoInt32    i, index = -1;
00260         for (i = 0; i < m_size; i++) {
00261             if (m_list[i] == value) 
00262             {
00263                 index = i;        
00264                 break;
00265             }
00266         }
00267         return index;
00268     }
00269 
00270 private:
00271 
00272     void resize()
00273     {
00274         FdoInt32    i, old_capacity = m_capacity;
00275         m_capacity = (FdoInt32)(m_capacity*(1.4));
00276         OBJ** newArray = new OBJ*[m_capacity];
00277         for (i = 0; i < old_capacity; i++) {
00278             newArray[i] = m_list[i];
00279         }
00280         delete[] m_list;
00281         m_list = newArray;
00282     }
00283 
00284 private:
00285     OBJ**       m_list;
00286     FdoInt32    m_capacity;
00287     FdoInt32    m_size;
00288 };
00289 #endif
00290 
00291 

Comments or suggestions? Send us feedback.