terminal.h
Go to the documentation of this file.
1 #ifndef TTG_BASE_TERMINAL_H
2 #define TTG_BASE_TERMINAL_H
3 
4 #include <string>
5 #include <vector>
6 #include "ttg/fwd.h"
7 
8 namespace ttg {
9 
12  class TerminalBase {
13  public:
14  static constexpr bool is_a_terminal = true;
15  bool is_pull_terminal = false; //< Default is push terminal
16 
18  enum class Type {
19  Write,
20  Read,
21  Consume
22  };
23 
24  private:
25  TTBase *tt; //< Pointer to containing operation
26  size_t n = 0; //< Index of terminal
27  std::string name = ""; //< Name of terminal
28  bool connected = false; //< True if is connected
29  Type type;
30  std::string key_type_str; //< String describing key type
31  std::string value_type_str; //< String describing value type
32 
33  std::vector<TerminalBase *> successors_;
34  std::vector<TerminalBase *> predecessors_; //This is required for pull terminals.
35 
36  TerminalBase(const TerminalBase &) = delete;
37  TerminalBase(TerminalBase &&) = delete;
38 
39  friend class TTBase;
40  template <typename keyT, typename valueT>
41  friend class In;
42  template <typename keyT, typename valueT>
43  friend class Out;
44 
45  protected:
46  TerminalBase(Type type) : type(type) {}
47 
48  void set(TTBase *tt, size_t index, const std::string &name, const std::string &key_type_str,
49  const std::string &value_type_str, Type type) {
50  this->tt = tt;
51  this->n = index;
52  this->name = name;
53  this->key_type_str = key_type_str;
54  this->value_type_str = value_type_str;
55  this->type = type;
56  }
57 
60  void connect_base(TerminalBase *successor) {
61  successors_.push_back(successor);
62  connected = true;
63  successor->connected = true;
64  }
65 
66  void connect_pull(TerminalBase *predecessor) {
67  predecessors_.push_back(predecessor);
68  predecessor->connected = true;
69  connected = true;
70  }
71 
72  public:
74  TTBase *get_tt() const {
75  if (!tt) throw "ttg::TerminalBase:get_tt() but tt is null";
76  return tt;
77  }
78 
80  size_t get_index() const {
81  if (!tt) throw "ttg::TerminalBase:get_index() but tt is null";
82  return n;
83  }
84 
86  const std::string &get_name() const {
87  if (!tt) throw "ttg::TerminalBase:get_name() but tt is null";
88  return name;
89  }
90 
92  const std::string &get_key_type_str() const {
93  if (!tt) throw "ttg::TerminalBase:get_key_type_str() but tt is null";
94  return key_type_str;
95  }
96 
98  const std::string &get_value_type_str() const {
99  if (!tt) throw "ttg::TerminalBase:get_value_type_str() but tt is null";
100  return value_type_str;
101  }
102 
104  Type get_type() const { return this->type; }
105 
107  const std::vector<TerminalBase *> &get_connections() const { return successors_; }
108  // Get connections to predecessors
109  const std::vector<TerminalBase *> &get_predecessors() const {return predecessors_; }
110 
111  //Connect Container pull terminals without incoming terminals
112  //This is a hack, is there a better way?
114  p->connected = true;
115  }
116 
118  bool is_connected() const { return connected; }
119 
124  virtual void connect(TerminalBase *in) = 0;
125 
126  virtual ~TerminalBase() = default;
127  };
128 
129  namespace meta {
130  template <typename T, typename = void>
131  inline constexpr bool is_terminal_v = false;
132  template <typename T>
133  inline constexpr bool is_terminal_v<T, std::is_base_of<TerminalBase, T>> = true;
134  template <typename T>
135  struct is_terminal : std::bool_constant<is_terminal_v<T>> {};
136  } // namespace meta
137 } // namespace ttg
138 
139 #endif // TTG_BASE_TERMINAL_H
A base class for all template tasks.
Definition: tt.h:30
static constexpr bool is_a_terminal
Definition: terminal.h:14
bool is_connected() const
Returns true if this terminal (input or output) is connected.
Definition: terminal.h:118
TTBase * get_tt() const
Return ptr to containing tt.
Definition: terminal.h:74
const std::string & get_value_type_str() const
Returns string representation of value type.
Definition: terminal.h:98
bool is_pull_terminal
Definition: terminal.h:15
const std::vector< TerminalBase * > & get_predecessors() const
Definition: terminal.h:109
const std::string & get_key_type_str() const
Returns string representation of key type.
Definition: terminal.h:92
size_t get_index() const
Returns index of terminal.
Definition: terminal.h:80
virtual void connect(TerminalBase *in)=0
void connect_base(TerminalBase *successor)
Definition: terminal.h:60
Type get_type() const
Returns the terminal type.
Definition: terminal.h:104
void connect_pull_nopred(TerminalBase *p)
Definition: terminal.h:113
void set(TTBase *tt, size_t index, const std::string &name, const std::string &key_type_str, const std::string &value_type_str, Type type)
Definition: terminal.h:48
virtual ~TerminalBase()=default
const std::string & get_name() const
Returns name of terminal.
Definition: terminal.h:86
TerminalBase(Type type)
Definition: terminal.h:46
const std::vector< TerminalBase * > & get_connections() const
Get connections to successors.
Definition: terminal.h:107
void connect_pull(TerminalBase *predecessor)
Definition: terminal.h:66
Type
describes the terminal type
Definition: terminal.h:18
@ Write
can only be written to
@ Read
can only be used to read immutable data
@ Consume
can only be used to read consumable data
constexpr bool is_terminal_v
Definition: terminal.h:131
top-level TTG namespace contains runtime-neutral functionality
Definition: keymap.h:8