strategy-choice.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "strategy-choice.hpp"
23 #include "encoding/tlv-nfd.hpp"
25 #include "util/concepts.hpp"
26 
27 namespace ndn {
28 namespace nfd {
29 
30 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<StrategyChoice>));
31 BOOST_CONCEPT_ASSERT((WireEncodable<StrategyChoice>));
33 BOOST_CONCEPT_ASSERT((WireDecodable<StrategyChoice>));
34 static_assert(std::is_base_of<tlv::Error, StrategyChoice::Error>::value,
35  "StrategyChoice::Error must inherit from tlv::Error");
36 
38 
40 {
41  this->wireDecode(payload);
42 }
43 
44 template<encoding::Tag TAG>
45 size_t
46 StrategyChoice::wireEncode(EncodingImpl<TAG>& encoder) const
47 {
48  size_t totalLength = 0;
49 
50  totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy);
51  totalLength += m_name.wireEncode(encoder);
52 
53  totalLength += encoder.prependVarNumber(totalLength);
54  totalLength += encoder.prependVarNumber(tlv::nfd::StrategyChoice);
55  return totalLength;
56 }
57 
58 template size_t
59 StrategyChoice::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const;
60 
61 template size_t
62 StrategyChoice::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const;
63 
64 const Block&
66 {
67  if (m_wire.hasWire())
68  return m_wire;
69 
70  EncodingEstimator estimator;
71  size_t estimatedSize = wireEncode(estimator);
72 
73  EncodingBuffer buffer(estimatedSize, 0);
74  wireEncode(buffer);
75 
76  m_wire = buffer.block();
77  return m_wire;
78 }
79 
80 void
82 {
83  if (block.type() != tlv::nfd::StrategyChoice) {
84  BOOST_THROW_EXCEPTION(Error("expecting StrategyChoice block"));
85  }
86  m_wire = block;
87  m_wire.parse();
89 
90  if (val != m_wire.elements_end() && val->type() == tlv::Name) {
91  m_name.wireDecode(*val);
92  ++val;
93  }
94  else {
95  BOOST_THROW_EXCEPTION(Error("missing required Name field"));
96  }
97 
98  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Strategy) {
99  val->parse();
100  if (val->elements().empty()) {
101  BOOST_THROW_EXCEPTION(Error("expecting Strategy/Name"));
102  }
103  else {
104  m_strategy.wireDecode(*val->elements_begin());
105  }
106  ++val;
107  }
108  else {
109  BOOST_THROW_EXCEPTION(Error("missing required Strategy field"));
110  }
111 }
112 
115 {
116  m_wire.reset();
117  m_name = name;
118  return *this;
119 }
120 
123 {
124  m_wire.reset();
125  m_strategy = strategy;
126  return *this;
127 }
128 
129 bool
131 {
132  return a.getName() == b.getName() && a.getStrategy() == b.getStrategy();
133 }
134 
135 std::ostream&
136 operator<<(std::ostream& os, const StrategyChoice& sc)
137 {
138  return os << "StrategyChoice("
139  << "Name: " << sc.getName() << ", "
140  << "Strategy: " << sc.getStrategy()
141  << ")";
142 }
143 
144 } // namespace nfd
145 } // namespace ndn
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
EncodingImpl< EstimatorTag > EncodingEstimator
size_t prependNestedBlock(EncodingImpl< TAG > &encoder, uint32_t type, const U &value)
Prepend a TLV block of type type with WireEncodable value as a value.
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: name.cpp:122
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
void wireDecode(const Block &wire)
element_const_iterator elements_end() const
Definition: block.cpp:595
element_const_iterator elements_begin() const
Definition: block.cpp:589
const Block & wireEncode() const
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:56
EncodingImpl< EncoderTag > EncodingBuffer
bool operator==(const StrategyChoice &a, const StrategyChoice &b)
element_container::const_iterator element_const_iterator
Definition: block.hpp:48
StrategyChoice & setStrategy(const Name &strategy)
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
void reset()
Reset wire buffer of the element.
Definition: block.cpp:302
Name abstraction to represent an absolute name.
Definition: name.hpp:46
const Name & getStrategy() const
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
uint32_t type() const
Definition: block.hpp:324
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
represents NFD StrategyChoice dataset
void wireDecode(const Block &wire)
Definition: name.cpp:161
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:40
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:76
StrategyChoice & setName(const Name &name)
const Name & getName() const