summaryrefslogtreecommitdiff
path: root/include/treekey.h
blob: 02b944e11ad3e22210a90b75b3dccb4dc9d2da45 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/******************************************************************************
 *
 *  treekey.h -	code for class 'TreeKey'
 *
 * $Id: treekey.h 2833 2013-06-29 06:40:28Z chrislit $
 *
 * Copyright 2002-2013 CrossWire Bible Society (http://www.crosswire.org)
 *	CrossWire Bible Society
 *	P. O. Box 2528
 *	Tempe, AZ  85280-2528
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation version 2.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 */


#ifndef TREEKEY_H
#define TREEKEY_H

#include <swkey.h>
#include <swbuf.h>

#include <defs.h>

SWORD_NAMESPACE_START

/**
 * Class TreeKey
 * The base class for all tree-based key implementations in Sword.
 */
class SWDLLEXPORT TreeKey : public SWKey {
	static SWClass classdef;
	void init();

protected:
	// hold on to setText until we've done a snap action: getText or navigation
	// if we set, and then write out userData, we want to assure the path exists.
	// This better conforms to the SWORD write methodology: mod.setKey, mod.setEntry
	mutable SWBuf unsnappedKeyText;

	// called whenever position of this key is changed.  Should we move this
	// to a more base class?
	void positionChanged() { if (posChangeListener) posChangeListener->positionChanged(); }

public:

	class PositionChangeListener {
		TreeKey *treeKey;
	public:
		PositionChangeListener() {}
		virtual ~PositionChangeListener() {}
		virtual void positionChanged() = 0;
		TreeKey *getTreeKey() { return treeKey; }
		void setTreeKey(TreeKey *tk) { treeKey = tk; }
	} *posChangeListener;

	void setPositionChangeListener(PositionChangeListener *pcl) { posChangeListener = pcl; posChangeListener->setTreeKey(this); }

//	TreeKey (const char *ikey = 0);
//	TreeKey (const SWKey * ikey);
//	TreeKey (TreeKey const &k);
	TreeKey () { init(); };
	~TreeKey () {};


	virtual const char *getLocalName() = 0;
	virtual const char *setLocalName(const char *) = 0;

	virtual int getLevel() { long bm = getOffset(); int level = 0; do { level++; } while (parent()); setOffset(bm); return level; }

	virtual const char *getUserData(int *size = 0) const = 0;
	virtual void setUserData(const char *userData, int size = 0) = 0;

	/** Go to the root node
	*/
	virtual void root() = 0;

	/** Go to the parent of the current node
	* @return success or failure
	*/
	virtual bool parent() = 0;

	/** Go to the first child of the current node
	* @return success or failure
	*/
	virtual bool firstChild() = 0;

	/** Go to the next sibling of the current node
	* @return success or failure
	*/
	virtual bool nextSibling() = 0;

	/** Go to the previous sibling of the current node
	* @return success or failure
	*/
	virtual bool previousSibling() = 0;

	/** Does the current node have children?
	* @return whether or not it does
	*/
	virtual bool hasChildren() = 0;

	virtual void append() = 0;
	virtual void appendChild() = 0;
	virtual void insertBefore() = 0;

	virtual void remove() = 0;


	virtual void setOffset(unsigned long offset) = 0;
	virtual unsigned long getOffset() const = 0;

	virtual void setText(const char *ikey) = 0;
	virtual void setPosition(SW_POSITION p) = 0;
	virtual const char *getText() const = 0;
	virtual int compare(const SWKey &ikey) = 0;
	virtual void decrement(int steps = 1) = 0;
	virtual void increment(int steps = 1) = 0;
	virtual bool isTraversable() const { return true; }
	virtual long getIndex() const { return getOffset(); }
	virtual void setIndex(long iindex) { setOffset(iindex); }

	/** Set the key to this path.  If the path doesn't exist, then
	 *	nodes are created as necessary
	 * @param keyPath path to set/create; if unsupplied, then use any unsnapped setText value.
	 */
	virtual void assureKeyPath(const char *keyPath = 0);
	virtual void save() {}

	SWKEY_OPERATORS

	};

SWORD_NAMESPACE_END
#endif