src: Improving consistency and correcting code style
As of this commit, all data structures can be directly constructed from
wire format.
This commit excludes full correction of code style in security/ and
tools/ndnsec*, which will be part of a different commit.
Change-Id: I121ac1f81948bc7468990df52cdefeb2988d91a1
Refs: #1403
diff --git a/src/management/ndnd-controller.cpp b/src/management/ndnd-controller.cpp
index a283826..32a19cd 100644
--- a/src/management/ndnd-controller.cpp
+++ b/src/management/ndnd-controller.cpp
@@ -197,7 +197,7 @@
Block::element_const_iterator val = content.elements_begin();
- switch(val->type())
+ switch (val->type())
{
case tlv::ndnd::FaceInstance:
{
@@ -243,7 +243,7 @@
Block::element_const_iterator val = content.elements_begin();
- switch(val->type())
+ switch (val->type())
{
case tlv::ndnd::ForwardingEntry:
{
diff --git a/src/management/ndnd-face-instance.hpp b/src/management/ndnd-face-instance.hpp
index 60c07dc..530f10d 100644
--- a/src/management/ndnd-face-instance.hpp
+++ b/src/management/ndnd-face-instance.hpp
@@ -19,12 +19,12 @@
*/
class FaceInstance {
public:
- FaceInstance(const std::string &action,
+ FaceInstance(const std::string& action,
int64_t faceId,
uint32_t ipProto,
- const std::string &host,
- const std::string &port,
- const std::string &multicastInterface,
+ const std::string& host,
+ const std::string& port,
+ const std::string& multicastInterface,
uint32_t multicastTtl,
const time::milliseconds& freshnessPeriod)
: action_(action)
@@ -46,6 +46,15 @@
{
}
+ /**
+ * @brief Create from wire encoding
+ */
+ explicit
+ FaceInstance(const Block& wire)
+ {
+ wireDecode(wire);
+ }
+
// Action
const std::string&
getAction() const { return action_; }
@@ -79,7 +88,7 @@
getPort() const { return port_; }
void
- setPort(const std::string &port) { port_ = port; wire_.reset(); }
+ setPort(const std::string& port) { port_ = port; wire_.reset(); }
// MulticastInterface
const std::string&
@@ -113,7 +122,7 @@
wireEncode() const;
inline void
- wireDecode(const Block &wire);
+ wireDecode(const Block& wire);
private:
std::string action_;
@@ -207,7 +216,7 @@
}
inline void
-FaceInstance::wireDecode(const Block &wire)
+FaceInstance::wireDecode(const Block& wire)
{
action_.clear();
faceId_ = -1;
@@ -289,7 +298,7 @@
}
inline std::ostream&
-operator << (std::ostream &os, const FaceInstance &entry)
+operator << (std::ostream& os, const FaceInstance& entry)
{
os << "FaceInstance(";
diff --git a/src/management/ndnd-forwarding-entry.hpp b/src/management/ndnd-forwarding-entry.hpp
index 03272b1..a084964 100644
--- a/src/management/ndnd-forwarding-entry.hpp
+++ b/src/management/ndnd-forwarding-entry.hpp
@@ -40,6 +40,15 @@
{
}
+ /**
+ * @brief Create from wire encoding
+ */
+ explicit
+ ForwardingEntry(const Block& wire)
+ {
+ wireDecode(wire);
+ }
+
const std::string&
getAction() const { return action_; }
@@ -50,7 +59,7 @@
getPrefix() const { return prefix_; }
void
- setPrefix(const Name &prefix) { prefix_ = prefix; wire_.reset(); }
+ setPrefix(const Name& prefix) { prefix_ = prefix; wire_.reset(); }
int
getFaceId() const { return faceId_; }
@@ -74,7 +83,7 @@
wireEncode() const;
inline void
- wireDecode(const Block &wire);
+ wireDecode(const Block& wire);
private:
std::string action_; /**< empty for none. */
@@ -135,7 +144,7 @@
}
inline void
-ForwardingEntry::wireDecode(const Block &wire)
+ForwardingEntry::wireDecode(const Block& wire)
{
action_.clear();
prefix_.clear();
@@ -190,7 +199,7 @@
}
inline std::ostream&
-operator << (std::ostream &os, const ForwardingEntry &entry)
+operator << (std::ostream& os, const ForwardingEntry& entry)
{
os << "ForwardingEntry(";
diff --git a/src/management/ndnd-forwarding-flags.hpp b/src/management/ndnd-forwarding-flags.hpp
index f238d06..803dabc 100644
--- a/src/management/ndnd-forwarding-flags.hpp
+++ b/src/management/ndnd-forwarding-flags.hpp
@@ -24,7 +24,7 @@
/**
* Create a new ForwardingFlags with "active" and "childInherit" set and all other flags cleared.
*/
- ForwardingFlags()
+ ForwardingFlags()
: active_(true)
, childInherit_(true)
, advertise_(false)
@@ -37,47 +37,56 @@
}
/**
+ * @brief Create from wire encoding
+ */
+ explicit
+ ForwardingFlags(const Block& wire)
+ {
+ wireDecode(wire);
+ }
+
+ /**
* Get the value of the "active" flag.
* @return true if the flag is set, false if it is cleared.
*/
bool getActive() const { return active_; }
-
+
/**
* Get the value of the "childInherit" flag.
* @return true if the flag is set, false if it is cleared.
*/
bool getChildInherit() const { return childInherit_; }
-
+
/**
* Get the value of the "advertise" flag.
* @return true if the flag is set, false if it is cleared.
*/
bool getAdvertise() const { return advertise_; }
-
+
/**
* Get the value of the "last" flag.
* @return true if the flag is set, false if it is cleared.
*/
bool getLast() const { return last_; }
-
+
/**
* Get the value of the "capture" flag.
* @return true if the flag is set, false if it is cleared.
*/
bool getCapture() const { return capture_; }
-
+
/**
* Get the value of the "local" flag.
* @return true if the flag is set, false if it is cleared.
*/
bool getLocal() const { return local_; }
-
+
/**
* Get the value of the "tap" flag.
* @return true if the flag is set, false if it is cleared.
*/
bool getTap() const { return tap_; }
-
+
/**
* Get the value of the "captureOk" flag.
* @return true if the flag is set, false if it is cleared.
@@ -87,57 +96,57 @@
/**
* Set the value of the "active" flag
* @param active true to set the flag, false to clear it.
- */
+ */
void setActive(bool active) { this->active_ = active; wire_.reset(); }
-
+
/**
* Set the value of the "childInherit" flag
* @param childInherit true to set the flag, false to clear it.
- */
+ */
void setChildInherit(bool childInherit) { this->childInherit_ = childInherit; wire_.reset(); }
-
+
/**
* Set the value of the "advertise" flag
* @param advertise true to set the flag, false to clear it.
- */
+ */
void setAdvertise(bool advertise) { this->advertise_ = advertise; wire_.reset(); }
-
+
/**
* Set the value of the "last" flag
* @param last true to set the flag, false to clear it.
- */
+ */
void setLast(bool last) { this->last_ = last; wire_.reset(); }
-
+
/**
* Set the value of the "capture" flag
* @param capture true to set the flag, false to clear it.
- */
+ */
void setCapture(bool capture) { this->capture_ = capture; wire_.reset(); }
-
+
/**
* Set the value of the "local" flag
* @param local true to set the flag, false to clear it.
- */
+ */
void setLocal(bool local) { this->local_ = local; wire_.reset(); }
-
+
/**
* Set the value of the "tap" flag
* @param tap true to set the flag, false to clear it.
- */
+ */
void setTap(bool tap) { this->tap_ = tap; wire_.reset(); }
-
+
/**
* Set the value of the "captureOk" flag
* @param captureOk true to set the flag, false to clear it.
- */
+ */
void setCaptureOk(bool captureOk) { this->captureOk_ = captureOk; wire_.reset(); }
inline const Block&
wireEncode() const;
inline void
- wireDecode(const Block &block);
-
+ wireDecode(const Block& block);
+
private:
bool active_;
bool childInherit_;
@@ -174,19 +183,19 @@
result |= tlv::ndnd::FORW_TAP;
if (captureOk_)
result |= tlv::ndnd::FORW_CAPTURE_OK;
-
+
wire_ = nonNegativeIntegerBlock(tlv::ndnd::ForwardingFlags, result);
return wire_;
}
inline void
-ForwardingFlags::wireDecode(const Block &wire)
+ForwardingFlags::wireDecode(const Block& wire)
{
wire_ = wire;
uint32_t flags = readNonNegativeInteger(wire_);
-
+
active_ = (flags & tlv::ndnd::FORW_ACTIVE) ? true : false;
childInherit_ = (flags & tlv::ndnd::FORW_CHILD_INHERIT) ? true : false;
advertise_ = (flags & tlv::ndnd::FORW_ADVERTISE) ? true : false;
@@ -198,7 +207,7 @@
}
inline std::ostream&
-operator << (std::ostream &os, const ForwardingFlags &flags)
+operator << (std::ostream& os, const ForwardingFlags& flags)
{
if (flags.getActive())
os << "ACTIVE ";
diff --git a/src/management/ndnd-status-response.hpp b/src/management/ndnd-status-response.hpp
index e58de7e..23597b9 100644
--- a/src/management/ndnd-status-response.hpp
+++ b/src/management/ndnd-status-response.hpp
@@ -20,30 +20,39 @@
{
}
- StatusResponse(uint32_t code, const std::string &info)
+ StatusResponse(uint32_t code, const std::string& info)
: code_(code)
, info_(info)
{
}
-
+
+ /**
+ * @brief Create from wire encoding
+ */
+ explicit
+ StatusResponse(const Block& wire)
+ {
+ wireDecode(wire);
+ }
+
inline uint32_t
getCode() const;
inline void
setCode(uint32_t code);
- inline const std::string &
+ inline const std::string&
getInfo() const;
inline void
- setInfo(const std::string &info);
+ setInfo(const std::string& info);
inline const Block&
wireEncode() const;
inline void
- wireDecode(const Block &block);
-
+ wireDecode(const Block& block);
+
private:
uint32_t code_;
std::string info_;
@@ -64,14 +73,14 @@
wire_.reset();
}
-inline const std::string &
+inline const std::string&
StatusResponse::getInfo() const
{
return info_;
}
inline void
-StatusResponse::setInfo(const std::string &info)
+StatusResponse::setInfo(const std::string& info)
{
info_ = info;
wire_.reset();
@@ -93,13 +102,13 @@
wire_.push_back
(dataBlock(tlv::ndnd::StatusText, info_.c_str(), info_.size()));
}
-
- wire_.encode();
+
+ wire_.encode();
return wire_;
}
inline void
-StatusResponse::wireDecode(const Block &wire)
+StatusResponse::wireDecode(const Block& wire)
{
wire_ = wire;
wire_.parse();
@@ -114,7 +123,7 @@
}
inline std::ostream&
-operator << (std::ostream &os, const StatusResponse &status)
+operator << (std::ostream& os, const StatusResponse& status)
{
os << status.getCode() << " " << status.getInfo();
return os;
diff --git a/src/management/nfd-control-response.hpp b/src/management/nfd-control-response.hpp
index c959407..a4a5ae3 100644
--- a/src/management/nfd-control-response.hpp
+++ b/src/management/nfd-control-response.hpp
@@ -20,14 +20,22 @@
*/
class ControlResponse {
public:
- struct Error : public Tlv::Error { Error(const std::string &what) : Tlv::Error(what) {} };
+ class Error : public Tlv::Error
+ {
+ public:
+ explicit
+ Error(const std::string& what)
+ : Tlv::Error(what)
+ {
+ }
+ };
ControlResponse()
: m_code(200)
{
}
- ControlResponse(uint32_t code, const std::string &text)
+ ControlResponse(uint32_t code, const std::string& text)
: m_code(code)
, m_text(text)
{
@@ -37,31 +45,31 @@
{
wireDecode(block);
}
-
+
inline uint32_t
getCode() const;
inline void
setCode(uint32_t code);
- inline const std::string &
+ inline const std::string&
getText() const;
inline void
- setText(const std::string &text);
+ setText(const std::string& text);
inline const Block&
getBody() const;
inline void
setBody(const Block& body);
-
+
inline const Block&
wireEncode() const;
inline void
- wireDecode(const Block &block);
-
+ wireDecode(const Block& block);
+
protected:
uint32_t m_code;
std::string m_text;
@@ -83,14 +91,14 @@
m_wire.reset();
}
-inline const std::string &
+inline const std::string&
ControlResponse::getText() const
{
return m_text;
}
inline void
-ControlResponse::setText(const std::string &text)
+ControlResponse::setText(const std::string& text)
{
m_text = text;
m_wire.reset();
@@ -128,27 +136,27 @@
{
m_wire.push_back(m_body);
}
-
- m_wire.encode();
+
+ m_wire.encode();
return m_wire;
}
inline void
-ControlResponse::wireDecode(const Block &wire)
+ControlResponse::wireDecode(const Block& wire)
{
m_wire = wire;
m_wire.parse();
if (m_wire.type() != tlv::nfd::ControlResponse)
- throw Error("Requested decoding of ControlResponse, but Block is of different type");
-
+ throw Error("Requested decoding of ControlResponse, but Block is of different type");
+
Block::element_const_iterator val = m_wire.elements_begin();
if (val == m_wire.elements_end() ||
val->type() != tlv::nfd::StatusCode)
{
throw Error("Incorrect ControlResponse format (StatusCode missing or not the first item)");
}
-
+
m_code = readNonNegativeInteger(*val);
++val;
@@ -167,7 +175,7 @@
}
inline std::ostream&
-operator << (std::ostream &os, const ControlResponse &status)
+operator << (std::ostream& os, const ControlResponse& status)
{
os << status.getCode() << " " << status.getText();
return os;
diff --git a/src/management/nfd-local-control-header.hpp b/src/management/nfd-local-control-header.hpp
index d09fe8a..9bbdebf 100644
--- a/src/management/nfd-local-control-header.hpp
+++ b/src/management/nfd-local-control-header.hpp
@@ -16,7 +16,15 @@
class LocalControlHeader
{
public:
- struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
+ class Error : public std::runtime_error
+ {
+ public:
+ explicit
+ Error(const std::string& what)
+ : std::runtime_error(what)
+ {
+ }
+ };
LocalControlHeader()
: m_incomingFaceId(INVALID_FACE_ID)
@@ -25,6 +33,18 @@
}
/**
+ * @brief Create from wire encoding
+ *
+ * @sa wireDecode
+ */
+ explicit
+ LocalControlHeader(const Block& wire,
+ bool encodeIncomingFaceId = true, bool encodeNextHopFaceId = true)
+ {
+ wireDecode(wire, encodeIncomingFaceId, encodeNextHopFaceId);
+ }
+
+ /**
* @brief Create wire encoding with options LocalControlHeader and the supplied item
*
* The caller is responsible of checking whether LocalControlHeader contains
@@ -47,20 +67,20 @@
inline Block
wireEncode(const U& payload,
bool encodeIncomingFaceId, bool encodeNextHopFaceId) const;
-
+
/**
* @brief Decode from the wire format and set LocalControlHeader on the supplied item
*
* The supplied wire MUST contain LocalControlHeader. Determination whether the optional
* LocalControlHeader should be done before calling this method.
*/
- inline void
+ inline void
wireDecode(const Block& wire,
bool encodeIncomingFaceId = true, bool encodeNextHopFaceId = true);
inline static const Block&
getPayload(const Block& wire);
-
+
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
@@ -72,15 +92,15 @@
return !((encodeIncomingFaceId && hasIncomingFaceId()) ||
(encodeNextHopFaceId && hasNextHopFaceId()));
}
-
+
//
-
+
bool
hasIncomingFaceId() const
{
return m_incomingFaceId != INVALID_FACE_ID;
}
-
+
uint64_t
getIncomingFaceId() const
{
@@ -100,7 +120,7 @@
{
return m_nextHopFaceId != INVALID_FACE_ID;
}
-
+
uint64_t
getNextHopFaceId() const
{
@@ -118,7 +138,7 @@
inline size_t
wireEncode(EncodingImpl<T>& block, size_t payloadSize,
bool encodeIncomingFaceId, bool encodeNextHopFaceId) const;
-
+
private:
uint64_t m_incomingFaceId;
uint64_t m_nextHopFaceId;
@@ -133,23 +153,23 @@
LocalControlHeader::wireEncode(EncodingImpl<T>& block, size_t payloadSize,
bool encodeIncomingFaceId, bool encodeNextHopFaceId) const
{
- size_t total_len = payloadSize;
+ size_t totalLength = payloadSize;
if (encodeIncomingFaceId && hasIncomingFaceId())
{
- total_len += prependNonNegativeIntegerBlock(block,
- tlv::nfd::IncomingFaceId, getIncomingFaceId());
+ totalLength += prependNonNegativeIntegerBlock(block,
+ tlv::nfd::IncomingFaceId, getIncomingFaceId());
}
if (encodeNextHopFaceId && hasNextHopFaceId())
{
- total_len += prependNonNegativeIntegerBlock(block,
- tlv::nfd::NextHopFaceId, getNextHopFaceId());
+ totalLength += prependNonNegativeIntegerBlock(block,
+ tlv::nfd::NextHopFaceId, getNextHopFaceId());
}
-
- total_len += block.prependVarNumber(total_len);
- total_len += block.prependVarNumber(tlv::nfd::LocalControlHeader);
- return total_len;
+
+ totalLength += block.prependVarNumber(totalLength);
+ totalLength += block.prependVarNumber(tlv::nfd::LocalControlHeader);
+ return totalLength;
}
template<class U>
@@ -164,7 +184,7 @@
EncodingEstimator estimator;
size_t length = wireEncode(estimator, payload.wireEncode().size(),
encodeIncomingFaceId, encodeNextHopFaceId);
-
+
EncodingBuffer buffer(length);
wireEncode(buffer, payload.wireEncode().size(),
encodeIncomingFaceId, encodeNextHopFaceId);
@@ -172,7 +192,7 @@
return buffer.block(false);
}
-inline void
+inline void
LocalControlHeader::wireDecode(const Block& wire,
bool encodeIncomingFaceId/* = true*/, bool encodeNextHopFaceId/* = true*/)
{
@@ -186,7 +206,7 @@
i != wire.elements_end();
++i)
{
- switch(i->type())
+ switch (i->type())
{
case tlv::nfd::IncomingFaceId:
if (encodeIncomingFaceId)
diff --git a/src/management/nrd-controller.cpp b/src/management/nrd-controller.cpp
index 1d0e7a1..66156d2 100644
--- a/src/management/nrd-controller.cpp
+++ b/src/management/nrd-controller.cpp
@@ -115,7 +115,7 @@
PrefixRegOptions options(response.getBody());
return onSuccess(options);
}
- catch(ndn::Tlv::Error& e)
+ catch (ndn::Tlv::Error& e)
{
if (static_cast<bool>(onFail))
return onFail(e.what());
diff --git a/src/management/nrd-prefix-reg-options.hpp b/src/management/nrd-prefix-reg-options.hpp
index c80d04b..2c37c4a 100644
--- a/src/management/nrd-prefix-reg-options.hpp
+++ b/src/management/nrd-prefix-reg-options.hpp
@@ -26,7 +26,15 @@
*/
class PrefixRegOptions {
public:
- struct Error : public Tlv::Error { Error(const std::string& what) : Tlv::Error(what) {} };
+ class Error : public Tlv::Error
+ {
+ public:
+ explicit
+ Error(const std::string& what)
+ : Tlv::Error(what)
+ {
+ }
+ };
PrefixRegOptions()
: m_faceId(INVALID_FACE_ID)
@@ -36,6 +44,7 @@
{
}
+ explicit
PrefixRegOptions(const Block& block)
{
wireDecode(block);
@@ -162,7 +171,7 @@
inline size_t
PrefixRegOptions::wireEncode(EncodingImpl<T>& block) const
{
- size_t total_len = 0;
+ size_t totalLength = 0;
// PrefixRegOptions ::= PREFIX-REG-OPTIONS-TYPE TLV-LENGTH
// Name
@@ -177,44 +186,44 @@
// Protocol
if (!m_protocol.empty())
{
- total_len += prependByteArrayBlock(block,
- tlv::nrd::Protocol,
- reinterpret_cast<const uint8_t*>(m_protocol.c_str()),
- m_protocol.size());
+ totalLength += prependByteArrayBlock(block,
+ tlv::nrd::Protocol,
+ reinterpret_cast<const uint8_t*>(m_protocol.c_str()),
+ m_protocol.size());
}
// ExpirationPeriod
if (m_expirationPeriod > time::milliseconds::zero())
{
- total_len += prependNonNegativeIntegerBlock(block,
- tlv::nrd::ExpirationPeriod,
- m_expirationPeriod.count());
+ totalLength += prependNonNegativeIntegerBlock(block,
+ tlv::nrd::ExpirationPeriod,
+ m_expirationPeriod.count());
}
// Cost
if (m_cost != DEFAULT_COST)
{
- total_len += prependNonNegativeIntegerBlock(block, tlv::nrd::Cost, m_cost);
+ totalLength += prependNonNegativeIntegerBlock(block, tlv::nrd::Cost, m_cost);
}
// Flags
if (m_flags != DEFAULT_FLAGS)
{
- total_len += prependNonNegativeIntegerBlock(block, tlv::nrd::Flags, m_flags);
+ totalLength += prependNonNegativeIntegerBlock(block, tlv::nrd::Flags, m_flags);
}
// FaceId
if (m_faceId != INVALID_FACE_ID)
{
- total_len += prependNonNegativeIntegerBlock(block, tlv::nrd::FaceId, m_faceId);
+ totalLength += prependNonNegativeIntegerBlock(block, tlv::nrd::FaceId, m_faceId);
}
// Name
- total_len += m_name.wireEncode(block);
+ totalLength += m_name.wireEncode(block);
- total_len += block.prependVarNumber(total_len);
- total_len += block.prependVarNumber(tlv::nrd::PrefixRegOptions);
- return total_len;
+ totalLength += block.prependVarNumber(totalLength);
+ totalLength += block.prependVarNumber(tlv::nrd::PrefixRegOptions);
+ return totalLength;
}
inline const Block&
@@ -303,7 +312,7 @@
}
inline std::ostream&
-operator << (std::ostream &os, const PrefixRegOptions &option)
+operator << (std::ostream& os, const PrefixRegOptions& option)
{
os << "PrefixRegOptions(";