name: make `Component(const Block&)` constructor explicit
Refs: #5186
Change-Id: I181c6a94f56ccedf2fa74027d9715187642f1f71
diff --git a/ndn-cxx/impl/name-component-types.hpp b/ndn-cxx/impl/name-component-types.hpp
index d14718e..e1edd0e 100644
--- a/ndn-cxx/impl/name-component-types.hpp
+++ b/ndn-cxx/impl/name-component-types.hpp
@@ -58,7 +58,7 @@
virtual std::tuple<bool, Component>
getSuccessor(const Component& comp) const
{
- return {false, std::get<Block>(getSuccessorImpl(comp))};
+ return {false, Component(std::get<Block>(getSuccessorImpl(comp)))};
}
/** \brief Return the minimum allowable TLV-VALUE of this component type.
@@ -196,10 +196,7 @@
bool isExtended = false;
Block successor;
std::tie(isExtended, successor) = getSuccessorImpl(comp);
- if (isExtended) {
- return {true, comp};
- }
- return {false, Component(successor)};
+ return {isExtended, isExtended ? comp : Component(successor)};
}
span<const uint8_t>
diff --git a/ndn-cxx/name-component.cpp b/ndn-cxx/name-component.cpp
index 406785c..0c80361 100644
--- a/ndn-cxx/name-component.cpp
+++ b/ndn-cxx/name-component.cpp
@@ -359,7 +359,7 @@
Component
Component::fromNumber(uint64_t number, uint32_t type)
{
- return makeNonNegativeIntegerBlock(type, number);
+ return Component(makeNonNegativeIntegerBlock(type, number));
}
Component
@@ -378,7 +378,7 @@
encoder.prependVarNumber(valueLength);
encoder.prependVarNumber(tlv::GenericNameComponent);
- return encoder.block();
+ return Component(encoder.block());
}
Component
@@ -537,14 +537,14 @@
EncodingBuffer buffer(estimatedSize, 0);
wireEncode(buffer);
- const_cast<Component&>(*this) = buffer.block();
+ const_cast<Component*>(this)->wireDecode(buffer.block());
return *this;
}
void
Component::wireDecode(const Block& wire)
{
- *this = wire;
+ *this = Component(wire);
// validity check is done within Component(const Block& wire)
}
diff --git a/ndn-cxx/name-component.hpp b/ndn-cxx/name-component.hpp
index acd87db..31cf2d4 100644
--- a/ndn-cxx/name-component.hpp
+++ b/ndn-cxx/name-component.hpp
@@ -124,10 +124,9 @@
/**
* @brief Construct a NameComponent from @p block.
* @throw Error the NameComponent is invalid.
- *
- * This contructor enables implicit conversion from a Block.
*/
- Component(const Block& wire);
+ explicit
+ Component(const Block& block);
/**
* @brief Construct a NameComponent of TLV-TYPE @p type, using TLV-VALUE from @p buffer.