mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Merge branch 'master' into release
Change-Id: I56ea2217899e92bdd9d6cb56797ac9895e194fff
This commit is contained in:
@@ -15,10 +15,11 @@
|
||||
#ifndef PLATFORM_V2_BASE_BYTE_ARRAY_H_
|
||||
#define PLATFORM_V2_BASE_BYTE_ARRAY_H_
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
@@ -27,13 +28,22 @@ class ByteArray {
|
||||
public:
|
||||
// Create an empty ByteArray
|
||||
ByteArray() = default;
|
||||
template <size_t N>
|
||||
explicit ByteArray(const std::array<char, N>& data) {
|
||||
SetData(data.data(), data.size());
|
||||
}
|
||||
ByteArray(const ByteArray&) = default;
|
||||
ByteArray& operator=(const ByteArray&) = default;
|
||||
ByteArray(ByteArray&&) = default;
|
||||
ByteArray& operator=(ByteArray&&) = default;
|
||||
|
||||
// Create ByteArray from string.
|
||||
explicit ByteArray(absl::string_view source) {
|
||||
// Moves string out of temporary, allowing for a zero-copy constructions.
|
||||
// This is an optimization for very large strings.
|
||||
explicit ByteArray(std::string&& source) : data_(std::move(source)) {}
|
||||
|
||||
// Create ByteArray by copy of a std::string. This can't be a string_view,
|
||||
// because it will conflict with std::string&& version of constructor.
|
||||
explicit ByteArray(const std::string& source) {
|
||||
SetData(source.data(), source.size());
|
||||
}
|
||||
|
||||
@@ -73,7 +83,12 @@ class ByteArray {
|
||||
friend bool operator!=(const ByteArray& lhs, const ByteArray& rhs);
|
||||
friend bool operator<(const ByteArray& lhs, const ByteArray& rhs);
|
||||
|
||||
explicit operator std::string() const { return data_; }
|
||||
// Returns a copy of internal representation as std::string.
|
||||
explicit operator std::string() const& { return data_; }
|
||||
|
||||
// Moves string out of temporary ByteArray, allowing for a zero-copy
|
||||
// operation.
|
||||
explicit operator std::string() const&& { return std::move(data_); }
|
||||
|
||||
private:
|
||||
std::string data_;
|
||||
|
||||
Reference in New Issue
Block a user