2008-11-24 04:34:58 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
|
|
|
* Copyright (C) 1992-2008 Kicad Developers, see change_log.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef DLIST_H_
|
|
|
|
#define DLIST_H_
|
|
|
|
|
|
|
|
|
2010-12-08 20:12:46 +00:00
|
|
|
class EDA_ITEM;
|
2008-11-24 04:34:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class DHEAD
|
|
|
|
* is only for use by template class DLIST, use that instead.
|
|
|
|
*/
|
|
|
|
class DHEAD
|
|
|
|
{
|
|
|
|
protected:
|
2010-12-08 20:12:46 +00:00
|
|
|
EDA_ITEM* first; ///< first element in list, or NULL if list empty
|
|
|
|
EDA_ITEM* last; ///< last elment in list, or NULL if empty
|
|
|
|
unsigned count; ///< how many elements are in the list, automatically maintained.
|
|
|
|
bool meOwner; ///< I must delete the objects I hold in my destructor
|
2008-11-24 04:34:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor DHEAD
|
|
|
|
* is protected so that a DHEAD can only be instantiated from within a
|
|
|
|
* DLIST template.
|
|
|
|
*/
|
|
|
|
DHEAD() :
|
|
|
|
first(0),
|
|
|
|
last(0),
|
|
|
|
count(0),
|
|
|
|
meOwner(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~DHEAD();
|
|
|
|
|
|
|
|
/**
|
2008-11-24 21:03:40 +00:00
|
|
|
* Function append
|
2008-11-24 04:34:58 +00:00
|
|
|
* adds \a aNewElement to the end of the list.
|
|
|
|
*/
|
2010-12-08 20:12:46 +00:00
|
|
|
void append( EDA_ITEM* aNewElement );
|
2008-11-24 04:34:58 +00:00
|
|
|
|
|
|
|
/**
|
2008-11-24 21:03:40 +00:00
|
|
|
* Function insert
|
2008-11-24 04:34:58 +00:00
|
|
|
* puts aNewElement just in front of aElementAfterMe in the list sequence.
|
2008-12-04 04:28:11 +00:00
|
|
|
* If aElementAfterMe is NULL, then simply append().
|
|
|
|
* @param aNewElement The element to insert.
|
|
|
|
* @param aElementAfterMe The element to insert \a aNewElement before,
|
|
|
|
* if NULL then append aNewElement onto end of list.
|
2008-11-24 04:34:58 +00:00
|
|
|
*/
|
2010-12-08 20:12:46 +00:00
|
|
|
void insert( EDA_ITEM* aNewElement, EDA_ITEM* aElementAfterMe );
|
2008-11-24 04:34:58 +00:00
|
|
|
|
|
|
|
/**
|
2008-11-24 21:03:40 +00:00
|
|
|
* Function insert
|
2008-11-24 04:34:58 +00:00
|
|
|
* puts aNewElement in front of list sequence.
|
|
|
|
*/
|
2010-12-08 20:12:46 +00:00
|
|
|
void insert( EDA_ITEM* aNewElement )
|
2008-11-24 04:34:58 +00:00
|
|
|
{
|
2008-11-24 21:03:40 +00:00
|
|
|
insert( aNewElement, first );
|
2008-11-24 04:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-11-24 21:03:40 +00:00
|
|
|
* Function remove
|
2008-11-24 04:34:58 +00:00
|
|
|
* removes \a aElement from the list, but does not delete it.
|
|
|
|
*/
|
2010-12-08 20:12:46 +00:00
|
|
|
void remove( EDA_ITEM* aElement );
|
2008-11-24 04:34:58 +00:00
|
|
|
|
2008-11-24 07:30:11 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
2008-12-02 09:13:28 +00:00
|
|
|
* Function DeleteAll
|
|
|
|
* deletes all items on the list and leaves the list empty. The destructor
|
|
|
|
* for each item is called.
|
2008-11-24 07:30:11 +00:00
|
|
|
*/
|
2008-12-02 09:13:28 +00:00
|
|
|
void DeleteAll();
|
2008-11-24 07:30:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function SetOwnership
|
|
|
|
* controls whether the list owns the objects and is responsible for
|
|
|
|
* deleteing their memory at time of this object's destruction.
|
|
|
|
*/
|
|
|
|
void SetOwnership( bool Iown ) { meOwner = Iown; }
|
|
|
|
|
|
|
|
|
2008-11-24 04:34:58 +00:00
|
|
|
/**
|
|
|
|
* Function GetCount
|
|
|
|
* returns the number of elements in the list.
|
|
|
|
*/
|
|
|
|
unsigned GetCount() { return count; }
|
2008-12-02 18:20:49 +00:00
|
|
|
|
|
|
|
#if defined(DEBUG)
|
|
|
|
void VerifyListIntegrity();
|
|
|
|
#endif
|
2008-11-24 04:34:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class DLIST
|
|
|
|
* is the head of a doubly linked list. It contains pointers to the first
|
|
|
|
* and last elements in a doubly linked list. The elements in the list must
|
2010-12-08 20:12:46 +00:00
|
|
|
* be of class T or derived from T, and T must be derived from EDA_ITEM.
|
2008-11-24 06:53:43 +00:00
|
|
|
* @see DHEAD for additional public functions.
|
2008-11-24 04:34:58 +00:00
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
class DLIST : public DHEAD
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* operator T*
|
2008-12-02 09:13:28 +00:00
|
|
|
* is a casting operator that returns \a GetFirst(), a T*
|
2008-11-24 04:34:58 +00:00
|
|
|
*/
|
|
|
|
operator T* () const { return GetFirst(); }
|
|
|
|
|
2008-12-02 09:13:28 +00:00
|
|
|
/**
|
|
|
|
* operator ->
|
|
|
|
* is a dereferencing operator that returns \a GetFirst(), a T*
|
|
|
|
*/
|
|
|
|
T* operator -> () const { return GetFirst(); }
|
|
|
|
|
2008-11-24 04:34:58 +00:00
|
|
|
/**
|
|
|
|
* Function GetFirst
|
2008-12-02 09:13:28 +00:00
|
|
|
* returns the first T* in the list without removing it, or NULL if
|
|
|
|
* the list is empty.
|
2008-11-24 04:34:58 +00:00
|
|
|
*/
|
|
|
|
T* GetFirst() const { return (T*) first; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function GetLast
|
2008-12-02 09:13:28 +00:00
|
|
|
* returns the last T* in the list without removing it,
|
|
|
|
* or NULL if the list is empty.
|
2008-11-24 04:34:58 +00:00
|
|
|
*/
|
|
|
|
T* GetLast() const { return (T*) last; }
|
2008-11-24 07:30:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Append
|
|
|
|
* adds \a aNewElement to the end of the list.
|
|
|
|
*/
|
|
|
|
void Append( T* aNewElement )
|
|
|
|
{
|
2008-11-24 21:03:40 +00:00
|
|
|
append( aNewElement );
|
2008-11-24 07:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Insert
|
|
|
|
* puts aNewElement just in front of aElementAfterMe in the list sequence.
|
|
|
|
* If aElementAfterMe is NULL, then simply Append()
|
|
|
|
*/
|
|
|
|
void Insert( T* aNewElement, T* aElementAfterMe )
|
|
|
|
{
|
2008-11-24 21:03:40 +00:00
|
|
|
insert( aNewElement, aElementAfterMe );
|
2008-11-24 07:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Remove
|
|
|
|
* removes \a aElement from the list, but does not delete it.
|
2008-11-24 21:03:40 +00:00
|
|
|
* @return T* - the removed element, so you can easily delete it upon return.
|
2008-11-24 07:30:11 +00:00
|
|
|
*/
|
2008-11-24 21:03:40 +00:00
|
|
|
T* Remove( T* aElement )
|
2008-11-24 07:30:11 +00:00
|
|
|
{
|
2008-11-24 21:03:40 +00:00
|
|
|
remove( aElement );
|
|
|
|
return aElement;
|
2008-11-24 07:30:11 +00:00
|
|
|
}
|
|
|
|
|
2008-12-02 09:13:28 +00:00
|
|
|
//-----< STL like functions >---------------------------------------
|
|
|
|
T* begin() const { return GetFirst(); }
|
|
|
|
T* end() const { return NULL; }
|
|
|
|
|
|
|
|
T* PopFront()
|
|
|
|
{
|
|
|
|
if( GetFirst() )
|
|
|
|
return Remove( GetFirst() );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* PopBack()
|
|
|
|
{
|
|
|
|
if( GetLast() )
|
|
|
|
return Remove( GetLast() );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function PushFront
|
|
|
|
* puts aNewElement at front of list sequence.
|
|
|
|
*/
|
|
|
|
void PushFront( T* aNewElement )
|
|
|
|
{
|
|
|
|
insert( aNewElement );
|
|
|
|
}
|
|
|
|
|
2008-12-04 04:28:11 +00:00
|
|
|
/**
|
|
|
|
* Function PushBack
|
|
|
|
* puts aNewElement at the end of the list sequence.
|
|
|
|
*/
|
|
|
|
void PushBack( T* aNewElement )
|
2008-12-02 09:13:28 +00:00
|
|
|
{
|
2008-12-04 04:28:11 +00:00
|
|
|
append( aNewElement );
|
2008-12-02 09:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----</ STL like functions >--------------------------------------
|
2008-11-24 04:34:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // DLIST_H_
|