22 #ifndef NDN_CXX_LP_PACKET_HPP 23 #define NDN_CXX_LP_PACKET_HPP 68 return m_wire.elements_size() == 0;
76 template<
typename FIELD>
80 return count<FIELD>() > 0;
86 template<
typename FIELD>
90 return std::count_if(m_wire.elements_begin(), m_wire.elements_end(),
91 [] (
const Block& block) {
92 return block.type() == FIELD::TlvType::value; });
99 template<
typename FIELD>
100 typename FIELD::ValueType
101 get(
size_t index = 0)
const 105 if (element.type() != FIELD::TlvType::value) {
108 if (count++ == index) {
109 return FIELD::decode(element);
113 BOOST_THROW_EXCEPTION(std::out_of_range(
"Index out of range"));
119 template<
typename FIELD>
120 std::vector<typename FIELD::ValueType>
123 std::vector<typename FIELD::ValueType> output;
126 if (element.type() != FIELD::TlvType::value) {
129 output.push_back(FIELD::decode(element));
139 template<
typename FIELD>
141 set(
const typename FIELD::ValueType& value)
144 return add<FIELD>(value);
151 template<
typename FIELD>
153 add(
const typename FIELD::ValueType& value)
155 if (!FIELD::IsRepeatable::value && has<FIELD>()) {
156 BOOST_THROW_EXCEPTION(std::length_error(
"Field cannot be repeated"));
160 size_t estimatedSize = FIELD::encode(estimator, value);
162 FIELD::encode(buffer, value);
163 Block block = buffer.block();
165 auto pos = std::upper_bound(m_wire.elements_begin(), m_wire.elements_end(),
166 FIELD::TlvType::value, comparePos);
167 m_wire.
insert(pos, block);
176 template<
typename FIELD>
178 remove(
size_t index = 0)
181 for (
auto it = m_wire.elements_begin(); it != m_wire.elements_end(); ++it) {
182 if (it->type() == FIELD::TlvType::value) {
183 if (count == index) {
191 BOOST_THROW_EXCEPTION(std::out_of_range(
"Index out of range"));
197 template<
typename FIELD>
201 m_wire.
remove(FIELD::TlvType::value);
207 comparePos(uint64_t first,
const Block& second);
210 mutable Block m_wire;
216 #endif // NDN_CXX_LP_PACKET_HPP Copyright (c) 2013-2017 Regents of the University of California.
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.
Error(const std::string &what)
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