summaryrefslogtreecommitdiff
path: root/support/dktools/Dk4Container.cpt
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2020-09-20 03:03:26 +0000
committerNorbert Preining <norbert@preining.info>2020-09-20 03:03:26 +0000
commit1f457376b478257b88d4a857f5ec1b6155442dd7 (patch)
tree2a06a60551dea362cf8cb0cb0ba66c78608717c4 /support/dktools/Dk4Container.cpt
parentac690ca29ad5bf8a5203a65fd6252f7b564f4727 (diff)
CTAN sync 202009200303
Diffstat (limited to 'support/dktools/Dk4Container.cpt')
-rw-r--r--support/dktools/Dk4Container.cpt554
1 files changed, 327 insertions, 227 deletions
diff --git a/support/dktools/Dk4Container.cpt b/support/dktools/Dk4Container.cpt
index be082f5619..cd01807f8f 100644
--- a/support/dktools/Dk4Container.cpt
+++ b/support/dktools/Dk4Container.cpt
@@ -2,166 +2,243 @@
copyright owner = Dirk Krause
copyright year = 2013-xxxx
-license = bsd
+SPDX-License-Identifier: BSD-3-Clause
%% header
-#include <dk4error.h>
-#include <dk4sto.h>
+#ifndef DK4CONF_H_INCLUDED
+#if DK4_BUILDING_DKTOOLS4
+#include "dk4conf.h"
+#else
+#include <dktools-4/dk4conf.h>
+#endif
+#endif
+
+#ifndef DK4ERROR_H_INCLUDED
+#if DK4_BUILDING_DKTOOLS4
+#include "dk4error.h"
+#else
+#include <dktools-4/dk4error.h>
+#endif
+#endif
+
+#ifndef DK4STO_H_INCLUDED
+#if DK4_BUILDING_DKTOOLS4
+#include "dk4sto.h"
+#else
+#include <dktools-4/dk4sto.h>
+#endif
+#endif
#include <stdexcept>
#include <new>
+#ifndef DK4COMPARABLE_H_INCLUDED
+#if DK4_BUILDING_DKTOOLS4
#include "Dk4Comparable.h"
+#else
+#include <dktools-4/Dk4Comparable.h>
+#endif
+#endif
+/** Namespace to protect Container and Iterator from name collisions.
+*/
namespace DkTools4 {
- template<class T> class Container;
- template<class T> class Iterator;
-
- /** A container class for sorted storage.
- The objects must be derived from the Comparable class.
- */
- template<class T>
- class Container
- {
- friend class Iterator<T>;
-
- 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:
- /** Create container.
- @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.
- */
- Container<T>(int crit, bool owner = false) throw (std::bad_alloc);
-
- /** Destructor.
- */
- ~Container<T>();
-
- /** 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.
- */
- bool
- remove(T *ptr);
-
- /** Check owner flag.
- @return Owner flag value.
- */
- void
- isOwner(void) const;
-
- /** Reset iterator.
- */
- void
- reset(void);
-
- /** Set iterator to exact position.
- @param data
- @param crit
- @return Pointer to found object on success, NULL otherwise.
- */
- T *
- findExact(void const *data, int crit);
-
- /** Set iterator to object evaluating equally.
- @param data
- @param crit
- @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 Container */
-
- /** An iterator class to traverse a Containter.
- */
- template<class T>
- class Iterator
- {
- private:
- /** Low-level iterator.
- */
- dk4_sto_it_t *it;
-
- public:
- /** Constructor.
- @param cont Container for which the iterator is for.
- */
- Iterator<T>(Container<T>& cont) throw (std::bad_alloc);
-
- /** Destructor.
- */
- ~Iterator<T>();
-
- /** Reset iterator.
- */
- void
- reset(void);
-
- /** Set iterator to exact position.
- @param data
- @param crit
- @return Pointer to found object on success, NULL otherwise.
- */
- T *
- findExact(void const *data, int crit);
-
- /** Set iterator to object evaluating equally.
- @param data
- @param crit
- @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 */
+ /** Container class for sorted storage.
+ */
+ template<class T> class Container;
+
+ /** Iterator class to traverse a Container.
+ */
+ template<class T> class Iterator;
+
+ /** A container class for sorted storage.
+ The objects saved in the storage must be derived from the Comparable
+ class.
+ */
+ template<class T>
+ class Container
+ {
+ /** Iterator is allowed to access Containers internals.
+ */
+ friend class Iterator<T>;
+
+ 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<br>
+ if there is not enough memory available.
+ */
+
+ Container<T>(int crit, bool owner = false)
+ /* throws(std::bad_alloc) */
+ ;
+
+
+ /** Destructor.
+ */
+
+ ~Container<T>();
+
+
+ /** 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 T>
+ 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<br>
+ if there is not enough memory available.
+ */
+
+ Iterator<T>(Container<T> & cont)
+ /* throws(std::bad_alloc) */
+ ;
+
+
+ /** Destructor.
+ */
+
+ ~Iterator<T>();
+
+
+ /** 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 */
@@ -172,27 +249,34 @@ $*
$*
template<class T>
-DkTools4::Container<T>::Container(int crit, bool owner) throw (std::bad_alloc)
+DkTools4::Container<T>::Container(int crit, bool owner)
{
- bool mustThrow = true;
-
- bOwner = owner;
- cr = crit;
- it = NULL;
- 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;
- } else {
- dk4sto_close(sto);
- sto = NULL;
- }
- }
- if (mustThrow) {
- throw(std::bad_alloc());
- }
+ 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());
+ }
}
@@ -205,24 +289,30 @@ DkTools4::Container<T>::Container(int crit, bool owner) throw (std::bad_alloc)
template<class T>
DkTools4::Container<T>::~Container<T>()
{
- T *ptr;
-
- 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);
- }
- sto = NULL;
- it = NULL;
- cr = 0;
- bOwner = false;
+ 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;
}
@@ -237,11 +327,12 @@ template<class T>
bool
DkTools4::Container<T>::add(T *ptr)
{
- bool back = false;
- if(0 != dk4sto_add(sto, (void *)ptr, NULL)) {
- back = true;
- }
- return back;
+ bool back = false;
+
+ if(0 != dk4sto_add(sto, (void *)ptr, NULL)) {
+ back = true;
+ }
+ return back;
}
@@ -250,20 +341,21 @@ template<class T>
bool
DkTools4::Container<T>::remove(T *ptr)
{
- bool back = false;
- if(0 != dk4sto_remove(sto, (void *)ptr, NULL)) {
- back = true;
- }
- return back;
+ bool back = false;
+
+ if(0 != dk4sto_remove(sto, (void *)ptr, NULL)) {
+ back = true;
+ }
+ return back;
}
template<class T>
-void
+bool
DkTools4::Container<T>::isOwner(void) const
{
- return bOwner;
+ return bOwner;
}
@@ -276,7 +368,7 @@ template<class T>
void
DkTools4::Container<T>::reset(void)
{
- dk4sto_it_reset(it);
+ dk4sto_it_reset(it);
}
@@ -285,9 +377,10 @@ template<class T>
T *
DkTools4::Container<T>::findExact(void const *data, int crit)
{
- T *back = NULL;
- back = (T *)dk4sto_it_find_exact(it, data);
- return back;
+ T *back = NULL;
+
+ back = (T *)dk4sto_it_find_exact(it, data);
+ return back;
}
@@ -296,9 +389,10 @@ template<class T>
T *
DkTools4::Container<T>::findLike(void const *data, int crit)
{
- T *back;
- back = (T *)dk4sto_it_find_like(it, data, crit);
- return back;
+ T *back;
+
+ back = (T *)dk4sto_it_find_like(it, data, crit);
+ return back;
}
@@ -307,9 +401,10 @@ template<class T>
T *
DkTools4::Container<T>::next(void)
{
- T *back = NULL;
- back = (T *)dk4sto_it_next(it);
- return back;
+ T *back = NULL;
+
+ back = (T *)dk4sto_it_next(it);
+ return back;
}
@@ -319,14 +414,15 @@ $*
$*
template<class T>
-DkTools4::Iterator<T>::Iterator(Container<T>& cont) throw (std::bad_alloc)
+DkTools4::Iterator<T>::Iterator(Container<T> & cont)
{
- it = dk4sto_it_open(cont.sto, NULL);
- if(NULL != it) {
- dk4sto_it_reset(it);
- } else {
- throw(std::bad_alloc());
- }
+ it = dk4sto_it_open(cont.sto, NULL);
+ if(NULL != it) {
+ dk4sto_it_reset(it);
+ }
+ else {
+ throw(std::bad_alloc());
+ }
}
@@ -338,9 +434,9 @@ DkTools4::Iterator<T>::Iterator(Container<T>& cont) throw (std::bad_alloc)
template<class T>
DkTools4::Iterator<T>::~Iterator<T>()
{
- if (NULL != it) {
- dk4sto_it_close(it); it = NULL;
- }
+ if (NULL != it) {
+ dk4sto_it_close(it); it = NULL;
+ }
}
@@ -353,7 +449,7 @@ template<class T>
void
DkTools4::Iterator<T>::reset(void)
{
- dk4sto_it_reset(it);
+ dk4sto_it_reset(it);
}
@@ -362,9 +458,9 @@ template<class T>
T *
DkTools4::Iterator<T>::findExact(void const *data, int crit)
{
- T *back = NULL;
- back = (T *)dk4sto_it_find_exact(it, data);
- return back;
+ T *back = NULL;
+ back = (T *)dk4sto_it_find_exact(it, data);
+ return back;
}
@@ -373,9 +469,9 @@ template<class T>
T *
DkTools4::Iterator<T>::findLike(void const *data, int crit)
{
- T *back;
- back = (T *)dk4sto_it_find_like(it, data, crit);
- return back;
+ T *back;
+ back = (T *)dk4sto_it_find_like(it, data, crit);
+ return back;
}
@@ -384,8 +480,12 @@ template<class T>
T *
DkTools4::Iterator<T>::next(void)
{
- T *back = NULL;
- back = (T *)dk4sto_it_next(it);
- return back;
+ T *back = NULL;
+ back = (T *)dk4sto_it_next(it);
+ return back;
}
+
+
+/* vim: set ai sw=4 ts=4 : */
+