Skip to content

Commit

Permalink
FIx indentation for Values-inl.h
Browse files Browse the repository at this point in the history
  • Loading branch information
varunagrawal committed Nov 19, 2020
1 parent 32070b0 commit 5749565
Showing 1 changed file with 92 additions and 91 deletions.
183 changes: 92 additions & 91 deletions gtsam/nonlinear/Values-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,97 +259,98 @@ namespace gtsam {
}

/* ************************************************************************* */
template<>
inline bool Values::filterHelper<Value>(const boost::function<bool(Key)> filter,
const ConstKeyValuePair& key_value) {
// Filter and check the type
return filter(key_value.key);
}

/* ************************************************************************* */

namespace internal {

// Check the type and throw exception if incorrect
// Generic version, partially specialized below for various Eigen Matrix types
template <typename ValueType>
struct handle {
ValueType operator()(Key j, const Value* const pointer) {
try {
// value returns a const ValueType&, and the return makes a copy !!!!!
return dynamic_cast<const GenericValue<ValueType>&>(*pointer).value();
} catch (std::bad_cast&) {
throw ValuesIncorrectType(j, typeid(*pointer), typeid(ValueType));
}
}
};

template <typename MatrixType, bool isDynamic>
struct handle_matrix;

// Handle dynamic matrices
template <int M, int N>
struct handle_matrix<Eigen::Matrix<double, M, N>, true> {
Eigen::Matrix<double, M, N> operator()(Key j, const Value* const pointer) {
try {
// value returns a const Matrix&, and the return makes a copy !!!!!
return dynamic_cast<const GenericValue<Eigen::Matrix<double, M, N>>&>(*pointer).value();
} catch (std::bad_cast&) {
// If a fixed matrix was stored, we end up here as well.
throw ValuesIncorrectType(j, typeid(*pointer), typeid(Eigen::Matrix<double, M, N>));
}
}
};

// Handle fixed matrices
template <int M, int N>
struct handle_matrix<Eigen::Matrix<double, M, N>, false> {
Eigen::Matrix<double, M, N> operator()(Key j, const Value* const pointer) {
try {
// value returns a const MatrixMN&, and the return makes a copy !!!!!
return dynamic_cast<const GenericValue<Eigen::Matrix<double, M, N>>&>(*pointer).value();
} catch (std::bad_cast&) {
Matrix A;
try {
// Check if a dynamic matrix was stored
A = handle_matrix<Eigen::MatrixXd, true>()(j, pointer); // will throw if not....
} catch (const ValuesIncorrectType&) {
// Or a dynamic vector
A = handle_matrix<Eigen::VectorXd, true>()(j, pointer); // will throw if not....
}
// Yes: check size, and throw if not a match
if (A.rows() != M || A.cols() != N)
throw NoMatchFoundForFixed(M, N, A.rows(), A.cols());
else
return A; // copy but not malloc
}
}
};

// Handle matrices
template <int M, int N>
struct handle<Eigen::Matrix<double, M, N>> {
Eigen::Matrix<double, M, N> operator()(Key j, const Value* const pointer) {
return handle_matrix<Eigen::Matrix<double, M, N>,
(M == Eigen::Dynamic || N == Eigen::Dynamic)>()(j, pointer);
}
};

} // internal

/* ************************************************************************* */
template <typename ValueType>
const ValueType Values::at(Key j) const {
// Find the item
KeyValueMap::const_iterator item = values_.find(j);

// Throw exception if it does not exist
if (item == values_.end()) throw ValuesKeyDoesNotExist("at", j);

// Check the type and throw exception if incorrect
// h() split in two lines to avoid internal compiler error (MSVC2017)
auto h = internal::handle<ValueType>();
return h(j, item->second);
template<>
inline bool Values::filterHelper<Value>(const boost::function<bool(Key)> filter,
const ConstKeyValuePair& key_value) {
// Filter and check the type
return filter(key_value.key);
}

/* ************************************************************************* */

namespace internal {

// Check the type and throw exception if incorrect
// Generic version, partially specialized below for various Eigen Matrix types
template <typename ValueType>
struct handle {
ValueType operator()(Key j, const Value* const pointer) {
try {
// value returns a const ValueType&, and the return makes a copy !!!!!
return dynamic_cast<const GenericValue<ValueType>&>(*pointer).value();
} catch (std::bad_cast&) {
throw ValuesIncorrectType(j, typeid(*pointer), typeid(ValueType));
}
}
};

template <typename MatrixType, bool isDynamic>
struct handle_matrix;

// Handle dynamic matrices
template <int M, int N>
struct handle_matrix<Eigen::Matrix<double, M, N>, true> {
Eigen::Matrix<double, M, N> operator()(Key j, const Value* const pointer) {
try {
// value returns a const Matrix&, and the return makes a copy !!!!!
return dynamic_cast<const GenericValue<Eigen::Matrix<double, M, N>>&>(*pointer).value();
} catch (std::bad_cast&) {
// If a fixed matrix was stored, we end up here as well.
throw ValuesIncorrectType(j, typeid(*pointer), typeid(Eigen::Matrix<double, M, N>));
}
}
};

// Handle fixed matrices
template <int M, int N>
struct handle_matrix<Eigen::Matrix<double, M, N>, false> {
Eigen::Matrix<double, M, N> operator()(Key j, const Value* const pointer) {
try {
// value returns a const MatrixMN&, and the return makes a copy !!!!!
return dynamic_cast<const GenericValue<Eigen::Matrix<double, M, N>>&>(*pointer).value();
} catch (std::bad_cast&) {
Matrix A;
try {
// Check if a dynamic matrix was stored
A = handle_matrix<Eigen::MatrixXd, true>()(j, pointer); // will throw if not....
} catch (const ValuesIncorrectType&) {
// Or a dynamic vector
A = handle_matrix<Eigen::VectorXd, true>()(j, pointer); // will throw if not....
}
// Yes: check size, and throw if not a match
if (A.rows() != M || A.cols() != N)
throw NoMatchFoundForFixed(M, N, A.rows(), A.cols());
else
return A; // copy but not malloc
}
}
};

// Handle matrices
template <int M, int N>
struct handle<Eigen::Matrix<double, M, N>> {
Eigen::Matrix<double, M, N> operator()(Key j, const Value* const pointer) {
return handle_matrix<Eigen::Matrix<double, M, N>,
(M == Eigen::Dynamic || N == Eigen::Dynamic)>()(j, pointer);
}
};

} // internal

/* *************************************************************************
*/
template <typename ValueType>
const ValueType Values::at(Key j) const {
// Find the item
KeyValueMap::const_iterator item = values_.find(j);

// Throw exception if it does not exist
if (item == values_.end()) throw ValuesKeyDoesNotExist("at", j);

// Check the type and throw exception if incorrect
// h() split in two lines to avoid internal compiler error (MSVC2017)
auto h = internal::handle<ValueType>();
return h(j, item->second);
}

/* ************************************************************************* */
Expand Down

0 comments on commit 5749565

Please sign in to comment.