Use C++23 std::expected (#8823)

This commit is contained in:
Gold856
2026-07-02 02:10:52 -04:00
committed by GitHub
parent 1299abc173
commit 4e7dd4cfbb
37 changed files with 133 additions and 2709 deletions

View File

@@ -1,41 +0,0 @@
#!/usr/bin/env python3
import shutil
from pathlib import Path
from upstream_utils import Lib
def copy_upstream_src(wpilib_root: Path):
wpiutil = wpilib_root / "wpiutil"
# Copy expected header into allwpilib
dest_filename = (
wpiutil / "src/main/native/thirdparty/expected/include/wpi/util/expected"
)
shutil.copyfile("include/tl/expected.hpp", dest_filename)
# Rename namespace from tl to wpi, and detail to detail_expected
with open(dest_filename) as f:
content = f.read()
content = content.replace("namespace tl", "namespace wpi::util")
content = content.replace("tl::", "wpi::util::")
content = content.replace("TL_", "WPI_")
content = content.replace("namespace detail", "namespace detail_expected")
content = content.replace("detail::", "detail_expected::")
with open(dest_filename, "w") as f:
f.write(content)
def main():
name = "expected"
url = "https://github.com/TartanLlama/expected"
# master on 2024-01-25
tag = "3f0ca7b19253129700a073abfa6d8638d9f7c80c"
expected = Lib(name, url, tag, copy_upstream_src)
expected.main()
if __name__ == "__main__":
main()

View File

@@ -9,7 +9,7 @@ Subject: [PATCH 15/25] Change parse to return expected with error string
2 files changed, 15 insertions(+), 13 deletions(-)
diff --git a/json.cpp b/json.cpp
index 2b4191865e84f4c160450fa8380ad578d37e1cd1..baefe6317126f95ec75fe9d9a896923d0a90e720 100644
index 2b4191865e84f4c160450fa8380ad578d37e1cd1..11f2ccde854aed19724c991b36905b221e71ae84 100644
--- a/json.cpp
+++ b/json.cpp
@@ -1258,21 +1258,22 @@ json::parse(json& j, const char*& p, const char* e, int context, int depth)
@@ -17,7 +17,7 @@ index 2b4191865e84f4c160450fa8380ad578d37e1cd1..baefe6317126f95ec75fe9d9a896923d
}
-std::pair<json::Status, json>
+wpi::util::expected<json, const char*>
+std::expected<json, const char*>
json::parse(std::string_view s)
{
- json::Status s2;
@@ -33,30 +33,30 @@ index 2b4191865e84f4c160450fa8380ad578d37e1cd1..baefe6317126f95ec75fe9d9a896923d
+ json j;
+ Status result = parse(j, p, e, 0, DEPTH);
+ if (result != success) {
+ return wpi::util::unexpected(StatusToString(result));
+ return std::unexpected(StatusToString(result));
}
- return res;
+ json j2;
+ Status s2 = parse(j2, p, e, 0, DEPTH);
+ if (s2 != absent_value) {
+ return wpi::util::unexpected(StatusToString(trailing_content));
+ return std::unexpected(StatusToString(trailing_content));
+ }
+ return j;
}
const char*
diff --git a/json.h b/json.h
index 1c0dc26a460e17dab44f13d62059cf6e69f355c9..d2962c322678b0dd664707f34151094fec90d281 100644
index 1c0dc26a460e17dab44f13d62059cf6e69f355c9..1553eecb7251d509622ab00a4ade4a672252845e 100644
--- a/json.h
+++ b/json.h
@@ -22,6 +22,7 @@
@@ -17,6 +17,7 @@
#pragma once
+#include <expected>
#include <string>
#include <string_view>
#include <vector>
#include "wpi/util/StringMap.hpp"
+#include "wpi/util/expected"
namespace wpi::util {
@@ -43,6 +44,7 @@ class json
Object
};
@@ -79,7 +79,7 @@ index 1c0dc26a460e17dab44f13d62059cf6e69f355c9..d2962c322678b0dd664707f34151094f
public:
- static const char* StatusToString(Status);
- static std::pair<Status, json> parse(std::string_view);
+ static wpi::util::expected<json, const char*> parse(std::string_view);
+ static std::expected<json, const char*> parse(std::string_view);
json(const json&);
json(json&&);

View File

