1818 */
1919/* !
2020 * \file tvm/ffi/cast.h
21- * \brief Value casting support
21+ * \brief Extra value casting helpers
2222 */
2323#ifndef TVM_FFI_CAST_H_
2424#define TVM_FFI_CAST_H_
2525
2626#include < tvm/ffi/any.h>
27- #include < tvm/ffi/dtype.h>
28- #include < tvm/ffi/error.h>
2927#include < tvm/ffi/object.h>
3028#include < tvm/ffi/optional.h>
3129
32- #include < utility>
33-
3430namespace tvm {
3531namespace ffi {
32+
3633/* !
3734 * \brief Get a reference type from a raw object ptr type
3835 *
@@ -46,7 +43,7 @@ namespace ffi {
4643 * \return The corresponding RefType
4744 */
4845template <typename RefType, typename ObjectType>
49- TVM_FFI_INLINE RefType GetRef (const ObjectType* ptr) {
46+ inline RefType GetRef (const ObjectType* ptr) {
5047 static_assert (std::is_base_of_v<typename RefType::ContainerType, ObjectType>,
5148 " Can only cast to the ref of same container type" );
5249
@@ -75,92 +72,9 @@ inline ObjectPtr<BaseType> GetObjectPtr(ObjectType* ptr) {
7572 " Can only cast to the ref of same container type" );
7673 return details::ObjectUnsafe::ObjectPtrFromUnowned<BaseType>(ptr);
7774}
78-
79- /* !
80- * \brief Downcast a base reference type to a more specific type.
81- *
82- * \param ref The input reference
83- * \return The corresponding SubRef.
84- * \tparam SubRef The target specific reference type.
85- * \tparam BaseRef the current reference type.
86- */
87- template <typename SubRef, typename BaseRef,
88- typename = std::enable_if_t <std::is_base_of_v<ObjectRef, BaseRef>>>
89- inline SubRef Downcast (BaseRef ref) {
90- if (ref.defined ()) {
91- if (!ref->template IsInstance <typename SubRef::ContainerType>()) {
92- TVM_FFI_THROW (TypeError) << " Downcast from " << ref->GetTypeKey () << " to "
93- << SubRef::ContainerType::_type_key << " failed." ;
94- }
95- return SubRef (details::ObjectUnsafe::ObjectPtrFromObjectRef<Object>(std::move (ref)));
96- } else {
97- if constexpr (is_optional_type_v<SubRef> || SubRef::_type_is_nullable) {
98- return SubRef (ObjectPtr<Object>(nullptr ));
99- }
100- TVM_FFI_THROW (TypeError) << " Downcast from undefined(nullptr) to `"
101- << SubRef::ContainerType::_type_key
102- << " ` is not allowed. Use Downcast<Optional<T>> instead." ;
103- TVM_FFI_UNREACHABLE ();
104- }
105- }
106-
107- /* !
108- * \brief Downcast any to a specific type
109- *
110- * \param ref The input reference
111- * \return The corresponding SubRef.
112- * \tparam T The target specific reference type.
113- */
114- template <typename T>
115- inline T Downcast (const Any& ref) {
116- if constexpr (std::is_same_v<T, Any>) {
117- return ref;
118- } else {
119- return ref.cast <T>();
120- }
121- }
122-
123- /* !
124- * \brief Downcast any to a specific type
125- *
126- * \param ref The input reference
127- * \return The corresponding SubRef.
128- * \tparam T The target specific reference type.
129- */
130- template <typename T>
131- inline T Downcast (Any&& ref) {
132- if constexpr (std::is_same_v<T, Any>) {
133- return std::move (ref);
134- } else {
135- return std::move (ref).cast <T>();
136- }
137- }
138-
139- /* !
140- * \brief Downcast std::optional<Any> to std::optional<T>
141- *
142- * \param ref The input reference
143- * \return The corresponding SubRef.
144- * \tparam OptionalType The target optional type
145- */
146- template <typename OptionalType, typename = std::enable_if_t <is_optional_type_v<OptionalType>>>
147- inline OptionalType Downcast (const std::optional<Any>& ref) {
148- if (ref.has_value ()) {
149- if constexpr (std::is_same_v<OptionalType, Any>) {
150- return *ref;
151- } else {
152- return (*ref).cast <OptionalType>();
153- }
154- } else {
155- return OptionalType (std::nullopt );
156- }
157- }
158-
15975} // namespace ffi
16076
161- // Expose to the tvm namespace
162- // Rationale: convinience and no ambiguity
163- using ffi::Downcast;
77+ using ffi::GetObjectPtr;
16478using ffi::GetRef;
16579} // namespace tvm
16680#endif // TVM_FFI_CAST_H_
0 commit comments