FDO API Reference Feature Data Objects
Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

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 #ifdef _DEBUG
00077         if (NULL != m_list[index] && m_list[index]->GetRefCount() <= 0)
00078             throw FdoException::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_1_MEMORY_DEALLOCATION_ERROR), 
00079                                                                L"FdoCollection::GetItem",
00080                                                                L"FDO Object"));
00081 #endif
00082         return FDO_SAFE_ADDREF(m_list[index]);
00083     }
00084 
00085     /// \brief
00086     /// 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.
00087     /// 
00088     /// \param index 
00089     /// Input index
00090     /// \param value 
00091     /// Input value
00092     /// 
00093     /// \return
00094     /// Returns nothing
00095     /// 
00096     virtual void SetItem(FdoInt32 index, OBJ* value)
00097     {
00098         if (index < m_size && index >= 0)
00099         {
00100             FDO_SAFE_RELEASE(m_list[index]);
00101             m_list[index] = FDO_SAFE_ADDREF(value);
00102         }
00103         else
00104             throw EXC::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_5_INDEXOUTOFBOUNDS)));
00105     }
00106 
00107     /// \brief
00108     /// Adds the specified item to the end of the collection. Returns the index of the newly added item.
00109     /// 
00110     /// \param value 
00111     /// Input value
00112     /// 
00113     /// \return
00114     /// Returns the index of the newly added item
00115     /// 
00116     virtual FdoInt32 Add(OBJ* value)
00117     {
00118         if (m_size == m_capacity)
00119             resize();
00120 
00121         m_list[m_size] = FDO_SAFE_ADDREF(value);
00122         return m_size++;
00123     }
00124 
00125     /// \brief
00126     /// Inserts the specified item at the specified index within the collection. 
00127     /// Items following the insertion point are moved down to accommodate the new item. 
00128     /// Throws an invalid argument exception if the specified index is out of range.
00129     /// 
00130     /// \param index 
00131     /// Input index
00132     /// \param value 
00133     /// Input value
00134     /// 
00135     /// \return
00136     /// Returns nothing
00137     /// 
00138     virtual void Insert(FdoInt32 index, OBJ* value)
00139     {
00140         FdoInt32    i;
00141         if (m_size == m_capacity) 
00142             resize();
00143         if (index <= m_size && index >= 0)
00144         {
00145             for (i = m_size; i > index; i--) 
00146                 m_list[i] = m_list[i-1];
00147 
00148             m_list[index] = FDO_SAFE_ADDREF(value);
00149             m_size++;
00150         }
00151         else
00152             throw EXC::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_5_INDEXOUTOFBOUNDS)));
00153     }
00154 
00155     /// \brief
00156     /// Removes all items from the collection.
00157     /// 
00158     /// \return
00159     /// Returns nothing
00160     /// 
00161     virtual void Clear()
00162     {
00163         FdoInt32    i;
00164         for (i = 0; i < m_size; i++) 
00165         {
00166             FDO_SAFE_RELEASE(m_list[i]);
00167             m_list[i] = NULL;
00168         }
00169 
00170         m_size = 0;
00171     }
00172 
00173     /// \brief
00174     /// Removes the specified item from the collection. Throws an invalid argument exception if the item does not exist within the collection.
00175     /// 
00176     /// \param value 
00177     /// Input value
00178     /// 
00179     /// \return
00180     /// Returns nothing
00181     /// 
00182     virtual void Remove(const OBJ* value)
00183     {
00184         FdoInt32    i;
00185         for (i = 0; i < m_size; i++) 
00186         {
00187             if (m_list[i] == value)
00188                 break;
00189         }
00190 
00191         FDO_SAFE_RELEASE(m_list[i]);
00192 
00193         if (i == m_size)
00194             throw EXC::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_6_OBJECTNOTFOUND)));
00195         
00196         while (i < m_size - 1) 
00197         {
00198             m_list[i] = m_list[i+1];
00199             i++;
00200         }
00201 
00202         m_list[--m_size] = NULL;
00203     }
00204 
00205     /// \brief
00206     /// Removes the specified item from the collection. Throws an invalid argument exception if the item does not exist within the collection.
00207     /// 
00208     /// \param index 
00209     /// Input index
00210     /// 
00211     /// \return
00212     /// Returns nothing
00213     /// 
00214     virtual void RemoveAt(FdoInt32 index)
00215     {
00216         if (index < m_size && index >= 0) 
00217         {
00218             FdoInt32    i;
00219 
00220             FDO_SAFE_RELEASE(m_list[index]);    // also NULLs out m_list[index]
00221 
00222             for (i = index; i < m_size-1; i++)
00223                 m_list[i] = m_list[i+1];
00224 
00225             m_list[--m_size] = NULL;
00226         }
00227         else
00228             throw EXC::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_5_INDEXOUTOFBOUNDS)));
00229     }
00230 
00231     /// \brief
00232     /// Returns true if the collection contains the specified item, false otherwise.
00233     /// 
00234     /// \param value 
00235     /// Input value
00236     /// 
00237     /// \return
00238     /// Returns true if the collection contains the specified item, false otherwise
00239     /// 
00240     virtual bool Contains(const OBJ* value) const
00241     {
00242         FdoInt32    i;
00243         bool ret = false;
00244         for (i = 0; i < m_size; i++) {
00245             if (m_list[i] == value)
00246             {
00247                 ret = true;
00248                 break;
00249             }
00250         }
00251         return ret;
00252     }
00253 
00254     /// \brief
00255     /// Returns the index of the specified item in the collection or -1 if the item does not exist.
00256     /// 
00257     /// \param value 
00258     /// Input value
00259     /// 
00260     /// \return
00261     /// Returns the index of the specified item in the collection or -1 if the item does not exist
00262     /// 
00263     virtual FdoInt32 IndexOf(const OBJ* value) const
00264     {
00265         FdoInt32    i, index = -1;
00266         for (i = 0; i < m_size; i++) {
00267             if (m_list[i] == value) 
00268             {
00269                 index = i;        
00270                 break;
00271             }
00272         }
00273         return index;
00274     }
00275 
00276 private:
00277 
00278     void resize()
00279     {
00280         FdoInt32    i, old_capacity = m_capacity;
00281         m_capacity = (FdoInt32)(m_capacity*(1.4));
00282         OBJ** newArray = new OBJ*[m_capacity];
00283         for (i = 0; i < old_capacity; i++) {
00284             newArray[i] = m_list[i];
00285         }
00286         delete[] m_list;
00287         m_list = newArray;
00288     }
00289 
00290 private:
00291     OBJ**       m_list;
00292     FdoInt32    m_capacity;
00293     FdoInt32    m_size;
00294 };
00295 #endif
00296 
00297 

Comments or suggestions? Send us feedback.