FDO API Reference Feature Data Objects

PhysicalElementMappingCollection.h

Go to the documentation of this file.
00001 #ifndef FDO_PHYSICALELEMENTMAPPINGCOLLECTION
00002 #define FDO_PHYSICALELEMENTMAPPINGCOLLECTION
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 #include <FdoStd.h>
00027 #include <Common/NamedCollection.h>
00028 #include <Fdo/Commands/CommandException.h>
00029 #include <Fdo/Commands/Schema/PhysicalElementMapping.h>
00030 
00031 /// \brief
00032 /// FdoPhysicalElementMappingCollection is an abstract template class that is used to
00033 /// define collections of Physical Schema Mapping elements.
00034 template <class OBJ> class FdoPhysicalElementMappingCollection : public FdoNamedCollection<OBJ, FdoCommandException>
00035 {
00036 
00037 /// \cond DOXYGEN-IGNORE
00038 protected:
00039     FdoPhysicalElementMappingCollection()  : FdoNamedCollection<OBJ, FdoCommandException>(),
00040         m_parent(NULL)
00041     {
00042     }
00043 
00044     FdoPhysicalElementMappingCollection(FdoPhysicalElementMapping* parent) : FdoNamedCollection<OBJ, FdoCommandException>()
00045     {
00046         m_parent = parent;
00047     }
00048 
00049     virtual ~FdoPhysicalElementMappingCollection()
00050     {
00051         if ( m_parent)
00052         {
00053             for(FdoInt32 i = 0; i < FdoNamedCollection<OBJ, FdoCommandException>::GetCount(); i++)
00054             {
00055                 FdoPtr<OBJ>    pitem = FdoNamedCollection<OBJ, FdoCommandException>::GetItem(i);
00056 
00057     // When the collection disappears, the ownership does too.
00058     // So NULL out the parent.  This is necessary because the
00059     // parent reference within items is not refcounted to avoid
00060     // circular references/memory leaks.
00061     // 
00062     // \warning
00063     // The pitem->GetParent() should always equal m_parent, but we cannot verify this
00064     // because the call to pitem->GetParent() might AddRef() an item that has already
00065     // been disposed.  So we just NULL it out.
00066     // 
00067                 pitem->SetParent(NULL);
00068             }
00069         }
00070     }
00071 /// \endcond
00072 
00073 public:
00074     /// \brief
00075     /// 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.
00076     /// 
00077     /// \param index 
00078     /// Input index
00079     /// \param value 
00080     /// Input value
00081     /// 
00082     /// \return
00083     /// Returns nothing
00084     /// 
00085     virtual void SetItem(FdoInt32 index, OBJ* value)
00086     {
00087         // Verify index is in bounds.
00088         if (index < FdoNamedCollection<OBJ, FdoCommandException>::GetCount() && index >= 0)
00089         {
00090             if (m_parent)
00091             {
00092             // Validate parentage for object to add.  Membership in a
00093             // collection implys parentage, and a object can only have
00094             // one parent.
00095             // 
00096                 FdoPhysicalElementMappingP   pparent = value->GetParent();
00097                 if (pparent && (pparent != m_parent) )
00098                     throw FdoCommandException::Create(FdoException::NLSGetMessage(FDO_NLSID(COMMANDS_7_OBJECTHASPARENT),value->GetName()));
00099             }
00100 
00101             if (m_parent)
00102                 value->SetParent(m_parent);
00103  
00104             FdoPtr<OBJ>    pitemOld = FdoNamedCollection<OBJ, FdoCommandException>::GetItem(index);
00105             FdoPhysicalElementMappingP   pparentOldItem = pitemOld->GetParent();
00106             if ( pparentOldItem == m_parent )
00107             {
00108     // When an object is removed from a collection, that
00109     // collection's owner is no longer the object's
00110     // parent.
00111     //
00112                 pitemOld->SetParent(NULL);
00113             }
00114         }
00115 
00116         FdoNamedCollection<OBJ, FdoCommandException>::SetItem(index, value);
00117     }
00118 
00119     /// \brief
00120     /// Adds the specified item to the end of the collection. Returns the index of the newly added item.
00121     /// 
00122     /// \param value 
00123     /// Input value
00124     /// 
00125     /// \return
00126     /// Returns the index of the newly added item
00127     /// 
00128     virtual FdoInt32 Add(OBJ* value)
00129     {
00130         if (m_parent)
00131         {
00132         // Validate parentage for object to add.  Membership in a
00133         // collection implys parentage, and a object can only have
00134         // one parent.
00135         // 
00136             FdoPhysicalElementMappingP   pparent = value->GetParent();
00137             if (pparent && (pparent != m_parent) )
00138                 throw FdoCommandException::Create(FdoException::NLSGetMessage(FDO_NLSID(COMMANDS_7_OBJECTHASPARENT),value->GetName()));
00139         }
00140 
00141         if (m_parent)
00142             value->SetParent(m_parent);
00143 
00144         return FdoNamedCollection<OBJ, FdoCommandException>::Add(value);
00145     }
00146 
00147     /// \brief
00148     /// Inserts the specified item at the specified index within the collection.
00149     /// Items following the insertion point are moved down to accommodate the new item. 
00150     /// Throws an invalid argument exception if the specified index is out of range.
00151     /// 
00152     /// \param index 
00153     /// Input index
00154     /// \param value 
00155     /// Input value
00156     /// 
00157     /// \return
00158     /// Returns nothing
00159     /// 
00160     virtual void Insert(FdoInt32 index, OBJ* value)
00161     {
00162         if (m_parent)
00163         {
00164         // Validate parentage for object to add.  Membership in a
00165         // collection implys parentage, and a object can only have
00166         // one parent.
00167         // 
00168             FdoPhysicalElementMappingP  pparent = value->GetParent();
00169             if (pparent && (pparent != m_parent) )
00170                 throw FdoCommandException::Create(FdoException::NLSGetMessage(FDO_NLSID(COMMANDS_7_OBJECTHASPARENT),value->GetName()));
00171         }
00172 
00173         if (m_parent)
00174             value->SetParent(m_parent);
00175  
00176         FdoNamedCollection<OBJ, FdoCommandException>::Insert(index, value);
00177     }
00178 
00179     /// \brief
00180     /// Removes all items from the collection.
00181     /// 
00182     /// \return
00183     /// Returns nothing
00184     /// 
00185     virtual void Clear()
00186     {
00187         if (m_parent)
00188         {
00189             for(FdoInt32 i = 0; i < FdoNamedCollection<OBJ, FdoCommandException>::GetCount(); i++)
00190             {
00191                 FdoPtr<OBJ>    pitem = FdoNamedCollection<OBJ, FdoCommandException>::GetItem(i);
00192                 FdoPhysicalElementMappingP   pparent = pitem->GetParent();
00193 
00194                 if ( pparent == m_parent )
00195                 {
00196                 // When an object is removed from a collection, that
00197                 // collection's owner is no longer the object's
00198                 // parent.
00199                     pitem->SetParent(NULL);
00200                 }
00201             }
00202         }
00203 
00204         FdoNamedCollection<OBJ, FdoCommandException>::Clear();
00205     }
00206 
00207     /// \brief
00208     /// Removes the specified item from the collection. Throws an invalid argument exception if the item does not exist within the collection.
00209     /// 
00210     /// \param value 
00211     /// Input value
00212     /// 
00213     /// \return
00214     /// Returns nothing
00215     /// 
00216     virtual void Remove(const OBJ* value)
00217     {
00218         if (m_parent)
00219         {
00220             // Cast drops const
00221             FdoPtr<OBJ>    pitem = (OBJ*)value;
00222             FdoPhysicalElementMappingP   pparent = pitem->GetParent();
00223 
00224             if ( pparent == m_parent )
00225             {
00226             // When an object is removed from a collection, that
00227             // collection's owner is no longer the object's
00228             // parent.
00229             // 
00230                 pitem->SetParent(NULL);
00231             }
00232         }
00233 
00234         FdoNamedCollection<OBJ, FdoCommandException>::Remove(value);
00235     }
00236 
00237     /// \brief
00238     /// Removes the specified item from the collection. Throws an invalid argument exception if the item does not exist within the collection.
00239     /// 
00240     /// \param index 
00241     /// Input index
00242     /// 
00243     /// \return
00244     /// Returns nothing
00245     /// 
00246     virtual void RemoveAt(FdoInt32 index)
00247     {
00248 
00249         if (m_parent)
00250         {
00251             FdoPtr<OBJ>    pitem = FdoNamedCollection<OBJ, FdoCommandException>::GetItem(index);
00252             FdoPhysicalElementMappingP   pparent = pitem->GetParent();
00253 
00254             if ( pparent == m_parent )
00255             {
00256             // When an object is removed from a collection, that
00257             // collection's owner is no longer the object's
00258             // parent.
00259             // 
00260                 pitem->SetParent(NULL);
00261             }
00262 
00263         }
00264 
00265         FdoNamedCollection<OBJ, FdoCommandException>::RemoveAt(index);
00266     }
00267 
00268 /// \cond DOXYGEN-IGNORE
00269 protected:
00270 
00271     /// m_parent is a non-refcounted reference, to avoid circular references that prevent
00272     /// elements from being freed.
00273     FdoPhysicalElementMapping*   m_parent;
00274 /// \endcond
00275 };
00276 
00277 #endif
00278 
00279 

Comments or suggestions? Send us feedback.