Refactor Input/Output file to use const char* instead of standard string, and to use parent_folder and file_name for input and output files. This also incorporates the move from nearby_connections to nearby.

PiperOrigin-RevId: 418002199
This commit is contained in:
jfcarroll
2021-12-23 07:04:38 -08:00
committed by Copybara-Service
parent 8f20fecfab
commit 9ce7be9812
48 changed files with 373 additions and 541 deletions
+1
View File
@@ -29,6 +29,7 @@ cc_library(
"cancelable_alarm.h",
"cancellable_task.h",
"condition_variable.h",
"core_config.h",
"count_down_latch.h",
"crypto.h",
"file.h",
+15 -12
View File
@@ -17,19 +17,16 @@
namespace location {
namespace nearby {
InputFile::InputFile(const char* file_path)
: impl_(Platform::CreateInputFile(file_path)) {}
InputFile::InputFile(PayloadId payload_id, std::int64_t size)
: impl_(Platform::CreateInputFile(payload_id, size)), id_(payload_id) {}
InputFile::~InputFile() = default;
InputFile::InputFile(InputFile&& other) noexcept {
impl_ = std::move(other.impl_);
}
InputFile::InputFile(InputFile&&) noexcept = default;
InputFile& InputFile::operator=(InputFile&&) noexcept = default;
// Reads up to size bytes and returns as a ByteArray object wrapped by
// ExceptionOr.
// Returns Exception::kIo on error, or end of file.
ExceptionOr<ByteArray> InputFile::Read(std::int64_t size) const {
ExceptionOr<ByteArray> InputFile::Read(std::int64_t size) {
return impl_->Read(size);
}
@@ -39,13 +36,13 @@ std::string InputFile::GetFilePath() const { return impl_->GetFilePath(); }
// Returns total size of this file in bytes.
std::int64_t InputFile::GetTotalSize() const { return impl_->GetTotalSize(); }
ExceptionOr<size_t> InputFile::Skip(size_t offset) const {
ExceptionOr<size_t> InputFile::Skip(size_t offset) {
return impl_->Skip(offset);
}
// Disallows further reads from the file and frees system resources,
// associated with it.
Exception InputFile::Close() const { return impl_->Close(); }
Exception InputFile::Close() { return impl_->Close(); }
// Returns a handle to the underlying input stream.
//
@@ -54,10 +51,13 @@ Exception InputFile::Close() const { return impl_->Close(); }
// Side effects of any non-const operation invoked for InputFile (such as
// Read, or Close will be observable through InputStream& handle, and vice
// versa.
const InputStream& InputFile::GetInputStream() const { return *impl_; }
InputStream& InputFile::GetInputStream() { return *impl_; }
OutputFile::OutputFile(const char* file_path)
: impl_(Platform::CreateOutputFile(file_path)) {}
// Returns payload id of this file. The closest "file" equivalent is inode.
PayloadId InputFile::GetPayloadId() const { return id_; }
OutputFile::OutputFile(PayloadId payload_id)
: impl_(Platform::CreateOutputFile(payload_id)), id_(payload_id) {}
OutputFile::~OutputFile() = default;
OutputFile::OutputFile(OutputFile&&) noexcept = default;
OutputFile& OutputFile::operator=(OutputFile&&) noexcept = default;
@@ -85,5 +85,8 @@ Exception OutputFile::Close() { return impl_->Close(); }
// versa.
OutputStream& OutputFile::GetOutputStream() { return *impl_; }
// Returns payload id of this file. The closest "file" equivalent is inode.
PayloadId OutputFile::GetPayloadId() const { return id_; }
} // namespace nearby
} // namespace location
+15 -7
View File
@@ -23,10 +23,10 @@
#include "platform/api/output_file.h"
#include "platform/api/platform.h"
#include "platform/base/byte_array.h"
#include "platform/base/core_config.h"
#include "platform/base/exception.h"
#include "platform/base/input_stream.h"
#include "platform/base/output_stream.h"
#include "platform/public/core_config.h"
namespace location {
namespace nearby {
@@ -34,7 +34,7 @@ namespace nearby {
class DLL_API InputFile final {
public:
using Platform = api::ImplementationPlatform;
InputFile(const char* file_path);
InputFile(PayloadId payload_id, std::int64_t size);
~InputFile();
InputFile(InputFile&&) noexcept;
InputFile& operator=(InputFile&&) noexcept;
@@ -42,7 +42,7 @@ class DLL_API InputFile final {
// Reads up to size bytes and returns as a ByteArray object wrapped by
// ExceptionOr.
// Returns Exception::kIo on error, or end of file.
ExceptionOr<ByteArray> Read(std::int64_t size) const;
ExceptionOr<ByteArray> Read(std::int64_t size);
// Returns a string that uniqely identifies this file.
std::string GetFilePath() const;
@@ -50,11 +50,11 @@ class DLL_API InputFile final {
// Returns total size of this file in bytes.
std::int64_t GetTotalSize() const;
ExceptionOr<size_t> Skip(size_t offset) const;
ExceptionOr<size_t> Skip(size_t offset);
// Disallows further reads from the file and frees system resources,
// associated with it.
Exception Close() const;
Exception Close();
// Returns a handle to the underlying input stream.
//
@@ -63,16 +63,20 @@ class DLL_API InputFile final {
// Side effects of any non-const operation invoked for InputFile (such as
// Read, or Close will be observable through InputStream& handle, and vice
// versa.
const InputStream& GetInputStream() const;
InputStream& GetInputStream();
// Returns payload id of this file. The closest "file" equivalent is inode.
PayloadId GetPayloadId() const;
private:
std::unique_ptr<api::InputFile> impl_;
PayloadId id_;
};
class DLL_API OutputFile final {
public:
using Platform = api::ImplementationPlatform;
explicit OutputFile(const char* file_path);
explicit OutputFile(PayloadId payload_id);
~OutputFile();
OutputFile(OutputFile&&) noexcept;
OutputFile& operator=(OutputFile&&) noexcept;
@@ -98,8 +102,12 @@ class DLL_API OutputFile final {
// versa.
OutputStream& GetOutputStream();
// Returns payload id of this file. The closest "file" equivalent is inode.
PayloadId GetPayloadId() const;
private:
std::unique_ptr<api::OutputFile> impl_;
PayloadId id_;
};
} // namespace nearby