junrushao commented on code in PR #663:
URL: https://github.com/apache/tvm-ffi/pull/663#discussion_r3575928856
##########
include/tvm/ffi/reflection/registry.h:
##########
@@ -1004,16 +1104,32 @@ class TypeAttrDef : public ReflectionDefBase {
*
* \return The TypeAttrDef object.
*/
- template <typename Func>
+ template <typename Func, std::enable_if_t<!std::is_convertible_v<Func&&,
AnyView> &&
+
!std::is_convertible_v<std::decay_t<Func>, AnyView>,
+ int> = 0>
TypeAttrDef& def(const char* name, Func&& func) {
- TVMFFIByteArray name_array = {name, std::char_traits<char>::length(name)};
ffi::Function ffi_func =
GetMethod(std::string(type_key_) + "." + name,
std::forward<Func>(func));
- TVMFFIAny value_any = AnyView(ffi_func).CopyToTVMFFIAny();
- TVM_FFI_CHECK_SAFE_CALL(TVMFFITypeRegisterAttr(type_index_, &name_array,
&value_any));
+ RegisterTypeAttrValue(type_index_, name, ffi_func);
return *this;
}
+ /*!
+ * \brief Register the standard AnyView conversion type attribute.
+ *
+ * \tparam TSelf The type produced by the conversion.
+ *
+ * \return The TypeAttrDef object.
+ */
+ template <typename TSelf>
+ TVM_FFI_INLINE TypeAttrDef& def_convert() {
+ if constexpr (details::HasContainerType<TSelf>::value) {
+ static_assert(std::is_same_v<typename TSelf::ContainerType, Class>,
+ "TSelf::ContainerType must match TypeAttrDef's object
type");
+ }
+ return def(type_attr::kConvert,
&details::FFIConvertFromAnyViewToObjectRef<TSelf>);
Review Comment:
Thanks for the suggestion. I would like to keep `def_convert`: this is
expected to be a common registration for asymmetric conversions, where the
reflected object type and the returned public reference type differ. The helper
gives that pattern a named API, avoids repeating the
`kConvert`/`FFIConvertFromAnyViewToObjectRef` wiring at every call site, and
retains the compile-time `ContainerType` consistency check when available. I am
therefore keeping the helper rather than expanding each registration into the
generic one-liner.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]