-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhierarchy.cxx
45 lines (39 loc) · 859 Bytes
/
hierarchy.cxx
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
#include <iostream>
#include <unordered_map>
#include <maybe_ptr.h>
class Node
{
public:
using dictionary_type = std::unordered_map<std::string, int>;
// root node
Node() :
m_parent(nullptr),
m_dict(make_owner<dictionary_type>())
{}
// node with parent
Node(Node* parent) :
m_parent(parent),
m_dict(make_observer(parent->m_dict))
{}
bool has_parent() const
{
return m_parent != nullptr;
}
bool has_owning_dictionary() const
{
return m_dict.is_owning();
}
private:
Node* m_parent;
maybe_ptr<dictionary_type> m_dict;
};
int main()
{
Node root;
Node child(&root);
std::cout << not root.has_parent() << std::endl;
std::cout << root.has_owning_dictionary() << std::endl;
std::cout << child.has_parent() << std::endl;
std::cout << not child.has_owning_dictionary() << std::endl;
return 0;
}