File: tree.h

package info (click to toggle)
bulletml 0.0.6-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 724 kB
  • ctags: 1,346
  • sloc: cpp: 7,642; xml: 810; perl: 184; makefile: 167; yacc: 124
file content (71 lines) | stat: -rw-r--r-- 1,845 bytes parent folder | download | duplicates (22)
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
/// ʓIȃc[BĐY

#ifndef TREE_H_
#define TREE_H_

#include "bulletmlcommon.h"

#include <list>

/// c[̃NX
/**
 * c[Ă̂̓Rei݂ȂŴłƎvB
 * ŁAm[hĂттNX̏WReiłƁB
 * ŃC^[tFCX́A
 * class YourNode : public TreeNode<YourNode>;
 * ċB
 * |C^ǗOƂĂB
 * CX^X̊Ǘ͕i͂ȂǁA
 * setReleaseDuty Ă΂ꂽm[hj󂳂ƁA
 * ̑qȉ̐͑SĔj󂳂B
 */
template <class C_>
class TreeNode {
public:
    // ev[gō݌vɂ̂
    typedef std::list<C_*> Children;
    typedef typename Children::iterator ChildIterator;
    typedef typename Children::const_iterator ConstChildIterator;

public:
    DECLSPEC TreeNode() {
		releaseDuty_ = false;
    }
    DECLSPEC virtual ~TreeNode();

    DECLSPEC void addChild(C_* c) {
		c->setParent(dynamic_cast<C_*>(this));
		children_.push_back(c);
    }
    DECLSPEC void setReleaseDuty(bool bl) {
		releaseDuty_ = bl;
    }
    DECLSPEC void setParent(C_* c) {
		parent_ = c;
    }

    DECLSPEC ChildIterator childBegin() { return children_.begin(); }
    DECLSPEC ChildIterator childEnd() { return children_.end(); }
	DECLSPEC size_t childSize() { return children_.size(); }
    DECLSPEC ConstChildIterator childBegin() const { return children_.begin(); }
    DECLSPEC ConstChildIterator childEnd() const { return children_.end(); }
    DECLSPEC C_* getParent() { return parent_; }

private:
    Children children_;
    C_* parent_;
    bool releaseDuty_;
};

template <class C_>
TreeNode<C_>::~TreeNode() {
    if (releaseDuty_) {
		ChildIterator ite;
		for (ite = children_.begin(); ite != children_.end(); ite++) {
			(*ite)->setReleaseDuty(true);
			delete *ite;
		}
    }
}

#endif // ! TREE_H_