TVM  0.9.4
PairElementToken.h
Go to the documentation of this file.
1 
3 #pragma once
4 
5 #include <assert.h>
6 #include <memory>
7 #include <stdexcept>
8 
9 namespace tvm::internal
10 {
11 class PairElementToken;
12 
19 {
20 public:
26 
27  explicit PairElementTokenHandle(PairElementToken & t) noexcept;
28 
29  bool isValid() const { return token_ != nullptr; }
30 
31 private:
32  void invalidate() { token_ = nullptr; }
33 
34  PairElementToken * token_;
35 
36  friend PairElementToken;
37 };
38 
45 {
46 public:
47  PairElementToken() noexcept {}
48  PairElementToken(const PairElementToken &) = delete; // To keep a pair valid we cannot duplicate tokens
49  PairElementToken & operator=(const PairElementToken &) = delete; // idem
50 
52  PairElementToken(PairElementToken && other) noexcept : otherPairElement_(other.otherPairElement_)
53  {
54  other.otherPairElement_ = nullptr; // we need to leave the previous token in an unpaired state.
55  if(isPaired())
56  otherPairElement_->notifyMove(this); // if paired, we need to notify to otherPairElement_ that this changed place.
57  }
58 
62  PairElementToken(PairElementTokenHandle && h) noexcept : otherPairElement_(h.token_)
63  {
64  assert(h.isValid());
65  h.token_->otherPairElement_ = this;
66  h.invalidate();
67  }
68 
71  {
72  if(isPaired()) // If we move into an already paired token, we basically erase it and its paired element must be
73  // notified
74  otherPairElement_->notifyDeath();
75  otherPairElement_ = other.otherPairElement_;
76  other.otherPairElement_ = nullptr; // we need to leave the previous token in an unpaired state.
77  if(isPaired())
78  otherPairElement_->notifyMove(this); // if paired, we need to notify to otherPairElement_ that this changed place.
79  return *this;
80  }
81 
86  {
87  assert(h.isValid());
88  if(isPaired()) // If we move into an already paired token, we basically erase it and its paired element must be
89  // notified
90  otherPairElement_->notifyDeath();
91  otherPairElement_ = h.token_;
92  h.token_->otherPairElement_ = this;
93  h.invalidate(); // we need to leave the handle in an invalid state.
94  return *this;
95  }
96 
98  {
99  if(isPaired())
100  otherPairElement_->notifyDeath();
101  }
102 
105  {
106  if(isPaired() || other.isPaired())
107  throw std::runtime_error("Can only pair if both tokens are unpaired.");
108  otherPairElement_ = &other;
109  other.otherPairElement_ = this;
110  }
111 
113  bool isPaired() const { return otherPairElement_ != nullptr; }
114 
116  bool isPairedWith(const PairElementToken & other) const { return otherPairElement_ == &other; }
117 
120  {
121  if(!isPaired())
122  throw std::runtime_error("This token is not paired.");
123  return *otherPairElement_;
124  }
125 
126 private:
128  PairElementToken(PairElementToken * other) : otherPairElement_(other)
129  {
130  assert(!other->isPaired());
131  other->otherPairElement_ = this;
132  }
133 
134  void notifyMove(PairElementToken * newAddress) { otherPairElement_ = newAddress; }
135 
136  void notifyDeath() { otherPairElement_ = nullptr; }
137 
138  PairElementToken * otherPairElement_ = nullptr;
139 };
140 
142 { assert(!t.isPaired() && "Input token is already paired."); }
143 } // namespace tvm::internal
Definition: PairElementToken.h:19
PairElementTokenHandle & operator=(PairElementTokenHandle &&)=delete
bool isValid() const
Definition: PairElementToken.h:29
PairElementTokenHandle(PairElementTokenHandle &&)=delete
PairElementTokenHandle(const PairElementTokenHandle &)=delete
PairElementTokenHandle & operator=(const PairElementTokenHandle &)=delete
Definition: PairElementToken.h:45
PairElementToken(const PairElementToken &)=delete
PairElementToken(PairElementTokenHandle &&h) noexcept
Definition: PairElementToken.h:62
PairElementToken & operator=(PairElementTokenHandle &&h) noexcept
Definition: PairElementToken.h:85
PairElementToken & operator=(PairElementToken &&other) noexcept
Definition: PairElementToken.h:70
PairElementToken(PairElementToken &&other) noexcept
Definition: PairElementToken.h:52
PairElementToken() noexcept
Definition: PairElementToken.h:47
void pairWith(PairElementToken &other)
Definition: PairElementToken.h:104
bool isPaired() const
Definition: PairElementToken.h:113
bool isPairedWith(const PairElementToken &other) const
Definition: PairElementToken.h:116
PairElementToken & operator=(const PairElementToken &)=delete
const PairElementToken & otherPairElement() const
Definition: PairElementToken.h:119
~PairElementToken()
Definition: PairElementToken.h:97
Definition: CallbackManager.h:12