nfd-constants.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2020 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
25 
26 #include <boost/algorithm/string/predicate.hpp>
27 #include <boost/lexical_cast.hpp>
28 #include <istream>
29 #include <map>
30 #include <ostream>
31 
32 namespace ndn {
33 namespace nfd {
34 
35 std::ostream&
36 operator<<(std::ostream& os, FaceScope faceScope)
37 {
38  switch (faceScope) {
39  case FACE_SCOPE_NONE:
40  return os << "none";
42  return os << "non-local";
43  case FACE_SCOPE_LOCAL:
44  return os << "local";
45  }
46  return os << static_cast<unsigned>(faceScope);
47 }
48 
49 std::ostream&
50 operator<<(std::ostream& os, FacePersistency facePersistency)
51 {
52  switch (facePersistency) {
54  return os << "none";
56  return os << "persistent";
58  return os << "on-demand";
60  return os << "permanent";
61  }
62  return os << static_cast<unsigned>(facePersistency);
63 }
64 
65 std::ostream&
66 operator<<(std::ostream& os, LinkType linkType)
67 {
68  switch (linkType) {
69  case LINK_TYPE_NONE:
70  return os << "none";
72  return os << "point-to-point";
74  return os << "multi-access";
75  case LINK_TYPE_AD_HOC:
76  return os << "adhoc";
77  }
78  return os << static_cast<unsigned>(linkType);
79 }
80 
81 std::ostream&
82 operator<<(std::ostream& os, FaceEventKind faceEventKind)
83 {
84  switch (faceEventKind) {
85  case FACE_EVENT_NONE:
86  return os << "none";
87  case FACE_EVENT_CREATED:
88  return os << "created";
90  return os << "destroyed";
91  case FACE_EVENT_UP:
92  return os << "up";
93  case FACE_EVENT_DOWN:
94  return os << "down";
95  }
96  return os << static_cast<unsigned>(faceEventKind);
97 }
98 
99 std::istream&
100 operator>>(std::istream& is, RouteOrigin& routeOrigin)
101 {
102  using boost::algorithm::iequals;
103 
104  std::string s;
105  is >> s;
106 
107  if (iequals(s, "none"))
108  routeOrigin = ROUTE_ORIGIN_NONE;
109  else if (iequals(s, "app"))
110  routeOrigin = ROUTE_ORIGIN_APP;
111  else if (iequals(s, "autoreg"))
112  routeOrigin = ROUTE_ORIGIN_AUTOREG;
113  else if (iequals(s, "client"))
114  routeOrigin = ROUTE_ORIGIN_CLIENT;
115  else if (iequals(s, "autoconf"))
116  routeOrigin = ROUTE_ORIGIN_AUTOCONF;
117  else if (iequals(s, "nlsr"))
118  routeOrigin = ROUTE_ORIGIN_NLSR;
119  else if (iequals(s, "prefixann"))
120  routeOrigin = ROUTE_ORIGIN_PREFIXANN;
121  else if (iequals(s, "static"))
122  routeOrigin = ROUTE_ORIGIN_STATIC;
123  else {
124  // To reject negative numbers, we parse as a wider signed type, and compare with the range.
125  static_assert(std::numeric_limits<std::underlying_type_t<RouteOrigin>>::max() <=
126  std::numeric_limits<int>::max(), "");
127 
128  int v = -1;
129  try {
130  v = boost::lexical_cast<int>(s);
131  }
132  catch (const boost::bad_lexical_cast&) {
133  }
134 
135  if (v >= std::numeric_limits<std::underlying_type_t<RouteOrigin>>::min() &&
136  v <= std::numeric_limits<std::underlying_type_t<RouteOrigin>>::max()) {
137  routeOrigin = static_cast<RouteOrigin>(v);
138  }
139  else {
140  routeOrigin = ROUTE_ORIGIN_NONE;
141  is.setstate(std::ios::failbit);
142  }
143  }
144 
145  return is;
146 }
147 
148 std::ostream&
149 operator<<(std::ostream& os, RouteOrigin routeOrigin)
150 {
151  switch (routeOrigin) {
152  case ROUTE_ORIGIN_NONE:
153  return os << "none";
154  case ROUTE_ORIGIN_APP:
155  return os << "app";
157  return os << "autoreg";
158  case ROUTE_ORIGIN_CLIENT:
159  return os << "client";
161  return os << "autoconf";
162  case ROUTE_ORIGIN_NLSR:
163  return os << "nlsr";
165  return os << "prefixann";
166  case ROUTE_ORIGIN_STATIC:
167  return os << "static";
168  }
169  return os << static_cast<unsigned>(routeOrigin);
170 }
171 
172 std::ostream&
173 operator<<(std::ostream& os, RouteFlags routeFlags)
174 {
175  if (routeFlags == ROUTE_FLAGS_NONE) {
176  return os << "none";
177  }
178 
179  static const std::map<RouteFlags, std::string> knownBits = {
180  {ROUTE_FLAG_CHILD_INHERIT, "child-inherit"},
181  {ROUTE_FLAG_CAPTURE, "capture"}
182  };
183 
184  auto join = make_ostream_joiner(os, '|');
185  for (const auto& pair : knownBits) {
187  std::string token;
188  std::tie(bit, token) = pair;
189 
190  if ((routeFlags & bit) != 0) {
191  join = token;
192  routeFlags = static_cast<RouteFlags>(routeFlags & ~bit);
193  }
194  }
195 
196  if (routeFlags != ROUTE_FLAGS_NONE) {
197  join = AsHex{routeFlags};
198  }
199 
200  return os;
201 }
202 
203 } // namespace nfd
204 } // namespace ndn
Helper class to convert a number to hexadecimal format, for use with stream insertion operators.
@ FACE_PERSISTENCY_NONE
@ FACE_PERSISTENCY_ON_DEMAND
face is on-demand
@ FACE_PERSISTENCY_PERSISTENT
face is persistent
@ FACE_PERSISTENCY_PERMANENT
face is permanent
@ ROUTE_ORIGIN_PREFIXANN
@ ROUTE_ORIGIN_AUTOREG
@ ROUTE_ORIGIN_AUTOCONF
@ FACE_SCOPE_LOCAL
face is local
@ FACE_SCOPE_NON_LOCAL
face is non-local
@ ROUTE_FLAG_CHILD_INHERIT
@ LINK_TYPE_AD_HOC
link is ad hoc
@ LINK_TYPE_MULTI_ACCESS
link is multi-access
@ LINK_TYPE_POINT_TO_POINT
link is point-to-point
@ FACE_EVENT_DOWN
face went DOWN (from UP state)
@ FACE_EVENT_CREATED
face was created
@ FACE_EVENT_UP
face went UP (from DOWN state)
@ FACE_EVENT_DESTROYED
face was destroyed
std::istream & operator>>(std::istream &is, RouteOrigin &routeOrigin)
Extract RouteOrigin from stream.
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
Definition: data.cpp:25
ostream_joiner< std::decay_t< DelimT >, CharT, Traits > make_ostream_joiner(std::basic_ostream< CharT, Traits > &os, DelimT &&delimiter)
Backport of ostream_joiner from the Library Fundamentals v2 TS.