@@ -9,7 +9,7 @@ Subject: [PATCH 16/25] Add parse_or_throw
2 files changed, 11 insertions(+)
diff --git a/json.cpp b/json.cpp
index baefe6317126f95ec75fe9d9a896923d0a90e720..49fbf92ea6e67e771f051f10b65ac795351c6f3d 100644
index 11f2ccde854aed19724c991b36905b221e71ae84..84712da562ee6b1966bae788fbf32876eaec4cef 100644
--- a/json.cpp
+++ b/json.cpp
@@ -1276,6 +1276,16 @@ json::parse(std::string_view s)
@@ -30,13 +30,13 @@ index baefe6317126f95ec75fe9d9a896923d0a90e720..49fbf92ea6e67e771f051f10b65ac795
json::StatusToString(json::Status status)
{
diff --git a/json.h b/json.h
index d2962c322678b0dd664707f34151094fec90d281..f42ba9c135cb76a1b0bc31f9e2c03b1ed7dce749 100644
index 1553eecb7251d509622ab00a4ade4a672252845e..c5f39687e68289adadb988df9cfd81c031266859 100644
--- a/json.h
+++ b/json.h
@@ -96,6 +96,7 @@ class json
public:
static wpi::util::expected<json, const char*> parse(std::string_view);
static std::expected<json, const char*> parse(std::string_view);
+ static json parse_or_throw(std::string_view);
json(const json&);

View File

@@ -9,7 +9,7 @@ Subject: [PATCH 17/25] Add operator==
2 files changed, 32 insertions(+)
diff --git a/json.cpp b/json.cpp
index 49fbf92ea6e67e771f051f10b65ac795351c6f3d..40479bc266390f015e291b737b5751522dac76ac 100644
index 84712da562ee6b1966bae788fbf32876eaec4cef..839c311017ccbb2f5ea8f85aab4965e9ce7af090 100644
--- a/json.cpp
+++ b/json.cpp
@@ -1361,4 +1361,30 @@ json::StatusToString(json::Status status)
@@ -44,7 +44,7 @@ index 49fbf92ea6e67e771f051f10b65ac795351c6f3d..40479bc266390f015e291b737b575152
+
} // namespace wpi::util
diff --git a/json.h b/json.h
index f42ba9c135cb76a1b0bc31f9e2c03b1ed7dce749..fbc3fb959f71f6acc344f26247597959807d23bb 100644
index c5f39687e68289adadb988df9cfd81c031266859..2f2bb7b3b0a265299a011e4e8011ea14065dca36 100644
--- a/json.h
+++ b/json.h
@@ -28,6 +28,7 @@ namespace wpi::util {

View File

@@ -9,7 +9,7 @@ Subject: [PATCH 18/25] Add container functions
2 files changed, 120 insertions(+)
diff --git a/json.cpp b/json.cpp
index 40479bc266390f015e291b737b5751522dac76ac..2307536b15be9aae19da4ec9370a5aa17d5150d1 100644
index 839c311017ccbb2f5ea8f85aab4965e9ce7af090..a800ce44ab28117b957a2f7507908cbd4c730de9 100644
--- a/json.cpp
+++ b/json.cpp
@@ -606,6 +606,28 @@ json::contains(std::string_view key) const
@@ -129,7 +129,7 @@ index 40479bc266390f015e291b737b5751522dac76ac..2307536b15be9aae19da4ec9370a5aa1
json::to_string() const
{
diff --git a/json.h b/json.h
index fbc3fb959f71f6acc344f26247597959807d23bb..62af49cb18b713304ad95e89c52f5cc5bbc4b626 100644
index 2f2bb7b3b0a265299a011e4e8011ea14065dca36..b478523deea8ae3d852ebe369cea8ca60fdec01d 100644
--- a/json.h
+++ b/json.h
@@ -216,6 +216,9 @@ class json

View File

@@ -8,7 +8,7 @@ Subject: [PATCH 19/25] Add to_json, from_json, and json_serializer struct
1 file changed, 65 insertions(+)
diff --git a/json.h b/json.h
index 62af49cb18b713304ad95e89c52f5cc5bbc4b626..e52435a79ee7537928ba59fb146f635fc8511bfc 100644
index b478523deea8ae3d852ebe369cea8ca60fdec01d..d77ff1ea380614c54579d26cc233aa8424734b5b 100644
--- a/json.h
+++ b/json.h
@@ -26,6 +26,35 @@

View File

@@ -8,11 +8,11 @@ Subject: [PATCH 20/25] Add array and object factory functions
1 file changed, 30 insertions(+)
diff --git a/json.h b/json.h
index e52435a79ee7537928ba59fb146f635fc8511bfc..6a52615ab3e1799ebcf51a3b5ca445b1e2e64b97 100644
index d77ff1ea380614c54579d26cc233aa8424734b5b..08796527ea948c1cd602c0ba7a7b24dd75e57046 100644
--- a/json.h
+++ b/json.h
@@ -19,6 +19,8 @@
@@ -20,6 +20,8 @@
#include <expected>
#include <string>
#include <string_view>
+#include <tuple>

View File

@@ -8,14 +8,15 @@ Subject: [PATCH 21/25] Add array constructor
1 file changed, 21 insertions(+)
diff --git a/json.h b/json.h
index 6a52615ab3e1799ebcf51a3b5ca445b1e2e64b97..ef3ea13b7cb147f0b383ff1ead496d495cfece32 100644
index 08796527ea948c1cd602c0ba7a7b24dd75e57046..2dbf2cd9f8cc150c8f1dcfa7ef7ecd523b14d6ca 100644
--- a/json.h
+++ b/json.h
@@ -17,6 +17,8 @@
@@ -17,7 +17,9 @@
#pragma once
+#include <concepts>
#include <expected>
+#include <ranges>
#include <string>
#include <string_view>

View File

@@ -9,7 +9,7 @@ Subject: [PATCH 22/25] Improve stringize and marshal and use raw_ostream
2 files changed, 131 insertions(+), 87 deletions(-)
diff --git a/json.cpp b/json.cpp
index 2307536b15be9aae19da4ec9370a5aa17d5150d1..1beec7cc30e90ab2e31bc488ee15cedf9a9f32e5 100644
index a800ce44ab28117b957a2f7507908cbd4c730de9..b4a6689c2cf501abd884db0a8588e1042d2bac12 100644
--- a/json.cpp
+++ b/json.cpp
@@ -27,6 +27,7 @@
@@ -291,7 +291,7 @@ index 2307536b15be9aae19da4ec9370a5aa17d5150d1..1beec7cc30e90ab2e31bc488ee15cedf
break;
default:
diff --git a/json.h b/json.h
index ef3ea13b7cb147f0b383ff1ead496d495cfece32..e42ee96ac8697eaf17fd9d5298e877a45a4644a0 100644
index 2dbf2cd9f8cc150c8f1dcfa7ef7ecd523b14d6ca..a6a459fcda6015c2195b0a32611fc61525d5483c 100644
--- a/json.h
+++ b/json.h
@@ -31,6 +31,7 @@

View File

@@ -8,7 +8,7 @@ Subject: [PATCH 23/25] Update include path
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/json.cpp b/json.cpp
index 1beec7cc30e90ab2e31bc488ee15cedf9a9f32e5..f8416f3723df70032d274f32cda36b4ef1c7ed90 100644
index b4a6689c2cf501abd884db0a8588e1042d2bac12..27fb732b5af7a5ded9971c767e96e3f4f79a6566 100644
--- a/json.cpp
+++ b/json.cpp
@@ -15,7 +15,7 @@

View File

@@ -10,7 +10,7 @@ Make unsigned constructors constexpr
2 files changed, 112 insertions(+), 39 deletions(-)
diff --git a/json.cpp b/json.cpp
index f8416f3723df70032d274f32cda36b4ef1c7ed90..7f23ece0e8fa09ccb5fef8226de39c55ef70efdd 100644
index 27fb732b5af7a5ded9971c767e96e3f4f79a6566..c2a897f2ffe411fb6528741de1f4ea9e1a76a056 100644
--- a/json.cpp
+++ b/json.cpp
@@ -247,28 +247,6 @@ LongToString(char* p, long long x)
@@ -218,7 +218,7 @@ index f8416f3723df70032d274f32cda36b4ef1c7ed90..7f23ece0e8fa09ccb5fef8226de39c55
return lhs.float_value == rhs.float_value;
case json::Type::Double:
diff --git a/json.h b/json.h
index e42ee96ac8697eaf17fd9d5298e877a45a4644a0..31c5ea39b67286df158a9d6e13ab5073498126b8 100644
index a6a459fcda6015c2195b0a32611fc61525d5483c..ca7d76d837c9e0bdfb92e7c1b7bfd24cc8cb5ae2 100644
--- a/json.h
+++ b/json.h
@@ -17,6 +17,7 @@
@@ -227,8 +227,8 @@ index e42ee96ac8697eaf17fd9d5298e877a45a4644a0..31c5ea39b67286df158a9d6e13ab5073
+#include <climits>
#include <concepts>
#include <expected>
#include <ranges>
#include <string>
@@ -92,6 +93,7 @@ class json
Null,
Bool,

View File

@@ -8,7 +8,7 @@ Subject: [PATCH 25/25] Make bool constructor safe
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/json.h b/json.h
index 31c5ea39b67286df158a9d6e13ab5073498126b8..2746badf92a5bb4de6c38007d8e6dcfbed5b47eb 100644
index ca7d76d837c9e0bdfb92e7c1b7bfd24cc8cb5ae2..cd9c52ba855c28bc4fd7316195bead50a709e184 100644
--- a/json.h
+++ b/json.h
@@ -167,7 +167,7 @@ class json