taco-db  0.1.0
TreeNode.h
Go to the documentation of this file.
1 #ifndef UTILS_TREE_NODE_H
2 #define UTILS_TREE_NODE_H
3 
4 #include "tdb.h"
5 
6 #include "utils/misc.h"
7 #include "utils/tree/NodeTag.h"
8 
9 namespace taco {
10 
40 class TreeNode {
41 public:
42 
43  template<class ...UniquePtrs>
44  TreeNode(NodeTag tag, UniquePtrs&& ...input):
45  m_tag(tag),
46  m_input() {
47  m_input.reserve(sizeof...(UniquePtrs));
49  std::forward<UniquePtrs>(input)...);
50  }
51 
52  virtual ~TreeNode() {}
53 
54  TreeNode(TreeNode&&) = default;
55  TreeNode &operator=(TreeNode&&) = default;
56  TreeNode(const TreeNode&) = delete;
57  TreeNode &operator=(const TreeNode &) = delete;
58 
59  constexpr NodeTag
60  get_tag() const {
61  return m_tag;
62  }
63 
64  TreeNode *
65  get_input(size_t i) const {
66  if (i < m_input.size())
67  return m_input[i].get();
68  return nullptr;
69  }
70 
71  template<class Node>
72  Node *
73  get_input_as(size_t i) const {
74  if (i < m_input.size())
75  return static_cast<Node*>(m_input[i].get());
76  return nullptr;
77  }
78 
79  std::string
80  to_string() const {
81  std::string buf;
82  node_to_string(buf, 0);
83  return buf;
84  }
85 
91  void node_to_string(std::string &buf, int indent) const;
92 
93  const char *node_name() const {
94  return node_tag_name(get_tag());
95  }
96 
97  virtual void node_properties_to_string(std::string &buf,
98  int indent) const = 0;
99 
100  static void append_indent(std::string &buf, int indent);
101 private:
103 
104  std::vector<std::unique_ptr<TreeNode>> m_input;
105 
106  static constexpr const int TO_STRING_INDENT_SIZE = 4;
107 };
108 
109 } // namespace taco
110 
111 #endif // UTILS_TREE_NODE_H
TreeNode is the base class of all tree structures in TDB (e.g., parsing tree, logical plan,...
Definition: TreeNode.h:40
TreeNode(NodeTag tag, UniquePtrs &&...input)
Definition: TreeNode.h:44
virtual ~TreeNode()
Definition: TreeNode.h:52
TreeNode & operator=(const TreeNode &)=delete
static void append_indent(std::string &buf, int indent)
Definition: TreeNode.cpp:25
std::vector< std::unique_ptr< TreeNode > > m_input
Definition: TreeNode.h:104
std::string to_string() const
Definition: TreeNode.h:80
TreeNode & operator=(TreeNode &&)=default
NodeTag m_tag
Definition: TreeNode.h:102
static constexpr const int TO_STRING_INDENT_SIZE
Definition: TreeNode.h:106
Node * get_input_as(size_t i) const
Definition: TreeNode.h:73
virtual void node_properties_to_string(std::string &buf, int indent) const =0
TreeNode(const TreeNode &)=delete
constexpr NodeTag get_tag() const
Definition: TreeNode.h:60
void node_to_string(std::string &buf, int indent) const
Prints the node contents to a string buffer.
Definition: TreeNode.cpp:10
const char * node_name() const
Definition: TreeNode.h:93
TreeNode(TreeNode &&)=default
TreeNode * get_input(size_t i) const
Definition: TreeNode.h:65
Definition: datum.h:28
void emplace_back_parameter_pack(Container &c, Arg0 &&arg0, Args &&...args)
Definition: misc.h:102
NodeTag
Definition: NodeTag.h:12
const char * node_tag_name(NodeTag tag)
Returns the class name of the tag's class.
Definition: NodeTag.cpp:78