22 #ifndef NDN_CXX_LP_PACKET_HPP
23 #define NDN_CXX_LP_PACKET_HPP
76 template<
typename FIELD>
80 return count<FIELD>() > 0;
86 template<
typename FIELD>
91 [] (
const Block& block) { return block.type() == FIELD::TlvType::value; });
98 template<
typename FIELD>
99 typename FIELD::ValueType
100 get(
size_t index = 0)
const
104 if (element.type() != FIELD::TlvType::value) {
107 if (
count++ == index) {
108 return FIELD::decode(element);
112 NDN_THROW(std::out_of_range(
"lp::Packet::get: index out of range"));
118 template<
typename FIELD>
119 [[nodiscard]] std::vector<typename FIELD::ValueType>
122 std::vector<typename FIELD::ValueType> output;
125 if (element.type() != FIELD::TlvType::value) {
128 output.push_back(FIELD::decode(element));
138 template<
typename FIELD>
140 set(
const typename FIELD::ValueType& value)
143 return add<FIELD>(value);
150 template<
typename FIELD>
152 add(
const typename FIELD::ValueType& value)
154 if (!FIELD::IsRepeatable::value && has<FIELD>()) {
155 NDN_THROW(std::invalid_argument(
"lp::Packet::add: field cannot be repeated"));
159 size_t estimatedSize = FIELD::encode(estimator, value);
161 FIELD::encode(buffer, value);
162 Block block = buffer.block();
165 FIELD::TlvType::value, comparePos);
166 m_wire.
insert(pos, block);
175 template<
typename FIELD>
181 if (it->type() == FIELD::TlvType::value) {
182 if (
count == index) {
190 NDN_THROW(std::out_of_range(
"lp::Packet::remove: index out of range"));
196 template<
typename FIELD>
200 m_wire.
remove(FIELD::TlvType::value);
206 comparePos(uint32_t first,
const Block& second) noexcept;
Represents a TLV element of the NDN packet format.
element_const_iterator elements_begin() const noexcept
Equivalent to elements().begin().
element_iterator erase(element_const_iterator position)
Erase a sub-element.
size_t elements_size() const noexcept
Equivalent to elements().size().
const element_container & elements() const noexcept
Get container of sub-elements.
void remove(uint32_t type)
Remove all sub-elements of the specified TLV-TYPE.
element_const_iterator elements_end() const noexcept
Equivalent to elements().end().
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
Returns the value of the index-th occurrence of FIELD.
size_t count() const
Returns the number of occurrences of FIELD.
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
Returns the values of all occurrences of FIELD.
Packet & add(const typename FIELD::ValueType &value)
Add a FIELD with value.
Block wireEncode() const
Encode packet into wire format.
bool has() const
Returns true if FIELD occurs one or more times.
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
Contains classes and functions related to NDNLPv2.