-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
base:types add BufferSpan, ConstBufferSpan and use it in io:file and …
…db:sstable
- Loading branch information
Showing
11 changed files
with
122 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#ifndef _BASE_TYPES_H | ||
#define _BASE_TYPES_H | ||
|
||
#include <array> | ||
#include <cstddef> | ||
#include <cstdint> | ||
#include <string> | ||
#include <string_view> | ||
|
||
#include "absl/types/span.h" | ||
|
||
class BufferSpan : public absl::Span<uint8_t> { | ||
public: | ||
constexpr BufferSpan() = default; | ||
BufferSpan(void *data, size_t size) | ||
: Span(reinterpret_cast<uint8_t *>(data), size) {} | ||
|
||
BufferSpan(absl::Span<uint8_t> span) : Span(span) {} | ||
BufferSpan(std::vector<uint8_t> &v) : Span(v) {} | ||
|
||
template <size_t N> | ||
constexpr BufferSpan(std::array<uint8_t, N> &a) : Span(a) {} | ||
|
||
BufferSpan(std::string &s) : BufferSpan(s.data(), s.size()) {} | ||
|
||
template <size_t N> | ||
BufferSpan(std::array<char, N> &a) : BufferSpan(a.data(), N) {} | ||
|
||
operator std::string_view() const { | ||
return std::string_view(reinterpret_cast<char *>(data()), size()); | ||
} | ||
}; | ||
|
||
class ConstBufferSpan : public absl::Span<const uint8_t> { | ||
public: | ||
constexpr ConstBufferSpan() = default; | ||
ConstBufferSpan(const void *data, size_t size) | ||
: Span(reinterpret_cast<const uint8_t *>(data), size) {} | ||
|
||
constexpr ConstBufferSpan(absl::Span<const uint8_t> span) : Span(span) {} | ||
constexpr ConstBufferSpan(absl::Span<uint8_t> span) : Span(span) {} | ||
constexpr ConstBufferSpan(const std::vector<uint8_t> &v) : Span(v) {} | ||
|
||
template <size_t N> | ||
constexpr ConstBufferSpan(const std::array<uint8_t, N> &a) : Span(a) {} | ||
|
||
ConstBufferSpan(const std::string &s) | ||
: ConstBufferSpan(s.data(), s.size()) {} | ||
|
||
ConstBufferSpan(std::string_view s) : ConstBufferSpan(s.data(), s.size()) {} | ||
ConstBufferSpan(const char *c) : ConstBufferSpan(std::string_view(c)) {} | ||
|
||
template <size_t N> | ||
ConstBufferSpan(const std::array<char, N> &a) | ||
: ConstBufferSpan(a.data(), N) {} | ||
|
||
operator std::string_view() const { | ||
return std::string_view(reinterpret_cast<const char *>(data()), size()); | ||
} | ||
}; | ||
|
||
#endif // _BASE_TYPES_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.