diff --git a/cpp/analytics/BUILD b/cpp/analytics/BUILD index e72aeea4..efb20634 100644 --- a/cpp/analytics/BUILD +++ b/cpp/analytics/BUILD @@ -55,6 +55,7 @@ cc_test( "//absl/time", "//platform/base:error_code_recorder", "//platform/impl/g3", # build_cleaner: keep + "//platform/public:comm", "//platform/public:logging", "//platform/public:types", "//proto:connections_enums_portable_proto", diff --git a/cpp/core/BUILD b/cpp/core/BUILD index 7c97c43c..d4d5d83c 100644 --- a/cpp/core/BUILD +++ b/cpp/core/BUILD @@ -44,6 +44,7 @@ cc_library( name = "core_types", srcs = [ "options.cc", + "payload.cc", "strategy.cc", ], hdrs = [ diff --git a/cpp/core/internal/mediums/ble_v2/BUILD b/cpp/core/internal/mediums/ble_v2/BUILD index 2e6685fd..6aff3597 100644 --- a/cpp/core/internal/mediums/ble_v2/BUILD +++ b/cpp/core/internal/mediums/ble_v2/BUILD @@ -62,5 +62,6 @@ cc_test( "//absl/time", "//platform/base", "//platform/impl/g3", # buildcleaner: keep + "//platform/public:comm", ], ) diff --git a/cpp/core/payload.cc b/cpp/core/payload.cc new file mode 100644 index 00000000..0017588d --- /dev/null +++ b/cpp/core/payload.cc @@ -0,0 +1,96 @@ +// Copyright 2020 Google LLC +// +// 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 +// +// https://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. + +#include "core/payload.h" + +namespace location { +namespace nearby { +namespace connections { + +// Payload is default-constructible, and moveable, but not copyable container +// that holds at most one instance of one of: +// ByteArray, InputStream, or InputFile. +Payload::Payload(Payload&& other) noexcept = default; +Payload::~Payload() = default; +Payload& Payload::operator=(Payload&& other) noexcept = default; + +// Default (invalid) payload. +Payload::Payload() : content_(absl::monostate()) {} + +// Constructors for outgoing payloads. +Payload::Payload(ByteArray&& bytes) : content_(std::move(bytes)) {} + +Payload::Payload(const ByteArray& bytes) : content_(bytes) {} + +Payload::Payload(std::function stream) + : content_(std::move(stream)) {} + +// Constructors for incoming payloads. +Payload::Payload(Id id, ByteArray&& bytes) + : content_(std::move(bytes)), id_(id) {} + +Payload::Payload(Id id, const ByteArray& bytes) : content_(bytes), id_(id) {} + +Payload::Payload(Id id, std::function stream) + : content_(std::move(stream)), id_(id) {} + +// Constructor for incoming and outgoing file payloads. +Payload::Payload(Id id, InputFile file) : content_(std::move(file)), id_(id) {} + +// Returns ByteArray payload, if it has been defined, or empty ByteArray. +const ByteArray& Payload::AsBytes() const& { + static const ByteArray empty; // NOLINT: function-level static is OK. + auto* result = absl::get_if(&content_); + return result ? *result : empty; +} +ByteArray&& Payload::AsBytes() && { + auto* result = absl::get_if(&content_); + return result ? std::move(*result) : std::move(ByteArray()); +} +// Returns InputStream* payload, if it has been defined, or nullptr. +InputStream* Payload::AsStream() { + auto* result = absl::get_if>(&content_); + return result ? &(*result)() : nullptr; +} +// Returns InputFile* payload, if it has been defined, or nullptr. +InputFile* Payload::AsFile() { return absl::get_if(&content_); } + +// Returns Payload unique ID. +Payload::Id Payload::GetId() const { return id_; } + +// Returns Payload type. +Payload::Type Payload::GetType() const { return type_; } + +// Sets the payload offset in bytes +void Payload::SetOffset(size_t offset) { + CHECK(type_ == Type::kFile || type_ == Type::kStream); + InputFile* file = AsFile(); + if (file != nullptr) { + CHECK(file->GetTotalSize() > 0 && offset < (size_t)file->GetTotalSize()); + } + offset_ = offset; +} + +size_t Payload::GetOffset() { return offset_; } + +// Generate Payload Id; to be passed to outgoing file constructor. +Payload::Id Payload::GenerateId() { return Prng().NextInt64(); } + +Payload::Type Payload::FindType() const { + return static_cast(content_.index()); +} + +} // namespace connections +} // namespace nearby +} // namespace location diff --git a/cpp/core/payload.h b/cpp/core/payload.h index 997eb41f..3210b6d5 100644 --- a/cpp/core/payload.h +++ b/cpp/core/payload.h @@ -45,69 +45,51 @@ class DLL_API Payload { std::function, InputFile>; enum class Type { kUnknown = 0, kBytes = 1, kStream = 2, kFile = 3 }; - Payload(Payload&& other) = default; - ~Payload() = default; - Payload& operator=(Payload&& other) = default; + Payload(Payload&& other) noexcept; + ~Payload(); + Payload& operator=(Payload&& other) noexcept; // Default (invalid) payload. - Payload() : content_(absl::monostate()) {} + Payload(); // Constructors for outgoing payloads. - explicit Payload(ByteArray&& bytes) : content_(std::move(bytes)) {} - explicit Payload(const ByteArray& bytes) : content_(bytes) {} - explicit Payload(std::function stream) - : content_(std::move(stream)) {} + explicit Payload(ByteArray&& bytes); + + explicit Payload(const ByteArray& bytes); + explicit Payload(std::function stream); // Constructors for incoming payloads. - Payload(Id id, ByteArray&& bytes) : content_(std::move(bytes)), id_(id) {} - Payload(Id id, const ByteArray& bytes) : content_(bytes), id_(id) {} - Payload(Id id, std::function stream) - : content_(std::move(stream)), id_(id) {} + Payload(Id id, ByteArray&& bytes); + Payload(Id id, const ByteArray& bytes); + Payload(Id id, std::function stream); // Constructor for incoming and outgoing file payloads. - Payload(Id id, InputFile file) : content_(std::move(file)), id_(id) {} + Payload(Id id, InputFile file); // Returns ByteArray payload, if it has been defined, or empty ByteArray. - const ByteArray& AsBytes() const& { - static const ByteArray empty; // NOLINT: function-level static is OK. - auto* result = absl::get_if(&content_); - return result ? *result : empty; - } - ByteArray&& AsBytes() && { - auto* result = absl::get_if(&content_); - return result ? std::move(*result) : std::move(ByteArray()); - } + const ByteArray& AsBytes() const&; + ByteArray&& AsBytes() &&; // Returns InputStream* payload, if it has been defined, or nullptr. - InputStream* AsStream() { - auto* result = absl::get_if>(&content_); - return result ? &(*result)() : nullptr; - } + InputStream* AsStream(); // Returns InputFile* payload, if it has been defined, or nullptr. - InputFile* AsFile() { return absl::get_if(&content_); } + InputFile* AsFile(); // Returns Payload unique ID. - Id GetId() const { return id_; } + Id GetId() const; // Returns Payload type. - Type GetType() const { return type_; } + Type GetType() const; // Sets the payload offset in bytes - void SetOffset(size_t offset) { - CHECK(type_ == Type::kFile || type_ == Type::kStream); - InputFile* file = AsFile(); - if (file != nullptr) { - CHECK(file->GetTotalSize() > 0 && offset < (size_t)file->GetTotalSize()); - } - offset_ = offset; - } + void SetOffset(size_t offset); - size_t GetOffset() { return offset_; } + size_t GetOffset(); // Generate Payload Id; to be passed to outgoing file constructor. - static Id GenerateId() { return Prng().NextInt64(); } + static Id GenerateId(); private: - Type FindType() const { return static_cast(content_.index()); } + Type FindType() const; Content content_; Id id_{GenerateId()}; diff --git a/cpp/platform/public/BUILD b/cpp/platform/public/BUILD index 78f8a998..d1e122de 100644 --- a/cpp/platform/public/BUILD +++ b/cpp/platform/public/BUILD @@ -90,6 +90,7 @@ cc_library( copts = ["-DCORE_ADAPTER_DLL"], visibility = [ "//googlemac/iPhone/Shared/Nearby/Connections:__subpackages__", + "//analytics:__subpackages__", "//core:__subpackages__", "//platform/public:__pkg__", ],