mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 07:36:10 -04:00
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: 415587802
This commit is contained in:
committed by
Copybara-Service
parent
5ba6623eac
commit
f12bb6c405
+43
-7
@@ -21,9 +21,24 @@ 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(Payload&& other) noexcept {
|
||||
file_name_ = other.file_name_;
|
||||
parent_folder_ = other.parent_folder_;
|
||||
content_ = std::move(other.content_);
|
||||
id_ = other.id_;
|
||||
offset_ = other.offset_;
|
||||
type_ = other.type_;
|
||||
}
|
||||
Payload::~Payload() = default;
|
||||
Payload& Payload::operator=(Payload&& other) noexcept = default;
|
||||
Payload& Payload::operator=(Payload&& other) noexcept {
|
||||
file_name_ = other.file_name_;
|
||||
parent_folder_ = other.parent_folder_;
|
||||
content_ = std::move(other.content_);
|
||||
id_ = other.id_;
|
||||
offset_ = other.offset_;
|
||||
type_ = other.type_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Default (invalid) payload.
|
||||
Payload::Payload() : content_(absl::monostate()) {}
|
||||
@@ -33,9 +48,11 @@ Payload::Payload(ByteArray&& bytes) : content_(std::move(bytes)) {}
|
||||
|
||||
Payload::Payload(const ByteArray& bytes) : content_(bytes) {}
|
||||
|
||||
Payload::Payload(InputFile file)
|
||||
Payload::Payload(const char* parent_folder, const char* file_name,
|
||||
InputFile&& file)
|
||||
: content_(std::move(file)),
|
||||
id_(std::hash<std::string>()(file.GetFilePath())) {}
|
||||
parent_folder_(parent_folder),
|
||||
file_name_(file_name) {}
|
||||
|
||||
// TODO(jfcarroll): Convert std::function to function pointer
|
||||
Payload::Payload(std::function<InputStream&()> stream)
|
||||
@@ -47,7 +64,14 @@ Payload::Payload(Id id, ByteArray&& bytes)
|
||||
|
||||
Payload::Payload(Id id, const ByteArray& bytes) : content_(bytes), id_(id) {}
|
||||
|
||||
Payload::Payload(Id id, InputFile file) : content_(std::move(file)), id_(id) {}
|
||||
Payload::Payload(Id id, InputFile&& file)
|
||||
: content_(std::move(file)), id_(id), parent_folder_("") {
|
||||
auto fileName = std::to_string(id);
|
||||
file_name_ = fileName.c_str();
|
||||
|
||||
NEARBY_LOGS(INFO) << "Payload(Id,InputFile): parent folder ="
|
||||
<< parent_folder_ << " file name = " << file_name_ << "\n";
|
||||
}
|
||||
|
||||
// TODO(jfcarroll): Convert std::function to function pointer
|
||||
Payload::Payload(Id id, std::function<InputStream&()> stream)
|
||||
@@ -69,7 +93,9 @@ InputStream* Payload::AsStream() {
|
||||
return result ? &(*result)() : nullptr;
|
||||
}
|
||||
// Returns InputFile* payload, if it has been defined, or nullptr.
|
||||
InputFile* Payload::AsFile() { return absl::get_if<InputFile>(&content_); }
|
||||
const InputFile* Payload::AsFile() const {
|
||||
return absl::get_if<InputFile>(&content_);
|
||||
}
|
||||
|
||||
// Returns Payload unique ID.
|
||||
Payload::Id Payload::GetId() const { return id_; }
|
||||
@@ -80,8 +106,10 @@ 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();
|
||||
const InputFile* file = AsFile();
|
||||
if (file != nullptr) {
|
||||
NEARBY_LOGS(INFO) << "Payload::SetOffset: offset: " << offset
|
||||
<< " file total size : " << file->GetTotalSize() << "\n";
|
||||
CHECK(file->GetTotalSize() > 0 && offset < (size_t)file->GetTotalSize());
|
||||
}
|
||||
offset_ = offset;
|
||||
@@ -96,6 +124,14 @@ Payload::Type Payload::FindType() const {
|
||||
return static_cast<Type>(content_.index());
|
||||
}
|
||||
|
||||
const std::string Payload::GetParentFolder() const {
|
||||
return std::string(parent_folder_);
|
||||
}
|
||||
|
||||
const std::string Payload::GetFileName() const {
|
||||
return std::string(file_name_);
|
||||
}
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
Reference in New Issue
Block a user