1 // Copyright 2020 The Pigweed Authors
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
7 // https://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
18 #include <type_traits>
20 #include "pw_status/status_with_size.h"
26 struct FunctionTraits;
28 template <typename T, typename ReturnType, typename... Args>
29 struct FunctionTraits<ReturnType (T::*)(Args...)> {
31 using Return = ReturnType;
34 } // namespace internal
36 // Writes bytes to an unspecified output. Provides a Write function that takes a
37 // std::span of bytes and returns a Status.
40 StatusWithSize Write(std::span<const std::byte> data) {
44 // Convenience wrapper for writing data from a pointer and length.
45 StatusWithSize Write(const void* data, size_t size_bytes) {
46 return Write(std::span<const std::byte>(static_cast<const std::byte*>(data),
54 virtual StatusWithSize DoWrite(std::span<const std::byte> data) = 0;
59 StatusWithSize Read(std::span<std::byte> data) { return DoRead(data); }
61 // Convenience wrapper for reading data from a pointer and length.
62 StatusWithSize Read(void* data, size_t size_bytes) {
64 std::span<std::byte>(static_cast<std::byte*>(data), size_bytes));
71 virtual StatusWithSize DoRead(std::span<std::byte> data) = 0;
74 // Output adapter that calls a method on a class with a std::span of bytes. If
75 // the method returns void instead of the expected Status, Write always returns
77 template <typename T, T kMethod>
78 class OutputToMethod final : public Output {
79 using Class = typename internal::FunctionTraits<decltype(kMethod)>::Class;
82 constexpr OutputToMethod(Class* object) : object_(*object) {}
85 using Return = typename internal::FunctionTraits<decltype(kMethod)>::Return;
86 template <T kMethodImpl = kMethod>
87 typename std::enable_if<std::is_void<typename internal::FunctionTraits<
88 decltype(kMethodImpl)>::Return>::value,
90 DoWriteImpl(std::span<const std::byte> data) {
91 (object_.*kMethod)(data);
92 return StatusWithSize(data.size());
95 template <T kMethodImpl = kMethod>
96 typename std::enable_if<!std::is_void<typename internal::FunctionTraits<
97 decltype(kMethodImpl)>::Return>::value,
99 DoWriteImpl(std::span<const std::byte> data) {
100 return (object_.*kMethod)(data);
103 StatusWithSize DoWrite(std::span<const std::byte> data) override {
104 return DoWriteImpl(data);
111 // Output adapter that calls a free function.
112 class OutputToFunction final : public Output {
114 OutputToFunction(StatusWithSize (*function)(std::span<const std::byte>))
115 : function_(function) {}
118 StatusWithSize DoWrite(std::span<const std::byte> data) override {
119 return function_(data);
122 StatusWithSize (*function_)(std::span<const std::byte>);