22 #ifndef NDN_CXX_LP_PACKET_HPP 23 #define NDN_CXX_LP_PACKET_HPP 64 return m_wire.elements_size() == 0;
72 template<
typename FIELD>
76 return count<FIELD>() > 0;
82 template<
typename FIELD>
86 return std::count_if(m_wire.elements_begin(), m_wire.elements_end(),
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();
160 auto pos = std::upper_bound(m_wire.elements_begin(), m_wire.elements_end(),
161 FIELD::TlvType::value, comparePos);
162 m_wire.
insert(pos, block);
171 template<
typename FIELD>
173 remove(
size_t index = 0)
176 for (
auto it = m_wire.elements_begin(); it != m_wire.elements_end(); ++it) {
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;
211 #endif // NDN_CXX_LP_PACKET_HPP
std::vector< typename FIELD::ValueType > list() const
Packet & remove(size_t index=0)
remove the index-th occurrence of FIELD
Packet & add(const typename FIELD::ValueType &value)
add a FIELD with value
Represents a TLV element of the NDN packet format.
element_iterator insert(element_const_iterator pos, const Block &element)
Insert a sub-element.
const element_container & elements() const
Get container of sub-elements.
Packet & clear()
remove all occurrences of FIELD
void wireDecode(const Block &wire)
decode packet from wire format
#define NDN_CXX_NODISCARD
Block wireEncode() const
encode packet into wire format
EncodingImpl< EncoderTag > EncodingBuffer
Error(const char *expectedType, uint32_t actualType)
represents an error in TLV encoding or decoding
EncodingImpl< EstimatorTag > EncodingEstimator