Skip to content

Commit

Permalink
Patch up Java test and add Java overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold856 committed Jul 14, 2023
1 parent 25ecdda commit 6d684d1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 22 deletions.
8 changes: 6 additions & 2 deletions wpilibc/src/main/native/cpp/event/BooleanEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ BooleanEvent BooleanEvent::operator!() {
return BooleanEvent(this->m_loop, [lhs = m_condition] { return !lhs(); });
}

BooleanEvent BooleanEvent::operator&&(std::function<bool()> rhs) {
BooleanEvent BooleanEvent::operator&&(BooleanEvent rhs) {
if (m_loop != rhs.m_loop) {
m_loop->Bind(
[&rhs] { rhs.m_state = std::make_shared<bool>(rhs.m_condition()); });
}
return BooleanEvent(this->m_loop,
[lhs = m_condition, rhs] { return lhs() && rhs(); });
[state = m_state, &rhs] { return state && rhs.m_state; });
}

BooleanEvent BooleanEvent::operator||(std::function<bool()> rhs) {
Expand Down
2 changes: 1 addition & 1 deletion wpilibc/src/main/native/include/frc/event/BooleanEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class BooleanEvent {
* @param rhs the event to compose with
* @return the event that is active when both events are active
*/
BooleanEvent operator&&(std::function<bool()> rhs);
BooleanEvent operator&&(BooleanEvent rhs);

/**
* Composes this event with another event, returning a new event that is
Expand Down
18 changes: 7 additions & 11 deletions wpilibc/src/test/native/cpp/event/BooleanEventTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ TEST(BooleanEventTest, EventReuse) {
bool boolean3 = false;
std::atomic_int counter = 0;

auto event1 = BooleanEvent(&loop, [&] { return boolean1; });
auto event2 = BooleanEvent(&loop, [&] { return boolean2; });
auto event3 = BooleanEvent(&loop, [&] { return boolean3; });
auto event1 = BooleanEvent(&loop, [&] { return boolean1; }).Rising();
auto event2 = BooleanEvent(&loop, [&] { return boolean2; }).Rising();
auto event3 = BooleanEvent(&loop, [&] { return boolean3; }).Rising();
event1.IfHigh([&] {
boolean1 = false;
++counter;
Expand All @@ -233,26 +233,22 @@ TEST(BooleanEventTest, EventReuse) {
loop.Poll();

EXPECT_EQ(3, counter);

boolean1 = true;
loop.Poll();

EXPECT_EQ(4, counter);
EXPECT_EQ(3, counter);

boolean2 = true;
boolean3 = true;
boolean1 = true;
loop.Poll();

EXPECT_EQ(4, counter);

boolean1 = true;
loop.Poll();

EXPECT_EQ(7, counter);
EXPECT_EQ(4, counter);

boolean1 = true;
boolean2 = true;
loop.Poll();

EXPECT_EQ(9, counter);
EXPECT_EQ(6, counter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static edu.wpi.first.util.ErrorMessages.requireNonNullParam;

import edu.wpi.first.math.filter.Debouncer;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiFunction;
import java.util.function.BooleanSupplier;
Expand Down Expand Up @@ -160,6 +159,21 @@ public BooleanEvent negate() {
* @param other the event to compose with
* @return the event that is active when both events are active
*/
public BooleanEvent and(BooleanEvent other) {
requireNonNullParam(other, "other", "and");
if (!m_loop.equals(other.m_loop)) {
m_loop.bind(() -> other.m_state.set(other.m_signal.getAsBoolean()));
}
return new BooleanEvent(m_loop, () -> m_state.get() && other.m_state.get());
}

/**
* Composes this event with a signal, returning a new signal that is in the high state when both
* signals are in the high state.
*
* @param other the signal to compose with
* @return the event that is active when both this event and the supplier are active
*/
public BooleanEvent and(BooleanSupplier other) {
requireNonNullParam(other, "other", "and");
return new BooleanEvent(m_loop, () -> m_signal.getAsBoolean() && other.getAsBoolean());
Expand All @@ -174,6 +188,21 @@ public BooleanEvent and(BooleanSupplier other) {
* @param other the event to compose with
* @return a signal that is high when either signal is high.
*/
public BooleanEvent or(BooleanEvent other) {
requireNonNullParam(other, "other", "or");
if (!m_loop.equals(other.m_loop)) {
m_loop.bind(() -> other.m_state.set(other.m_signal.getAsBoolean()));
}
return new BooleanEvent(m_loop, () -> m_state.get() || other.m_state.get());
}

/**
* Composes this event with a signal, returning a new signal that is high when either signal is
* high.
*
* @param other the signal to compose with
* @return a signal that is high when either signal is high.
*/
public BooleanEvent or(BooleanSupplier other) {
requireNonNullParam(other, "other", "or");
return new BooleanEvent(m_loop, () -> m_signal.getAsBoolean() || other.getAsBoolean());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,26 +245,23 @@ void testEventReuse() {

assertEquals(3, counter.get());

bool1.set(true);
loop.poll();

assertEquals(4, counter.get());
assertEquals(3, counter.get());

bool2.set(true);
bool3.set(true);
bool1.set(true);
loop.poll();

assertEquals(4, counter.get());

bool1.set(true);
loop.poll();

assertEquals(7, counter.get());
assertEquals(4, counter.get());

bool1.set(true);
bool2.set(true);
loop.poll();

assertEquals(9, counter.get());
assertEquals(6, counter.get());
}
}

0 comments on commit 6d684d1

Please sign in to comment.