Skip to content

Commit

Permalink
Make wpi::ct_string implicitly convert to std::string
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul committed Dec 4, 2023
1 parent 507a082 commit 8a12a97
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions wpilibc/src/test/native/cpp/util/Color8BitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

#include <string>

#include <gtest/gtest.h>

#include "frc/util/Color8Bit.h"
Expand Down Expand Up @@ -59,6 +61,9 @@ TEST(Color8BitTest, ToHexString) {
constexpr frc::Color8Bit color1{255, 128, 64};
EXPECT_EQ("#FF8040", color1.HexString());

// Ensure conversion to std::string works
[[maybe_unused]] std::string str = color1.HexString();

frc::Color8Bit color2{255, 128, 64};
EXPECT_EQ("#FF8040", color2.HexString());
}
5 changes: 5 additions & 0 deletions wpilibc/src/test/native/cpp/util/ColorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

#include <string>

#include <gtest/gtest.h>

#include "frc/util/Color.h"
Expand Down Expand Up @@ -59,6 +61,9 @@ TEST(ColorTest, ToHexString) {
constexpr frc::Color color1{255, 128, 64};
EXPECT_EQ("#FF8040", color1.HexString());

// Ensure conversion to std::string works
[[maybe_unused]] std::string str = color1.HexString();

frc::Color color2{255, 128, 64};
EXPECT_EQ("#FF8040", color2.HexString());
}
5 changes: 5 additions & 0 deletions wpiutil/src/main/native/include/wpi/ct_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ struct ct_string {
constexpr auto data() const noexcept { return chars.data(); }
constexpr auto c_str() const noexcept { return chars.data(); }

constexpr operator std::basic_string<Char, Traits>() // NOLINT
const noexcept {
return std::basic_string<Char, Traits>{chars.data(), N};
}

constexpr operator std::basic_string_view<Char, Traits>() // NOLINT
const noexcept {
return std::basic_string_view<Char, Traits>{chars.data(), N};
Expand Down

0 comments on commit 8a12a97

Please sign in to comment.