tvm-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [incubator-tvm] zhiics commented on a change in pull request #5314: [RUNTIME][IR] Allow non-nullable ObjectRef, introduce Optional<T>.
Date Sun, 12 Apr 2020 23:58:37 GMT
zhiics commented on a change in pull request #5314: [RUNTIME][IR] Allow non-nullable ObjectRef,
introduce Optional<T>.
URL: https://github.com/apache/incubator-tvm/pull/5314#discussion_r407263583
 
 

 ##########
 File path: include/tvm/runtime/container.h
 ##########
 @@ -610,7 +611,123 @@ struct PackedFuncValueConverter<::tvm::runtime::String> {
   }
 };
 
+/*!
+ * \brief Optional container that to represent to a Nullable variant of T.
+ * \tparam T The original ObjectRef.
+ *
+ * \code
+ *
+ *  Optional<String> opt0 = nullptr;
+ *  Optional<String> opt1 = String("xyz");
+ *  CHECK(opt0 == nullptr);
+ *  CHECK(opt1 == "xyz");
+ *
+ * \endcode
+ */
+template<typename T>
+class Optional : public ObjectRef {
+ public:
+  using ContainerType = typename T::ContainerType;
+  static_assert(std::is_base_of<ObjectRef, T>::value,
+                "Optional is only defined for ObjectRef.");
+  // default constructors.
+  Optional() = default;
+  Optional(const Optional<T>&) = default;
+  Optional(Optional<T>&&) = default;
+  Optional<T>& operator=(const Optional<T>&) = default;
+  Optional<T>& operator=(Optional<T>&&) = default;
+  /*!
+   * \brief Construct from an ObjectPtr
+   *        whose type already matches the ContainerType.
+   * \param ptr
+   */
+  explicit Optional(ObjectPtr<Object> ptr) : ObjectRef(ptr) {}
+  // nullptr handling.
+  Optional(std::nullptr_t) {}    // NOLINT(*)
+  Optional<T>& operator=(std::nullptr_t) {
+    data_ = nullptr;
+    return *this;
+  }
+  // normal value handling.
+  Optional(T other)             // NOLINT(*)
+      : ObjectRef(std::move(other)) {
+  }
+  Optional<T>& operator=(T other) {
+    ObjectRef::operator=(std::move(other));
+    return *this;
+  }
+  /*!
+   * \return A not-null container value in the optional.
+   * \note This function performs not-null checking.
+   */
+  T value() const {
+    CHECK(data_ != nullptr);
+    return T(data_);
+  }
 
 Review comment:
   It might be also useful have value_or as std::optional provides? This way we can return
a "default" value.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message