From 3e78cef57ff22e7c204805977330812b76c2dd97 Mon Sep 17 00:00:00 2001 From: John Carroll Date: Tue, 23 Aug 2022 17:17:50 -0700 Subject: [PATCH] Flattening of Input/OutputFile. PiperOrigin-RevId: 469595318 --- connections/clients/windows/BUILD | 4 ++ .../clients/windows/dart/core_adapter_dart.cc | 3 +- connections/clients/windows/file_w.cc | 58 ++++++++++++++++++ connections/clients/windows/file_w.h | 60 +++++++++++++++++++ connections/clients/windows/listeners_w.cc | 4 +- connections/clients/windows/payload_w.cc | 15 ++--- connections/clients/windows/payload_w.h | 9 +-- internal/platform/file.cc | 2 + internal/platform/file.h | 17 +++--- 9 files changed, 149 insertions(+), 23 deletions(-) create mode 100644 connections/clients/windows/file_w.cc create mode 100644 connections/clients/windows/file_w.h diff --git a/connections/clients/windows/BUILD b/connections/clients/windows/BUILD index 5742b3c4..182cbd9a 100644 --- a/connections/clients/windows/BUILD +++ b/connections/clients/windows/BUILD @@ -78,6 +78,7 @@ lexan.cc_windows_dll( "connection_options_w.cc", "core_adapter.cc", "discovery_options_w.cc", + "file_w.cc", "listeners_w.cc", "payload_w.cc", "strategy_w.cc", @@ -88,6 +89,7 @@ lexan.cc_windows_dll( "core_adapter.h", "discovery_options_w.h", "dll_config.h", + "file_w.h", "listeners_w.h", "medium_selector_w.h", "options_base_w.h", @@ -123,6 +125,7 @@ lexan.cc_windows_dll( "core_adapter.cc", "dart/core_adapter_dart.cc", "discovery_options_w.cc", + "file_w.cc", "listeners_w.cc", "payload_w.cc", "strategy_w.cc", @@ -134,6 +137,7 @@ lexan.cc_windows_dll( "dart/core_adapter_dart.h", "discovery_options_w.h", "dll_config.h", + "file_w.h", "listeners_w.h", "medium_selector_w.h", "options_base_w.h", diff --git a/connections/clients/windows/dart/core_adapter_dart.cc b/connections/clients/windows/dart/core_adapter_dart.cc index 6393f715..1697071e 100644 --- a/connections/clients/windows/dart/core_adapter_dart.cc +++ b/connections/clients/windows/dart/core_adapter_dart.cc @@ -22,7 +22,6 @@ #include "connections/core.h" #include "connections/payload.h" #include "internal/platform/count_down_latch.h" -#include "internal/platform/file.h" #include "internal/platform/logging.h" namespace location::nearby::windows { @@ -609,7 +608,7 @@ void SendPayloadDart(Core *pCore, const char *endpoint_id, NEARBY_LOG(INFO, "File name: %s, size %d", payload_dart.data, payload_dart.size); std::string file_name_str(payload_dart.data); - InputFile input_file(file_name_str, payload_dart.size); + InputFileW input_file(file_name_str, payload_dart.size); PayloadW payload(input_file); std::vector c_string_array; diff --git a/connections/clients/windows/file_w.cc b/connections/clients/windows/file_w.cc new file mode 100644 index 00000000..a76ce35c --- /dev/null +++ b/connections/clients/windows/file_w.cc @@ -0,0 +1,58 @@ +// Copyright 2022 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 "connections/clients/windows/file_w.h" + +#include "internal/platform/file.h" + +namespace location::nearby { + +namespace windows { +InputFileW::InputFileW(InputFile* input_file) + : impl_(std::unique_ptr( + new nearby::InputFile(std::move(*input_file)))) {} +InputFileW::InputFileW(PayloadId payload_id, size_t size) + : impl_(std::unique_ptr( + new nearby::InputFile(payload_id, size))) {} +InputFileW::InputFileW(std::string file_path, size_t size) + : impl_(std::unique_ptr( + new nearby::InputFile(file_path, size))) {} +InputFileW::InputFileW(InputFileW&& other) noexcept + : impl_(std::move(other.impl_)) {} + +// Returns a string that uniquely identifies this file. +std::string InputFileW::GetFilePath() const { return impl_->GetFilePath(); } + +// Returns total size of this file in bytes. +size_t InputFileW::GetTotalSize() const { return impl_->GetTotalSize(); } + +std::unique_ptr +InputFileW::GetImpl() { + return std::move(impl_); +} + +OutputFileW::OutputFileW(PayloadId payload_id) {} +OutputFileW::OutputFileW(std::string file_path) {} +OutputFileW::OutputFileW(OutputFileW&&) noexcept {} +OutputFileW& OutputFileW::operator=(OutputFileW&& other) noexcept { + impl_ = std::move(other.impl_); + return *this; +} + +std::unique_ptr +OutputFileW::GetImpl() { + return std::move(impl_); +} + +} // namespace windows +} // namespace location::nearby diff --git a/connections/clients/windows/file_w.h b/connections/clients/windows/file_w.h new file mode 100644 index 00000000..c1def01b --- /dev/null +++ b/connections/clients/windows/file_w.h @@ -0,0 +1,60 @@ +// Copyright 2022 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. +#ifndef THIRD_PARTY_NEARBY_CONNECTIONS_CLIENTS_WINDOWS_FILE_W_H_ +#define THIRD_PARTY_NEARBY_CONNECTIONS_CLIENTS_WINDOWS_FILE_W_H_ + +#include +#include + +#include "connections/clients/windows/dll_config.h" +#include "internal/platform/file.h" +#include "internal/platform/payload_id.h" + +namespace location::nearby::windows { + +class DLL_API InputFileW { + public: + InputFileW(InputFile* input_file); + InputFileW(PayloadId payload_id, size_t size); + InputFileW(std::string file_path, size_t size); + InputFileW(InputFileW&&) noexcept; + + // Returns a string that uniquely identifies this file. + std::string GetFilePath() const; + + // Returns total size of this file in bytes. + size_t GetTotalSize() const; + + std::unique_ptr GetImpl(); + + private: + std::unique_ptr impl_; +}; + +class DLL_API OutputFileW { + public: + explicit OutputFileW(PayloadId payload_id); + explicit OutputFileW(std::string file_path); + OutputFileW(OutputFileW&&) noexcept; + OutputFileW& operator=(OutputFileW&&) noexcept; + + std::unique_ptr GetImpl(); + + private: + std::unique_ptr impl_; +}; + +} // namespace location::nearby::windows + +#endif // THIRD_PARTY_NEARBY_CONNECTIONS_CLIENTS_WINDOWS_FILE_W_H_ diff --git a/connections/clients/windows/listeners_w.cc b/connections/clients/windows/listeners_w.cc index eeb3788d..40584f4e 100644 --- a/connections/clients/windows/listeners_w.cc +++ b/connections/clients/windows/listeners_w.cc @@ -204,7 +204,7 @@ PayloadListenerW::PayloadListenerW(PayloadCB payloadCB, break; } case connections::PayloadType::kFile: { - InputFile file(std::move(*payload.AsFile())); + InputFileW file(std::move(payload.AsFile())); payloadW = PayloadW(payload.GetId(), std::move(file)); } break; @@ -213,7 +213,7 @@ PayloadListenerW::PayloadListenerW(PayloadCB payloadCB, // payloadW = PayloadW(payload.AsStream()); //} case connections::PayloadType::kStream: { - InputFile file(std::move(*payload.AsFile())); + InputFileW file(std::move(payload.AsFile())); payloadW = PayloadW(payload.GetId(), std::move(file)); } break; case connections::PayloadType::kUnknown: { diff --git a/connections/clients/windows/payload_w.cc b/connections/clients/windows/payload_w.cc index 9cc2cead..7d3f801b 100644 --- a/connections/clients/windows/payload_w.cc +++ b/connections/clients/windows/payload_w.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "connections/clients/windows/payload_w.h" +#include "connections/clients/windows/file_w.h" #include "connections/payload.h" #include "internal/platform/byte_array.h" #include "internal/platform/payload_id.h" @@ -44,9 +45,9 @@ PayloadW::PayloadW(const char *bytes, const size_t bytes_size) : impl_(std::unique_ptr( new connections::Payload(ByteArray(bytes, bytes_size)))) {} -PayloadW::PayloadW(InputFile &file) +PayloadW::PayloadW(InputFileW &file) : impl_(std::unique_ptr( - new connections::Payload(std::move(file)))) {} + new connections::Payload(InputFile(std::move(*file.GetImpl()))))) {} // TODO(jfcarroll): Convert std::function to function pointer PayloadW::PayloadW(std::function stream) @@ -58,15 +59,15 @@ PayloadW::PayloadW(PayloadId id, const char *bytes, const size_t bytes_size) : impl_(std::unique_ptr( new connections::Payload(id, ByteArray(bytes, bytes_size)))) {} -PayloadW::PayloadW(PayloadId id, InputFile file) +PayloadW::PayloadW(PayloadId id, InputFileW file) : impl_(std::unique_ptr( - new connections::Payload(id, std::move(file)))) {} + new connections::Payload(id, std::move(*file.GetImpl())))) {} PayloadW::PayloadW(const char *parent_folder, const char *file_name, - InputFile file) + InputFileW file) : impl_(std::unique_ptr( new connections::Payload(parent_folder, file_name, - std::move(file)))) {} + std::move(*file.GetImpl())))) {} PayloadW::PayloadW(PayloadId id, std::function stream) : impl_(std::unique_ptr( @@ -100,7 +101,7 @@ bool PayloadW::AsBytes(const char *bytes, size_t &bytes_size) && { // Returns InputStream* payload, if it has been defined, or nullptr. InputStream *PayloadW::AsStream() { return impl_->AsStream(); } // Returns InputFile* payload, if it has been defined, or nullptr. -const InputFile *PayloadW::AsFile() const { return impl_->AsFile(); } +InputFile *PayloadW::AsFile() const { return impl_->AsFile(); } // Returns Payload unique ID. int64_t PayloadW::GetId() const { return impl_->GetId(); } diff --git a/connections/clients/windows/payload_w.h b/connections/clients/windows/payload_w.h index 22c9d81d..4b4e60d0 100644 --- a/connections/clients/windows/payload_w.h +++ b/connections/clients/windows/payload_w.h @@ -21,6 +21,7 @@ #include #include "connections/clients/windows/dll_config.h" +#include "connections/clients/windows/file_w.h" #include "connections/payload_type.h" #include "internal/platform/payload_id.h" @@ -53,14 +54,14 @@ class DLL_API PayloadW { // Constructors for outgoing payloads. explicit PayloadW(const char* bytes, const size_t size); - explicit PayloadW(InputFile& file); + explicit PayloadW(InputFileW& file); explicit PayloadW(std::function stream); // Constructors for incoming payloads. PayloadW(PayloadId id, const char* bytes, const size_t size); - PayloadW(PayloadId id, InputFile file); + PayloadW(PayloadId id, InputFileW file); explicit PayloadW(const char* parent_folder, const char* file_name, - InputFile file); + InputFileW file); // TODO(jfcarroll): Convert std::function to function pointer PayloadW(PayloadId id, std::function stream); @@ -71,7 +72,7 @@ class DLL_API PayloadW { // Returns InputStream* payload, if it has been defined, or nullptr. InputStream* AsStream(); // Returns InputFile* payload, if it has been defined, or nullptr. - const InputFile* AsFile() const; + InputFile* AsFile() const; // Returns Payload unique ID. int64_t GetId() const; diff --git a/internal/platform/file.cc b/internal/platform/file.cc index c1516467..5e077515 100644 --- a/internal/platform/file.cc +++ b/internal/platform/file.cc @@ -16,6 +16,8 @@ namespace location { namespace nearby { +void InputFileDeleter::operator()(nearby::InputFile* p) { delete p; } +void OutputFileDeleter::operator()(nearby::OutputFile* p) { delete p; } InputFile::InputFile(PayloadId id, std::int64_t size) : impl_(Platform::CreateInputFile(id, size)) {} diff --git a/internal/platform/file.h b/internal/platform/file.h index 7ba72233..84c1af69 100644 --- a/internal/platform/file.h +++ b/internal/platform/file.h @@ -29,11 +29,16 @@ namespace location { namespace nearby { +class InputFile; +struct InputFileDeleter { + void operator()(InputFile* p); +}; + +class OutputFile; +struct OutputFileDeleter { + void operator()(nearby::OutputFile* p); +}; -// TODO(b/227677097) This will cause the dll to fail. Currently this class -// must be exported. There are future plans to wrap this -// and export it from connections/clients/windows -// class DLL_API InputFile final { class InputFile final { public: using Platform = api::ImplementationPlatform; @@ -73,10 +78,6 @@ class InputFile final { std::unique_ptr impl_; }; -// TODO(b/227677097) This will cause the dll to fail. Currently this class -// must be exported. There are future plans to wrap this -// and export it from connections/clients/windows -// class DLL_API OutputFile final { class OutputFile final { public: using Platform = api::ImplementationPlatform;