ModErn Text Analysis
META Enumerates Textual Applications
node.h
Go to the documentation of this file.
1 
9 #ifndef META_PARSE_TREE_NODE_H_
10 #define META_PARSE_TREE_NODE_H_
11 
12 #include <memory>
13 #include "meta.h"
14 #include "parser/trees/visitors/visitor.h"
15 
16 namespace meta
17 {
18 namespace parser
19 {
20 
24 class node
25 {
26  public:
31  node(class_label cat);
32 
36  const class_label& category() const;
37 
41  virtual bool is_leaf() const = 0;
42 
47  bool is_temporary() const;
48 
53  virtual bool equal(const node& other) const = 0;
54 
59  virtual std::unique_ptr<node> clone() const = 0;
60 
66  template <class Visitor>
67  typename std::remove_reference<Visitor>::type::result_type
68  accept(Visitor&& vtor)
69  {
70  // this is ugly, but since we cannot have virtual template member
71  // functions, it's a necessary evil
72  if (is_leaf())
73  return vtor(as<leaf_node>());
74  return vtor(as<internal_node>());
75  }
76 
84  template <class Visitor>
85  typename std::remove_reference<Visitor>::type::result_type
86  accept(Visitor&& vtor) const
87  {
88  if (is_leaf())
89  return vtor(as<leaf_node>());
90  return vtor(as<internal_node>());
91  }
92 
100  template <class Node>
101  Node& as()
102  {
103  return static_cast<Node&>(*this);
104  }
105 
113  template <class Node>
114  typename std::add_const<Node>::type& as() const
115  {
116  return static_cast<typename std::add_const<Node>::type&>(*this);
117  }
118 
122  virtual ~node() = default;
123 
124  private:
128  const class_label category_;
129 };
130 }
131 }
132 #endif
std::add_const< Node >::type & as() const
Casting operation for nodes to derived types.
Definition: node.h:114
A single node in a parse tree for a sentence.
Definition: node.h:24
Contains top-level namespace documentation for the META toolkit.
virtual ~node()=default
Default virtual destructor for deriving classes.
bool is_temporary() const
Definition: node.cpp:36
const class_label category_
The category for this node.
Definition: node.h:128
virtual bool equal(const node &other) const =0
virtual std::unique_ptr< node > clone() const =0
Clones the given node.
std::remove_reference< Visitor >::type::result_type accept(Visitor &&vtor) const
Accepts a visitor (const version).
Definition: node.h:86
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retreival, data mining, and other applications of text processing.
Definition: analyzer.h:24
Node & as()
Casting operation for nodes to derived types.
Definition: node.h:101
const class_label & category() const
Definition: node.cpp:26
virtual bool is_leaf() const =0
Definition: node.cpp:31
std::remove_reference< Visitor >::type::result_type accept(Visitor &&vtor)
Accepts a visitor.
Definition: node.h:68
node(class_label cat)
Constructs a new node with the given category.
Definition: node.cpp:21