Improve and simplify code with modern C++ features
Change-Id: I28d84df3087492ab2ecbeb91169a2cde12c9e31e
diff --git a/src/meta-info.cpp b/src/meta-info.cpp
index c6a43a4..f46784c 100644
--- a/src/meta-info.cpp
+++ b/src/meta-info.cpp
@@ -88,10 +88,9 @@
MetaInfo&
MetaInfo::setAppMetaInfo(const std::list<Block>& info)
{
- for (std::list<Block>::const_iterator i = info.begin(); i != info.end(); ++i) {
- if (!(128 <= i->type() && i->type() <= 252))
- BOOST_THROW_EXCEPTION(Error("AppMetaInfo block has type outside the application range "
- "[128, 252]"));
+ for (const auto& block : info) {
+ if (block.type() < 128 || block.type() > 252)
+ BOOST_THROW_EXCEPTION(Error("AppMetaInfo block has type outside the application range [128, 252]"));
}
m_wire.reset();
@@ -114,11 +113,10 @@
bool
MetaInfo::removeAppMetaInfo(uint32_t tlvType)
{
- for (std::list<Block>::iterator iter = m_appMetaInfo.begin();
- iter != m_appMetaInfo.end(); ++iter) {
- if (iter->type() == tlvType) {
+ for (auto it = m_appMetaInfo.begin(); it != m_appMetaInfo.end(); ++it) {
+ if (it->type() == tlvType) {
m_wire.reset();
- m_appMetaInfo.erase(iter);
+ m_appMetaInfo.erase(it);
return true;
}
}
@@ -128,13 +126,9 @@
const Block*
MetaInfo::findAppMetaInfo(uint32_t tlvType) const
{
- for (std::list<Block>::const_iterator iter = m_appMetaInfo.begin();
- iter != m_appMetaInfo.end(); ++iter) {
- if (iter->type() == tlvType) {
- return &*iter;
- }
- }
- return 0;
+ auto it = std::find_if(m_appMetaInfo.begin(), m_appMetaInfo.end(),
+ [=] (const Block& b) { return b.type() == tlvType; });
+ return it != m_appMetaInfo.end() ? &*it : nullptr;
}
template<encoding::Tag TAG>
@@ -149,9 +143,8 @@
size_t totalLength = 0;
- for (std::list<Block>::const_reverse_iterator appMetaInfoItem = m_appMetaInfo.rbegin();
- appMetaInfoItem != m_appMetaInfo.rend(); ++appMetaInfoItem) {
- totalLength += encoder.prependBlock(*appMetaInfoItem);
+ for (auto it = m_appMetaInfo.rbegin(); it != m_appMetaInfo.rend(); ++it) {
+ totalLength += encoder.prependBlock(*it);
}
// FinalBlockId
@@ -161,7 +154,8 @@
// FreshnessPeriod
if (m_freshnessPeriod != DEFAULT_FRESHNESS_PERIOD) {
- totalLength += prependNonNegativeIntegerBlock(encoder, tlv::FreshnessPeriod, m_freshnessPeriod.count());
+ totalLength += prependNonNegativeIntegerBlock(encoder, tlv::FreshnessPeriod,
+ static_cast<uint64_t>(m_freshnessPeriod.count()));
}
// ContentType
@@ -204,8 +198,7 @@
// FinalBlockId?
// AppMetaInfo*
-
- Block::element_const_iterator val = m_wire.elements_begin();
+ auto val = m_wire.elements_begin();
// ContentType
if (val != m_wire.elements_end() && val->type() == tlv::ContentType) {
@@ -247,7 +240,7 @@
os << "ContentType: " << info.getType();
// FreshnessPeriod
- if (info.getFreshnessPeriod() > time::milliseconds::zero()) {
+ if (info.getFreshnessPeriod() > 0_ms) {
os << ", FreshnessPeriod: " << info.getFreshnessPeriod();
}
@@ -258,9 +251,8 @@
}
// App-defined MetaInfo items
- for (std::list<Block>::const_iterator iter = info.getAppMetaInfo().begin();
- iter != info.getAppMetaInfo().end(); ++iter) {
- os << ", AppMetaInfoTlvType: " << iter->type();
+ for (const auto& block : info.getAppMetaInfo()) {
+ os << ", AppMetaInfoTlvType: " << block.type();
}
return os;