Fixed Symbol problem - related to using std::numeric_limits::max() statically on keys created outside of functions

release/4.3a0
Alex Cunningham 2012-06-09 19:43:14 +00:00
parent f45c236585
commit 9e26b32daa
3 changed files with 43 additions and 34 deletions

View File

@ -703,6 +703,14 @@
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
<runAllBuilders>true</runAllBuilders> <runAllBuilders>true</runAllBuilders>
</target> </target>
<target name="testKey.run" path="build/gtsam/nonlinear" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments>-j5</buildArguments>
<buildTarget>testKey.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
<runAllBuilders>true</runAllBuilders>
</target>
<target name="schedulingExample.run" path="build/gtsam_unstable/discrete" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="schedulingExample.run" path="build/gtsam_unstable/discrete" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments>-j5</buildArguments> <buildArguments>-j5</buildArguments>

View File

@ -16,8 +16,6 @@
* @author: Richard Roberts * @author: Richard Roberts
*/ */
#pragma once
#include <gtsam/nonlinear/Symbol.h> #include <gtsam/nonlinear/Symbol.h>
#include <boost/mpl/char.hpp> #include <boost/mpl/char.hpp>
@ -27,47 +25,50 @@
#include <boost/lambda/construct.hpp> #include <boost/lambda/construct.hpp>
#include <boost/lambda/lambda.hpp> #include <boost/lambda/lambda.hpp>
#include <limits.h>
#include <list> #include <list>
#include <iostream> #include <iostream>
namespace gtsam { namespace gtsam {
static const size_t keyBits = sizeof(Key) * 8; static const size_t keyBits = sizeof(Key) * 8;
static const size_t chrBits = sizeof(unsigned char) * 8; static const size_t chrBits = sizeof(unsigned char) * 8;
static const size_t indexBits = keyBits - chrBits; static const size_t indexBits = keyBits - chrBits;
static const Key chrMask = Key(std::numeric_limits<unsigned char>::max()) static const Key chrMask = Key(UCHAR_MAX) << indexBits; // For some reason, std::numeric_limits<unsigned char>::max() fails
<< indexBits; static const Key indexMask = ~chrMask;
static const Key indexMask = ~chrMask;
Symbol::Symbol(Key key) { Symbol::Symbol(Key key) {
c_ = (unsigned char) ((key & chrMask) >> indexBits); c_ = (unsigned char) ((key & chrMask) >> indexBits);
j_ = key & indexMask; j_ = key & indexMask;
} }
Key Symbol::key() const { Key Symbol::key() const {
if (j_ > indexMask) if (j_ > indexMask) {
throw std::invalid_argument("Symbol index is too large"); boost::format msg("Symbol index is too large, j=%d, indexMask=%d");
Key key = (Key(c_) << indexBits) | j_; msg % j_ % indexMask;
return key; throw std::invalid_argument(msg.str());
} }
Key key = (Key(c_) << indexBits) | j_;
return key;
}
void Symbol::print(const std::string& s) const { void Symbol::print(const std::string& s) const {
std::cout << s << (std::string) (*this) << std::endl; std::cout << s << (std::string) (*this) << std::endl;
} }
bool Symbol::equals(const Symbol& expected, double tol) const { bool Symbol::equals(const Symbol& expected, double tol) const {
return (*this) == expected; return (*this) == expected;
} }
Symbol::operator std::string() const { Symbol::operator std::string() const {
return str(boost::format("%c%d") % c_ % j_); return str(boost::format("%c%d") % c_ % j_);
} }
boost::function<bool(Key)> Symbol::ChrTest(unsigned char c) { boost::function<bool(Key)> Symbol::ChrTest(unsigned char c) {
namespace bl = boost::lambda; namespace bl = boost::lambda;
return bl::bind(&Symbol::chr, bl::bind(bl::constructor<Symbol>(), bl::_1)) return bl::bind(&Symbol::chr, bl::bind(bl::constructor<Symbol>(), bl::_1))
== c; == c;
} }
} // namespace gtsam } // namespace gtsam

View File

@ -21,11 +21,11 @@ using namespace boost::assign;
#include <gtsam/base/Testable.h> #include <gtsam/base/Testable.h>
#include <gtsam/base/TestableAssertions.h> #include <gtsam/base/TestableAssertions.h>
#include <gtsam/nonlinear/Symbol.h> #include <gtsam/nonlinear/Symbol.h>
using namespace std; using namespace std;
using namespace gtsam; 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) { TEST(Key, KeySymbolConversion) {