/* Copyright 2019-2020 Canaan Inc. * * Licensed 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. */ #pragma once #include #include namespace nncase { namespace runtime { class span_reader { public: span_reader(xtl::span span) : span_(span) { } bool empty() const noexcept { return span_.empty(); } template T read() { auto value = *reinterpret_cast(span_.data()); advance(sizeof(T)); return value; } template T read_unaligned() { T value; std::memcpy(&value, span_.data(), sizeof(T)); advance(sizeof(T)); return value; } template void read(T &value) { value = *reinterpret_cast(span_.data()); advance(sizeof(T)); } template void read_span(xtl::span &span, size_t size) { span = { reinterpret_cast(span_.data()), size }; advance(sizeof(T) * size); } template void read_span(xtl::span &span) { span = { reinterpret_cast(span_.data()), N }; advance(sizeof(T) * N); } void read_avail(xtl::span &span) { span = span_; span_ = {}; } template const T *peek() const noexcept { return reinterpret_cast(span_.data()); } template void get_array(const T *&value, size_t size) { value = peek(); advance(size * sizeof(T)); } template void get_ref(const T *&value) { value = peek(); advance(sizeof(T)); } void skip(size_t count) { advance(count); } private: void advance(size_t count) { span_ = span_.subspan(count); } private: xtl::span span_; }; } }