Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
format-util.hpp
Go to the documentation of this file.
1/*
2 FORMAT-UTIL.hpp - helpers for formatting and diagnostics
3
4 Copyright (C)
5 2009, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12*/
13
14
29#ifndef LIB_FORMAT_UTIL_H
30#define LIB_FORMAT_UTIL_H
31
32#include "lib/meta/trait.hpp"
33#include "lib/format-obj.hpp"
34#include "lib/itertools.hpp"
35#include "lib/symbol.hpp"
36#include "lib/util.hpp"
37
38#include <array>
39#include <string>
40#include <vector>
41#include <sstream>
42#include <utility>
43#include <typeinfo>
44
45
46
47namespace util {
48
50 using std::string;
51 using std::forward;
52 using std::move;
53
54
55
56 namespace { // helper to convert arbitrary elements toString
57
58 template<class CON>
59 inline void
61 { /* do nothing */ }
62
63 template<class CON, typename X, typename...ELMS>
64 inline void
65 do_stringify(CON& container, X const& elm, ELMS const& ...args)
66 {
67 container += util::toString (elm);
68 do_stringify (container, args...);
69 }
70
71
72 template<class CON, typename...ELMS>
74 : CON
75 {
76 void
77 operator+= (string&& s)
78 {
79 CON::push_back (move(s));
80 }
81 };
82
83 // most common case: use a vector container...
84 using std::vector;
85
86 template<typename X, typename...ELMS>
87 struct SeqContainer<vector<X>, ELMS...>
88 :vector<X>
89 {
91 {
92 this->reserve(sizeof...(ELMS));
93 }
94
95 void
96 operator+= (string&& s)
97 {
98 this->emplace_back (move(s));
99 }
100 };
101 }//(end) stringify helper
102
103
110 template<class CON, typename...ELMS>
111 inline CON
112 collectStr(ELMS const& ...elms)
113 {
114 SeqContainer<CON,ELMS...> storage;
115 do_stringify (storage, elms...);
116 return CON {move(storage)};
117 }
118
120 template<typename...ELMS>
121 inline vector<string>
122 stringify (ELMS const& ...elms)
123 {
124 return collectStr<vector<string>> (elms...);
125 }
126
132 template<class IT>
133 inline auto
134 stringify (IT&& src)
135 {
137
138 return lib::transformIterator(forward<IT>(src), util::toString<Val>);
139 }
140
141
142
143 namespace { // helper to build range iterator on demand
144 template<class CON, typename TOGGLE = void>
146 {
147 using StlIter = CON::const_iterator;
148
150
151 _RangeIter(CON const& collection)
152 : iter(begin(collection), end(collection))
153 { }
154 };
155
156 template<class IT>
157 struct _RangeIter<IT, lib::meta::enable_if< can_IterForEach<IT>> >
158 {
160
161 _RangeIter(IT&& srcIter)
162 : iter(std::forward<IT>(srcIter))
163 { }
164 _RangeIter(IT const& srcIter) // note: copy here
165 : iter(srcIter)
166 { }
167
168 };
169 }//(end) join helper
170
171
192 template<class COLL>
193 inline string
194 join (COLL&& coll, string const& delim =", ")
195 {
197 _RangeIter<Coll> range(std::forward<COLL>(coll)); // copies when CON is reference
198
199 auto strings = stringify (std::move (range.iter));
200 if (!strings) return "";
201
202 std::ostringstream buffer;
203 for ( ; strings; ++strings)
204 buffer << *strings << delim;
205
206 // chop off last delimiter
207 size_t len = buffer.str().length();
208 ASSERT (len >= delim.length());
209 return buffer.str().substr(0, len - delim.length());
210 }
211
212 template<class X>
213 inline string
214 join (std::initializer_list<X> const&& ili, string const& delim =", ")
215 {
216 return join (ili, delim);
217 }
218
219 // Note: offering a variant of join with var-args would create lots of ambiguities
220
222 template<typename...ARGS>
223 inline string
224 joinArgList (ARGS const& ...args)
225 {
226 return "("+join (stringify (args...))+")";
227 }
228
230 template<typename...ARGS>
231 inline string
232 joinDash (ARGS const& ...args)
233 {
234 return join (stringify (args...), "-");
235 }
236
238 template<typename...ARGS>
239 inline string
240 joinDot (ARGS const& ...args)
241 {
242 return join (stringify (args...), ".");
243 }
244
245
246
248 template<class COLL>
249 inline string
250 toStringParen (COLL&& coll)
251 {
252 return "("+join (forward<COLL> (coll))+")";
253 }
254
255 template<class COLL>
256 inline string
257 toStringBracket (COLL&& coll)
258 {
259 return "["+join (forward<COLL> (coll))+"]";
260 }
261
262
264 template<typename T, std::size_t N>
265 struct StringConv<std::array<T,N>>
266 {
267 static std::string
268 invoke (std::array<T,N> const& arr) noexcept
269 {
270 return util::toStringBracket (arr);
271 }
272 };
273
274
275} // namespace util
276#endif /*LIB_FORMAT_UTIL_H*/
Accessing a STL element range through a Lumiera forward iterator, An instance of this iterator adapte...
Trait template to detect a type usable immediately as "Lumiera Forward Iterator" in a specialised for...
Definition trait.hpp:511
Simple functions to represent objects, for debugging and diagnostics.
Helpers for working with iterators based on the pipeline model.
RefTraits< TY >::Value value_type
remove_cv_t< TypePointee > TypePlain
Definition trait.hpp:259
Implementation namespace for support and library code.
auto transformIterator(IT const &src, FUN processingFunc)
Build a TransformIter: convenience free function shortcut, picking up the involved types automaticall...
STL namespace.
vector< string > stringify(ELMS const &...elms)
standard setup: convert to string into a vector
string toStringBracket(COLL &&coll)
string joinDot(ARGS const &...args)
shortcut: join directly with dots
CON collectStr(ELMS const &...elms)
convert a sequence of elements to string
std::string toString(TY const &val) noexcept
get some string representation of any object, reliably.
string join(COLL &&coll, string const &delim=", ")
enumerate a collection's contents, separated by delimiter.
string joinArgList(ARGS const &...args)
shortcut: List in parentheses, separated by comma, using temporary vector
string toStringParen(COLL &&coll)
one-argument variant that can be forward declared...
string joinDash(ARGS const &...args)
shortcut: join directly with dashes
static std::string invoke(std::array< T, N > const &arr) noexcept
failsafe invocation of custom string conversion.
Marker types to indicate a literal string and a Symbol.
Helpers for type detection, type rewriting and metaprogramming.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...