From 9e26b32daa00f4181167f50ba7fa2180a01870f2 Mon Sep 17 00:00:00 2001 From: Alex Cunningham Date: Sat, 9 Jun 2012 19:43:14 +0000 Subject: [PATCH] Fixed Symbol problem - related to using std::numeric_limits::max() statically on keys created outside of functions --- .cproject | 8 ++++ gtsam/nonlinear/Symbol.cpp | 65 ++++++++++++++++--------------- gtsam/nonlinear/tests/testKey.cpp | 4 +- 3 files changed, 43 insertions(+), 34 deletions(-) diff --git a/.cproject b/.cproject index af740032b..24c4afa85 100644 --- a/.cproject +++ b/.cproject @@ -703,6 +703,14 @@ true true + + make + -j5 + testKey.run + true + true + true + make -j5 diff --git a/gtsam/nonlinear/Symbol.cpp b/gtsam/nonlinear/Symbol.cpp index a161e2fd7..19cf78eb5 100644 --- a/gtsam/nonlinear/Symbol.cpp +++ b/gtsam/nonlinear/Symbol.cpp @@ -16,8 +16,6 @@ * @author: Richard Roberts */ -#pragma once - #include #include @@ -27,47 +25,50 @@ #include #include +#include #include #include namespace gtsam { - static const size_t keyBits = sizeof(Key) * 8; - static const size_t chrBits = sizeof(unsigned char) * 8; - static const size_t indexBits = keyBits - chrBits; - static const Key chrMask = Key(std::numeric_limits::max()) - << indexBits; - static const Key indexMask = ~chrMask; +static const size_t keyBits = sizeof(Key) * 8; +static const size_t chrBits = sizeof(unsigned char) * 8; +static const size_t indexBits = keyBits - chrBits; +static const Key chrMask = Key(UCHAR_MAX) << indexBits; // For some reason, std::numeric_limits::max() fails +static const Key indexMask = ~chrMask; - Symbol::Symbol(Key key) { - c_ = (unsigned char) ((key & chrMask) >> indexBits); - j_ = key & indexMask; - } +Symbol::Symbol(Key key) { + c_ = (unsigned char) ((key & chrMask) >> indexBits); + j_ = key & indexMask; +} - Key Symbol::key() const { - if (j_ > indexMask) - throw std::invalid_argument("Symbol index is too large"); - Key key = (Key(c_) << indexBits) | j_; - return key; - } +Key Symbol::key() const { + if (j_ > indexMask) { + boost::format msg("Symbol index is too large, j=%d, indexMask=%d"); + msg % j_ % indexMask; + throw std::invalid_argument(msg.str()); + } + Key key = (Key(c_) << indexBits) | j_; + return key; +} - void Symbol::print(const std::string& s) const { - std::cout << s << (std::string) (*this) << std::endl; - } +void Symbol::print(const std::string& s) const { + std::cout << s << (std::string) (*this) << std::endl; +} - bool Symbol::equals(const Symbol& expected, double tol) const { - return (*this) == expected; - } +bool Symbol::equals(const Symbol& expected, double tol) const { + return (*this) == expected; +} - Symbol::operator std::string() const { - return str(boost::format("%c%d") % c_ % j_); - } +Symbol::operator std::string() const { + return str(boost::format("%c%d") % c_ % j_); +} - boost::function Symbol::ChrTest(unsigned char c) { - namespace bl = boost::lambda; - return bl::bind(&Symbol::chr, bl::bind(bl::constructor(), bl::_1)) - == c; - } +boost::function Symbol::ChrTest(unsigned char c) { + namespace bl = boost::lambda; + return bl::bind(&Symbol::chr, bl::bind(bl::constructor(), bl::_1)) + == c; +} } // namespace gtsam diff --git a/gtsam/nonlinear/tests/testKey.cpp b/gtsam/nonlinear/tests/testKey.cpp index 601176935..ee9734af5 100644 --- a/gtsam/nonlinear/tests/testKey.cpp +++ b/gtsam/nonlinear/tests/testKey.cpp @@ -21,11 +21,11 @@ using namespace boost::assign; #include #include #include - + using namespace std; using namespace gtsam; -Key aKey = gtsam::symbol_shorthand::X(4); // FIXME: throws index too large exception in Symbol.key() +Key aKey = gtsam::symbol_shorthand::X(4); /* ************************************************************************* */ TEST(Key, KeySymbolConversion) {