epfl-lasa / control-libraries

A collection of library modules to facilitate the creation of full control loop algorithms, including state representation, motion planning, kinematics, dynamics and control.
https://epfl-lasa.github.io/control-libraries
GNU General Public License v3.0
27 stars 2 forks source link

Truth value for State #303

Closed eeberhard closed 2 years ago

eeberhard commented 2 years ago

I'd like to be able to check the truthiness of a State object. There are many cases where we check if a state is empty or not before doing something, and I think it would be convenient to have a direct operator for it.

// old way
if (!state.is_empty()) {
  foo();
}

// new way
if (state) {
  foo();
}

This mainly came to mind as I was working with Parameter objects and validating them by their emptiness.

Note that I used the explicit keyword, which restricts this operator to singularly logical evaluations (the solution to the safe-bool idiom since C++11). In other words, implicit conversions like bool x = state1 and weird side-effects such as state1 == state2 will still not be affected because of this.