Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use strongly-typed enum flags. #340

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
0bcee06
Add flags to select what should be saved by Saver classes.
brianhou Feb 18, 2018
1620118
Add more tests.
brianhou Feb 18, 2018
eaf5718
Merge branch 'master' into enhancement/brianhou/saver-flags
brianhou Feb 18, 2018
a47a693
Restructure SaverOptions.
brianhou Feb 18, 2018
0ab8008
Address @jslee02's comments.
brianhou Feb 19, 2018
f08fa35
Remove incorrect const specifier.
brianhou Feb 19, 2018
d7e1b3e
Update CHANGELOG.md.
brianhou Feb 19, 2018
7d4ff7f
Use strongly-typed enum flags.
brianhou Feb 19, 2018
ca126d5
Merge branch 'master' into enhancement/brianhou/saver-enumflags
brianhou Feb 26, 2018
3b34c60
Merge branch 'master' into enhancement/brianhou/saver-enumflags
brianhou Mar 24, 2018
cc89d48
Merge branch 'master' into enhancement/brianhou/saver-enumflags
jslee02 Apr 11, 2018
d30437f
Merge branch 'master' into enhancement/brianhou/saver-enumflags
brianhou May 12, 2018
cfae5f5
Merge branch 'master' into enhancement/brianhou/saver-enumflags
jslee02 Oct 13, 2018
f74d96a
Merge branch 'master' into enhancement/brianhou/saver-enumflags
brianhou Feb 2, 2019
2c681ad
Address review comments
brianhou Feb 3, 2019
bdb01a3
Merge branch 'master' into enhancement/brianhou/saver-enumflags
brianhou Feb 3, 2019
f5d8f12
Merge branch 'master' into enhancement/brianhou/saver-enumflags
brianhou Jul 22, 2019
cf5a9f3
Merge branch 'master' into enhancement/brianhou/saver-enumflags
brianhou Sep 3, 2019
6f941fc
Merge branch 'master' into enhancement/brianhou/saver-enumflags
brianhou Sep 5, 2019
18a25be
Merge branch 'master' into enhancement/brianhou/saver-enumflags
egordon Sep 17, 2021
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
1 change: 1 addition & 0 deletions include/aikido/common.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common/EnumFlags.hpp"
#include "common/ExecutorMultiplexer.hpp"
#include "common/ExecutorThread.hpp"
#include "common/PseudoInverse.hpp"
Expand Down
44 changes: 44 additions & 0 deletions include/aikido/common/EnumFlags.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef AIKIDO_COMMON_ENUMFLAGS_HPP_
#define AIKIDO_COMMON_ENUMFLAGS_HPP_

#include <type_traits>

/// Enable bitwise operators for strongly-typed enums.
///
/// Adapted from
/// http://blog.bitwigglers.org/using-enum-classes-as-type-safe-bitmasks/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe we want to make this as non-doxygen style comment because this comment is not specifically tied to a code but just a note?


// clang-format off

#define AIKIDO_ENABLE_BITWISE_OPERATORS(X) \
template<> \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: template <> (a space between template and the angle brackets)

struct EnableBitwiseOperators< X > \
{ \
static const bool enable = true; \
};

template <typename Enum>
struct EnableBitwiseOperators
{
static const bool enable = false;
};

template <typename Enum>
typename std::enable_if<EnableBitwiseOperators<Enum>::enable, Enum>::type
operator&(Enum lhs, Enum rhs)
{
using T = typename std::underlying_type<Enum>::type;
return static_cast<Enum>(static_cast<T>(lhs) & static_cast<T>(rhs));
}

template <typename Enum>
typename std::enable_if<EnableBitwiseOperators<Enum>::enable, Enum>::type
operator|(Enum lhs, Enum rhs)
{
using T = typename std::underlying_type<Enum>::type;
return static_cast<Enum>(static_cast<T>(lhs) | static_cast<T>(rhs));
}

// clang-format on

#endif // AIKIDO_COMMON_ENUMFLAGS_HPP_
21 changes: 14 additions & 7 deletions include/aikido/planner/WorldStateSaver.hpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
#ifndef AIKIDO_PLANNER_WORLDSTATESAVER_HPP_
#define AIKIDO_PLANNER_WORLDSTATESAVER_HPP_

#include "aikido/common/EnumFlags.hpp"
#include "aikido/planner/World.hpp"

/// Options to specify what WorldStateSaver should save.
enum class WorldStateSaverOptions
{
NONE = 0,
CONFIGURATIONS = 1 << 0,
};

AIKIDO_ENABLE_BITWISE_OPERATORS(WorldStateSaverOptions)

