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-2018, 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 {
32 namespace tools {
33 namespace nfdc {
34 
35 namespace xml {
36 
37 void
38 printHeader(std::ostream& os);
39 
40 void
41 printFooter(std::ostream& os);
42 
43 struct Text
44 {
45  const std::string& s;
46 };
47 
50 std::ostream&
51 operator<<(std::ostream& os, const Text& text);
52 
55 struct Flag
56 {
57  const char* elementName;
58  bool flag;
59 };
60 
61 std::ostream&
62 operator<<(std::ostream& os, Flag v);
63 
68 std::string
69 formatDuration(time::nanoseconds d);
70 
75 std::string
76 formatTimestamp(time::system_clock::TimePoint t);
77 
78 } // namespace xml
79 
80 namespace text {
81 
84 struct Spaces
85 {
86  int nSpaces;
87 };
88 
89 std::ostream&
90 operator<<(std::ostream& os, const Spaces& spaces);
91 
102 class Separator : noncopyable
103 {
104 public:
105  Separator(const std::string& first, const std::string& subsequent);
106 
107  explicit
108  Separator(const std::string& subsequent);
109 
110  int
111  getCount() const
112  {
113  return m_count;
114  }
115 
116 private:
117  std::string m_first;
118  std::string m_subsequent;
119  int m_count;
120 
121  friend std::ostream& operator<<(std::ostream& os, Separator& sep);
122 };
123 
124 std::ostream&
125 operator<<(std::ostream& os, Separator& sep);
126 
143 class ItemAttributes : noncopyable
144 {
145 public:
150  explicit
151  ItemAttributes(bool wantMultiLine = false, int maxAttributeWidth = 0);
152 
153  struct Attribute
154  {
156  std::string attribute;
157  };
158 
162  Attribute
163  operator()(const std::string& attribute);
164 
165  std::string
166  end() const;
167 
168 private:
169  bool m_wantMultiLine;
170  int m_maxAttributeWidth;
171  int m_count;
172 
173  friend std::ostream& operator<<(std::ostream& os, const ItemAttributes::Attribute& attr);
174 };
175 
176 std::ostream&
177 operator<<(std::ostream& os, const ItemAttributes::Attribute& attr);
178 
181 struct OnOff
182 {
183  bool flag;
184 };
185 
186 std::ostream&
187 operator<<(std::ostream& os, OnOff v);
188 
191 struct YesNo
192 {
193  bool flag;
194 };
195 
196 std::ostream&
197 operator<<(std::ostream& os, YesNo v);
198 
199 namespace detail {
200 
201 template<typename DurationT>
202 std::string
203 getTimeUnit(bool isLong);
204 
205 template<>
206 inline std::string
207 getTimeUnit<time::nanoseconds>(bool isLong)
208 {
209  return isLong ? "nanoseconds" : "ns";
210 }
211 
212 template<>
213 inline std::string
214 getTimeUnit<time::microseconds>(bool isLong)
215 {
216  return isLong ? "microseconds" : "us";
217 }
218 
219 template<>
220 inline std::string
221 getTimeUnit<time::milliseconds>(bool isLong)
222 {
223  return isLong ? "milliseconds" : "ms";
224 }
225 
226 template<>
227 inline std::string
228 getTimeUnit<time::seconds>(bool isLong)
229 {
230  return isLong ? "seconds" : "s";
231 }
232 
233 template<>
234 inline std::string
235 getTimeUnit<time::minutes>(bool isLong)
236 {
237  return isLong ? "minutes" : "m";
238 }
239 
240 template<>
241 inline std::string
242 getTimeUnit<time::hours>(bool isLong)
243 {
244  return isLong ? "hours" : "h";
245 }
246 
247 template<>
248 inline std::string
249 getTimeUnit<time::days>(bool isLong)
250 {
251  return isLong ? "days" : "d";
252 }
253 
254 } // namespace detail
255 
256 template<typename OutputPrecision>
257 std::string
258 formatDuration(time::nanoseconds d, bool isLong = false)
259 {
260  return to_string(time::duration_cast<OutputPrecision>(d).count()) +
261  (isLong ? " " : "") + detail::getTimeUnit<OutputPrecision>(isLong);
262 }
263 
264 std::string
265 formatTimestamp(time::system_clock::TimePoint t);
266 
267 } // namespace text
268 
269 } // namespace nfdc
270 } // namespace tools
271 } // namespace nfd
272 
273 #endif // NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP
std::string formatDuration(time::nanoseconds d)
std::string formatTimestamp(time::system_clock::TimePoint t)
std::string getTimeUnit(bool isLong)
print true as an empty element and false as nothing
void printHeader(std::ostream &os)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
std::ostream & operator<<(std::ostream &os, const Text &text)
print XML text with special character represented as predefined entities
print attributes of an item
int nSpaces
number of spaces; print nothing if negative
print boolean as &#39;yes&#39; or &#39;no&#39;
print different string on first and subsequent usage
print a number of whitespaces
print boolean as &#39;on&#39; or &#39;off&#39;
void printFooter(std::ostream &os)