Streaming for LabeledSymbols

release/4.3a0
Frank Dellaert 2019-06-14 15:31:23 -04:00
parent 4e2713026c
commit 86b4be3304
3 changed files with 37 additions and 2 deletions

View File

@ -121,6 +121,13 @@ boost::function<bool(gtsam::Key)> 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

View File

@ -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 */

View File

@ -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);
}
/* ************************************************************************* */