31 #ifndef DRAIN_SPRINTER
32 #define DRAIN_SPRINTER
45 #include <drain/UniTuple.h>
69 prefix(this->next()), separator(this->next()), suffix(this->next()){
74 return ((prefix==0) && (separator==0) && (suffix==0));
93 set(prefix, 0, suffix);
97 TypeLayout(cstr_t prefix, cstr_t separator, cstr_t suffix){
98 set(prefix, separator, suffix);
152 SprinterLayout(
const char *arrayChars=
"[,]",
const char *mapChars=
"{,}",
const char *pairChars=
"(,)",
153 const char *stringChars=
nullptr,
const char *keyChars=
nullptr,
const char *mapPairChars=
nullptr){
158 this->stringChars.
setLayout(stringChars);
162 this->mapPairChars.
setLayout(mapPairChars);
166 arrayChars(layout.arrayChars), mapChars(layout.mapChars), pairChars(layout.pairChars),
167 stringChars(layout.stringChars), keyChars(layout.keyChars), mapPairChars(layout.mapPairChars){
172 arrayChars.separator = itemSeparator;
173 mapChars.separator = itemSeparator;
174 pairChars.separator = itemSeparator;
175 mapPairChars.separator = itemSeparator;
184 std::ostream & operator<<(std::ostream & ostr,
const SprinterLayout & layout){
185 ostr <<
"arrayChars: " << layout.arrayChars <<
'\n';
186 ostr <<
"mapChars: " << layout.mapChars <<
'\n';
187 ostr <<
"pairChars: " << layout.pairChars <<
'\n';
188 ostr <<
"stringChars: " << layout.stringChars <<
'\n';
189 ostr <<
"keyChars: " << layout.keyChars <<
'\n';
190 ostr <<
"mapPairChars: " << layout.mapPairChars <<
'\n';
256 ostr << layout.prefix;
260 void separatorToStream(std::ostream & ostr,
const TypeLayout & layout){
261 if (layout.separator)
262 ostr << layout.separator;
266 void suffixToStream(std::ostream & ostr,
const TypeLayout & layout){
268 ostr << layout.suffix;
273 template <
class K,
class V>
275 std::ostream & pairToStream(std::ostream & ostr,
const std::pair<K,V> & x,
const SprinterLayout & layout){
283 suffixToStream(ostr, layout.keyChars);
285 separatorToStream(ostr, layout.pairChars);
289 suffixToStream(ostr, layout.pairChars);
295 template <
class K,
class V>
305 suffixToStream(ostr, layout.keyChars);
307 separatorToStream(ostr, layout.mapPairChars);
311 suffixToStream(ostr, layout.mapPairChars);
334 for (
const auto & elem: x){
339 separatorToStream(ostr, myChars);
344 suffixToStream(ostr, myChars);
358 template <
class M,
class K>
366 const bool MAP_PAIRS = !layout.mapPairChars.empty();
369 for (
typename K::const_iterator it=keys.begin(); it != keys.end(); ++it){
371 if (it != keys.begin())
372 separatorToStream(ostr, layout.mapChars);
374 typename M::const_iterator mit = m.find(*it);
384 std::cerr << __FILE__ << __FUNCTION__ <<
':' <<
"missing key: " << *it << std::endl;
389 suffixToStream(ostr, layout.mapChars);
411 suffixToStream(ostr, myChars);
425 template <
class K,
class V>
428 return pairToStream(ostr, x, layout);
433 std::ostream &
toStream(std::ostream & ostr,
const std::vector<D> & x,
const SprinterLayout & layout =
defaultLayout){
439 std::ostream &
toStream(std::ostream & ostr,
const std::list<D> & x,
const SprinterLayout & layout =
defaultLayout){
445 std::ostream &
toStream(std::ostream & ostr,
const std::set<D> & x,
const SprinterLayout & layout =
defaultLayout){
449 template <
class K,
class V>
451 std::ostream &
toStream(std::ostream & ostr,
const std::map<K,V> & x,
const SprinterLayout & layout =
UNSET_LAYOUT){
462 template <
class D,
size_t N>
492 return ostr << (b ?
"true":
"false");
501 return ostr <<
"null";
503 return ostr <<
"null";
533 const bool DATA = !tree.data.empty();
534 const bool CHILDREN = !tree.empty();
536 if (! (DATA||CHILDREN)){
538 ostr << layout.mapChars.prefix << layout.mapChars.suffix;
542 const std::string pad(2*indent,
' ');
563 ostr << layout.mapChars.separator;
567 ostr << layout.mapChars.prefix <<
'\n';
571 for (
const auto & entry: tree){
577 sep = layout.mapChars.separator;
579 ostr << pad <<
" " <<
'"' << entry.first <<
'"' << layout.pairChars.separator <<
' ';
585 ostr << pad << layout.mapChars.suffix;
602 template <
class T,
class L=Spr
interLayout>
614 std::ostream & toStream(std::ostream & ostr)
const {
618 std::string str()
const {
619 std::stringstream sstr;
627 template <
class T,
class L>
629 std::ostream & operator<<(std::ostream & ostr,
const Sprintlet<T,L> & sp){
630 return sp.toStream(ostr);
638 Sprintlet<T> sprinter(
const T & x,
const char *arrayCaps,
const char *mapCaps=
"{,}",
const char *pairCaps=
"(:)",
const char *stringCaps=
"\""){
639 return Sprintlet<T>(x, SprinterLayout(arrayCaps,mapCaps,pairCaps,stringCaps));
644 Sprintlet<T> sprinter(
const T & x,
const SprinterLayout & layout = SprinterLayout()){
645 return Sprintlet<T>(x, layout);
Definition: Sprinter.h:195
static std::ostream & toStream(std::ostream &ostr, const char *x, const SprinterLayout &layout=defaultLayout)
Conventional type: C string.
Definition: Sprinter.h:477
static const SprinterLayout xmlAttributeLayout
Like attributes in XML (HTML, SVG, ...) tags.
Definition: Sprinter.h:227
static std::ostream & sequenceToStream(std::ostream &ostr, const T &x, const TypeLayout &myChars, const SprinterLayout &layout)
Print sequence x with myChars, continue recursively with layout.
Definition: Sprinter.h:329
static std::ostream & toStream(std::ostream &ostr, D *x, const SprinterLayout &layout=defaultLayout)
Pointer: redirect to actual target object.
Definition: Sprinter.h:499
static std::ostream & sequenceToStream(std::ostream &ostr, const T &x, const SprinterLayout &layout)
Convenience: if sequence type (array, list, set, map) not given, assume array.
Definition: Sprinter.h:321
static std::ostream & toStream(std::ostream &ostr, const std::string &x, const SprinterLayout &layout=defaultLayout)
Conventional type: std::string. Notice prefix revealed.
Definition: Sprinter.h:470
static const SprinterLayout pythonLayout
C++ code initializer list style: all objects with {...}, with comma ',' separator.
Definition: Sprinter.h:245
static const SprinterLayout emptyLayout
Simply concatenate values without punctuation.
Definition: Sprinter.h:206
static const SprinterLayout plainLayout
Display plain values, concatenating them with comma (except for strings).
Definition: Sprinter.h:209
static void prefixToStream(std::ostream &ostr, const TypeLayout &layout)
Output Dictionary, which is a list.
Definition: Sprinter.h:254
static const SprinterLayout defaultLayout
Displays objects with {...}, arrays with [...], pairs with (,) and strings without hyphens.
Definition: Sprinter.h:203
static const SprinterLayout jsLayout
JavaScript layout. Like JSON layout, but keys without hyphens.
Definition: Sprinter.h:224
static std::ostream & toStream(std::ostream &ostr, bool b, const SprinterLayout &layout=defaultLayout)
Single char gets styled same way as strings.
Definition: Sprinter.h:490
static std::ostream & toStream(std::ostream &ostr, const D *x, const SprinterLayout &layout=defaultLayout)
Pointer: redirect to actual target object.
Definition: Sprinter.h:511
static std::ostream & treeToStream(std::ostream &ostr, const T &tree, const drain::SprinterLayout &layout, short indent=0)
Write drain::Tree's or any trees that have tree::data[] member.
Definition: Sprinter.h:529
static std::ostream & toStream(std::ostream &ostr, const std::initializer_list< T > &x, const SprinterLayout &layout=defaultLayout)
New (experimental)
Definition: Sprinter.h:420
static const SprinterLayout lineLayout
Put each array and object element on a separate line.
Definition: Sprinter.h:212
static std::ostream & mapPairToStream(std::ostream &ostr, const std::pair< K, V > &x, const SprinterLayout &layout)
If (key,value) pairs needs specific layout in maps, use this. (Compare with list of tuples).
Definition: Sprinter.h:297
static std::ostream & toStream(std::ostream &ostr, char c, const SprinterLayout &layout=defaultLayout)
Single char gets styled same way as strings.
Definition: Sprinter.h:484
static const SprinterLayout cppLayout
C++ code initializer list style: all objects with {...}, with comma ',' separator.
Definition: Sprinter.h:236
static std::ostream & mapToStream(std::ostream &ostr, const M &m, const SprinterLayout &layout, const K &keys)
Given a sequence or subsequence of keys, output values of a map in that order.
Definition: Sprinter.h:360
static const SprinterLayout UNSET_LAYOUT
Marker for unset layout.
Definition: Sprinter.h:200
static const SprinterLayout cmdLineLayout
Simulates how arguments are given to command line options.
Definition: Sprinter.h:248
static std::ostream & basicToStream(std::ostream &ostr, const T &x, const TypeLayout &myChars)
Routine for non-sequence types that may like prefix and suffix, anyway.
Definition: Sprinter.h:408
static const SprinterLayout jsonLayout
Resembles JSON structure: {"a":1,"b":22,"c":3}.
Definition: Sprinter.h:221
static std::ostream & toStream(std::ostream &ostr, const D &x, const SprinterLayout &layout=defaultLayout)
Default, unformatted output for basetypes and types not matching the other templates.
Definition: Sprinter.h:518
Definition: Sprinter.h:603
tuplebase_t & assignSequence(T &sequence, bool LENIENT=false)
Proposed for tuples only; derived classes should not shadow this.
Definition: TupleBase.h:244
Small container for printing style for putting of structured objects (array, maps,...
Definition: Sprinter.h:58
Tuple of N elements of type T.
Definition: UniTuple.h:65
Definition: DataSelector.cpp:1277
Definition: Sprinter.h:137
Definition: Sprinter.h:80
TypeLayout(const char layout[4])
Constructor accepting three-letter chars: {prefix,separator,suffix}.
Definition: Sprinter.h:102
TypeLayout & setLayout(const char *layout)
Set layout with a single string, for example: "{,}" .
Definition: Sprinter.cpp:41
TypeLayout(cstr_t prefix, cstr_t separator, cstr_t suffix)
Constructor.
Definition: Sprinter.h:97
TypeLayout(cstr_t separator=',')
Constructor for simple layout using a separator but no parentheses/braces.
Definition: Sprinter.h:84
TypeLayout(cstr_t prefix, cstr_t suffix)
Constructor.
Definition: Sprinter.h:92