nfd-constants.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "nfd-constants.hpp"
23 #include "util/string-helper.hpp"
24 
25 #include <boost/algorithm/string.hpp>
26 #include <boost/lexical_cast.hpp>
27 #include <istream>
28 #include <map>
29 #include <ostream>
30 
31 namespace ndn {
32 namespace nfd {
33 
34 std::ostream&
35 operator<<(std::ostream& os, FaceScope faceScope)
36 {
37  switch (faceScope) {
38  case FACE_SCOPE_NONE:
39  return os << "none";
41  return os << "non-local";
42  case FACE_SCOPE_LOCAL:
43  return os << "local";
44  }
45  return os << static_cast<unsigned>(faceScope);
46 }
47 
48 std::ostream&
49 operator<<(std::ostream& os, FacePersistency facePersistency)
50 {
51  switch (facePersistency) {
53  return os << "none";
55  return os << "persistent";
57  return os << "on-demand";
59  return os << "permanent";
60  }
61  return os << static_cast<unsigned>(facePersistency);
62 }
63 
64 std::ostream&
65 operator<<(std::ostream& os, LinkType linkType)
66 {
67  switch (linkType) {
68  case LINK_TYPE_NONE:
69  return os << "none";
71  return os << "point-to-point";
73  return os << "multi-access";
74  case LINK_TYPE_AD_HOC:
75  return os << "adhoc";
76  }
77  return os << static_cast<unsigned>(linkType);
78 }
79 
80 std::ostream&
81 operator<<(std::ostream& os, FaceEventKind faceEventKind)
82 {
83  switch (faceEventKind) {
84  case FACE_EVENT_NONE:
85  return os << "none";
86  case FACE_EVENT_CREATED:
87  return os << "created";
89  return os << "destroyed";
90  case FACE_EVENT_UP:
91  return os << "up";
92  case FACE_EVENT_DOWN:
93  return os << "down";
94  }
95  return os << static_cast<unsigned>(faceEventKind);
96 }
97 
98 std::istream&
99 operator>>(std::istream& is, RouteOrigin& routeOrigin)
100 {
101  using boost::algorithm::iequals;
102 
103  std::string s;
104  is >> s;
105 
106  if (iequals(s, "none"))
107  routeOrigin = ROUTE_ORIGIN_NONE;
108  else if (iequals(s, "app"))
109  routeOrigin = ROUTE_ORIGIN_APP;
110  else if (iequals(s, "autoreg"))
111  routeOrigin = ROUTE_ORIGIN_AUTOREG;
112  else if (iequals(s, "client"))
113  routeOrigin = ROUTE_ORIGIN_CLIENT;
114  else if (iequals(s, "autoconf"))
115  routeOrigin = ROUTE_ORIGIN_AUTOCONF;
116  else if (iequals(s, "nlsr"))
117  routeOrigin = ROUTE_ORIGIN_NLSR;
118  else if (iequals(s, "static"))
119  routeOrigin = ROUTE_ORIGIN_STATIC;
120  else {
121  // To reject negative numbers, we parse as a wider signed type, and compare with the range.
122  static_assert(std::numeric_limits<std::underlying_type<RouteOrigin>::type>::max() <=
123  std::numeric_limits<int>::max(), "");
124 
125  int v = -1;
126  try {
127  v = boost::lexical_cast<int>(s);
128  }
129  catch (const boost::bad_lexical_cast&) {
130  }
131 
132  if (v >= std::numeric_limits<std::underlying_type<RouteOrigin>::type>::min() &&
133  v <= std::numeric_limits<std::underlying_type<RouteOrigin>::type>::max()) {
134  routeOrigin = static_cast<RouteOrigin>(v);
135  }
136  else {
137  routeOrigin = ROUTE_ORIGIN_NONE;
138  is.setstate(std::ios::failbit);
139  }
140  }
141 
142  return is;
143 }
144 
145 std::ostream&
146 operator<<(std::ostream& os, RouteOrigin routeOrigin)
147 {
148  switch (routeOrigin) {
149  case ROUTE_ORIGIN_NONE:
150  return os << "none";
151  case ROUTE_ORIGIN_APP:
152  return os << "app";
154  return os << "autoreg";
155  case ROUTE_ORIGIN_CLIENT:
156  return os << "client";
158  return os << "autoconf";
159  case ROUTE_ORIGIN_NLSR:
160  return os << "nlsr";
161  case ROUTE_ORIGIN_STATIC:
162  return os << "static";
163  }
164  return os << static_cast<unsigned>(routeOrigin);
165 }
166 
167 std::ostream&
168 operator<<(std::ostream& os, RouteFlags routeFlags)
169 {
170  if (routeFlags == ROUTE_FLAGS_NONE) {
171  return os << "none";
172  }
173 
174  static const std::map<RouteFlags, std::string> knownBits = {
175  {ROUTE_FLAG_CHILD_INHERIT, "child-inherit"},
176  {ROUTE_FLAG_CAPTURE, "capture"}
177  };
178 
179  auto join = make_ostream_joiner(os, '|');
180  for (const auto& pair : knownBits) {
182  std::string token;
183  std::tie(bit, token) = pair;
184 
185  if ((routeFlags & bit) != 0) {
186  join = token;
187  routeFlags = static_cast<RouteFlags>(routeFlags & ~bit);
188  }
189  }
190 
191  if (routeFlags != ROUTE_FLAGS_NONE) {
192  join = AsHex{routeFlags};
193  }
194 
195  return os;
196 }
197 
198 } // namespace nfd
199 } // namespace ndn
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
std::istream & operator>>(std::istream &is, RouteOrigin &routeOrigin)
extract RouteOrigin from stream
Helper class to convert a number to hexadecimal format, for use with stream insertion operators...
ostream_joiner< typename std::decay< DelimT >::type, CharT, Traits > make_ostream_joiner(std::basic_ostream< CharT, Traits > &os, DelimT &&delimiter)
face went UP (from DOWN state)
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
face went DOWN (from UP state)