aboutsummaryrefslogtreecommitdiffhomepage
path: root/Eigen/src/Core/util/IntegralConstant.h
diff options
context:
space:
mode:
authorGravatar Gael Guennebaud <g.gael@free.fr>2017-01-16 16:17:01 +0100
committerGravatar Gael Guennebaud <g.gael@free.fr>2017-01-16 16:17:01 +0100
commita9232af845702d62d81c49b4d92b39f29410cea0 (patch)
tree6efb7ecec7c49880a16d2ab31599d9c0dba3de7f /Eigen/src/Core/util/IntegralConstant.h
parent6e97698161275db750868afb99f405cdb849f412 (diff)
Introduce a variable_or_fixed<N> proxy returned by fix<N>(val) to pass both a compile-time and runtime fallback value in case N means "runtime".
This mechanism is used by the seq/seqN functions. The proxy object is immediately converted to pure compile-time (as fix<N>) or pure runtime (i.e., an Index) to avoid redundant template instantiations.
Diffstat (limited to 'Eigen/src/Core/util/IntegralConstant.h')
-rw-r--r--Eigen/src/Core/util/IntegralConstant.h69
1 files changed, 67 insertions, 2 deletions
diff --git a/Eigen/src/Core/util/IntegralConstant.h b/Eigen/src/Core/util/IntegralConstant.h
index a4394c464..dc8c8413d 100644
--- a/Eigen/src/Core/util/IntegralConstant.h
+++ b/Eigen/src/Core/util/IntegralConstant.h
@@ -15,13 +15,35 @@ namespace Eigen {
namespace internal {
+template<int N> struct fix_t;
+template<int N> class variable_or_fixed;
+
template<int N> struct fix_t {
static const int value = N;
operator int() const { return value; }
- fix_t (fix_t<N> (*)() ) {}
fix_t() {}
+ fix_t(variable_or_fixed<N> other) {
+ EIGEN_ONLY_USED_FOR_DEBUG(other);
+ eigen_internal_assert(int(other)==N);
+ }
+
+#if EIGEN_HAS_CXX14
// Needed in C++14 to allow fix<N>():
fix_t operator() () const { return *this; }
+
+ variable_or_fixed<N> operator() (int val) const { return variable_or_fixed<N>(val); }
+#else
+ fix_t (fix_t<N> (*)() ) {}
+#endif
+};
+
+template<int N> class variable_or_fixed {
+public:
+ static const int value = N;
+ operator int() const { return m_value; }
+ variable_or_fixed(int val) { m_value = val; }
+protected:
+ int m_value;
};
template<typename T, int Default=Dynamic> struct get_compile_time {
@@ -32,6 +54,10 @@ template<int N,int Default> struct get_compile_time<fix_t<N>,Default> {
enum { value = N };
};
+template<int N,int Default> struct get_compile_time<variable_or_fixed<N>,Default> {
+ enum { value = N };
+};
+
template<typename T, int N, int Default>
struct get_compile_time<variable_if_dynamic<T,N>,Default> {
enum { value = N };
@@ -45,12 +71,17 @@ template<int N> struct is_compile_time<fix_t<N> > { enum { value = true }; };
#ifndef EIGEN_PARSED_BY_DOXYGEN
-#if __cplusplus > 201103L
+#if EIGEN_HAS_CXX14
template<int N>
static const internal::fix_t<N> fix{};
#else
template<int N>
inline internal::fix_t<N> fix() { return internal::fix_t<N>(); }
+
+// The generic typename T is mandatory. Otherwise, a code like fix<N> could refer to either the function above or this next overload.
+// This way a code like fix<N> can only refer to the previous function.
+template<int N,typename T>
+inline internal::variable_or_fixed<N> fix(T val) { return internal::variable_or_fixed<N>(val); }
#endif
#else // EIGEN_PARSED_BY_DOXYGEN
@@ -67,6 +98,8 @@ inline internal::fix_t<N> fix() { return internal::fix_t<N>(); }
* seqN(10,fix<4>,fix<-3>) // <=> [10 7 4 1]
* \endcode
*
+ * See also the function fix(int) to pass both a compile-time and runtime value.
+ *
* In c++14, it is implemented as:
* \code
* template<int N> static const internal::fix_t<N> fix{};
@@ -82,10 +115,42 @@ inline internal::fix_t<N> fix() { return internal::fix_t<N>(); }
* Here internal::fix_t<N> is thus a pointer to function.
*
* If for some reason you want a true object in c++98 then you can write: \code fix<N>() \endcode which is also valid in c++14.
+ *
+ * \sa fix(int), seq, seqN
*/
template<int N>
static const auto fix;
+/** \fn fix(int)
+ * \ingroup Core_Module
+ *
+ * This function returns an object embedding both a compile-time integer \c N, and a runtime value \a val.
+ *
+ * \tparam N the compile-time integer value
+ * \param val the runtime integer value
+ *
+ * This function is a more general version of the \ref fix identifier/function that can be used in template code
+ * where the compile-time value coudl turned out to actually mean "undefined at compile-time". For positive integers
+ * such as a size of a dimension, this case is identified by Eigen::Dynamic, whereas runtime signed integers (e.g., an increment/stride) are identified
+ * as Eigen::DynamicIndex.
+ *
+ * A typical use case would be:
+ * \code
+ * template<typename Derived> void foo(const MatrixBase<Derived> &mat) {
+ * const int N = Derived::RowsAtCompileTime==Dynamic ? Dynamic : Derived::RowsAtCompileTime/2;
+ * const int n = mat.rows()/2;
+ * ... mat( seqN(0,fix<N>(n) ) ...;
+ * }
+ * \endcode
+ * In this example, the function Eigen::seqN knows that the second argument is expected to be a size.
+ * If the passed compile-time value N equals Eigen::Dynamic, then the proxy object returned by fix will be dissmissed, and converted to an Eigen::Index of value \c n.
+ * Otherwise, the runtime-value \c n will be dissmissed, and the returned ArithmeticSequence will be of the exact same type as <tt> seqN(0,fix<N>) </tt>.
+ *
+ * \sa fix, seqN, class ArithmeticSequence
+ */
+template<int N>
+static const auto fix(int val);
+
#endif // EIGEN_PARSED_BY_DOXYGEN
} // end namespace Eigen