1 #ifndef MENUPP_NODE_HPP_
2 #define MENUPP_NODE_HPP_
13 INode
* _ptr
= nullptr;
18 NodeId(INode
* ptr
, size_t n_child
)
19 : _ptr(ptr
), _n_child(static_cast<int>(n_child
))
22 static const NodeId ROOT
;
24 bool is_valid() const { return _n_child
>= 0; }
26 bool is_root() const { return is_valid() && !_ptr
; }
28 INode
* addr() const { return _ptr
; }
30 int n() const { return _n_child
; }
32 NodeId
parent() const;
34 INode
* get(INode
& root
);
36 friend std::ostream
& operator<<(std::ostream
& os
, const NodeId
& nid
)
37 { return os
<< '(' << nid
._ptr
<< ", " << nid
._n_child
<< ')'; }
39 friend bool operator==(const NodeId
& x
, const NodeId
& y
)
40 { return x
._ptr
== y
._ptr
&& x
._n_child
== y
._n_child
; }
42 friend bool operator!=(const NodeId
& x
, const NodeId
& y
)
46 const NodeId
NodeId::ROOT
{ nullptr, 0 };
50 virtual ~INode() = default;
51 virtual void set_id(NodeId id
) = 0;
52 virtual NodeId
id() const = 0;
53 virtual void dump(std::ostream
& os
, size_t indent
= 4, size_t total_indent
= 0) const = 0;
54 virtual NodeId
move(Move m
) = 0;
55 virtual INode
* get(size_t n
) = 0;
56 virtual const char* label() const = 0;
58 friend std::ostream
& operator<<(std::ostream
& os
, const INode
& node
);
61 NodeId
NodeId::parent() const
66 ? (is_root() ? NodeId() : ROOT
)
70 INode
* NodeId::get(INode
& root
)
75 : _ptr
->get(static_cast<size_t>(_n_child
)))
79 std::ostream
& operator<<(std::ostream
& os
, const INode
& node
)
80 { node
.dump(os
, 4); return os
; }
84 #endif // MENUPP_NODE_HPP_