tools: simplify on/off attribute printing
refs #4004
Change-Id: Idb666ff4dbeddc37f1cb10710ba3d75f7fe8bdb5
diff --git a/tests/tools/nfdc/format-helpers.t.cpp b/tests/tools/nfdc/format-helpers.t.cpp
index a94d2ba..5de002b 100644
--- a/tests/tools/nfdc/format-helpers.t.cpp
+++ b/tests/tools/nfdc/format-helpers.t.cpp
@@ -118,6 +118,14 @@
BOOST_CHECK(os.is_equal(" id=500\nuri=udp4://192.0.2.1:6363\n"));
}
+BOOST_AUTO_TEST_CASE(OnOff)
+{
+ output_test_stream os;
+ os << 'A' << text::OnOff{true} << 'B' << text::OnOff{false} << 'C';
+
+ BOOST_CHECK(os.is_equal("AonBoffC"));
+}
+
BOOST_AUTO_TEST_SUITE_END() // Text
BOOST_AUTO_TEST_SUITE_END() // TestFormatHelpers
diff --git a/tools/nfdc/face-module.cpp b/tools/nfdc/face-module.cpp
index 93a6563..85673a8 100644
--- a/tools/nfdc/face-module.cpp
+++ b/tools/nfdc/face-module.cpp
@@ -509,8 +509,8 @@
void
FaceModule::printFaceParams(std::ostream& os, text::ItemAttributes& ia, const ControlParameters& resp)
{
- os << ia("reliability") << (resp.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED) ? "on" : "off")
- << ia("congestion-marking") << (resp.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED) ? "on" : "off");
+ os << ia("reliability") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)}
+ << ia("congestion-marking") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)};
if (resp.hasBaseCongestionMarkingInterval()) {
os << ia("congestion-marking-interval")
<< text::formatDuration<time::milliseconds>(resp.getBaseCongestionMarkingInterval());
diff --git a/tools/nfdc/format-helpers.cpp b/tools/nfdc/format-helpers.cpp
index 6d5a983..a329102 100644
--- a/tools/nfdc/format-helpers.cpp
+++ b/tools/nfdc/format-helpers.cpp
@@ -177,6 +177,12 @@
return os << attr.attribute << '=';
}
+std::ostream&
+operator<<(std::ostream& os, OnOff v)
+{
+ return os << (v.flag ? "on" : "off");
+}
+
std::string
formatTimestamp(time::system_clock::TimePoint t)
{
diff --git a/tools/nfdc/format-helpers.hpp b/tools/nfdc/format-helpers.hpp
index c0f8fd1..0d131f1 100644
--- a/tools/nfdc/format-helpers.hpp
+++ b/tools/nfdc/format-helpers.hpp
@@ -165,6 +165,16 @@
std::ostream&
operator<<(std::ostream& os, const ItemAttributes::Attribute& attr);
+/** \brief print boolean as 'on' or 'off'
+ */
+struct OnOff
+{
+ bool flag;
+};
+
+std::ostream&
+operator<<(std::ostream& os, OnOff v);
+
namespace detail {
template<typename DurationT>