////////////////////////////////////////////////////////////////////// /// (C)opyright 2004 /// /// Institute of Computer Science V /// Prof. Männer /// University of Mannheim, Germany /// /// ******************************************************************* /// /// Designer(s): Steinle / Gläß /// /// ******************************************************************* /// /// Project: Trackfinder for CBM-Project at GSI-Darmstadt, Germany /// /// ******************************************************************* /// /// Description: /// /// class: /// - template class for handling memory for special vectors of objects /// - because of being a template class the function-bodies must be /// in this header file, too. There is actually no compiler which /// can handle a different cxx-source file for the bodies. /// /// /// ******************************************************************* /// /// $Author: csteinle $ /// $Date: 2007-06-21 13:20:20 $ /// $Revision: 1.2 $ /// ////////////////////////////////////////////////////////////////////// #ifndef _SPECIALMEM_H #define _SPECIALMEM_H #include "../../MiscLIB/include/errorHandling.h" #include #include /* ************************************************************** * TEMPLATE CLASS specialMem * * **************************************************************/ template class specialMem { private: typename std::vector stackMem; /**< Memory to store all objects. */ typename std::vector::iterator activeObjectPointer; /**< Iterator to access one object. */ /** * method returns the iterator for the last element in memory */ typename std::vector::iterator getLastIterator(); /** * method pushes the element at the end of the memory. */ typename std::vector::iterator pushIntern(objectClass element); /** * method returns the last entry and deletes it */ objectClass popIntern(); public: /** * Default constructor */ specialMem(); /** * Constructor */ specialMem(const specialMem& value); /** * Destructor */ ~specialMem(); /** * operator = () */ specialMem& operator =(const specialMem& value); /** * method returns the number of entries */ unsigned long getNumberOfEntries(); unsigned long size(); /** * method pushes the element at the end of the memory. */ void push(objectClass element); /** * method returns the last entry and deletes it */ objectClass pop(); /** * method returns the first entry and deletes it */ objectClass popF(); /** * method returns if the memory is empty */ bool isEmpty(); /** * sets the activeObject to the initial value */ void resetActiveObject(); /** * returns a reference of the activeObject */ objectClass readActiveObject(); /** * set the next one to the active object. */ void makeNextOneActive(); /** * returns a reference of the activeObject and set the next one * to the active object. */ objectClass readActiveObjectAndMakeNextOneActive(); /** * erases the activeObject */ void eraseActiveObject(); /** * method clears the memory */ void clear(); }; /* ************************************************************** * TEMPLATE CLASS specialMem * * **************************************************************/ /** * method returns the iterator for the last element in memory */ template typename std::vector::iterator specialMem::getLastIterator() { typename std::vector::iterator lastIterator; lastIterator = stackMem.end(); if (!isEmpty()) lastIterator--; return lastIterator; } /** * method pushes the element at the end of the memory. */ template typename std::vector::iterator specialMem::pushIntern(objectClass element) { typename std::vector::iterator returnValue; stackMem.push_back(element); returnValue = getLastIterator(); return returnValue; } /** * method returns the last entry and deletes it */ template objectClass specialMem::popIntern() { objectClass returnValue; returnValue = stackMem.back(); stackMem.pop_back(); return returnValue; } /** * Default constructor */ template specialMem::specialMem() { stackMem.clear(); resetActiveObject(); } /** * Constructor */ template specialMem::specialMem(const specialMem& value) { *this = value; } /** * Destructor */ template specialMem::~specialMem() { stackMem.clear(); } /** * operator = () */ template specialMem& specialMem::operator=(const specialMem& value) { typename std::vector::iterator iterator; unsigned long difference; stackMem.clear(); stackMem = value.stackMem; iterator = value.activeObjectPointer; difference = 0; while (iterator != value.stackMem.begin()) { difference++; iterator--; } activeObjectPointer = stackMem.begin(); for (unsigned long i = 0; i < difference; i++) makeNextOneActive(); return *this; } /** * method returns the number of entries */ template unsigned long specialMem::getNumberOfEntries() { typename std::vector::size_type numberOfEntries; numberOfEntries = stackMem.size(); return (unsigned long) numberOfEntries; } template unsigned long specialMem::size() { return getNumberOfEntries(); } /** * method pushes the element at the end of the memory. */ template void specialMem::push(objectClass element) { activeObjectPointer = pushIntern(element); } /** * method returns the last entry and deletes it */ template objectClass specialMem::pop() { objectClass returnValue; if ((activeObjectPointer == getLastIterator()) && (!isEmpty())) activeObjectPointer--; returnValue = popIntern(); return returnValue; } /** * method returns the first entry and deletes it */ template objectClass specialMem::popF() { objectClass returnValue; if ((activeObjectPointer == stackMem.begin()) && (!isEmpty())) makeNextOneActive(); returnValue = stackMem.front(); stackMem.pop_front(); return returnValue; } /** * method returns if the memory is empty */ template bool specialMem::isEmpty() { return stackMem.empty(); } /** * sets the activeObject to the initial value */ template void specialMem::resetActiveObject() { activeObjectPointer = stackMem.begin(); } /** * returns a reference of the activeObject */ template objectClass specialMem::readActiveObject() { if (!isEmpty()) return *activeObjectPointer; else throw cannotReadEmptyMemoryError(DATAOBJECTLIB); } /** * set the next one to the active object. */ template void specialMem::makeNextOneActive() { if (activeObjectPointer == getLastIterator()) resetActiveObject(); else activeObjectPointer++; } /** * returns a reference of the activeObject and set the next one * to the active object. */ template objectClass specialMem::readActiveObjectAndMakeNextOneActive() { objectClass returnValue; returnValue = readActiveObject(); makeNextOneActive(); return returnValue; } /** * erases the activeObject */ template void specialMem::eraseActiveObject() { typename std::vector::iterator position; if (activeObjectPointer != stackMem.end()) { position = activeObjectPointer; makeNextOneActive(); stackMem.erase(position); } } /** * method clears the memory */ template void specialMem::clear() { stackMem.clear(); resetActiveObject(); } #endif