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 
189 namespace detail {
190 
191 template<typename DurationT>
192 std::string
193 getTimeUnit(bool isLong);
194 
195 template<>
196 inline std::string
197 getTimeUnit<time::nanoseconds>(bool isLong)
198 {
199  return isLong ? "nanoseconds" : "ns";
200 }
201 
202 template<>
203 inline std::string
204 getTimeUnit<time::microseconds>(bool isLong)
205 {
206  return isLong ? "microseconds" : "us";
207 }
208 
209 template<>
210 inline std::string
211 getTimeUnit<time::milliseconds>(bool isLong)
212 {
213  return isLong ? "milliseconds" : "ms";
214 }
215 
216 template<>
217 inline std::string
218 getTimeUnit<time::seconds>(bool isLong)
219 {
220  return isLong ? "seconds" : "s";
221 }
222 
223 template<>
224 inline std::string
225 getTimeUnit<time::minutes>(bool isLong)
226 {
227  return isLong ? "minutes" : "m";
228 }
229 
230 template<>
231 inline std::string
232 getTimeUnit<time::hours>(bool isLong)
233 {
234  return isLong ? "hours" : "h";
235 }
236 
237 template<>
238 inline std::string
239 getTimeUnit<time::days>(bool isLong)
240 {
241  return isLong ? "days" : "d";
242 }
243 
244 } // namespace detail
245 
246 template<typename OutputPrecision>
247 std::string
248 formatDuration(time::nanoseconds d, bool isLong = false)
249 {
250  return to_string(time::duration_cast<OutputPrecision>(d).count()) +
251  (isLong ? " " : "") + detail::getTimeUnit<OutputPrecision>(isLong);
252 }
253 
254 std::string
255 formatTimestamp(time::system_clock::TimePoint t);
256 
257 } // namespace text
258 
259 } // namespace nfdc
260 } // namespace tools
261 } // namespace nfd
262 
263 #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 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)