tvm
optional.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
24 #ifndef TVM_RUNTIME_CONTAINER_OPTIONAL_H_
25 #define TVM_RUNTIME_CONTAINER_OPTIONAL_H_
26 
27 #include <utility>
28 
29 #include "./base.h"
30 
31 namespace tvm {
32 namespace runtime {
33 
35 struct NullOptType {};
36 
50 template <typename T>
51 class Optional : public ObjectRef {
52  public:
53  using ContainerType = typename T::ContainerType;
54  static_assert(std::is_base_of<ObjectRef, T>::value, "Optional is only defined for ObjectRef.");
55  // default constructors.
56  Optional() = default;
57  Optional(const Optional<T>&) = default;
58  Optional(Optional<T>&&) = default;
59  Optional<T>& operator=(const Optional<T>&) = default;
60  Optional<T>& operator=(Optional<T>&&) = default;
66  explicit Optional(ObjectPtr<Object> ptr) : ObjectRef(ptr) {}
68  Optional(NullOptType) {} // NOLINT(*)
69  // nullptr handling.
70  // disallow implicit conversion as 0 can be implicitly converted to nullptr_t
71  explicit Optional(std::nullptr_t) {}
72  Optional<T>& operator=(std::nullptr_t) {
73  data_ = nullptr;
74  return *this;
75  }
76  // normal value handling.
77  Optional(T other) // NOLINT(*)
78  : ObjectRef(std::move(other)) {}
79  Optional<T>& operator=(T other) {
80  ObjectRef::operator=(std::move(other));
81  return *this;
82  }
83  // delete the int constructor
84  // since Optional<Integer>(0) is ambiguious
85  // 0 can be implicitly casted to nullptr_t
86  explicit Optional(int val) = delete;
87  Optional<T>& operator=(int val) = delete;
92  T value() const {
93  ICHECK(data_ != nullptr);
94  return T(data_);
95  }
100  T value_or(T default_value) const { return data_ != nullptr ? T(data_) : default_value; }
101 
103  explicit operator bool() const { return *this != nullptr; }
104  // operator overloadings
105  bool operator==(std::nullptr_t) const { return data_ == nullptr; }
106  bool operator!=(std::nullptr_t) const { return data_ != nullptr; }
107  auto operator==(const Optional<T>& other) const {
108  // support case where sub-class returns a symbolic ref type.
109  using RetType = decltype(value() == other.value());
110  if (same_as(other)) return RetType(true);
111  if (*this != nullptr && other != nullptr) {
112  return value() == other.value();
113  } else {
114  // one of them is nullptr.
115  return RetType(false);
116  }
117  }
118  auto operator!=(const Optional<T>& other) const {
119  // support case where sub-class returns a symbolic ref type.
120  using RetType = decltype(value() != other.value());
121  if (same_as(other)) return RetType(false);
122  if (*this != nullptr && other != nullptr) {
123  return value() != other.value();
124  } else {
125  // one of them is nullptr.
126  return RetType(true);
127  }
128  }
129  auto operator==(const T& other) const {
130  using RetType = decltype(value() == other);
131  if (same_as(other)) return RetType(true);
132  if (*this != nullptr) return value() == other;
133  return RetType(false);
134  }
135  auto operator!=(const T& other) const { return !(*this == other); }
136  template <typename U>
137  auto operator==(const U& other) const {
138  using RetType = decltype(value() == other);
139  if (*this == nullptr) return RetType(false);
140  return value() == other;
141  }
142  template <typename U>
143  auto operator!=(const U& other) const {
144  using RetType = decltype(value() != other);
145  if (*this == nullptr) return RetType(true);
146  return value() != other;
147  }
148  static constexpr bool _type_is_nullable = true;
149 };
150 
151 } // namespace runtime
152 
153 // expose the functions to the root namespace.
154 using runtime::Optional;
156 } // namespace tvm
157 
158 #endif // TVM_RUNTIME_CONTAINER_OPTIONAL_H_
A custom smart pointer for Object.
Definition: object.h:356
auto operator!=(const Optional< T > &other) const
Definition: optional.h:118
auto operator!=(const T &other) const
Definition: optional.h:135
bool operator==(std::nullptr_t) const
Definition: optional.h:105
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
Definition: loop_state.h:456
Base utilities for common POD(plain old data) container types.
base class of all object containers.
Definition: object.h:165
T value_or(T default_value) const
Definition: optional.h:100
Optional(NullOptType)
Nullopt handling.
Definition: optional.h:68
T value() const
Definition: optional.h:92
auto operator==(const T &other) const
Definition: optional.h:129
Optional(std::nullptr_t)
Definition: optional.h:71
auto operator==(const U &other) const
Definition: optional.h:137
Optional< T > & operator=(T other)
Definition: optional.h:79
auto operator!=(const U &other) const
Definition: optional.h:143
Optional< T > & operator=(std::nullptr_t)
Definition: optional.h:72
Base class of all object reference.
Definition: object.h:504
auto operator==(const Optional< T > &other) const
Definition: optional.h:107
Optional(T other)
Definition: optional.h:77
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
constexpr runtime::NullOptType NullOpt
Definition: optional.h:155
Helper to represent nullptr for optional.
Definition: optional.h:35
bool operator!=(std::nullptr_t) const
Definition: optional.h:106
Optional(ObjectPtr< Object > ptr)
Construct from an ObjectPtr whose type already matches the ContainerType.
Definition: optional.h:66