22 #ifndef NDN_CXX_IMPL_NAME_COMPONENT_TYPES_HPP 23 #define NDN_CXX_IMPL_NAME_COMPONENT_TYPES_HPP 30 #include <unordered_map> 38 class ComponentType : noncopyable
44 ~ComponentType() =
default;
58 virtual std::tuple<bool, Component>
61 return {
false,
Component(std::get<Block>(getSuccessorImpl(comp)))};
66 virtual span<const uint8_t>
69 static const std::vector<uint8_t>
value;
83 getAltUriPrefix()
const 94 parseAltUriValue(
const std::string&)
const 105 writeUri(std::ostream& os,
const Component& comp)
const 107 os << comp.
type() <<
'=';
108 writeUriEscapedValue(os, comp);
116 static std::tuple<bool, Block>
122 bool isOverflow =
true;
124 for (; isOverflow && i > 0; i--) {
125 uint8_t newValue =
static_cast<uint8_t
>((comp.
value()[i - 1] + 1) & 0xFF);
126 encoder.prependBytes({newValue});
127 isOverflow = (newValue == 0);
129 encoder.prependBytes({comp.
value(), i});
133 encoder.appendBytes({0});
136 encoder.prependVarNumber(encoder.size());
137 encoder.prependVarNumber(comp.
type());
138 return {isOverflow, encoder.block()};
145 writeUriEscapedValue(std::ostream& os,
const Component& comp)
148 [] (uint8_t x) {
return x ==
'.'; });
161 class GenericNameComponentType final :
public ComponentType
165 writeUri(std::ostream& os,
const Component& comp)
const final 167 writeUriEscapedValue(os, comp);
174 class Sha256ComponentType final :
public ComponentType
177 Sha256ComponentType(uint32_t type,
const std::string& typeName,
const std::string& uriPrefix)
179 , m_typeName(typeName)
180 , m_uriPrefix(uriPrefix)
187 BOOST_ASSERT(comp.
type() == m_type);
193 std::tuple<bool, Component>
194 getSuccessor(
const Component& comp)
const final 196 bool isExtended =
false;
198 std::tie(isExtended, successor) = getSuccessorImpl(comp);
199 return {isExtended, isExtended ? comp :
Component(successor)};
203 getMinValue()
const final 210 getAltUriPrefix()
const final 212 return m_uriPrefix.data();
216 parseAltUriValue(
const std::string& input)
const final 218 shared_ptr<Buffer>
value;
223 NDN_THROW(Error(
"Cannot convert to " + m_typeName +
" (invalid hex encoding)"));
229 writeUri(std::ostream& os,
const Component& comp)
const final 231 os << m_uriPrefix <<
'=';
236 const uint32_t m_type;
237 const std::string m_typeName;
238 const std::string m_uriPrefix;
244 class DecimalComponentType final :
public ComponentType
247 DecimalComponentType(uint32_t type,
const std::string& typeName,
const std::string& uriPrefix)
249 , m_typeName(typeName)
250 , m_uriPrefix(uriPrefix)
260 getAltUriPrefix()
const final 262 return m_uriPrefix.data();
266 parseAltUriValue(
const std::string& input)
const final 270 n = std::stoull(input);
272 catch (
const std::invalid_argument&) {
273 NDN_THROW(Error(
"Cannot convert to " + m_typeName +
" (invalid format)"));
275 catch (
const std::out_of_range&) {
276 NDN_THROW(Error(
"Cannot convert to " + m_typeName +
" (out of range)"));
279 NDN_THROW(Error(
"Cannot convert to " + m_typeName +
" (invalid format)"));
285 writeUri(std::ostream& os,
const Component& comp)
const final 288 os << m_uriPrefix <<
'=' << comp.
toNumber();
291 ComponentType::writeUri(os, comp);
296 const uint32_t m_type;
297 const std::string m_typeName;
298 const std::string m_uriPrefix;
304 class ComponentTypeTable : noncopyable
307 ComponentTypeTable();
313 get(uint32_t type)
const 315 if (type >= m_table.size() || m_table[type] ==
nullptr) {
318 return *m_table[type];
325 findByUriPrefix(
const std::string& prefix)
const 327 auto it = m_uriPrefixes.find(prefix);
328 if (it == m_uriPrefixes.end()) {
336 set(uint32_t type,
const ComponentType& ct)
338 m_table.at(type) = &ct;
339 if (ct.getAltUriPrefix() !=
nullptr) {
340 m_uriPrefixes[ct.getAltUriPrefix()] = &ct;
345 const ComponentType m_baseType;
346 std::array<const ComponentType*, 60> m_table;
347 std::unordered_map<std::string, const ComponentType*> m_uriPrefixes;
351 ComponentTypeTable::ComponentTypeTable()
353 m_table.fill(
nullptr);
356 "ImplicitSha256DigestComponent",
"sha256digest");
359 "ParametersSha256DigestComponent",
"params-sha256");
362 static const GenericNameComponentType ct8;
365 static const ComponentType ct32;
383 inline const ComponentTypeTable&
384 getComponentTypeTable()
386 static ComponentTypeTable ctt;
394 #endif // NDN_CXX_IMPL_NAME_COMPONENT_TYPES_HPP
Copyright (c) 2011-2015 Regents of the University of California.
std::string to_string(const T &val)
static const size_t DIGEST_SIZE
Length in bytes of a SHA-256 digest.
const_iterator value_begin() const noexcept
Get begin iterator of TLV-VALUE.
size_t value_size() const noexcept
Return the size of TLV-VALUE, i.e., the TLV-LENGTH.
Represents a TLV element of the NDN packet format.
bool isNumber() const
Check if the component is a NonNegativeInteger.
size_t size() const
Return the size of the encoded wire, i.e., of the whole TLV.
shared_ptr< Buffer > fromHex(const std::string &hexString)
Convert the hex string to buffer.
std::string escape(const std::string &str)
Percent-encode a string.
uint64_t toNumber() const
Interpret this name component as a NonNegativeInteger.
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
span< const uint8_t > value_bytes() const noexcept
Return a read-only view of TLV-VALUE as a contiguous range of bytes.
Represents a name component.
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
static Component fromNumber(uint64_t number, uint32_t type=tlv::GenericNameComponent)
Create a component encoded as NonNegativeInteger.
#define NDN_CXX_UNREACHABLE
void printHex(std::ostream &os, uint64_t num, bool wantUpperCase)
Output the hex representation of num to the output stream os.
const_iterator value_end() const noexcept
Get end iterator of TLV-VALUE.
EncodingImpl< EncoderTag > EncodingBuffer