Laxkit  0.0.7.1
Public Member Functions | Public Attributes | Protected Attributes
Laxkit::PtrStack< T > Class Template Reference

A generic stack for pointers (like anXWindow *, char *), not values. More...

Inheritance diagram for Laxkit::PtrStack< T >:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 PtrStack (char nar=1)
virtual ~PtrStack ()
 PtrStack Destructor, just calls flush().
virtual T * operator[] (int i)
 Return pointer to element i, or NULL.
virtual void flush ()
 Flush the stack. Makes e==NULL.
virtual int howmany ()
 Returns how many things are on the stack.
virtual void swap (int i1, int i2)
 Swap the elements with indices i1 and i2.
virtual int push (T *nd, char local=-1, int where=-1)
 Push a pointer onto the stack before index where. Transfers pointer, does not duplicate.
virtual int popp (T *topop, int *local=NULL)
 Pop the first item that points where topop points.
virtual int pop (T *&popped, int which=-1, int *local=NULL)
 Pop element with index which (defaults to top), and make popped point to it.
virtual T * pop (int which=-1, int *local=NULL)
 Pop element with index which, or the top if which not specified.
virtual int findindex (T *t)
 Find the index (in the range [0,n-1]) corresponding to the pointer t.
virtual int remove (int which=-1)
 Pop and delete (if islocal) the element at index which.
virtual int pushnodup (T *nd, char local, int where=-1)
 Pushes an element only if it is not already on the stack.
virtual void Delta (int ndelta)
 Set the delta.
virtual int Delta ()
 Get the delta.
virtual T ** extractArrays (char **local=NULL, int *nn=NULL)
 Return the e and islocal arrays, and internally set them to NULL, max=n=0.
virtual int insertArrays (T **a, char *nl, int nn)
 Flush, then use a as the new array.

Public Attributes

charislocal
int n
 The number of elements on the stack.
T ** e
 The elements of the stack.

Protected Attributes

int max
 The number of spaces allocated in the internal array.
int delta
 Size of chunks of memory to add or remove from internal array.
char arrays
 The default local value for elements of the stack.

Detailed Description

template<class T>
class Laxkit::PtrStack< T >

A generic stack for pointers (like anXWindow *, char *), not values.

The internal array has max elements allocated. When an item is pushed, and the number of actual elements is greater than max, then the array is reallocated with max+delta spaces. If an element is popped, and the number of elements is less than max-2*delta, then the array is reallocated with max-delta spaces.

If created PtrStack(2), then all the elements are assumed to be arrays, and will be deleted with a call like delete[] element rather than delete element. If PtrStack(1) (the defualt constructor), then default is delete item. If PtrStack(0), then default is to not delete at all.

When elements are pushed, there is the option of specifying that the stack should not delete the element at all. Calling push(blah,1) means that "delete blah" will be called when removing the item or flushing the stack. Calling push(blah,2) means that "delete[] blah" will be called. When push(blah,-1) is called, the value of arrays is used for its local tag. push(blah, any other number) will result in the element not being delete'd at all.


Constructor & Destructor Documentation

template<class T >
Laxkit::PtrStack< T >::PtrStack ( char  nar = 1)

nar==1 (the default) means elements get delete'd, 2 means delete[]'d, other means don't delete.


Member Function Documentation

template<class T >
T ** Laxkit::PtrStack< T >::extractArrays ( char **  local = NULL,
int nn = NULL 
)
virtual

Return the e and islocal arrays, and internally set them to NULL, max=n=0.

If local==NULL, then just delete local, rather than return it.

template<class T>
int Laxkit::PtrStack< T >::findindex ( T *  t)
virtual
template<class T >
void Laxkit::PtrStack< T >::flush ( )
virtual
template<class T>
int Laxkit::PtrStack< T >::howmany ( )
inlinevirtual

Returns how many things are on the stack.

This function is not called from within PtrStack, so derived classes can make "how many things on the stack" mean something other than how many elements are in e.

References Laxkit::PtrStack< T >::n.

template<class T>
int Laxkit::PtrStack< T >::insertArrays ( T **  a,
char nl,
int  nn 
)
virtual

Flush, then use a as the new array.

Note that it is assumed that flush sets e and islocal=NULL. e is then set to a. No element copying is done, the actual arrays a and nl are used. Does not alter delta, sets max=nn.

If nl==NULL, the islocal array is filled with the value of the arrays variable.

Returns 0 for success, nonzero for error.

Please note that if you are simply reaaranging the elements currently on the stack, this function will not work, since the current stack is flush()'d first, potentiall deleting the elements.

template<class T>
int Laxkit::PtrStack< T >::pop ( T *&  popped,
int  which = -1,
int local = NULL 
)
virtual

Pop element with index which (defaults to top), and make popped point to it.

If there is no element to return, popped is set to NULL. If which is out of bounds, then the top most element is popped.

If local!=NULL, then put whether the item was local there.

Returns the number of elements still in the stack.

Referenced by Laxkit::PrintDialog::addOptions(), Laxkit::SliderPopup::DeleteItem(), LaxInterfaces::PathInterface::DeletePoint(), LaxInterfaces::PathInterface::MakeCircle(), Laxkit::DoublePanner::PopAxes(), Laxkit::DisplayerCairo::PopAxes(), Laxkit::DisplayerXlib::PopAxes(), and Laxkit::RefPtrStack< T >::remove().

