mirror of
https://fuchsia.googlesource.com/third_party/pigweed.googlesource.com/pigweed/pigweed
synced 2024-08-02 06:46:04 +00:00
pw_{digital_io,protobuf,stream}: Use pw::internal::SiblingCast
Consolidate "sibling casts" with the pw::internal::SiblingCast function. Change-Id: Ib5211265299eade460fc13f2fb52cc60986f3611 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/189015 Presubmit-Verified: CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com> Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com> Reviewed-by: Armando Montanez <amontanez@google.com> Commit-Queue: Auto-Submit <auto-submit@pigweed-service-accounts.iam.gserviceaccount.com>
This commit is contained in:
parent
36746572ed
commit
6b7bdab53a
|
@ -39,6 +39,7 @@ cc_library(
|
|||
"//pw_function",
|
||||
"//pw_result",
|
||||
"//pw_status",
|
||||
"//pw_toolchain:sibling_cast",
|
||||
],
|
||||
)
|
||||
|
||||
|
|
|
@ -34,6 +34,7 @@ pw_source_set("pw_digital_io") {
|
|||
]
|
||||
sources = [ "digital_io.cc" ]
|
||||
public_deps = [
|
||||
"$dir_pw_toolchain:sibling_cast",
|
||||
dir_pw_assert,
|
||||
dir_pw_function,
|
||||
dir_pw_result,
|
||||
|
|
|
@ -28,6 +28,7 @@ pw_add_library(pw_digital_io STATIC
|
|||
pw_function
|
||||
pw_result
|
||||
pw_status
|
||||
pw_toolchain._sibling_cast
|
||||
)
|
||||
|
||||
pw_add_test(pw_digital_io.stream_test
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
|
||||
#include <type_traits>
|
||||
|
||||
#include "pw_toolchain/internal/sibling_cast.h"
|
||||
|
||||
namespace pw::digital_io {
|
||||
namespace internal {
|
||||
|
||||
|
@ -80,13 +82,14 @@ class Conversions {
|
|||
|
||||
template <typename T, typename = Enabled<T>>
|
||||
constexpr T& as() {
|
||||
return static_cast<T&>(static_cast<CommonBase&>(static_cast<Self&>(*this)));
|
||||
return pw::internal::SiblingCast<T&, CommonBase>(static_cast<Self&>(*this));
|
||||
}
|
||||
|
||||
template <typename T, typename = Enabled<T>>
|
||||
constexpr const T& as() const {
|
||||
return static_cast<const T&>(
|
||||
static_cast<const CommonBase&>(static_cast<const Self&>(*this)));
|
||||
pw::internal::SiblingCast<const T&, CommonBase>(
|
||||
static_cast<const Self&>(*this)));
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -75,6 +75,7 @@ cc_library(
|
|||
"//pw_stream",
|
||||
"//pw_stream:interval_reader",
|
||||
"//pw_string:string",
|
||||
"//pw_toolchain:sibling_cast",
|
||||
"//pw_varint",
|
||||
"//pw_varint:stream",
|
||||
],
|
||||
|
|
|
@ -50,6 +50,7 @@ pw_source_set("pw_protobuf") {
|
|||
"$dir_pw_containers:vector",
|
||||
"$dir_pw_stream:interval_reader",
|
||||
"$dir_pw_string:string",
|
||||
"$dir_pw_toolchain:sibling_cast",
|
||||
"$dir_pw_varint:stream",
|
||||
dir_pw_assert,
|
||||
dir_pw_bytes,
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
#include "pw_status/try.h"
|
||||
#include "pw_stream/memory_stream.h"
|
||||
#include "pw_stream/stream.h"
|
||||
#include "pw_toolchain/internal/sibling_cast.h"
|
||||
#include "pw_varint/varint.h"
|
||||
|
||||
namespace pw::protobuf {
|
||||
|
@ -910,7 +911,7 @@ inline ToStreamEncoder& StreamEncoderCast(FromStreamEncoder& encoder) {
|
|||
static_assert(std::is_base_of<StreamEncoder, ToStreamEncoder>::value,
|
||||
"Cannot cast to a type that is not a derived class of "
|
||||
"pw::protobuf::StreamEncoder");
|
||||
return static_cast<ToStreamEncoder&>(static_cast<StreamEncoder&>(encoder));
|
||||
return pw::internal::SiblingCast<ToStreamEncoder&, StreamEncoder>(encoder);
|
||||
}
|
||||
|
||||
} // namespace pw::protobuf
|
||||
|
|
|
@ -40,6 +40,7 @@ cc_library(
|
|||
"//pw_result",
|
||||
"//pw_span",
|
||||
"//pw_status",
|
||||
"//pw_toolchain:sibling_cast",
|
||||
],
|
||||
)
|
||||
|
||||
|
|
|
@ -37,6 +37,7 @@ pw_source_set("pw_stream") {
|
|||
]
|
||||
sources = [ "memory_stream.cc" ]
|
||||
public_deps = [
|
||||
"$dir_pw_toolchain:sibling_cast",
|
||||
dir_pw_assert,
|
||||
dir_pw_bytes,
|
||||
dir_pw_polyfill,
|
||||
|
|
|
@ -31,6 +31,7 @@ pw_add_library(pw_stream STATIC
|
|||
pw_result
|
||||
pw_span
|
||||
pw_status
|
||||
pw_toolchain._sibling_cast
|
||||
)
|
||||
|
||||
pw_add_library(pw_stream.socket_stream STATIC
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include "pw_span/span.h"
|
||||
#include "pw_status/status.h"
|
||||
#include "pw_status/status_with_size.h"
|
||||
#include "pw_toolchain/internal/sibling_cast.h"
|
||||
|
||||
namespace pw::stream {
|
||||
|
||||
|
@ -471,18 +472,18 @@ class NonSeekableWriter : public Writer {
|
|||
class ReaderWriter : public Stream {
|
||||
public:
|
||||
// ReaderWriters may be used as Readers.
|
||||
Reader& as_reader() { return *std::launder(reinterpret_cast<Reader*>(this)); }
|
||||
Reader& as_reader() { return internal::SiblingCast<Reader&, Stream>(*this); }
|
||||
const Reader& as_reader() const {
|
||||
return *std::launder(reinterpret_cast<const Reader*>(this));
|
||||
return internal::SiblingCast<const Reader&, Stream>(*this);
|
||||
}
|
||||
|
||||
operator Reader&() { return as_reader(); }
|
||||
operator const Reader&() const { return as_reader(); }
|
||||
|
||||
// ReaderWriters may be used as Writers.
|
||||
Writer& as_writer() { return *std::launder(reinterpret_cast<Writer*>(this)); }
|
||||
Writer& as_writer() { return internal::SiblingCast<Writer&, Stream>(*this); }
|
||||
const Writer& as_writer() const {
|
||||
return *std::launder(reinterpret_cast<const Writer*>(this));
|
||||
return internal::SiblingCast<const Writer&, Stream>(*this);
|
||||
}
|
||||
|
||||
operator Writer&() { return as_writer(); }
|
||||
|
@ -516,16 +517,16 @@ class RelativeSeekableReaderWriter : public ReaderWriter {
|
|||
// RelativeSeekableReaderWriters may be used as RelativeSeekableReaders or
|
||||
// RelativeSeekableWriters.
|
||||
operator RelativeSeekableReader&() {
|
||||
return *std::launder(reinterpret_cast<RelativeSeekableReader*>(this));
|
||||
return internal::SiblingCast<RelativeSeekableReader&, Stream>(*this);
|
||||
}
|
||||
operator const RelativeSeekableReader&() const {
|
||||
return *std::launder(reinterpret_cast<const RelativeSeekableReader*>(this));
|
||||
return internal::SiblingCast<const RelativeSeekableReader&, Stream>(*this);
|
||||
}
|
||||
operator RelativeSeekableWriter&() {
|
||||
return *std::launder(reinterpret_cast<RelativeSeekableWriter*>(this));
|
||||
return internal::SiblingCast<RelativeSeekableWriter&, Stream>(*this);
|
||||
}
|
||||
operator const RelativeSeekableWriter&() const {
|
||||
return *std::launder(reinterpret_cast<const RelativeSeekableWriter*>(this));
|
||||
return internal::SiblingCast<const RelativeSeekableWriter&, Stream>(*this);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
@ -552,10 +553,10 @@ class SeekableReaderWriter : public RelativeSeekableReaderWriter {
|
|||
public:
|
||||
// SeekableReaderWriters may be used as SeekableReaders.
|
||||
SeekableReader& as_seekable_reader() {
|
||||
return *std::launder(reinterpret_cast<SeekableReader*>(this));
|
||||
return internal::SiblingCast<SeekableReader&, Stream>(*this);
|
||||
}
|
||||
const SeekableReader& as_seekable_reader() const {
|
||||
return *std::launder(reinterpret_cast<const SeekableReader*>(this));
|
||||
return internal::SiblingCast<const SeekableReader&, Stream>(*this);
|
||||
}
|
||||
|
||||
operator SeekableReader&() { return as_seekable_reader(); }
|
||||
|
@ -563,10 +564,10 @@ class SeekableReaderWriter : public RelativeSeekableReaderWriter {
|
|||
|
||||
// SeekableReaderWriters may be used as SeekableWriters.
|
||||
SeekableWriter& as_seekable_writer() {
|
||||
return *std::launder(reinterpret_cast<SeekableWriter*>(this));
|
||||
return internal::SiblingCast<SeekableWriter&, Stream>(*this);
|
||||
}
|
||||
const SeekableWriter& as_seekable_writer() const {
|
||||
return *std::launder(reinterpret_cast<const SeekableWriter*>(this));
|
||||
return internal::SiblingCast<const SeekableWriter&, Stream>(*this);
|
||||
}
|
||||
|
||||
operator SeekableWriter&() { return as_seekable_writer(); }
|
||||
|
@ -592,16 +593,16 @@ class NonSeekableReaderWriter : public ReaderWriter {
|
|||
// NonSeekableWriters. Note that NonSeekableReaderWriter& generally should not
|
||||
// be used in APIs, which should accept ReaderWriter& instead.
|
||||
operator NonSeekableReader&() {
|
||||
return *std::launder(reinterpret_cast<NonSeekableReader*>(this));
|
||||
return internal::SiblingCast<NonSeekableReader&, Stream>(*this);
|
||||
}
|
||||
operator const NonSeekableReader&() const {
|
||||
return *std::launder(reinterpret_cast<const NonSeekableReader*>(this));
|
||||
return internal::SiblingCast<const NonSeekableReader&, Stream>(*this);
|
||||
}
|
||||
operator NonSeekableWriter&() {
|
||||
return *std::launder(reinterpret_cast<NonSeekableWriter*>(this));
|
||||
return internal::SiblingCast<NonSeekableWriter&, Stream>(*this);
|
||||
}
|
||||
operator const NonSeekableWriter&() const {
|
||||
return *std::launder(reinterpret_cast<const NonSeekableWriter*>(this));
|
||||
return internal::SiblingCast<const NonSeekableWriter&, Stream>(*this);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
|
Loading…
Reference in New Issue
Block a user