Drake
EigenPtr< PlainObjectType > Class Template Reference

This wrapper class provides a way to write non-template functions taking raw pointers to Eigen objects as parameters while limiting the number of copies, similar to Eigen::Ref. More...

#include <drake/common/eigen_types.h>

Public Types

typedef Eigen::Ref< PlainObjectType > RefType
 

Public Member Functions

 EigenPtr ()
 
 EigenPtr (std::nullptr_t)
 Overload for nullptr. More...
 
 EigenPtr (const EigenPtr &other)
 Constructs with a reference to the given matrix type. More...
 
template<typename PlainObjectTypeIn >
 EigenPtr (PlainObjectTypeIn *m)
 Constructs with a reference to another matrix type. More...
 
template<typename PlainObjectTypeIn >
 EigenPtr (const EigenPtr< PlainObjectTypeIn > &other)
 Constructs from another EigenPtr. More...
 
EigenPtroperator= (const EigenPtr &other)
 
template<typename PlainObjectTypeIn >
EigenPtroperator= (const EigenPtr< PlainObjectTypeIn > &other)
 
RefTypeoperator* () const
 
RefTypeoperator-> () const
 
 operator bool () const
 Returns whether or not this contains a valid reference. More...
 
bool operator== (std::nullptr_t) const
 
bool operator!= (std::nullptr_t) const
 

Detailed Description

template<typename PlainObjectType>
class drake::EigenPtr< PlainObjectType >

This wrapper class provides a way to write non-template functions taking raw pointers to Eigen objects as parameters while limiting the number of copies, similar to Eigen::Ref.

Internally, it keeps an instance of Eigen::Ref<T> and provides access to it via operator* and operator->.

The motivation of this class is to follow GSG's "output arguments should be pointers" rule while taking advantage of using Eigen::Ref. Here is an example.

// This function is taking an Eigen::Ref of a matrix and modifies it in
// the body. This violates GSG's rule on output parameters.
void foo(Eigen::Ref<Eigen::MatrixXd> M) {
M(0, 0) = 0;
}
// At Call-site, we have:
foo(M);
foo(M.block(0, 0, 2, 2));
// We can rewrite the above function into the following using EigenPtr.
void foo(EigenPtr<Eigen::MatrixXd> M) {
(*M)(0, 0) = 0;
}
// Note that, call sites should be changed to:
foo(&M);
// We need tmp to avoid taking the address of a temporary object such as the
// return value of .block().
auto tmp = M.block(0, 0, 2, 2);
foo(&tmp);
Note
This class provides a way to avoid the const_cast hack introduced in Eigen's documentation.

Member Typedef Documentation

typedef Eigen::Ref<PlainObjectType> RefType

Constructor & Destructor Documentation

EigenPtr ( )
inline
EigenPtr ( std::nullptr_t  )
inline

Overload for nullptr.

EigenPtr ( const EigenPtr< PlainObjectType > &  other)
inline

Constructs with a reference to the given matrix type.

EigenPtr ( PlainObjectTypeIn *  m)
inline

Constructs with a reference to another matrix type.

May be nullptr.

EigenPtr ( const EigenPtr< PlainObjectTypeIn > &  other)
inline

Constructs from another EigenPtr.

Member Function Documentation

operator bool ( ) const
inline

Returns whether or not this contains a valid reference.

bool operator!= ( std::nullptr_t  ) const
inline
RefType& operator* ( ) const
inline
Exceptions
std::runtime_errorif this is a null dereference.
RefType* operator-> ( ) const
inline
Exceptions
std::runtime_errorif this is a null dereference.
EigenPtr& operator= ( const EigenPtr< PlainObjectType > &  other)
inline
EigenPtr& operator= ( const EigenPtr< PlainObjectTypeIn > &  other)
inline
bool operator== ( std::nullptr_t  ) const
inline

The documentation for this class was generated from the following file: