/* Copyright (C) 2013-2020, Dirk Krause SPDX-License-Identifier: BSD-3-Clause */ /* WARNING: This file was generated by the dkct program (see http://dktools.sourceforge.net/ for details). Changes you make here will be lost if dkct is run again! You should modify the original source and run dkct on it. Original source: Dk4Container.cpt */ /** @file Dk4Container.h Header file for the Dk4Container module. */ #ifndef DK4CONTAINER_H_INCLUDED /** Avoid multiple inclusions. */ #define DK4CONTAINER_H_INCLUDED 1 #line 8 "Dk4Container.cpt" #ifndef DK4CONF_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "dk4conf.h" #else #include #endif #endif #ifndef DK4ERROR_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "dk4error.h" #else #include #endif #endif #ifndef DK4STO_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "dk4sto.h" #else #include #endif #endif #include #include #ifndef DK4COMPARABLE_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "Dk4Comparable.h" #else #include #endif #endif /** Namespace to protect Container and Iterator from name collisions. */ namespace DkTools4 { /** Container class for sorted storage. */ template class Container; /** Iterator class to traverse a Container. */ template class Iterator; /** A container class for sorted storage. The objects saved in the storage must be derived from the Comparable class. */ template class Container { /** Iterator is allowed to access Containers internals. */ friend class Iterator; private: /** Storage to store elements. */ dk4_sto_t *sto; /** Iterator, used in destructor. */ dk4_sto_it_t *it; /** Comparison criteria for ordering elements. */ int cr; /** Flag: Container is owner of the objects and in charge for destruction. */ bool bOwner; public: /** Constructor. @param crit Sort criteria for objects in container. @param owner Owner flag value. If a container is the owner of the objects stored in it, the objects are deleted in the containers destructor. Exceptions: - std::bad_alloc
if there is not enough memory available. */ Container(int crit, bool owner = false) /* throws(std::bad_alloc) */ ; /** Destructor. */ ~Container(); /** Add an object to the container. @param ptr Pointer to add. @return True on success, false on error (object not added). */ bool add(T *ptr); /** Remove object from container. @param ptr Pointer to remove. @return True on success, false on error (element not found). */ bool remove(T *ptr); /** Check owner flag. @return Owner flag value. */ bool isOwner(void) const; /** Reset iterator. */ void reset(void); /** Set iterator to exact position. @param data Object to search for. @param crit Comparison criteria. @return Pointer to found object on success, NULL otherwise. */ T * findExact(void const *data, int crit); /** Set iterator to object evaluating equally. @param data Object to search for. @param crit Comparison criteria. @return Pointer to found object on success, NULL otherwise. */ T * findLike(void const *data, int crit); /** Step to next element. @return Pointer to next object (if there is one), NULL if there is no next object. */ T * next(void); }; /* End of class Container */ /** Iterator class to traverse a Container. */ template class Iterator { private: /** Low-level iterator. */ dk4_sto_it_t *it; public: /** Constructor. @param cont Container for which the iterator is for. Exceptions: - std::bad_alloc
if there is not enough memory available. */ Iterator(Container & cont) /* throws(std::bad_alloc) */ ; /** Destructor. */ ~Iterator(); /** Reset iterator. */ void reset(void); /** Set iterator to exact position. @param data Object to search for. @param crit Comparison criteria. @return Pointer to found object on success, NULL otherwise. */ T * findExact(void const *data, int crit); /** Set iterator to object evaluating equally. @param data Object to search for. @param crit Comparison criteria. @return Pointer to found object on success, NULL otherwise. */ T * findLike(void const *data, int crit); /** Step to next element. @return Pointe to next object (if there is one), NULL if there is no next object. */ T * next(void); }; /* End of class Iterator */ } /* End of namespace DkTools4 */ /* ************************************************************************ */ /* * * */ /* * Implementation for container methods. * */ /* * * */ /* ************************************************************************ */ #line 250 "Dk4Container.cpt" template DkTools4::Container::Container(int crit, bool owner) { bool mustThrow = true; /* Flag: Must throw exception. */ /* Initialize members. */ bOwner = owner; cr = crit; sto = NULL; it = NULL; /* Allocate memory. */ sto = dk4sto_open(NULL); if(NULL != sto) { dk4sto_set_comp(sto, DkTools4::Comparable::compareTwoPointers, crit); it = dk4sto_it_open(sto, NULL); if (NULL != it) { mustThrow = false; /* All memory allocations succeeded. */ } else { dk4sto_close(sto); sto = NULL; } } if (mustThrow) { throw(std::bad_alloc()); } } /* The destructor is also called when an exception is thrown by the constructor. So we must check the sto and it component for being not NULL. */ template DkTools4::Container::~Container() { T *ptr; /* Close storage and iterator. If we are the owner of the objects stored, delete the objects too. */ if (NULL != it) { if (bOwner) { dk4sto_it_reset(it); while(NULL != (ptr = (T *)dk4sto_it_next(it))) { delete ptr; } } dk4sto_it_close(it); } if (NULL != sto) { dk4sto_close(sto); } /* Mark all components as not allocated. */ sto = NULL; it = NULL; cr = 0; bOwner = false; } /* The remaining container methods are not called if there was an exception from the constructor. So no NULL pointer checks are necessary here. */ template bool DkTools4::Container::add(T *ptr) { bool back = false; if(0 != dk4sto_add(sto, (void *)ptr, NULL)) { back = true; } return back; } template bool DkTools4::Container::remove(T *ptr) { bool back = false; if(0 != dk4sto_remove(sto, (void *)ptr, NULL)) { back = true; } return back; } template bool DkTools4::Container::isOwner(void) const { return bOwner; } /* The remaining iterator methods are not invoked if the constructor throws an exception, so we do not need to check the it component. */ template void DkTools4::Container::reset(void) { dk4sto_it_reset(it); } template T * DkTools4::Container::findExact(void const *data, int crit) { T *back = NULL; back = (T *)dk4sto_it_find_exact(it, data); return back; } template T * DkTools4::Container::findLike(void const *data, int crit) { T *back; back = (T *)dk4sto_it_find_like(it, data, crit); return back; } template T * DkTools4::Container::next(void) { T *back = NULL; back = (T *)dk4sto_it_next(it); return back; } /* ************************************************************************ */ /* * * */ /* * Implementation for iterator methods. * */ /* * * */ /* ************************************************************************ */ #line 415 "Dk4Container.cpt" template DkTools4::Iterator::Iterator(Container & cont) { it = dk4sto_it_open(cont.sto, NULL); if(NULL != it) { dk4sto_it_reset(it); } else { throw(std::bad_alloc()); } } /* The iterators destructor is also invoked if the constructor throws an exception. So we must check the it component to be not NULL. */ template DkTools4::Iterator::~Iterator() { if (NULL != it) { dk4sto_it_close(it); it = NULL; } } /* The remaining iterator methods are not invoked if the constructor throws an exception, so we do not need to check the it component. */ template void DkTools4::Iterator::reset(void) { dk4sto_it_reset(it); } template T * DkTools4::Iterator::findExact(void const *data, int crit) { T *back = NULL; back = (T *)dk4sto_it_find_exact(it, data); return back; } template T * DkTools4::Iterator::findLike(void const *data, int crit) { T *back; back = (T *)dk4sto_it_find_like(it, data, crit); return back; } template T * DkTools4::Iterator::next(void) { T *back = NULL; back = (T *)dk4sto_it_next(it); return back; } /* vim: set ai sw=4 ts=4 : */ #endif