Skip to content

Commit

Permalink
[apriltag] Use char instead of uint8_t for json::parse
Browse files Browse the repository at this point in the history
Add GetCharBuffer() to MemoryBuffer classes to make this reusable.
  • Loading branch information
PeterJohnson committed Nov 5, 2023
1 parent 63dbf5c commit 6792865
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion apriltag/src/main/native/cpp/AprilTagFieldLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ AprilTagFieldLayout::AprilTagFieldLayout(std::string_view path) {
throw std::runtime_error(fmt::format("Cannot open file: {}", path));
}

wpi::json json = wpi::json::parse(fileBuffer->begin(), fileBuffer->end());
wpi::json json = wpi::json::parse(fileBuffer->GetCharBuffer());

for (const auto& tag : json.at("tags").get<std::vector<AprilTag>>()) {
m_apriltags[tag.ID] = tag;
Expand Down
13 changes: 13 additions & 0 deletions wpiutil/src/main/native/thirdparty/llvm/include/wpi/MemoryBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class MemoryBuffer {

std::span<const uint8_t> GetBuffer() const { return {begin(), end()}; }

std::span<const char> GetCharBuffer() const {
return {reinterpret_cast<const char*>(begin()),
reinterpret_cast<const char*>(end())};
}

/// Return an identifier for this buffer, typically the filename it was read
/// from.
virtual std::string_view GetBufferIdentifier() const {
Expand Down Expand Up @@ -145,6 +150,10 @@ class WritableMemoryBuffer : public MemoryBuffer {
uint8_t* begin() { return const_cast<uint8_t*>(MemoryBuffer::begin()); }
uint8_t* end() { return const_cast<uint8_t*>(MemoryBuffer::end()); }
std::span<uint8_t> GetBuffer() { return {begin(), end()}; }
std::span<char> GetCharBuffer() const {
return {reinterpret_cast<char*>(const_cast<uint8_t*>(begin())),
reinterpret_cast<char*>(const_cast<uint8_t*>(end()))};
}

static std::unique_ptr<WritableMemoryBuffer> GetFile(
std::string_view filename, std::error_code& ec, int64_t fileSize = -1);
Expand Down Expand Up @@ -196,6 +205,10 @@ class WriteThroughMemoryBuffer : public MemoryBuffer {
uint8_t* begin() { return const_cast<uint8_t*>(MemoryBuffer::begin()); }
uint8_t* end() { return const_cast<uint8_t*>(MemoryBuffer::end()); }
std::span<uint8_t> GetBuffer() { return {begin(), end()}; }
std::span<char> GetCharBuffer() const {
return {reinterpret_cast<char*>(const_cast<uint8_t*>(begin())),
reinterpret_cast<char*>(const_cast<uint8_t*>(end()))};
}

static std::unique_ptr<WriteThroughMemoryBuffer> GetFile(
std::string_view filename, std::error_code& ec, int64_t fileSize = -1);
Expand Down

0 comments on commit 6792865

Please sign in to comment.