Skip to content

Commit

Permalink
Replace std::make_pair with std::pair CTAD (#7405)
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul authored Nov 18, 2024
1 parent b4bec56 commit a04c40f
Show file tree
Hide file tree
Showing 18 changed files with 64 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DefaultCameraServerShared : public frc::CameraServerShared {
void ReportDriverStationErrorV(fmt::string_view format,
fmt::format_args args) override {}
std::pair<std::thread::id, bool> GetRobotMainThreadId() const override {
return std::make_pair(std::thread::id(), false);
return std::pair{std::thread::id(), false};
}
};
} // namespace
Expand Down
10 changes: 5 additions & 5 deletions cscore/src/main/native/cpp/Telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Telemetry::Thread : public wpi::SafeThread {

int64_t Telemetry::Thread::GetValue(CS_Handle handle, CS_TelemetryKind kind,
CS_Status* status) {
auto it = m_user.find(std::make_pair(handle, static_cast<int>(kind)));
auto it = m_user.find(std::pair{handle, static_cast<int>(kind)});
if (it == m_user.end()) {
*status = CS_EMPTY_VALUE;
return 0;
Expand Down Expand Up @@ -136,8 +136,8 @@ void Telemetry::RecordSourceBytes(const SourceImpl& source, int quantity) {
return;
}
auto handleData = Instance::GetInstance().FindSource(source);
thr->m_current[std::make_pair(Handle{handleData.first, Handle::kSource},
static_cast<int>(CS_SOURCE_BYTES_RECEIVED))] +=
thr->m_current[std::pair{Handle{handleData.first, Handle::kSource},
static_cast<int>(CS_SOURCE_BYTES_RECEIVED)}] +=
quantity;
}

Expand All @@ -147,7 +147,7 @@ void Telemetry::RecordSourceFrames(const SourceImpl& source, int quantity) {
return;
}
auto handleData = Instance::GetInstance().FindSource(source);
thr->m_current[std::make_pair(Handle{handleData.first, Handle::kSource},
static_cast<int>(CS_SOURCE_FRAMES_RECEIVED))] +=
thr->m_current[std::pair{Handle{handleData.first, Handle::kSource},
static_cast<int>(CS_SOURCE_FRAMES_RECEIVED)}] +=
quantity;
}
4 changes: 2 additions & 2 deletions cscore/src/main/native/cpp/UnlimitedHandleResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ UnlimitedHandleResource<THandle, TStruct, typeValue, TMutex>::FindIf(F func) {
for (size_t i = 0; i < m_structures.size(); i++) {
auto& structure = m_structures[i];
if (structure != nullptr && func(*structure)) {
return std::make_pair(MakeHandle(i), structure);
return std::pair{MakeHandle(i), structure};
}
}
return std::make_pair(0, nullptr);
return std::pair{0, nullptr};
}

} // namespace cs
Expand Down
2 changes: 1 addition & 1 deletion ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ Java_edu_wpi_first_networktables_NetworkTablesJNI_setServer__I_3Ljava_lang_Strin
}
names.emplace_back(JStringRef{env, elem}.str());
servers.emplace_back(
std::make_pair(std::string_view{names.back()}, portInts[i]));
std::pair{std::string_view{names.back()}, portInts[i]});
}
nt::SetServer(inst, servers);
}
Expand Down
2 changes: 1 addition & 1 deletion ntcore/src/main/native/cpp/networktables/NetworkTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ NT_Listener NetworkTable::AddSubTableListener(SubTableListener listener) {
if (notified_tables->find(sub_table_key) != notified_tables->end()) {
return;
}
notified_tables->insert(std::make_pair(sub_table_key, '\0'));
notified_tables->insert(std::pair{sub_table_key, '\0'});
cb(this, sub_table_key, this->GetSubTable(sub_table_key));
});
}
Expand Down
2 changes: 1 addition & 1 deletion ntcore/src/main/native/cpp/ntcore_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ void NT_SetServerMulti(NT_Inst inst, size_t count,
servers.reserve(count);
for (size_t i = 0; i < count; ++i) {
servers.emplace_back(
std::make_pair(wpi::to_string_view(&server_names[i]), ports[i]));
std::pair{wpi::to_string_view(&server_names[i]), ports[i]});
}
nt::SetServer(inst, servers);
}
Expand Down
2 changes: 1 addition & 1 deletion roborioteamnumbersetter/src/main/native/cpp/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static void FindDevices() {
if (macKey != data.txt.end()) {
auto& mac = macKey->second;
auto& foundDevice = foundDevices[mac];
foundDevice = std::make_pair(data.ipv4Address, data.hostName);
foundDevice = std::pair{data.ipv4Address, data.hostName};
auto& deviceStatus = deviceStatuses[mac];
if (!deviceStatus) {
deploySession.GetStatus(mac, foundDevice.first);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class TestableSelectCommand : public SelectCommand<int> {
std::vector<std::pair<int, std::unique_ptr<Command>>> vec;
int index = 0;
for (auto&& command : commands) {
vec.emplace_back(std::make_pair(index, std::move(command)));
vec.emplace_back(std::pair{index, std::move(command)});
index++;
}
return vec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ ShuffleboardLayout& ShuffleboardContainer::GetLayout(std::string_view title,
auto layout = std::make_unique<ShuffleboardLayout>(*this, title, type);
auto ptr = layout.get();
m_components.emplace_back(std::move(layout));
m_layouts.insert(std::make_pair(title, ptr));
m_layouts.insert(std::pair{title, ptr});
}
return *m_layouts[title];
}
Expand Down
2 changes: 1 addition & 1 deletion wpilibc/src/main/native/cppcs/RobotBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class WPILibCameraServerShared : public frc::CameraServerShared {
ReportErrorV(err::Error, __FILE__, __LINE__, __FUNCTION__, format, args);
}
std::pair<std::thread::id, bool> GetRobotMainThreadId() const override {
return std::make_pair(RobotBase::GetThreadId(), true);
return std::pair{RobotBase::GetThreadId(), true};
}
};
class WPILibMathShared : public wpi::math::MathShared {
Expand Down
2 changes: 1 addition & 1 deletion wpinet/src/main/native/cpp/HttpUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ HttpLocation::HttpLocation(std::string_view url_, bool* error,
return;
}

params.emplace_back(std::make_pair(param, value));
params.emplace_back(std::pair{param, value});
}

*error = false;
Expand Down
4 changes: 2 additions & 2 deletions wpinet/src/main/native/include/wpinet/HttpUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,10 @@ class HttpRequest {
: host{loc.host}, port{loc.port} {
SmallVector<std::pair<std::string_view, std::string_view>, 4> params;
for (const auto& p : loc.params) {
params.emplace_back(std::make_pair(GetFirst(p), GetSecond(p)));
params.emplace_back(std::pair{GetFirst(p), GetSecond(p)});
}
for (const auto& p : extraParams) {
params.emplace_back(std::make_pair(GetFirst(p), GetSecond(p)));
params.emplace_back(std::pair{GetFirst(p), GetSecond(p)});
}
SetPath(loc.path, params);
SetAuth(loc);
Expand Down
2 changes: 1 addition & 1 deletion wpinet/src/main/native/include/wpinet/uv/Loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class Loop final : public std::enable_shared_from_this<Loop> {
*/
std::pair<bool, Time> GetTimeout() const noexcept {
auto to = uv_backend_timeout(m_loop);
return std::make_pair(to == -1, Time{to});
return std::pair{to == -1, Time{to}};
}

/**
Expand Down
2 changes: 1 addition & 1 deletion wpinet/src/main/native/include/wpinet/uv/Tty.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Tty final : public StreamImpl<Tty, uv_tty_t> {
std::pair<int, int> GetWindowSize() {
int width = 0, height = 0;
Invoke(&uv_tty_get_winsize, GetRaw(), &width, &height);
return std::make_pair(width, height);
return std::pair{width, height};
}
};

Expand Down
2 changes: 1 addition & 1 deletion wpinet/src/test/native/cpp/MulticastTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ TEST(MulticastServiceAnnouncerTest, SingleText) {
const std::string_view serviceType = "_wpitxt";
const int port = std::rand();
std::array<std::pair<std::string, std::string>, 1> txt = {
std::make_pair("hello", "world")};
std::pair{"hello", "world"}};
wpi::MulticastServiceAnnouncer announcer(serviceName, serviceType, port, txt);
wpi::MulticastServiceResolver resolver(serviceType);

Expand Down
35 changes: 35 additions & 0 deletions wpiutil/src/main/native/include/wpi/ct_map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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.

#pragma once

#include <algorithm>
#include <array>
#include <cstddef>
#include <stdexcept>
#include <utility>

namespace wpi {

template <typename Key, typename Value, size_t Size>
class ct_map {
constexpr ct_map();

[[nodiscard]]
constexpr const Value& operator[](const Key& key) const {
if (const auto it =
std::find_if(data.begin(), data.end(),
[&key](const auto& v) { return v.first == key; });
it != data.end()) {
return it->second;
} else {
throw std::range_error("Not found");
}
}

private:
std::array<std::pair<Key, Value>, Size> data;
};

} // namespace wpi
4 changes: 2 additions & 2 deletions wpiutil/src/main/native/include/wpi/interpolating_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class interpolating_map {
* @param value The value.
*/
void insert(const Key& key, const Value& value) {
m_container.insert(std::make_pair(key, value));
m_container.insert(std::pair{key, value});
}

/**
Expand All @@ -39,7 +39,7 @@ class interpolating_map {
* @param value The value.
*/
void insert(Key&& key, Value&& value) {
m_container.insert(std::make_pair(key, value));
m_container.insert(std::pair{key, value});
}

/**
Expand Down
13 changes: 6 additions & 7 deletions wpiutil/src/test/native/cpp/StringMapTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ TEST_F(StringMapTest, Iteration) {
// Test insert() method.
TEST_F(StringMapTest, Insert) {
SCOPED_TRACE("InsertTest");
testMap.insert(
std::make_pair(std::string_view(testKeyFirst, testKeyLength), 1u));
testMap.insert(std::pair{std::string_view(testKeyFirst, testKeyLength), 1u});
assertSingleItemMap();
}

Expand All @@ -260,7 +259,7 @@ TEST_F(StringMapTest, InsertPair) {
bool Inserted;
StringMap<uint32_t>::iterator NewIt;
std::tie(NewIt, Inserted) =
testMap.insert(std::make_pair(testKeyFirst, testValue));
testMap.insert(std::pair{testKeyFirst, testValue});
EXPECT_EQ(1u, testMap.size());
EXPECT_EQ(testValue, testMap[testKeyFirst]);
EXPECT_EQ(testKeyFirst, NewIt->first);
Expand All @@ -269,7 +268,7 @@ TEST_F(StringMapTest, InsertPair) {

StringMap<uint32_t>::iterator ExistingIt;
std::tie(ExistingIt, Inserted) =
testMap.insert(std::make_pair(testKeyFirst, testValue + 1));
testMap.insert(std::pair{testKeyFirst, testValue + 1});
EXPECT_EQ(1u, testMap.size());
EXPECT_EQ(testValue, testMap[testKeyFirst]);
EXPECT_FALSE(Inserted);
Expand Down Expand Up @@ -306,7 +305,7 @@ struct StringMapTestStruct {

TEST_F(StringMapTest, NonDefaultConstructable) {
StringMap<StringMapTestStruct> t;
t.insert(std::make_pair("Test", StringMapTestStruct(123)));
t.insert(std::pair{"Test", StringMapTestStruct(123)});
StringMap<StringMapTestStruct>::iterator iter = t.find("Test");
ASSERT_NE(iter, t.end());
ASSERT_EQ(iter->second.i, 123);
Expand Down Expand Up @@ -443,7 +442,7 @@ struct Countable {
TEST_F(StringMapTest, MoveDtor) {
int InstanceCount = 0;
StringMap<Countable> A;
A.insert(std::make_pair("x", Countable(42, InstanceCount)));
A.insert(std::pair{"x", Countable(42, InstanceCount)});
ASSERT_EQ(InstanceCount, 1);
auto I = A.find("x");
ASSERT_NE(I, A.end());
Expand Down Expand Up @@ -474,7 +473,7 @@ TEST_F(StringMapTest, StructuredBindings) {

TEST_F(StringMapTest, StructuredBindingsMoveOnly) {
StringMap<MoveOnly> A;
A.insert(std::make_pair("a", MoveOnly(42)));
A.insert(std::pair{"a", MoveOnly(42)});

for (auto&& [Key, Value] : A) {
EXPECT_EQ("a", Key);
Expand Down

0 comments on commit a04c40f

Please sign in to comment.