Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions common/drake_throw.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ namespace internal {
::drake::internal::Throw(#condition, __func__, __FILE__, __LINE__); \
} \
} while (0)

/// Provides the same behavior as DRAKE_THROW_UNLESS, except that it throws an
/// exception iff the value is true.
#define DRAKE_THROW_IF(condition) DRAKE_THROW_UNLESS(!(condition))
7 changes: 6 additions & 1 deletion common/test/drake_throw_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@

namespace {

GTEST_TEST(DrakeThrowTest, BasicTest) {
GTEST_TEST(DrakeThrowTest, UnlessTest) {
DRAKE_EXPECT_NO_THROW(DRAKE_THROW_UNLESS(true));
EXPECT_THROW(DRAKE_THROW_UNLESS(false), std::runtime_error);
}

GTEST_TEST(DrakeThrowTest, IfTest) {
DRAKE_EXPECT_NO_THROW(DRAKE_THROW_IF(false));
EXPECT_THROW(DRAKE_THROW_IF(true), std::runtime_error);
}

} // namespace
4 changes: 2 additions & 2 deletions geometry/meshcat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -769,8 +769,8 @@ class Meshcat::Impl {
main_thread_id_(std::this_thread::get_id()),
params_(params),
rate_calculator_(params_.realtime_rate_period) {
DRAKE_THROW_UNLESS(!params.port.has_value() || *params.port == 0 ||
*params.port >= 1024);
DRAKE_THROW_IF(params.port.has_value() && *params.port == 0 &&
*params.port >= 1024);
if (!drake::internal::IsNetworkingAllowed("meshcat")) {
throw std::runtime_error(
"Meshcat has been disabled via the DRAKE_ALLOW_NETWORK environment "
Expand Down
2 changes: 1 addition & 1 deletion geometry/optimization/hpolyhedron.cc
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ HPolyhedron HPolyhedron::SimplifyByIncrementalFaceTranslation(
const HPolyhedron circumbody = this->ReduceInequalities(0);
MatrixXd circumbody_A = circumbody.A();
VectorXd circumbody_b = circumbody.b();
DRAKE_THROW_UNLESS(!circumbody.IsEmpty());
DRAKE_THROW_IF(circumbody.IsEmpty());
DRAKE_THROW_UNLESS(circumbody.IsBounded());

for (int i = 0; i < points_to_contain.cols(); ++i) {
Expand Down
2 changes: 1 addition & 1 deletion geometry/proximity/volume_mesh_refiner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ void VolumeMeshRefiner::CutTetrahedron(const int tetrahedron,

std::vector<int> VolumeMeshRefiner::GetTetrahedraOnTriangle(int v0, int v1,
int v2) const {
DRAKE_THROW_UNLESS(v0 != v1 && v1 != v2 && v2 != v0);
DRAKE_THROW_IF(v0 == v1 || v1 == v2 || v2 == v0);
std::vector<int> incident_tetrahedra;
const std::vector<int>& tetrahedra0 = vertex_to_tetrahedra_[v0];
const std::vector<int>& tetrahedra1 = vertex_to_tetrahedra_[v1];
Expand Down
2 changes: 1 addition & 1 deletion multibody/plant/multibody_plant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ MultibodyConstraintId MultibodyPlant<T>::AddTendonConstraint(
upper_limit = kInf;
}

DRAKE_THROW_UNLESS(*lower_limit != -kInf || *upper_limit != kInf);
DRAKE_THROW_IF(*lower_limit == -kInf && *upper_limit == kInf);
DRAKE_THROW_UNLESS(*lower_limit <= *upper_limit);

if (stiffness.has_value()) {
Expand Down
2 changes: 1 addition & 1 deletion multibody/tree/multibody_tree_topology.cc
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ JointActuatorIndex MultibodyTreeTopology::add_joint_actuator(int num_dofs) {
void MultibodyTreeTopology::RemoveJointActuator(
JointActuatorIndex actuator_index) {
DRAKE_DEMAND(actuator_index < ssize(joint_actuators_));
DRAKE_THROW_UNLESS(!is_valid());
DRAKE_THROW_IF(is_valid());
DRAKE_THROW_UNLESS(joint_actuators_[actuator_index].has_value());

// Reduce the total number of actuated dofs.
Expand Down