MLIR  19.0.0git
OwningOpRef.h
Go to the documentation of this file.
1 //===- OwningOpRef.h - MLIR OwningOpRef -------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file provides a base class for owning op refs.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_IR_OWNINGOPREF_H
14 #define MLIR_IR_OWNINGOPREF_H
15 
16 #include <utility>
17 
18 namespace mlir {
19 class Operation;
20 
21 /// This class acts as an owning reference to an op, and will automatically
22 /// destroy the held op on destruction if the held op is valid.
23 ///
24 /// Note that OpBuilder and related functionality should be highly preferred
25 /// instead, and this should only be used in situations where existing solutions
26 /// are not viable.
27 template <typename OpTy>
28 class OwningOpRef {
29 public:
30  /// The underlying operation type stored in this reference.
31  using OperationT = OpTy;
32 
33  OwningOpRef(std::nullptr_t = nullptr) : op(nullptr) {}
34  OwningOpRef(OpTy op) : op(op) {}
35  OwningOpRef(OwningOpRef &&other) : op(other.release()) {}
37  if (op)
38  op->erase();
39  }
40 
41  /// Assign from another op reference.
43  if (op)
44  op->erase();
45  op = other.release();
46  return *this;
47  }
48 
49  /// Allow accessing the internal op.
50  OpTy get() const { return op; }
51  OpTy operator*() const { return op; }
52  auto operator->() {
53  // Specialize for the case where OpTy is a pointer, to allow using
54  // OwningOpRef<Operation*>.
55  if constexpr (std::is_pointer<OpTy>::value)
56  return op;
57  else
58  return &op;
59  }
60  explicit operator bool() const { return op; }
61 
62  /// Downcast to generic operation.
63  operator OwningOpRef<Operation *>() && { return release().getOperation(); }
64 
65  /// Release the referenced op.
66  OpTy release() {
67  OpTy released(nullptr);
68  std::swap(released, op);
69  return released;
70  }
71 
72 private:
73  OpTy op;
74 };
75 
76 } // namespace mlir
77 
78 #endif // MLIR_IR_OWNINGOPREF_H
This class acts as an owning reference to an op, and will automatically destroy the held op on destru...
Definition: OwningOpRef.h:28
OwningOpRef(OpTy op)
Definition: OwningOpRef.h:34
OpTy operator*() const
Definition: OwningOpRef.h:51
OpTy get() const
Allow accessing the internal op.
Definition: OwningOpRef.h:50
OpTy release()
Release the referenced op.
Definition: OwningOpRef.h:66
OwningOpRef & operator=(OwningOpRef &&other)
Assign from another op reference.
Definition: OwningOpRef.h:42
OpTy OperationT
The underlying operation type stored in this reference.
Definition: OwningOpRef.h:31
OwningOpRef(std::nullptr_t=nullptr)
Definition: OwningOpRef.h:33
OwningOpRef(OwningOpRef &&other)
Definition: OwningOpRef.h:35
Include the generated interface declarations.