source: src/Common/Stats/Base.h@ 8e70823

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since 8e70823 was 8e70823, checked in by tdelisle <tdelisle@…>, 7 years ago

Extracted out BaseCounter as a generic key-value tree

  • Property mode set to 100644
File size: 1.2 KB
Line 
1#pragma once
2
3#include <string>
4
5namespace Stats {
6 namespace Base {
7 class TreeImpl {
8 public:
9 struct Top {
10 TreeImpl * head = nullptr;
11 TreeImpl * tail = nullptr;
12
13 void append(TreeImpl * node) {
14 if(!head) { head = node; }
15 else { tail->next = node;}
16 tail = node;
17 }
18 };
19
20 virtual void print(std::ostream &) = 0;
21
22 const std::string name;
23 TreeImpl(const std::string & name) : name(name) {}
24
25 protected:
26 virtual ~TreeImpl() = default;
27
28 TreeImpl * next = nullptr;
29 Top children;
30
31 template<typename func_t>
32 friend void ForAll(TreeImpl::Top & range, size_t level, func_t func, bool destroy = false);
33 };
34
35 template<typename func_t>
36 inline void ForAll(TreeImpl::Top & range, size_t level, func_t func, bool destroy) {
37 auto it = range.head;
38 while(it) {
39 auto next = it->next;
40 func(it, level);
41 ForAll(it->children, level + 1, func);
42 if(destroy) delete it;
43 it = next;
44 }
45 }
46
47 template<TreeImpl::Top & top>
48 class Tree : public TreeImpl {
49 public:
50 Tree(const std::string & name) : TreeImpl{name} {
51 top.append(this);
52 }
53
54 Tree(const std::string & name, Tree * parent) : TreeImpl{name} {
55 parent->children.append(this);
56 }
57 protected:
58 virtual ~Tree() = default;
59 };
60 }
61}
Note: See TracBrowser for help on using the repository browser.