format-helpers.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #ifndef NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP
27 #define NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP
28 
29 #include "core/common.hpp"
30 
31 namespace nfd::tools::nfdc {
32 
33 namespace xml {
34 
35 void
36 printHeader(std::ostream& os);
37 
38 void
39 printFooter(std::ostream& os);
40 
41 struct Text
42 {
43  const std::string& s;
44 };
45 
48 std::ostream&
49 operator<<(std::ostream& os, const Text& text);
50 
53 struct Flag
54 {
55  const char* elementName;
56  bool flag;
57 };
58 
59 std::ostream&
60 operator<<(std::ostream& os, Flag v);
61 
66 std::string
67 formatDuration(time::nanoseconds d);
68 
73 std::string
74 formatTimestamp(time::system_clock::time_point t);
75 
76 } // namespace xml
77 
78 namespace text {
79 
82 struct Spaces
83 {
84  int nSpaces;
85 };
86 
87 std::ostream&
88 operator<<(std::ostream& os, const Spaces& spaces);
89 
100 class Separator : noncopyable
101 {
102 public:
103  Separator(std::string_view first, std::string_view subsequent);
104 
105  explicit
106  Separator(std::string_view subsequent);
107 
108  int
109  getCount() const
110  {
111  return m_count;
112  }
113 
114 private:
115  const std::string m_first;
116  const std::string m_subsequent;
117  int m_count = 0;
118 
119  friend std::ostream& operator<<(std::ostream& os, Separator& sep);
120 };
121 
122 std::ostream&
123 operator<<(std::ostream& os, Separator& sep);
124 
141 class ItemAttributes : noncopyable
142 {
143 public:
148  explicit
149  ItemAttributes(bool wantMultiLine = false, int maxAttributeWidth = 0);
150 
151  struct Attribute
152  {
154  std::string attribute;
155  };
156 
160  Attribute
161  operator()(const std::string& attribute);
162 
163  std::string
164  end() const;
165 
166 private:
167  const bool m_wantMultiLine;
168  const int m_maxAttributeWidth;
169  int m_count = 0;
170 
171  friend std::ostream& operator<<(std::ostream& os, const ItemAttributes::Attribute& attr);
172 };
173 
174 std::ostream&
175 operator<<(std::ostream& os, const ItemAttributes::Attribute& attr);
176 
179 struct OnOff
180 {
181  bool flag;
182 };
183 
184 std::ostream&
185 operator<<(std::ostream& os, OnOff v);
186 
189 struct YesNo
190 {
191  bool flag;
192 };
193 
194 std::ostream&
195 operator<<(std::ostream& os, YesNo v);
196 
197 namespace detail {
198 
199 template<typename DurationT>
200 std::string
201 getTimeUnit(bool isLong);
202 
203 template<>
204 inline std::string
205 getTimeUnit<time::nanoseconds>(bool isLong)
206 {
207  return isLong ? "nanoseconds" : "ns";
208 }
209 
210 template<>
211 inline std::string
212 getTimeUnit<time::microseconds>(bool isLong)
213 {
214  return isLong ? "microseconds" : "us";
215 }
216 
217 template<>
218 inline std::string
219 getTimeUnit<time::milliseconds>(bool isLong)
220 {
221  return isLong ? "milliseconds" : "ms";
222 }
223 
224 template<>
225 inline std::string
226 getTimeUnit<time::seconds>(bool isLong)
227 {
228  return isLong ? "seconds" : "s";
229 }
230 
231 template<>
232 inline std::string
233 getTimeUnit<time::minutes>(bool isLong)
234 {
235  return isLong ? "minutes" : "m";
236 }
237 
238 template<>
239 inline std::string
240 getTimeUnit<time::hours>(bool isLong)
241 {
242  return isLong ? "hours" : "h";
243 }
244 
245 template<>
246 inline std::string
247 getTimeUnit<time::days>(bool isLong)
248 {
249  return isLong ? "days" : "d";
250 }
251 
252 } // namespace detail
253 
254 template<typename OutputPrecision>
255 std::string
256 formatDuration(time::nanoseconds d, bool isLong = false)
257 {
258  return std::to_string(time::duration_cast<OutputPrecision>(d).count()) +
259  (isLong ? " " : "") + detail::getTimeUnit<OutputPrecision>(isLong);
260 }
261 
262 std::string
263 formatTimestamp(time::system_clock::time_point t);
264 
265 } // namespace text
266 
267 } // namespace nfd::tools::nfdc
268 
269 #endif // NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP
Print attributes of an item.
ItemAttributes(bool wantMultiLine=false, int maxAttributeWidth=0)
Constructor.
Attribute operator()(const std::string &attribute)
friend std::ostream & operator<<(std::ostream &os, const ItemAttributes::Attribute &attr)
Print different string on first and subsequent usage.
Separator(std::string_view first, std::string_view subsequent)
friend std::ostream & operator<<(std::ostream &os, Separator &sep)
std::string getTimeUnit(bool isLong)
std::string formatTimestamp(time::system_clock::time_point t)
std::ostream & operator<<(std::ostream &os, const Spaces &spaces)
std::string formatDuration(time::nanoseconds d, bool isLong=false)
std::string formatTimestamp(time::system_clock::time_point t)
std::string formatDuration(time::nanoseconds d)
void printFooter(std::ostream &os)
void printHeader(std::ostream &os)
std::ostream & operator<<(std::ostream &os, const Text &text)
Print XML text with special character represented as predefined entities.
Print boolean as 'on' or 'off'.
Print a number of whitespaces.
int nSpaces
number of spaces; print nothing if negative
Print boolean as 'yes' or 'no'.
Print true as an empty element and false as nothing.