DarkSharpness commented on code in PR #228:
URL: https://github.com/apache/tvm-ffi/pull/228#discussion_r2493778175


##########
include/tvm/ffi/container/stl.h:
##########
@@ -0,0 +1,421 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   htT://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/ffi/container/stl.h
+ * \brief STL container support.
+ *
+ */
+#ifndef TVM_FFI_CONTAINER_STL_H_
+#define TVM_FFI_CONTAINER_STL_H_
+
+#include <tvm/ffi/base_details.h>
+#include <tvm/ffi/container/array.h>
+#include <tvm/ffi/container/tuple.h>
+#include <tvm/ffi/object.h>
+#include <tvm/ffi/type_traits.h>
+
+#include <algorithm>
+#include <array>
+#include <cstddef>
+#include <optional>
+#include <tuple>
+#include <type_traits>
+#include <variant>
+#include <vector>
+
+#include "tvm/ffi/c_api.h"
+#include "tvm/ffi/error.h"
+
+namespace tvm {
+namespace ffi {
+
+template <typename T, std::size_t Nm>
+struct TypeTraits<std::array<T, Nm>> : public TypeTraitsBase {
+ private:
+  using Self = std::array<T, Nm>;
+  using Array = ::tvm::ffi::Array<T>;
+  static_assert(Nm > 0, "Zero-length std::array is not supported.");
+
+ public:
+  static constexpr int32_t field_static_type_index = TypeIndex::kTVMFFIArray;
+
+  TVM_FFI_INLINE static void CopyToAnyView(const Self& src, TVMFFIAny* result) 
{
+    return TypeTraits<Array>::MoveToAny({src.begin(), src.end()}, result);
+  }
+
+  TVM_FFI_INLINE static void MoveToAny(Self&& src, TVMFFIAny* result) {
+    return TypeTraits<Array>::MoveToAny(
+        {std::make_move_iterator(src.begin()), 
std::make_move_iterator(src.end())}, result);
+  }
+
+  TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
+    if (src->type_index != TypeIndex::kTVMFFIArray) return false;
+    const ArrayObj& n = *reinterpret_cast<const ArrayObj*>(src->v_obj);
+    // check static length first
+    if (n.size_ != Nm) return false;
+    // then check element type
+    if constexpr (std::is_same_v<T, Any>) {
+      return true;
+    } else {
+      return std::all_of(n.begin(), n.end(), 
details::AnyUnsafe::CheckAnyStrict<T>);
+    }
+  }
+
+  TVM_FFI_INLINE static Self CopyFromAnyViewAfterCheck(const TVMFFIAny* src) {
+    auto array = TypeTraits<Array>::CopyFromAnyViewAfterCheck(src);
+    Self result;  // no initialization to avoid overhead
+    std::copy_n(std::make_move_iterator(array.begin()), Nm, result.begin());
+    return result;
+  }
+
+  TVM_FFI_INLINE static Self MoveFromAnyAfterCheck(TVMFFIAny* src) {
+    auto array = TypeTraits<Array>::MoveFromAnyAfterCheck(src);
+    Self result;  // no initialization to avoid overhead
+    std::copy_n(std::make_move_iterator(array.begin()), Nm, result.begin());
+    return result;
+  }
+
+  TVM_FFI_INLINE static std::optional<Self> TryCastFromAnyView(const 
TVMFFIAny* src) {
+    if (!CheckAnyStrict(src)) return std::nullopt;
+    return CopyFromAnyViewAfterCheck(src);
+  }
+
+  TVM_FFI_INLINE static std::string TypeStr() {
+    return "std::array<" + details::Type2Str<T>::v() + ", " + 
std::to_string(Nm) + ">";
+  }
+  TVM_FFI_INLINE static std::string TypeSchema() {
+    return R"({"type":"std::array","args":[)" + details::TypeSchema<T>::v() + 
"," +
+           std::to_string(Nm) + "]}";
+  }
+};
+
+template <typename T>
+struct TypeTraits<std::vector<T>> : public TypeTraitsBase {
+ private:
+  using Self = std::vector<T>;
+  using Array = ::tvm::ffi::Array<T>;
+
+ public:
+  static constexpr int32_t field_static_type_index = TypeIndex::kTVMFFIArray;
+
+  TVM_FFI_INLINE static void CopyToAnyView(const Self& src, TVMFFIAny* result) 
{
+    return TypeTraits<Array>::MoveToAny({src.begin(), src.end()}, result);
+  }
+
+  TVM_FFI_INLINE static void MoveToAny(Self&& src, TVMFFIAny* result) {
+    return TypeTraits<Array>::MoveToAny(
+        {std::make_move_iterator(src.begin()), 
std::make_move_iterator(src.end())}, result);
+  }
+
+  TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
+    if (src->type_index != TypeIndex::kTVMFFIArray) return false;
+    const ArrayObj& n = *reinterpret_cast<const ArrayObj*>(src->v_obj);
+    if constexpr (std::is_same_v<T, Any>) {
+      return true;
+    } else {
+      return std::all_of(n.begin(), n.end(), 
details::AnyUnsafe::CheckAnyStrict<T>);
+    }
+  }
+
+  TVM_FFI_INLINE static Self CopyFromAnyViewAfterCheck(const TVMFFIAny* src) {
+    auto array = TypeTraits<Array>::CopyFromAnyViewAfterCheck(src);
+    return Self{std::make_move_iterator(array.begin()), 
std::make_move_iterator(array.end())};
+  }
+
+  TVM_FFI_INLINE static Self MoveFromAnyAfterCheck(TVMFFIAny* src) {
+    auto array = TypeTraits<Array>::MoveFromAnyAfterCheck(src);
+    return Self{std::make_move_iterator(array.begin()), 
std::make_move_iterator(array.end())};
+  }
+
+  TVM_FFI_INLINE static std::optional<Self> TryCastFromAnyView(const 
TVMFFIAny* src) {
+    if (!CheckAnyStrict(src)) return std::nullopt;
+    return CopyFromAnyViewAfterCheck(src);
+  }
+
+  TVM_FFI_INLINE static std::string TypeStr() {
+    return "std::vector<" + details::Type2Str<T>::v() + ">";
+  }
+  TVM_FFI_INLINE static std::string TypeSchema() {
+    return R"({"type":"std::vector","args":[)" + details::TypeSchema<T>::v() + 
"]}";
+  }
+};
+
+template <typename T>
+struct TypeTraits<std::optional<T>> : public TypeTraitsBase {
+ private:
+  using Self = std::optional<T>;
+
+ public:
+  TVM_FFI_INLINE static void CopyToAnyView(const Self& src, TVMFFIAny* result) 
{
+    if (src.has_value()) {
+      TypeTraits<Self>::CopyToAnyView(*src, result);
+    } else {
+      TypeTraits<std::nullptr_t>::CopyToAnyView(nullptr, result);
+    }
+  }
+  TVM_FFI_INLINE static void MoveToAny(Self&& src, TVMFFIAny* result) {
+    if (src.has_value()) {
+      TypeTraits<Self>::MoveToAny(std::move(*src), result);
+    } else {
+      TypeTraits<std::nullptr_t>::MoveToAny(nullptr, result);
+    }
+  }

Review Comment:
   fixed



-- 
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]

Reply via email to