Question

How do I in compile time check if a template parameter has a particular enum and if that is true get that enum value.

I am looking for some thing like this for a template parameter T which may ( or may not) have an enum type NeedsToAlign :

compile_time_if (T has NeedToAlign) {
 // only then compile the following
 EIGEN_MAKE_ALIGNED_OPERATOR_IF(typename T::NeedToAlign)
}

Read on to see my actual problem:

So I have class called Payload which defines a enum NeedsToAlign as shown below:

struct Payload<int N> {
 typedef Eigen::Matrix<float, N, 1> DataType;
 DataType data;

  enum {
    NeedToAlign = (sizeof(DataType)%16)==0,  ///< Check for alignement need
  };
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(EigenNeedsToAlign)
};

Though not related to my actual question, EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF() macro is required for using Eigen library to generates aligned pointers for certain fixed size DataType.

Now say I have another template class which just stores a variable of type T which may be a Payload type or more simpler types such as int/float etc.

template <typename T>
class A {
 T value;

// My goal:
// if T is of type Payload we need to enable the following:
// EIGEN_MAKE_ALIGNED_OPERATOR_IF(T::NeedToAlign) 

};

Note I cannot simply use EIGEN_MAKE_ALIGNED_OPERATOR_IF(typename T::NeedToAlign) since I want class A to also compile with say T = int.

Is there a Boost MPL solution for these? This is what I have been able to come up with till now:

typedef typename boost::mpl::if_< 
        has_EigenNeedsToAlign<Payload>,
        typename Payload::NeedToAlign,
        boost::mpl::int_< 0 >
                                >::type result;
 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(result::value);

But of course the above fails for T = int types.

One way I can see a solution is to do some sort of partial specialization of class A for Payload types or T types which has T::NeedToAlign. But is there a Boost MPL way of doing this?

As you probably have figured out, I am very new to this.


Update1:

I have figured out one way which is by deriving class A as following:

template <typename T>
class A : public EigenAllocatorHelper<T>{
 T value;
 ....    
};

where EigenAllocatorHelper injects the appropriate behavior by partial specialization (shown below):

template<typename T, class Enable = void>
class EigenAllocatorHelper {
};

BOOST_MPL_HAS_XXX_TRAIT_DEF(NeedsToAlignMPL)

template< typename T >
class EigenAllocatorHelper< T, typename boost::enable_if< has_NeedsToAlignMPL<T> >::type > {
public:
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(typename T::NeedsToAlign);
};

Also i had to use typedef boost::mpl::int_<(sizeof(DataType)%16)==0> NeedToAlignMPL; since mpl has_xxx did not work when I used the simple enum.

This solution looks okay to me and infact I can reuse this EigenAllocatorHelper class elsewhere also. But if there is better MPL magic which does not require this, let me know.

Was it helpful?

Solution

I am currently using the following solution:

#include <boost/utility/enable_if.hpp>
#include <boost/mpl/has_xxx.hpp>
#include <boost/mpl/and.hpp>

//! The macro is used for enable_if if T is an Eigen Type
#define ENABLE_IF_EIGEN_TYPE(T)\
    typename boost::enable_if< is_eigen_type<T> >::type

/** The macro enables Eigen new() when required. T can be any type
 *
 * Example Usage:
 *  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_REQUIRED(Eigen::Vector2d) will enable Eigen's new()
 *  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_REQUIRED(Eigen::Vector3d) will NOT
 *  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_REQUIRED(int) will NOT
 */
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_REQUIRED(T)\
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(requires_eigen_new_allign<T>::value)

namespace detail {
BOOST_MPL_HAS_XXX_TRAIT_DEF(Scalar)
BOOST_MPL_HAS_XXX_TRAIT_DEF(Index)
BOOST_MPL_HAS_XXX_TRAIT_DEF(StorageKind)
}

/**
 * Traits for checking if T is indeed an Eigen Type
 * @tparam T any Type
 *
 * Example Usage:
 * is_eigen_type<int>::value // evaluates to false
 * is_eigen_type<int>::type // evaluates to false_type
 * is_eigen_type<Eigen::Vector2d>::value // evaluates to true
 * is_eigen_type<Eigen::Vector2d>::type // true_type
 */
template<typename T>
struct is_eigen_type:
    boost::mpl::and_<
      detail::has_Scalar<T>,
      detail::has_Index<T>,
      detail::has_StorageKind<T> > {
};


template<class T, class Enable = void>
struct requires_eigen_new_allign {
  static const bool value = false;
};

template<class T>
struct requires_eigen_new_allign<T, ENABLE_IF_EIGEN_TYPE(T)> {
  typedef typename T::Scalar Scalar;
  static const bool value = (((T::SizeAtCompileTime) != Eigen::Dynamic)
      && ((sizeof(Scalar) * (T::SizeAtCompileTime)) % 16 == 0));
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top