22 #ifndef NDN_CXX_LP_PACKET_HPP
23 #define NDN_CXX_LP_PACKET_HPP
75 template<
typename FIELD>
79 return count<FIELD>() > 0;
85 template<
typename FIELD>
90 [] (
const Block& block) { return block.type() == FIELD::TlvType::value; });
97 template<
typename FIELD>
98 typename FIELD::ValueType
99 get(
size_t index = 0)
const
103 if (element.type() != FIELD::TlvType::value) {
106 if (
count++ == index) {
107 return FIELD::decode(element);
111 NDN_THROW(std::out_of_range(
"lp::Packet::get: index out of range"));
117 template<
typename FIELD>
121 std::vector<typename FIELD::ValueType> output;
124 if (element.type() != FIELD::TlvType::value) {
127 output.push_back(FIELD::decode(element));
137 template<
typename FIELD>
139 set(
const typename FIELD::ValueType& value)
142 return add<FIELD>(value);
149 template<
typename FIELD>
151 add(
const typename FIELD::ValueType& value)
153 if (!FIELD::IsRepeatable::value && has<FIELD>()) {
154 NDN_THROW(std::invalid_argument(
"lp::Packet::add: field cannot be repeated"));
158 size_t estimatedSize = FIELD::encode(estimator, value);
160 FIELD::encode(buffer, value);
161 Block block = buffer.block();
164 FIELD::TlvType::value, comparePos);
165 m_wire.
insert(pos, block);
174 template<
typename FIELD>
180 if (it->type() == FIELD::TlvType::value) {
181 if (
count == index) {
189 NDN_THROW(std::out_of_range(
"lp::Packet::remove: index out of range"));
195 template<
typename FIELD>
199 m_wire.
remove(FIELD::TlvType::value);
205 comparePos(uint64_t first,
const Block& second) noexcept;
208 mutable Block m_wire;
#define NDN_CXX_NODISCARD
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