From ab9647ff5bdb6b79c8ec24a0d173cc1adab73ae8 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Fri, 10 Jan 2020 14:53:35 -0800 Subject: [PATCH] CommandScheduler: Don't store NetworkTableEntry --- .../wpilibj2/command/CommandScheduler.java | 21 +++++++----------- .../cpp/frc2/command/CommandScheduler.cpp | 22 ++++++++----------- .../wpi/first/wpilibj/command/Scheduler.java | 21 ++++++++---------- 3 files changed, 26 insertions(+), 38 deletions(-) diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java index b2110df708..536b27f28a 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java @@ -70,11 +70,6 @@ public final class CommandScheduler implements Sendable { private boolean m_disabled; - //NetworkTable entries for use in Sendable impl - private NetworkTableEntry m_namesEntry; - private NetworkTableEntry m_idsEntry; - private NetworkTableEntry m_cancelEntry; - //Lists of user-supplied actions to be executed on scheduling events for every command. private final List> m_initActions = new ArrayList<>(); private final List> m_executeActions = new ArrayList<>(); @@ -473,12 +468,12 @@ public final class CommandScheduler implements Sendable { @Override public void initSendable(SendableBuilder builder) { builder.setSmartDashboardType("Scheduler"); - m_namesEntry = builder.getEntry("Names"); - m_idsEntry = builder.getEntry("Ids"); - m_cancelEntry = builder.getEntry("Cancel"); + final NetworkTableEntry namesEntry = builder.getEntry("Names"); + final NetworkTableEntry idsEntry = builder.getEntry("Ids"); + final NetworkTableEntry cancelEntry = builder.getEntry("Cancel"); builder.setUpdateTable(() -> { - if (m_namesEntry == null || m_idsEntry == null || m_cancelEntry == null) { + if (namesEntry == null || idsEntry == null || cancelEntry == null) { return; } @@ -489,21 +484,21 @@ public final class CommandScheduler implements Sendable { ids.put((double) command.hashCode(), command); } - double[] toCancel = m_cancelEntry.getDoubleArray(new double[0]); + double[] toCancel = cancelEntry.getDoubleArray(new double[0]); if (toCancel.length > 0) { for (double hash : toCancel) { cancel(ids.get(hash)); ids.remove(hash); } - m_cancelEntry.setDoubleArray(new double[0]); + cancelEntry.setDoubleArray(new double[0]); } List names = new ArrayList<>(); ids.values().forEach(command -> names.add(command.getName())); - m_namesEntry.setStringArray(names.toArray(new String[0])); - m_idsEntry.setNumberArray(ids.keySet().toArray(new Double[0])); + namesEntry.setStringArray(names.toArray(new String[0])); + idsEntry.setNumberArray(ids.keySet().toArray(new Double[0])); }); } } diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp index fbfabfb069..a58b6d5756 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp @@ -43,11 +43,6 @@ class CommandScheduler::Impl { bool disabled{false}; - // NetworkTable entries for use in Sendable impl - nt::NetworkTableEntry namesEntry; - nt::NetworkTableEntry idsEntry; - nt::NetworkTableEntry cancelEntry; - // Lists of user-supplied actions to be executed on scheduling events for // every command. wpi::SmallVector initActions; @@ -382,14 +377,14 @@ void CommandScheduler::OnCommandFinish(Action action) { void CommandScheduler::InitSendable(frc::SendableBuilder& builder) { builder.SetSmartDashboardType("Scheduler"); - m_impl->namesEntry = builder.GetEntry("Names"); - m_impl->idsEntry = builder.GetEntry("Ids"); - m_impl->cancelEntry = builder.GetEntry("Cancel"); + auto namesEntry = builder.GetEntry("Names"); + auto idsEntry = builder.GetEntry("Ids"); + auto cancelEntry = builder.GetEntry("Cancel"); - builder.SetUpdateTable([this] { + builder.SetUpdateTable([=] { double tmp[1]; tmp[0] = 0; - auto toCancel = m_impl->cancelEntry.GetDoubleArray(tmp); + auto toCancel = cancelEntry.GetDoubleArray(tmp); for (auto cancel : toCancel) { uintptr_t ptrTmp = static_cast(cancel); Command* command = reinterpret_cast(ptrTmp); @@ -397,7 +392,8 @@ void CommandScheduler::InitSendable(frc::SendableBuilder& builder) { m_impl->scheduledCommands.end()) { Cancel(command); } - m_impl->cancelEntry.SetDoubleArray(wpi::ArrayRef{}); + nt::NetworkTableEntry(cancelEntry) + .SetDoubleArray(wpi::ArrayRef{}); } wpi::SmallVector names; @@ -407,8 +403,8 @@ void CommandScheduler::InitSendable(frc::SendableBuilder& builder) { uintptr_t ptrTmp = reinterpret_cast(command.first); ids.emplace_back(static_cast(ptrTmp)); } - m_impl->namesEntry.SetStringArray(names); - m_impl->idsEntry.SetDoubleArray(ids); + nt::NetworkTableEntry(namesEntry).SetStringArray(names); + nt::NetworkTableEntry(idsEntry).SetDoubleArray(ids); }); } diff --git a/wpilibOldCommands/src/main/java/edu/wpi/first/wpilibj/command/Scheduler.java b/wpilibOldCommands/src/main/java/edu/wpi/first/wpilibj/command/Scheduler.java index e25a5a7470..98d2fd0fc3 100644 --- a/wpilibOldCommands/src/main/java/edu/wpi/first/wpilibj/command/Scheduler.java +++ b/wpilibOldCommands/src/main/java/edu/wpi/first/wpilibj/command/Scheduler.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -81,9 +81,6 @@ public final class Scheduler implements Sendable, AutoCloseable { */ @SuppressWarnings({"PMD.LooseCoupling", "PMD.UseArrayListInsteadOfVector"}) private final Vector m_additions = new Vector<>(); - private NetworkTableEntry m_namesEntry; - private NetworkTableEntry m_idsEntry; - private NetworkTableEntry m_cancelEntry; /** * A list of all {@link edu.wpi.first.wpilibj.buttons.Trigger.ButtonScheduler Buttons}. It is * created lazily. @@ -319,13 +316,13 @@ public final class Scheduler implements Sendable, AutoCloseable { @Override public void initSendable(SendableBuilder builder) { builder.setSmartDashboardType("Scheduler"); - m_namesEntry = builder.getEntry("Names"); - m_idsEntry = builder.getEntry("Ids"); - m_cancelEntry = builder.getEntry("Cancel"); + final NetworkTableEntry namesEntry = builder.getEntry("Names"); + final NetworkTableEntry idsEntry = builder.getEntry("Ids"); + final NetworkTableEntry cancelEntry = builder.getEntry("Cancel"); builder.setUpdateTable(() -> { - if (m_namesEntry != null && m_idsEntry != null && m_cancelEntry != null) { + if (namesEntry != null && idsEntry != null && cancelEntry != null) { // Get the commands to cancel - double[] toCancel = m_cancelEntry.getDoubleArray(new double[0]); + double[] toCancel = cancelEntry.getDoubleArray(new double[0]); if (toCancel.length > 0) { for (LinkedListElement e = m_firstCommand; e != null; e = e.getNext()) { for (double d : toCancel) { @@ -334,7 +331,7 @@ public final class Scheduler implements Sendable, AutoCloseable { } } } - m_cancelEntry.setDoubleArray(new double[0]); + cancelEntry.setDoubleArray(new double[0]); } if (m_runningCommandsChanged) { @@ -351,8 +348,8 @@ public final class Scheduler implements Sendable, AutoCloseable { ids[number] = e.getData().hashCode(); number++; } - m_namesEntry.setStringArray(commands); - m_idsEntry.setDoubleArray(ids); + namesEntry.setStringArray(commands); + idsEntry.setDoubleArray(ids); } } });