using static_cast in GenericValue's virtual functions

should be more efficient, but perhaps will crash instead of throwing an exception when the Value& derived class doesn't match.
release/4.3a0
Mike Bosse 2014-10-25 22:23:26 +02:00
parent c2cdd21a7a
commit 9ef8980362
2 changed files with 7 additions and 5 deletions

View File

@ -12,8 +12,8 @@
/* /*
* @file GenericValue.h * @file GenericValue.h
* @date Jan 26, 2012 * @date Jan 26, 2012
* @author Duy Nguyen Ta * @author Michael Bosse, Abel Gawel, Renaud Dube
* @author Mike Bosse, Abel Gawel, Renaud Dube * based on DrivedValue.h by Duy Nguyen Ta
*/ */
#pragma once #pragma once
@ -117,7 +117,7 @@ public:
/// equals implementing generic Value interface /// equals implementing generic Value interface
virtual bool equals_(const Value& p, double tol = 1e-9) const { virtual bool equals_(const Value& p, double tol = 1e-9) const {
// Cast the base class Value pointer to a templated generic class pointer // Cast the base class Value pointer to a templated generic class pointer
const This& genericValue2 = dynamic_cast<const This&>(p); const This& genericValue2 = static_cast<const This&>(p);
// Return the result of using the equals traits for the derived class // Return the result of using the equals traits for the derived class
return traits::equals<T>(this->value_, genericValue2.value_, tol); return traits::equals<T>(this->value_, genericValue2.value_, tol);
@ -140,7 +140,7 @@ public:
/// Generic Value interface version of localCoordinates /// Generic Value interface version of localCoordinates
virtual Vector localCoordinates_(const Value& value2) const { virtual Vector localCoordinates_(const Value& value2) const {
// Cast the base class Value pointer to a templated generic class pointer // Cast the base class Value pointer to a templated generic class pointer
const This& genericValue2 = dynamic_cast<const This&>(value2); const This& genericValue2 = static_cast<const This&>(value2);
// Return the result of calling localCoordinates trait on the derived class // Return the result of calling localCoordinates trait on the derived class
return traits::localCoordinates<T>(value_,genericValue2.value_); return traits::localCoordinates<T>(value_,genericValue2.value_);
@ -156,7 +156,7 @@ public:
/// Assignment operator /// Assignment operator
virtual Value& operator=(const Value& rhs) { virtual Value& operator=(const Value& rhs) {
// Cast the base class Value pointer to a derived class pointer // Cast the base class Value pointer to a derived class pointer
const This& derivedRhs = dynamic_cast<const This&>(rhs); const This& derivedRhs = static_cast<const This&>(rhs);
// Do the assignment and return the result // Do the assignment and return the result
this->value_ = derivedRhs.value_; this->value_ = derivedRhs.value_;

View File

@ -354,10 +354,12 @@ TEST(Values, filter) {
if(i == 0) { if(i == 0) {
LONGS_EQUAL(2, (long)key_value.key); LONGS_EQUAL(2, (long)key_value.key);
try {key_value.value.cast<Pose2>();} catch (const std::bad_cast& e) { FAIL("can't cast Value to Pose2");} try {key_value.value.cast<Pose2>();} catch (const std::bad_cast& e) { FAIL("can't cast Value to Pose2");}
THROWS_EXCEPTION(key_value.value.cast<Pose3>());
EXPECT(assert_equal(pose2, key_value.value.cast<Pose2>())); EXPECT(assert_equal(pose2, key_value.value.cast<Pose2>()));
} else if(i == 1) { } else if(i == 1) {
LONGS_EQUAL(3, (long)key_value.key); LONGS_EQUAL(3, (long)key_value.key);
try {key_value.value.cast<Pose3>();} catch (const std::bad_cast& e) { FAIL("can't cast Value to Pose3");} try {key_value.value.cast<Pose3>();} catch (const std::bad_cast& e) { FAIL("can't cast Value to Pose3");}
THROWS_EXCEPTION(key_value.value.cast<Pose2>());
EXPECT(assert_equal(pose3, key_value.value.cast<Pose3>())); EXPECT(assert_equal(pose3, key_value.value.cast<Pose3>()));
} else { } else {
EXPECT(false); EXPECT(false);