[wpiutil] Add std::expected shim (#6310)

Its tests use Catch2 instead of GoogleTest, so we can't import them.
This commit is contained in:
Tyler Veness
2024-01-25 22:21:37 -08:00
committed by GitHub
parent 64a9d413bf
commit d895a0c09f
6 changed files with 2563 additions and 1 deletions

View File

@@ -0,0 +1,45 @@
#!/usr/bin/env python3
import os
import re
import shutil
from upstream_utils import (
get_repo_root,
clone_repo,
comment_out_invalid_includes,
walk_cwd_and_copy_if,
git_am,
)
def main():
upstream_root = clone_repo(
"https://github.com/TartanLlama/expected",
# master on 2024-01-25
"3f0ca7b19253129700a073abfa6d8638d9f7c80c",
shallow=False,
)
wpilib_root = get_repo_root()
wpiutil = os.path.join(wpilib_root, "wpiutil")
# Copy expected header into allwpilib
dest_filename = os.path.join(
wpiutil, "src/main/native/thirdparty/expected/include/wpi/expected"
)
shutil.copyfile(
os.path.join(upstream_root, "include/tl/expected.hpp"), dest_filename
)
# Rename namespace from tl to wpi
with open(dest_filename) as f:
content = f.read()
content = content.replace("namespace tl", "namespace wpi")
content = content.replace("tl::", "wpi::")
content = content.replace("TL_", "WPI_")
with open(dest_filename, "w") as f:
f.write(content)
if __name__ == "__main__":
main()

View File

@@ -3,6 +3,7 @@ cppHeaderFileInclude {
\.hpp$
\.inc$
\.inl$
expected$
math$
numbers$
scope$

View File

@@ -197,6 +197,17 @@ else()
endif()
endif()
install(
DIRECTORY src/main/native/thirdparty/expected/include/
DESTINATION "${include_dest}/wpiutil"
)
target_include_directories(
wpiutil
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/thirdparty/expected/include>
$<INSTALL_INTERFACE:${include_dest}/wpiutil>
)
install(DIRECTORY src/main/native/thirdparty/memory/include/ DESTINATION "${include_dest}/wpiutil")
target_include_directories(
wpiutil

View File

@@ -180,6 +180,9 @@ nativeUtils.exportsConfigs {
}
cppHeadersZip {
from('src/main/native/thirdparty/expected/include') {
into '/'
}
from('src/main/native/thirdparty/fmtlib/include') {
into '/'
}
@@ -232,7 +235,7 @@ model {
all {
it.sources.each {
it.exportedHeaders {
srcDirs 'src/main/native/include', 'src/main/native/thirdparty/fmtlib/include', 'src/main/native/thirdparty/llvm/include', 'src/main/native/thirdparty/sigslot/include', 'src/main/native/thirdparty/json/include', 'src/main/native/thirdparty/memory/include', 'src/main/native/thirdparty/mpack/include', 'src/main/native/thirdparty/protobuf/include'
srcDirs 'src/main/native/include', 'src/main/native/thirdparty/expected/include', 'src/main/native/thirdparty/fmtlib/include', 'src/main/native/thirdparty/llvm/include', 'src/main/native/thirdparty/sigslot/include', 'src/main/native/thirdparty/json/include', 'src/main/native/thirdparty/memory/include', 'src/main/native/thirdparty/mpack/include', 'src/main/native/thirdparty/protobuf/include'
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,58 @@
// Copyright (c) FIRST and other WPILib contributors.
// 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 <initializer_list>
#include <tuple>
#include <vector>
#include <gtest/gtest.h>
#include "wpi/expected"
namespace {
struct TakesInitAndVariadic {
std::vector<int> v;
std::tuple<int, int> t;
template <class... Args>
TakesInitAndVariadic(std::initializer_list<int> l, // NOLINT
Args&&... args)
: v(l), t(std::forward<Args>(args)...) {}
};
} // namespace
TEST(ExpectedTest, Emplace) {
{
wpi::expected<std::unique_ptr<int>, int> e;
e.emplace(new int{42});
EXPECT_TRUE(e);
EXPECT_EQ(**e, 42);
}
{
wpi::expected<std::vector<int>, int> e;
e.emplace({0, 1});
EXPECT_TRUE(e);
EXPECT_EQ((*e)[0], 0);
EXPECT_EQ((*e)[1], 1);
}
{
wpi::expected<std::tuple<int, int>, int> e;
e.emplace(2, 3);
EXPECT_TRUE(e);
EXPECT_EQ(std::get<0>(*e), 2);
EXPECT_EQ(std::get<1>(*e), 3);
}
{
wpi::expected<TakesInitAndVariadic, int> e = wpi::make_unexpected(0);
e.emplace({0, 1}, 2, 3);
EXPECT_TRUE(e);
EXPECT_EQ(e->v[0], 0);
EXPECT_EQ(e->v[1], 1);
EXPECT_EQ(std::get<0>(e->t), 2);
EXPECT_EQ(std::get<1>(e->t), 3);
}
}