summaryrefslogtreecommitdiff
path: root/utils/Chain.h
blob: b9ecf6191d645571689656975c3a3e3a9752202b (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
/*
 *	CENA C++ Utilities
 *
 *	by Stephane Chatty
 *
 *	Copyright 1992
 *	Centre d'Etudes de la Navigation Aerienne (CENA)
 *
 *	generic chains of objects - to be updated
 *
 *	$Id$
 *	$CurLog$
 */

#ifndef Chain_H_
#define Chain_H_

#include "cplus_bugs.h"

typedef void CcuChainItem;

class CcuChainIter;


class CcuChain {
friend	class	CcuChainIter;
public:
	enum	chain_status	{ NoError, WasEmpty, TooEarly, TooFar, BadIterator };

protected:
	CcuChainItem*	LastLink;
	chain_status	StatusFlag;
inline		CcuChain () : LastLink (0), StatusFlag (NoError)	{ }
inline		CcuChain (CcuChainItem* e) : LastLink (e), StatusFlag (NoError) { }
virtual		~CcuChain ();

virtual	CcuChainItem*	GetNext (CcuChainItem*) const = 0;
virtual	void	SetNext (CcuChainItem*, CcuChainItem*) const = 0;
virtual	void	Delete (CcuChainItem*) const = 0;
	CcuChainItem*	GetPrevious (CcuChainItem*) ;

public:
inline	chain_status	GetStatus () const		{ return StatusFlag; }
inline	int	IsEmpty () const			{ return !LastLink; }
	CcuChainItem*	First ();
	CcuChainItem*	Last ();
	CcuChainItem*	Nth (int n);
	int	Length () const;
	int	Find (CcuChainItem*) const;

	void	Append (CcuChainItem*);
	void	Prepend (CcuChainItem*);
inline	CcuChain&	operator << (CcuChainItem* it)	{ Append (it); return *this; }

	CcuChainItem*	RemoveFirst ();
	CcuChainItem*	RemoveLast ();
	int	Remove (CcuChainItem*);
	int	Remove (int (*) (CcuChainItem*));
	void	Clear ();

	void	InsertAfter (const CcuChainIter&, CcuChainItem*);
	void	InsertBefore (const CcuChainIter&, CcuChainItem*);
	CcuChainItem*	RemoveAfter (const CcuChainIter&);
	void	InsertAfter (CcuChainItem*, CcuChainItem*);
	void	InsertBefore (CcuChainItem*, CcuChainItem*);
	CcuChainItem*	RemoveAfter (CcuChainItem*);
};

class CcuChainIter {
friend	class	CcuChain;
public:
	enum	chainiter_status	{ Normal, StartOfChain, EndOfChain };
private:
	const CcuChain*	TheChain;
	CcuChainItem*	CurLink;
	chainiter_status	StatusFlag;

public:
inline		CcuChainIter (const CcuChain& l) : TheChain (&l), CurLink (0), StatusFlag (StartOfChain)	{ }
inline	void	Reset ()		{ CurLink = 0; StatusFlag = StartOfChain; }
	CcuChainIter&	operator ++ ();
	CcuChainItem*	operator * () const;
		int	Find (CcuChainItem*);
inline	chainiter_status	GetStatus () const		{ return StatusFlag; }
inline	operator int () const	{ return StatusFlag == Normal; }
};

#ifndef CPLUS_BUG19

template <class ITEM> class CcuChainOf : public CcuChain {
protected:
	CcuChainItem*	GetNext (CcuChainItem* i) const	{ return ((ITEM*) i)->GetNext (); }
	void	SetNext (CcuChainItem* i, CcuChainItem* j) const	{ ((ITEM*) i)->SetNext ((ITEM*) j); }
	void	Delete (CcuChainItem* i) const	{ delete (ITEM*) i; }

public:
inline		CcuChainOf () : CcuChain ()		{ }
inline		CcuChainOf (ITEM* e) : CcuChain (e)		{ }
inline		~CcuChainOf ()	{}
};

template <class ITEM> class CcuChainIterOf : public CcuChainIter {
public:
inline		CcuChainIterOf (const CcuChainOf <ITEM>& l) : CcuChainIter (l)	{ }
inline	ITEM*	operator * () const	{ return (ITEM*) CcuChainIter::operator * (); }
};

#endif	/* CPLUS_BUG19 */

#endif	/* Chain_H_ */