Honeycomb  0.1
Component-Model Framework
Stream.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
4 #include "Honey/String/String.h"
5 #include "Honey/Misc/StdUtil.h"
7 
8 namespace honey
9 {
10 
13 
15 inline ostringstream sout() { return ostringstream(); }
16 
18 namespace stringstream
19 {
21  namespace priv
22  {
23  struct Indent : Manip<Indent>
24  {
25  int level = 0;
26  int size = 4;
27  };
28  }
31  inline ostream& indentInc(ostream& os) { ++priv::Indent::inst(os).level; return os; }
34  inline ostream& indentDec(ostream& os) { --priv::Indent::inst(os).level; return os; }
36  inline auto indentSize(int size) { return manipFunc([=](ostream& os) { priv::Indent::inst(os).size = size; }); }
37 }
38 
40 inline ostream& endl(ostream& os)
41 {
42  os << std::endl;
43  if (stringstream::priv::Indent::hasInst(os))
44  {
45  auto& indent = stringstream::priv::Indent::inst(os);
46  for (int i = 0, end = indent.level * indent.size; i < end; ++i) os << ' ';
47  }
48  return os;
49 }
50 
52 
53 }
54 
56 namespace std
57 {
60 
62  inline ostream& operator<<(ostream& os, const exception& e) { return os << e.what(); }
64  template<class T1, class T2>
65  ostream& operator<<(ostream& os, const pair<T1,T2>& p) { return os << "[" << p.first << ", " << p.second << "]"; }
67  namespace priv
68  {
69  template<class Tuple, size_t... Seq>
70  void tupleToString(ostream& os, Tuple&& t, honey::mt::idxseq<Seq...>)
71  { os << "["; honey::mt::exec([&]() { os << get<Seq>(forward<Tuple>(t)) << (Seq < sizeof...(Seq)-1 ? ", " : ""); }...); os << "]"; }
72  template<class List>
73  void listToString(ostream& os, const List& list) { size_t i = 0; os << "["; for (auto& e: list) os << (i++ > 0 ? ", " : "") << e; os << "]"; }
74  }
76  template<class Tuple>
78  typename enable_if<honey::mt::isTuple<Tuple>::value, ostream&>::type
79  operator<<(ostream& os, Tuple&& t) { priv::tupleToString(os, forward<Tuple>(t), honey::mt::make_idxseq<tuple_size<typename honey::mt::removeRef<Tuple>::type>::value>()); return os; }
81  template<class T, size_t N>
82  ostream& operator<<(ostream& os, const array<T,N>& a) { priv::listToString(os, a); return os; }
84  template<class T, size_t N, typename std::enable_if<
85  !std::is_same<T,char>::value && //stdlib handles char/uint8
86  !std::is_same<T,honey::uint8>::value &&
87  !std::is_same<T,honey::Char>::value,int>::type=0> //String handles Char
88  ostream& operator<<(ostream& os, const T (&a)[N]) { priv::listToString(os, a); return os; }
90  template<class T, class Alloc>
91  ostream& operator<<(ostream& os, const vector<T,Alloc>& vec)
92  { priv::listToString(os, vec); return os; }
94  template<class T, class Compare, class Alloc>
95  ostream& operator<<(ostream& os, const set<T,Compare,Alloc>& set)
96  { priv::listToString(os, set); return os; }
98  template<class T, class Compare, class Alloc>
99  ostream& operator<<(ostream& os, const multiset<T,Compare,Alloc>& set)
100  { priv::listToString(os, set); return os; }
102  template<class Key, class Hash, class KeyEqual, class Alloc>
103  ostream& operator<<(ostream& os, const unordered_set<Key,Hash,KeyEqual,Alloc>& set)
104  { priv::listToString(os, set); return os; }
106  template<class Key, class Hash, class KeyEqual, class Alloc>
107  ostream& operator<<(ostream& os, const unordered_multiset<Key,Hash,KeyEqual,Alloc>& set)
108  { priv::listToString(os, set); return os; }
110  template<class Key, class T, class Compare, class Alloc>
111  ostream& operator<<(ostream& os, const map<Key,T,Compare,Alloc>& map)
112  { priv::listToString(os, map); return os; }
114  template<class Key, class T, class Compare, class Alloc>
115  ostream& operator<<(ostream& os, const multimap<Key,T,Compare,Alloc>& map)
116  { priv::listToString(os, map); return os; }
118  template<class Key, class T, class Hash, class KeyEqual, class Alloc>
119  ostream& operator<<(ostream& os, const unordered_map<Key,T,Hash,KeyEqual,Alloc>& map)
120  { priv::listToString(os, map); return os; }
122  template<class Key, class T, class Hash, class KeyEqual, class Alloc>
123  ostream& operator<<(ostream& os, const unordered_multimap<Key,T,Hash,KeyEqual,Alloc>& map)
124  { priv::listToString(os, map); return os; }
126  template<class T, class Fin>
127  ostream& operator<<(ostream& os, const honey::UniquePtr<T,Fin>& p)
128  { return p ? os << *p : os << "nullptr"; }
130  template<class T>
131  ostream& operator<<(ostream& os, const honey::SharedPtr<T>& p)
132  { return p ? os << *p : os << "nullptr"; }
134 }
135 
auto manipFunc(Func &&f, Args &&...args)
Helper to create a manipulator that takes arguments. eg. A manip named 'foo': auto foo(int val) { ret...
Definition: StdUtil.h:243
ostream & operator<<(ostream &os, const honey::SharedPtr< T > &p)
SharedPtr to string, outputs object pointed to or 'nullptr'.
Definition: Stream.h:131
STL namespace.
std::index_sequence< Ints... > idxseq
Shorthand for std::index_sequence.
Definition: Meta.h:111
std::remove_reference< T > removeRef
Remove reference from type.
Definition: Meta.h:44
void exec()
Definition: Meta.h:134
ostringstream sout()
Shorthand to create ostringstream.
Definition: Stream.h:15
ostream & indentInc(ostream &os)
Increase stream indent level by 1.
Definition: Stream.h:32
ostream & indentDec(ostream &os)
Decrease stream indent level by 1.
Definition: Stream.h:34
int size(const StdContainer &cont)
Safely get the size of a std container as a signed integer.
Definition: StdUtil.h:19
std::make_index_sequence< N > make_idxseq
Shorthand for std::make_index_sequence.
Definition: Meta.h:113
Base class to hold iostream manipulator state. Inherit from this class and call Subclass::inst(ios) t...
Definition: StdUtil.h:206
auto indentSize(int size)
Set number of spaces per indent level.
Definition: Stream.h:36
ostream & endl(ostream &os)
End line and apply any indentation to the next line.
Definition: Stream.h:40
Global Honeycomb namespace.
OutSeq && map(Range &&, Seqs &&..., OutSeq &&, Func &&)
Transform a series of sequences into an output.