template<class T>
T * Laxkit::PtrStack< T >::pop ( int  which = -1,
int local = NULL 
)
virtual

Pop element with index which, or the top if which not specified.

If which==-1 (the default) then pop off the top (highest index) of the stack. If which==-2, do nothing (see findindex()) and return NULL.

Returns:
Returns the popped pointer, or NULL on error.

If local!=NULL, then put whether the item was local there.

template<class T>
int Laxkit::PtrStack< T >::popp ( T *  topop,
int local = NULL 
)
virtual

Pop the first item that points where topop points.

Return 1 if item popped, else 0.

If local!=NULL, then put whether the item was local there.

Referenced by LaxInterfaces::ViewportWindow::Pop().

template<class T>
int Laxkit::PtrStack< T >::push ( T *  ne,
char  local = -1,
int  where = -1 
)
virtual

Push a pointer onto the stack before index where. Transfers pointer, does not duplicate.

If called without where, pointer is pushed onto the top (highest n) position.

If local==-1, then use arrays for local. If local==1, then when the stack flushes or the the element is removed, then it is delete'd. If local==2, then the element will be delete[]'d. If local is any other value, then delete is not called on the element.

Please note that pushing NULL objects is ok.

Returns the index of the new element on the stack, or -1 if the push failed.

Reimplemented in Laxkit::RefPtrStack< T >, Laxkit::RefPtrStack< anInterface >, Laxkit::RefPtrStack< Color >, Laxkit::RefPtrStack< ShortcutHandler >, Laxkit::RefPtrStack< anXWindow >, Laxkit::RefPtrStack< SomeData >, Laxkit::RefPtrStack< TouchObject >, Laxkit::RefPtrStack< MenuItem >, and Laxkit::RefPtrStack< LaxFont >.

Referenced by Laxkit::SplitWindow::Add(), Laxkit::ShortcutHandler::AddAction(), Laxkit::IconSelector::AddBox(), Laxkit::TreeSelector::AddColumn(), Laxkit::TagCloud::AddObject(), Laxkit::IconManager::addpath(), LaxInterfaces::PathInterface::AddPoint(), Laxkit::ShortcutHandler::AddShortcut(), Laxkit::SimpleUnit::AddUnits(), Laxkit::TabFrame::AddWin(), Laxkit::SplitWindow::AddWindowType(), LaxInterfaces::PathsData::append(), Laxkit::RowColBox::arrangeBoxes(), Laxkit::TreeSelector::CharInput(), LaxInterfaces::SomeDataFactory::DefineNewObject(), LaxInterfaces::EngraverFillData::dump_in_atts(), LaxInterfaces::PathsData::dump_in_atts(), LaxInterfaces::EngraverFillData::FillRegularLinesHorizontal(), Laxkit::SplitWindow::init(), Laxkit::Tagged::InsertTag(), Laxkit::lark_id_from_str(), LaxInterfaces::FreehandInterface::LBDown(), LaxInterfaces::FreehandInterface::MouseMove(), Laxkit::newCoreXlibDeviceManager(), Laxkit::RefPtrStack< T >::push(), Laxkit::ListBox::Push(), Laxkit::RowColBox::Push(), Laxkit::DoublePanner::PushAxes(), Laxkit::DisplayerCairo::PushAxes(), Laxkit::DisplayerXlib::PushAxes(), LaxInterfaces::PathsData::pushEmpty(), and Laxkit::SplitWindow::splitthewindow().

template<class T>
int Laxkit::PtrStack< T >::pushnodup ( T *  ne,
char  local,
int  where = -1 
)
virtual

Pushes an element only if it is not already on the stack.

Please note that this checks for whether anything on the stack points to the same address that the supplied pointer points to, (address equality) NOT whether the contents of whatever the pointer points to match the contents of any element on the stack (content equality).

If item already exists, where is ignored... should it cause a swap instead?

local is interpreted same way as in normal push(): -1 use default, 1 delete item, 2 delete[] item. Other value, don't delete item.

Returns -1 if the item was pushed, otherwise the index of the item in the stack.

Referenced by Laxkit::TreeSelector::addselect(), LaxInterfaces::PathInterface::LBDown(), LaxInterfaces::PathInterface::MakeCircle(), LaxInterfaces::PathInterface::MergeEndpoints(), and LaxInterfaces::PathInterface::selectPoint().

template<class T >
int Laxkit::PtrStack< T >::remove ( int  which = -1)
virtual
template<class T >
void Laxkit::PtrStack< T >::swap ( int  i1,
int  i2 
)
virtual

Swap the elements with indices i1 and i2.

If i1 or i2 is out of bounds, then substitute the top of the stack for it.


Member Data Documentation

template<class T>
char Laxkit::PtrStack< T >::arrays
protected

The default local value for elements of the stack.

This should be 2 to delete[] elements, rather than just delete (1) or 0 to not delete at all.


The documentation for this class was generated from the following files:

Mon Feb 17 2014 11:52:59, Laxkit