diff --git a/gtsam/inference/Symbol.cpp b/gtsam/inference/Symbol.cpp index ccabcb07e..5e41b3eac 100644 --- a/gtsam/inference/Symbol.cpp +++ b/gtsam/inference/Symbol.cpp @@ -66,5 +66,10 @@ boost::function Symbol::ChrTest(unsigned char c) { return bind(&Symbol::chr, bind(make, _1)) == c; } +std::ostream &operator<<(std::ostream &os, const Symbol &symbol) { + os << StreamedKey(symbol); + return os; +} + } // namespace gtsam diff --git a/gtsam/inference/Symbol.h b/gtsam/inference/Symbol.h index 8e22202ed..86574f70d 100644 --- a/gtsam/inference/Symbol.h +++ b/gtsam/inference/Symbol.h @@ -112,6 +112,9 @@ public: */ static boost::function ChrTest(unsigned char c); + /// Output stream operator that can be used with key_formatter (see Key.h). + friend std::ostream &operator<<(std::ostream &, const Symbol &); + private: /** Serialization function */ diff --git a/gtsam/inference/tests/testSymbol.cpp b/gtsam/inference/tests/testSymbol.cpp new file mode 100644 index 000000000..43a0e219a --- /dev/null +++ b/gtsam/inference/tests/testSymbol.cpp @@ -0,0 +1,50 @@ +/* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + +/* + * @file testSymbol.cpp + * @author Frank Dellaert + */ + +#include + +#include + +using namespace std; +using namespace gtsam; + +/* ************************************************************************* */ +// A custom (nonsensical) formatter. +string myFormatter(Key key) { + return "special"; +} + +TEST(Symbol, Formatting) { + Symbol symbol('c', 3); + + // use key_formatter with a function pointer + stringstream ss2; + ss2 << key_formatter(myFormatter) << symbol; + EXPECT("special" == ss2.str()); + + // use key_formatter with a function object. + stringstream ss3; + ss3 << key_formatter(MultiRobotKeyFormatter) << symbol; + EXPECT("c3" == ss3.str()); +} + +/* ************************************************************************* */ +int main() { + TestResult tr; + return TestRegistry::runAllTests(tr); +} +/* ************************************************************************* */ +