22 #ifndef NDN_CXX_LP_PACKET_HPP
23 #define NDN_CXX_LP_PACKET_HPP
72 template<
typename FIELD>
76 return count<FIELD>() > 0;
82 template<
typename FIELD>
87 [] (
const Block& block) { return block.type() == FIELD::TlvType::value; });
94 template<
typename FIELD>
95 typename FIELD::ValueType
96 get(
size_t index = 0)
const
100 if (element.type() != FIELD::TlvType::value) {
103 if (
count++ == index) {
104 return FIELD::decode(element);
108 NDN_THROW(std::out_of_range(
"lp::Packet::get: index out of range"));
114 template<
typename FIELD>
118 std::vector<typename FIELD::ValueType> output;
121 if (element.type() != FIELD::TlvType::value) {
124 output.push_back(FIELD::decode(element));
134 template<
typename FIELD>
136 set(
const typename FIELD::ValueType& value)
139 return add<FIELD>(value);
146 template<
typename FIELD>
148 add(
const typename FIELD::ValueType& value)
150 if (!FIELD::IsRepeatable::value && has<FIELD>()) {
151 NDN_THROW(std::invalid_argument(
"lp::Packet::add: field cannot be repeated"));
155 size_t estimatedSize = FIELD::encode(estimator, value);
157 FIELD::encode(buffer, value);
158 Block block = buffer.block();
161 FIELD::TlvType::value, comparePos);
162 m_wire.
insert(pos, block);
171 template<
typename FIELD>
177 if (it->type() == FIELD::TlvType::value) {
178 if (
count == index) {
186 NDN_THROW(std::out_of_range(
"lp::Packet::remove: index out of range"));
192 template<
typename FIELD>
196 m_wire.
remove(FIELD::TlvType::value);
202 comparePos(uint64_t first,
const Block& second) noexcept;
205 mutable Block m_wire;
#define NDN_CXX_NODISCARD
Represents a TLV element of the NDN packet format.
size_t elements_size() const
Equivalent to elements().size()
element_iterator erase(element_const_iterator position)
Erase a sub-element.
void remove(uint32_t type)
Remove all sub-elements of the specified TLV-TYPE.
element_const_iterator elements_end() const
Equivalent to elements().end()
element_const_iterator elements_begin() const
Equivalent to elements().begin()
const element_container & elements() const
Get container of sub-elements.
element_iterator insert(element_const_iterator pos, const Block &element)
Insert a sub-element.
Packet & remove(size_t index=0)
remove the index-th occurrence of FIELD
Packet & clear()
remove all occurrences of FIELD
FIELD::ValueType get(size_t index=0) const
Packet & set(const typename FIELD::ValueType &value)
remove all occurrences of FIELD, and add a FIELD with value
std::vector< typename FIELD::ValueType > list() const
Packet & add(const typename FIELD::ValueType &value)
add a FIELD with value
Block wireEncode() const
encode packet into wire format
void wireDecode(const Block &wire)
decode packet from wire format
represents an error in TLV encoding or decoding
Error(const char *expectedType, uint32_t actualType)
EncodingImpl< EstimatorTag > EncodingEstimator
EncodingImpl< EncoderTag > EncodingBuffer