summaryrefslogtreecommitdiff
path: root/support/dktools/Dk4Comparable.cpt
blob: 68c66538165d38afdef4fcc9d8e237f0903d2ac3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
%%	options

copyright owner =			 Dirk Krause
copyright year  =			 2013-xxxx
SPDX-License-Identifier:	BSD-3-Clause

%%	header

#ifndef	DK4CONF_H_INCLUDED
#if	DK4_BUILDING_DKTOOLS4
#include "dk4conf.h"
#else
#include <dktools-4/dk4conf.h>
#endif
#endif

#if DK4_HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if DK4_HAVE_UNISTD_H
#include <unistd.h>
#endif


/**	Namespace to protect Container and Iterator from name collisions.
*/
namespace DkTools4 {

	/**	Comparable is a general base class for objects we want to compare,
		for example for sorted storage.

		Derived classes must implement the compareAgainst() method.
	*/
	class Comparable
	{
		public:

			/**	Compare object against another object or some data.
				Derived classes must implement this method.
				The comparison is used by sorted containers.
				Example: When storing persons (name and age) we can use
				crit=0 as comparison criteria in the container to compare
				person object against person object.
				When searching the container for an object with a specific name
				we can use crit=1 for comparisons of the object against a
				string.
			 	@param	pt		Address of other object or data.
				@param	crit	Comparison criteria, see documentation of
								derived classes for class-specific numbers.
				@return			Positive number if larger than ptr, 0 if
								evaluates equal to ptr, negative number if
								smaller than ptr.
			*/

			virtual
			int
			compareAgainst(void const *ptr, int crit) const = 0;


			/**	Compare two pointers
				@param	l	Left pointer.
				@param	r	Right pointer.
				@param	cr	Comparison criteria.
				@return	1	for left object is large, 0 for right object
							is large, 0 for objects evaluating equally.
			*/

			static
			int
			compareTwoPointers(void const *l, void const *r, int cr);


			/**	Virtual destructor required by compiler.
			*/

			virtual
			~Comparable();

	};	/* End of class Comparable. */

}	/* End of namespace DkTools4 */


%%	module

#include "Dk4Comparable.h"

int
DkTools4::Comparable::compareTwoPointers(void const *l, void const *r, int cr)
{
	int back = 0;
	if(NULL != l) {
		if(NULL != r) {
			DkTools4::Comparable const *pl = (DkTools4::Comparable const *)l;
			back = pl->compareAgainst(r, cr);
		}
		else {
			back = 1;
		}
	} else {
		if(r) back = -1;
	}
	return back;
}



DkTools4::Comparable::~Comparable()
{
}



/* vim: set ai sw=4 ts=4 : */