TVM  0.9.4
OutputSelector.h
Go to the documentation of this file.
1 
3 #pragma once
4 
5 #include <stdexcept>
6 #include <vector>
7 
9 
10 namespace tvm
11 {
12 
13 namespace graph
14 {
15 
16 namespace abstract
17 {
18 
22 template<bool add = true>
24 {
26  std::vector<bool> dynamicallyEnabled_;
28  bool locked_ = false;
29 };
30 
31 template<>
32 struct SelectorMembers<false>
33 {};
34 
35 // forward declaration
36 template<typename T, typename Base>
37 class OutputSelector;
38 
40 template<typename T, typename Base>
41 std::true_type is_output_selector_impl(OutputSelector<T, Base> const volatile &);
42 
44 std::false_type is_output_selector_impl(...);
45 
54 template<typename T>
55 constexpr bool is_output_selector()
56 { return decltype(is_output_selector_impl(std::declval<const T &>()))::value; }
57 
77 template<typename OutputProvider, typename Base = OutputProvider>
78 class OutputSelector : public Base, protected SelectorMembers<!is_output_selector<Base>()>
79 {
80 public:
82  template<typename... Args>
83  OutputSelector(Args &&... args) : Base(std::forward<Args>(args)...)
84  {
85  static_assert(std::is_base_of<Outputs, Base>::value, "Cannot build for a type that is not derived of Outputs");
86  this->dynamicallyEnabled_.resize(OutputProvider::OutputSize, true);
87  }
88 
90  void lock() { this->locked_ = true; }
91 
93  void unlock() { this->locked_ = false; }
94 
96  bool isLocked() const { return this->locked_; }
97 
98 protected:
100  template<typename EnumT>
101  void disableOutput(EnumT e)
102  {
103  if(!is_valid_output<OutputProvider>(e))
104  throw std::runtime_error("You can only disable outputs that are part of the class.");
105 
106  if(!this->locked_)
107  this->dynamicallyEnabled_[static_cast<size_t>(e)] = false;
108  else
109  throw std::runtime_error("Outputs have been locked, you cannot disable one.");
110  }
111 
113  template<typename EnumT, typename... Args>
114  void disableOutput(EnumT e, Args... args)
115  {
116  disableOutput(e);
117  disableOutput(args...);
118  }
119 
121  template<typename EnumT>
122  void enableOutput(EnumT e)
123  {
124  if(!is_valid_output<OutputProvider>(e))
125  throw std::runtime_error("You can only disable outputs that are part of the class.");
126 
127  if(!this->isOutputStaticallyEnabled(static_cast<int>(e)))
128  throw std::runtime_error("You can not enable outputs that are statically disabled.");
129 
130  if(!this->locked_)
131  this->dynamicallyEnabled_[static_cast<size_t>(e)] = true;
132  else
133  throw std::runtime_error("Outputs have been locked, you cannot enable one.");
134  }
135 
137  template<typename EnumT, typename... Args>
138  void enableOutput(EnumT e, Args... args)
139  {
140  enableOutput(e);
141  enableOutput(args...);
142  }
143 
145  bool isOutputCustomEnabled(int e) const override
146  {
147  if(e < 0 || e >= static_cast<int>(this->dynamicallyEnabled_.size()))
148  throw std::runtime_error("Enum value is invalid");
149  return this->dynamicallyEnabled_[e];
150  }
151 };
152 
153 } // namespace abstract
154 
155 } // namespace graph
156 
157 } // namespace tvm
Definition: OutputSelector.h:79
void unlock()
Definition: OutputSelector.h:93
void enableOutput(EnumT e, Args... args)
Definition: OutputSelector.h:138
OutputSelector(Args &&... args)
Definition: OutputSelector.h:83
void lock()
Definition: OutputSelector.h:90
void disableOutput(EnumT e)
Definition: OutputSelector.h:101
bool isOutputCustomEnabled(int e) const override
Definition: OutputSelector.h:145
void enableOutput(EnumT e)
Definition: OutputSelector.h:122
bool isLocked() const
Definition: OutputSelector.h:96
void disableOutput(EnumT e, Args... args)
Definition: OutputSelector.h:114
Definition: probe.h:44
constexpr bool is_output_selector()
Definition: OutputSelector.h:55
std::true_type is_output_selector_impl(OutputSelector< T, Base > const volatile &)
Definition: Clock.h:12
Definition: OutputSelector.h:24
bool locked_
Definition: OutputSelector.h:28
std::vector< bool > dynamicallyEnabled_
Definition: OutputSelector.h:26