/* * ===================================================================================== * * Filename: 8.00.Ringliste.cc * * Description: Implements a generic ringlist * * Version: 1.0 * Created: 25.06.2014 18:32:32 * Revision: none * Compiler: gcc * * Author: Stefan Suhren (SSuhren), suhren.stefan@fh-swf.de * Organization: FH Südwestfalen, Iserlohn * * ===================================================================================== */ #include #include #include #include using namespace std; typedef unsigned int uint; static const uint RinglisteErsatzLaenge = 20; // Ersatzwert für die Listenlänge // ===================================================================================== // Class: Ringliste // Description: // ===================================================================================== template < class T > class Ringliste { public: // ==================== LIFECYCLE ======================================= Ringliste ( uint lng = RinglisteErsatzLaenge ); // constructor Ringliste ( const Ringliste &other ); // copy constructor ~Ringliste (); // destructor // ==================== ACCESSORS ======================================= T last(); // letzten Wert zurückgeben void dump(); // gesamte Ringliste ausgeben (Test) // ==================== MUTATORS ======================================= Ringliste& add ( T wert ); // neuen Wert aufnehmen // ==================== OPERATORS ======================================= Ringliste& operator = ( const Ringliste &other ); // assignment operator private: // ==================== DATA MEMBERS ======================================= uint laenge; // Listenlänge uint index; // nächste Position T *element; // Zeiger auf das Feld der Listenelem. }; // ----- end of class Ringliste ----- // === FUNCTION ====================================================================== // Name: Ringliste::Ringliste // Description: // ===================================================================================== template < class T > Ringliste::Ringliste ( uint lng ){ index = 0; laenge = lng; element = new T[laenge]; } // ----- end of function Ringliste::Ringliste ----- // === FUNCTION ====================================================================== // Name: Ringliste::Ringliste // Description: // ===================================================================================== template < class T > Ringliste::Ringliste ( const Ringliste &other ){ // Exeption safty T *tmp = new T[other.laenge]; for(uint i = 0; i < other.laenge; i++){ tmp[i] = other.element[i]; } laenge = other.laenge; index = other.index; delete[] element; element = tmp; } // ----- end of function Ringliste::Ringliste ----- // === FUNCTION ====================================================================== // Name: Ringliste::last // Description: // ===================================================================================== template < class T > T Ringliste::last (){ if( index == 0 ){ return element[laenge - 1]; } else{ return element[index - 1]; } } // ----- end of function Ringliste::last ----- // === FUNCTION ====================================================================== // Name: Ringliste::dump // Description: // ===================================================================================== template < class T > void Ringliste::dump (){ for(uint i = 0; i < laenge; i++){ if ( i % 10 == 0 && i != 0 ) cout << "\n"; cout << element[i] << " "; } } // ----- end of function Ringliste::dump ----- // === FUNCTION ====================================================================== // Name: Ringliste::add // Description: // ===================================================================================== template < class T > Ringliste& Ringliste::add ( T wert ){ assert( element != NULL ); assert( index < laenge ); element[index] = wert; if(++index >= laenge){ index = 0; } return *this; } // ----- end of function Ringliste::add ----- // === FUNCTION ====================================================================== // Name: Ringliste::~Ringliste // Description: // ===================================================================================== template < class T > Ringliste::~Ringliste (){ delete[] element; } // ----- end of function Ringliste::~Ringliste ----- // === FUNCTION ====================================================================== // Name: Ringliste::operator= // Description: // ===================================================================================== template < class T > Ringliste& Ringliste::operator= ( const Ringliste &other ){ // No self assignments if ( this != &other ){ // Exeption safty T *tmp = new T[other.laenge]; for(uint i = 0; i < other.laenge; i++){ tmp[i] = other.element[i]; } laenge = other.laenge; index = other.index; delete[] element; element = tmp; } return *this; } // ----- end of function Ringliste::operator= ----- // === FUNCTION ====================================================================== // Name: main // Description: // ===================================================================================== int main ( int argc, char *argv[] ){ Ringliste rnglst1(5); for(int i = 0; i < 10; i++){ rnglst1.add(i); rnglst1.dump(); cout << "\n"; } cout << "\n" << rnglst1.last() << "\n\n"; Ringliste rnglst2(5); string werte[10] = {"0","1","2","3","4","5","6","7","8","9"}; for(int i = 0; i < 10; i++){ rnglst2.add(werte[i]); rnglst2.dump(); cout << "\n"; } cout << "\n" << rnglst2.last(); return EXIT_SUCCESS; } // ---------- end of function main ----------