diff --git a/gtsam/inference/LabeledSymbol.cpp b/gtsam/inference/LabeledSymbol.cpp index 0d9c35d2c..6d34883fa 100644 --- a/gtsam/inference/LabeledSymbol.cpp +++ b/gtsam/inference/LabeledSymbol.cpp @@ -121,6 +121,13 @@ boost::function LabeledSymbol::TypeLabelTest(unsigned char c, return boost::bind(&LabeledSymbol::chr, boost::bind(make, _1)) == c && boost::bind(&LabeledSymbol::label, boost::bind(make, _1)) == label; } + +/* ************************************************************************* */ +std::ostream &operator<<(std::ostream &os, const LabeledSymbol &symbol) { + os << StreamedKey(symbol); + return os; +} + /* ************************************************************************* */ } // \namespace gtsam diff --git a/gtsam/inference/LabeledSymbol.h b/gtsam/inference/LabeledSymbol.h index 8c521b067..5b3ec8766 100644 --- a/gtsam/inference/LabeledSymbol.h +++ b/gtsam/inference/LabeledSymbol.h @@ -100,10 +100,15 @@ public: LabeledSymbol upper() const { return LabeledSymbol(c_, toupper(label_), j_); } LabeledSymbol lower() const { return LabeledSymbol(c_, tolower(label_), j_); } - // Create a new symbol with a different value + // Create a new symbol with a different character. LabeledSymbol newChr(unsigned char c) const { return LabeledSymbol(c, label_, j_); } + + // Create a new symbol with a different label. LabeledSymbol newLabel(unsigned char label) const { return LabeledSymbol(c_, label, j_); } + /// Output stream operator that can be used with key_formatter (see Key.h). + friend std::ostream &operator<<(std::ostream &, const LabeledSymbol &); + private: /** Serialization function */ diff --git a/gtsam/inference/tests/testLabeledSymbol.cpp b/gtsam/inference/tests/testLabeledSymbol.cpp index b463f4131..2a56b39c2 100644 --- a/gtsam/inference/tests/testLabeledSymbol.cpp +++ b/gtsam/inference/tests/testLabeledSymbol.cpp @@ -80,6 +80,29 @@ TEST(LabeledSymbol, ChrTest) { } /* ************************************************************************* */ -int main() { TestResult tr; return TestRegistry::runAllTests(tr); } +// A custom (nonsensical) formatter. +string myFormatter(Key key) { + return "special"; +} + +TEST(LabeledSymbol, Formatting) { + LabeledSymbol symbol('c', 'A', 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("cA3" == ss3.str()); +} + +/* ************************************************************************* */ +int main() { + TestResult tr; + return TestRegistry::runAllTests(tr); +} /* ************************************************************************* */