Skip to content

Commit fc70c7d

Browse files
find some instances where THROW_IF is more natural
1 parent cae6e65 commit fc70c7d

File tree

6 files changed

+8
-9
lines changed

6 files changed

+8
-9
lines changed

common/drake_throw.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ namespace internal {
3434
#define DRAKE_THROW_UNLESS(condition) \
3535
do { \
3636
typedef ::drake::assert::ConditionTraits< \
37-
typename std::remove_cv_t<decltype(condition)>> \
38-
Trait; \
37+
typename std::remove_cv_t<decltype(condition)>> Trait; \
3938
static_assert(Trait::is_valid, "Condition should be bool-convertible."); \
4039
static_assert( \
4140
!std::is_pointer_v<decltype(condition)>, \
@@ -49,4 +48,4 @@ namespace internal {
4948

5049
/// Provides the same behavior as DRAKE_THROW_UNLESS, except that it throws an
5150
/// exception iff the value is true.
52-
#define DRAKE_THROW_IF(condition) DRAKE_THROW_UNLESS(!condition)
51+
#define DRAKE_THROW_IF(condition) DRAKE_THROW_UNLESS(!(condition))

geometry/meshcat.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,8 +769,8 @@ class Meshcat::Impl {
769769
main_thread_id_(std::this_thread::get_id()),
770770
params_(params),
771771
rate_calculator_(params_.realtime_rate_period) {
772-
DRAKE_THROW_UNLESS(!params.port.has_value() || *params.port == 0 ||
773-
*params.port >= 1024);
772+
DRAKE_THROW_IF(params.port.has_value() && *params.port == 0 &&
773+
*params.port >= 1024);
774774
if (!drake::internal::IsNetworkingAllowed("meshcat")) {
775775
throw std::runtime_error(
776776
"Meshcat has been disabled via the DRAKE_ALLOW_NETWORK environment "

geometry/optimization/hpolyhedron.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ HPolyhedron HPolyhedron::SimplifyByIncrementalFaceTranslation(
919919
const HPolyhedron circumbody = this->ReduceInequalities(0);
920920
MatrixXd circumbody_A = circumbody.A();
921921
VectorXd circumbody_b = circumbody.b();
922-
DRAKE_THROW_UNLESS(!circumbody.IsEmpty());
922+
DRAKE_THROW_IF(circumbody.IsEmpty());
923923
DRAKE_THROW_UNLESS(circumbody.IsBounded());
924924

925925
for (int i = 0; i < points_to_contain.cols(); ++i) {

geometry/proximity/volume_mesh_refiner.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ void VolumeMeshRefiner::CutTetrahedron(const int tetrahedron,
181181

182182
std::vector<int> VolumeMeshRefiner::GetTetrahedraOnTriangle(int v0, int v1,
183183
int v2) const {
184-
DRAKE_THROW_UNLESS(v0 != v1 && v1 != v2 && v2 != v0);
184+
DRAKE_THROW_IF(v0 == v1 || v1 == v2 || v2 == v0);
185185
std::vector<int> incident_tetrahedra;
186186
const std::vector<int>& tetrahedra0 = vertex_to_tetrahedra_[v0];
187187
const std::vector<int>& tetrahedra1 = vertex_to_tetrahedra_[v1];

multibody/plant/multibody_plant.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ MultibodyConstraintId MultibodyPlant<T>::AddTendonConstraint(
779779
upper_limit = kInf;
780780
}
781781

782-
DRAKE_THROW_UNLESS(*lower_limit != -kInf || *upper_limit != kInf);
782+
DRAKE_THROW_IF(*lower_limit == -kInf && *upper_limit == kInf);
783783
DRAKE_THROW_UNLESS(*lower_limit <= *upper_limit);
784784

785785
if (stiffness.has_value()) {

multibody/tree/multibody_tree_topology.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ JointActuatorIndex MultibodyTreeTopology::add_joint_actuator(int num_dofs) {
225225
void MultibodyTreeTopology::RemoveJointActuator(
226226
JointActuatorIndex actuator_index) {
227227
DRAKE_DEMAND(actuator_index < ssize(joint_actuators_));
228-
DRAKE_THROW_UNLESS(!is_valid());
228+
DRAKE_THROW_IF(is_valid());
229229
DRAKE_THROW_UNLESS(joint_actuators_[actuator_index].has_value());
230230

231231
// Reduce the total number of actuated dofs.

0 commit comments

Comments
 (0)