/* * 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 * * http://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/support/io.h * \brief Binary stream I/O interface. */ #ifndef TVM_SUPPORT_IO_H_ #define TVM_SUPPORT_IO_H_ #include #include namespace tvm { namespace support { /*! * \brief Primary Serializer template. Specialize for each serializable type. * * A valid specialization must provide: * static constexpr bool enabled = true; * static void Write(Stream* strm, const T& data); * static bool Read(Stream* strm, T* data); */ template struct Serializer { static constexpr bool enabled = false; }; /*! * \brief Abstract binary stream for serialization. * * Subclasses implement the raw Read/Write byte methods. * The template Write/Read methods delegate to Serializer * for endian-aware, type-safe binary I/O. * * \note Subclasses that override Read(void*, size_t) or Write(const void*, size_t) * must add `using Stream::Read;` and `using Stream::Write;` to make the * template overloads visible (C++ name-hiding rule). */ class Stream { public: /*! * \brief Read raw bytes from the stream. * \param ptr Destination buffer. * \param size Number of bytes to read. * \return Number of bytes actually read. */ virtual size_t Read(void* ptr, size_t size) = 0; /*! * \brief Write raw bytes to the stream. * \param ptr Source buffer. * \param size Number of bytes to write. * \return Number of bytes actually written. */ virtual size_t Write(const void* ptr, size_t size) = 0; /*! \brief Virtual destructor. */ virtual ~Stream() = default; /*! * \brief Write a typed value using Serializer. * \tparam T The data type (must have a Serializer specialization). * \param data The value to write. */ template void Write(const T& data) { static_assert(Serializer::enabled, "No Serializer specialization for this type"); Serializer::Write(this, data); } /*! * \brief Read a typed value using Serializer. * \tparam T The data type (must have a Serializer specialization). * \param out_data Pointer to receive the value. * \return true on success. */ template bool Read(T* out_data) { static_assert(Serializer::enabled, "No Serializer specialization for this type"); return Serializer::Read(this, out_data); } /*! * \brief Write an array of typed values element by element. * \param data Pointer to the first element. * \param num_elems Number of elements. */ template void WriteArray(const T* data, size_t num_elems) { for (size_t i = 0; i < num_elems; ++i) Write(data[i]); } /*! * \brief Read an array of typed values element by element. * \param data Pointer to the first element. * \param num_elems Number of elements. * \return true on success. */ template bool ReadArray(T* data, size_t num_elems) { for (size_t i = 0; i < num_elems; ++i) { if (!Read(data + i)) return false; } return true; } }; } // namespace support } // namespace tvm #endif // TVM_SUPPORT_IO_H_