namespace aikido {
namespace planner {

/// RAII class to save and restore a World's state.
class WorldStateSaver
{
public:
/// Options to specify what WorldStateSaver should save.
enum Options
{
CONFIGURATIONS = 1 << 0,
};
using Options = WorldStateSaverOptions;

/// Construct a WorldStateSaver and save the current state of the \c World.
/// This state will be restored when WorldStateSaver is destructed.
///
/// \param[in] world World to save state from and restore to.
/// \param[in] options Options to specify what should be saved
explicit WorldStateSaver(World* world, int options = CONFIGURATIONS);
explicit WorldStateSaver(
World* world, Options options = Options::CONFIGURATIONS);

virtual ~WorldStateSaver();

Expand All @@ -30,7 +37,7 @@ class WorldStateSaver
World* mWorld;

/// Options to specify what should be saved
int mOptions;
Options mOptions;

/// Saved state
World::State mWorldState;
Expand Down
22 changes: 14 additions & 8 deletions include/aikido/statespace/dart/MetaSkeletonStateSaver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
#define AIKIDO_STATESPACE_DART_METASKELETONSTATESAVER_HPP_

#include <dart/dynamics/MetaSkeleton.hpp>
#include "aikido/common/EnumFlags.hpp"

/// Options to specify what MetaSkeletonStateSaver should save.
enum class MetaSkeletonStateSaverOptions
{
NONE = 0,
POSITIONS = 1 << 0,
POSITION_LIMITS = 1 << 1,
};

AIKIDO_ENABLE_BITWISE_OPERATORS(MetaSkeletonStateSaverOptions)

namespace aikido {
namespace statespace {
Expand All @@ -12,12 +23,7 @@ namespace dart {
class MetaSkeletonStateSaver
{
public:
/// Options to specify what MetaSkeletonStateSaver should save.
enum Options
{
POSITIONS = 1 << 0,
POSITION_LIMITS = 1 << 1,
};
using Options = MetaSkeletonStateSaverOptions;

/// Construct a MetaSkeletonStateSaver and save the current state of the \c
/// MetaSkeleton. This state will be restored when MetaSkeletonStateSaver is
Expand All @@ -27,7 +33,7 @@ class MetaSkeletonStateSaver
/// \param[in] options Options to specify what should be saved
explicit MetaSkeletonStateSaver(
::dart::dynamics::MetaSkeletonPtr metaskeleton,
int options = POSITIONS | POSITION_LIMITS);
Options options = Options::POSITIONS | Options::POSITION_LIMITS);

virtual ~MetaSkeletonStateSaver();

Expand All @@ -43,7 +49,7 @@ class MetaSkeletonStateSaver
::dart::dynamics::MetaSkeletonPtr mMetaSkeleton;

/// Options to specify what should be saved
int mOptions;
Options mOptions;

/// Saved positions
Eigen::VectorXd mPositions;
Expand Down
6 changes: 3 additions & 3 deletions src/planner/WorldStateSaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
namespace aikido {
namespace planner {

WorldStateSaver::WorldStateSaver(World* world, int options)
WorldStateSaver::WorldStateSaver(World* world, Options options)
: mWorld{std::move(world)}, mOptions{options}
{
if (!mWorld)
throw std::invalid_argument("World must not be nullptr.");

if (mOptions & Options::CONFIGURATIONS)
if ((mOptions & Options::CONFIGURATIONS) != Options::NONE)
mWorldState = mWorld->getState();
}

WorldStateSaver::~WorldStateSaver()
{
if (mOptions & Options::CONFIGURATIONS)
if ((mOptions & Options::CONFIGURATIONS) != Options::NONE)
mWorld->setState(mWorldState);
}

Expand Down
12 changes: 7 additions & 5 deletions src/statespace/dart/MetaSkeletonStateSaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ namespace dart {

//==============================================================================
MetaSkeletonStateSaver::MetaSkeletonStateSaver(
::dart::dynamics::MetaSkeletonPtr metaskeleton, int options)
::dart::dynamics::MetaSkeletonPtr metaskeleton, Options options)
: mMetaSkeleton(std::move(metaskeleton)), mOptions(std::move(options))
{
if (mOptions & Options::POSITIONS)
if ((mOptions & Options::POSITIONS) != Options::NONE)
{
mPositions = mMetaSkeleton->getPositions();
}

if (mOptions & Options::POSITION_LIMITS)
if ((mOptions & Options::POSITION_LIMITS) != Options::NONE)
{
mPositionLowerLimits = mMetaSkeleton->getPositionLowerLimits();
mPositionUpperLimits = mMetaSkeleton->getPositionUpperLimits();
Expand All @@ -23,7 +25,7 @@ MetaSkeletonStateSaver::MetaSkeletonStateSaver(
//==============================================================================
MetaSkeletonStateSaver::~MetaSkeletonStateSaver()
{
if (mOptions & Options::POSITIONS)
if ((mOptions & Options::POSITIONS) != Options::NONE)
{
if (static_cast<std::size_t>(mPositions.size())
!= mMetaSkeleton->getNumDofs())
Expand All @@ -35,7 +37,7 @@ MetaSkeletonStateSaver::~MetaSkeletonStateSaver()
mMetaSkeleton->setPositions(mPositions);
}

if (mOptions & Options::POSITION_LIMITS)
if ((mOptions & Options::POSITION_LIMITS) != Options::NONE)
{
if (static_cast<std::size_t>(mPositionLowerLimits.size())
!= mMetaSkeleton->getNumDofs())
Expand Down
3 changes: 2 additions & 1 deletion tests/statespace/dart/test_MetaSkeletonStateSaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ TEST_F(MetaSkeletonStateSaverTest, Flags_None)
EXPECT_DOUBLE_EQ(3., mSkeleton->getPositionUpperLimit(0));

{
auto saver = MetaSkeletonStateSaver(mSkeleton, 0);
auto saver = MetaSkeletonStateSaver(
mSkeleton, MetaSkeletonStateSaver::Options::NONE);
DART_UNUSED(saver);

mSkeleton->setPositionLowerLimit(0, 1.);
Expand Down