22 #ifndef NDN_CXX_LP_PACKET_HPP 23 #define NDN_CXX_LP_PACKET_HPP 36 using ndn::tlv::Error::Error;
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) {
88 return block.type() == FIELD::TlvType::value; });
95 template<
typename FIELD>
96 typename FIELD::ValueType
97 get(
size_t index = 0)
const 101 if (element.type() != FIELD::TlvType::value) {
104 if (count++ == index) {
105 return FIELD::decode(element);
109 BOOST_THROW_EXCEPTION(std::out_of_range(
"Index out of range"));
115 template<
typename FIELD>
116 std::vector<typename FIELD::ValueType>
119 std::vector<typename FIELD::ValueType> output;
122 if (element.type() != FIELD::TlvType::value) {
125 output.push_back(FIELD::decode(element));
135 template<
typename FIELD>
137 set(
const typename FIELD::ValueType& value)
140 return add<FIELD>(value);
147 template<
typename FIELD>
149 add(
const typename FIELD::ValueType& value)
151 if (!FIELD::IsRepeatable::value && has<FIELD>()) {
152 BOOST_THROW_EXCEPTION(std::length_error(
"Field cannot be repeated"));
156 size_t estimatedSize = FIELD::encode(estimator, value);
158 FIELD::encode(buffer, value);
159 Block block = buffer.block();
161 auto pos = std::upper_bound(m_wire.elements_begin(), m_wire.elements_end(),
162 FIELD::TlvType::value, comparePos);
163 m_wire.
insert(pos, block);
172 template<
typename FIELD>
174 remove(
size_t index = 0)
177 for (
auto it = m_wire.elements_begin(); it != m_wire.elements_end(); ++it) {
178 if (it->type() == FIELD::TlvType::value) {
179 if (count == index) {
187 BOOST_THROW_EXCEPTION(std::out_of_range(
"Index out of range"));
193 template<
typename FIELD>
197 m_wire.
remove(FIELD::TlvType::value);
203 comparePos(uint64_t first,
const Block& second) noexcept;
206 mutable Block m_wire;
212 #endif // NDN_CXX_LP_PACKET_HPP
const element_container & elements() const
Get container of sub elements.
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 NDN packet format.
element_iterator insert(element_const_iterator pos, const Block &element)
Insert a sub element.
Packet & clear()
remove all occurrences of FIELD
void wireDecode(const Block &wire)
decode packet from wire format
Block wireEncode() const
encode packet into wire format
std::vector< typename FIELD::ValueType > list() const
EncodingImpl< EncoderTag > EncodingBuffer
represents an error in TLV encoding or decoding
EncodingImpl< EstimatorTag > EncodingEstimator