TooN Algorithm Library - tag  0.2
stdpp.h
Go to the documentation of this file.
1 #ifndef TAG_STDPP_H
2 #define TAG_STDPP_H
3 
4 #include <iostream>
5 #include <utility>
6 
7 namespace tag {
8 
11 
38 #ifndef DOXYGEN_IGNORE_INTERNAL
39 
40 static struct noendl_s {} noendl;
41 
42 template <class T> struct NotFirst {
43  inline NotFirst(T & d) : data(d), last(true) {}
44  inline ~NotFirst() { if(last) data << std::endl; }
45  T & data;
46  bool last;
47 
48  template <class S>
49  inline NotFirst<T> & operator,( const S & arg ){
50  data << data.fill() << arg;
51  return *this;
52  }
53 
54  inline T & operator,(T & (*modifier)(T &)){
55  data << modifier;
56  last = false;
57  return data;
58  }
59 
60  inline NotFirst<T> & operator,( noendl_s & arg ){
61  last = false;
62  return *this;
63  }
64 };
65 
66 template <class T, class Char, class Traits>
67 inline NotFirst<std::basic_ostream<Char,Traits> > operator,(std::basic_ostream<Char,Traits> & stream, const T & arg ){
68  stream << arg;
69  return NotFirst<std::basic_ostream<Char,Traits> >(stream);
70 }
71 
72 template <class Char, class Traits>
73 inline std::basic_ostream<Char,Traits> & operator,(std::basic_ostream<Char,Traits> & stream, std::basic_ostream<Char,Traits> & (*modifier)(std::basic_ostream<Char,Traits> &)){
74  stream << modifier;
75  return stream;
76 }
77 
78 template <class Char, class Traits>
79 inline std::basic_ostream<Char,Traits> & operator,(std::basic_ostream<Char,Traits> & stream, noendl_s & arg ){
80  return stream;
81 }
82 
83 namespace Internal
84 {
85 
86  struct add_fill_s{};
87  struct like_print_s{};
88  struct no_space_s{};
89 
90  template<class S> struct add_fill_bound
91  {
92  add_fill_bound(S& os)
93  :o(os),first(1) {}
94 
95 
96  template<class C> add_fill_bound& operator<<(const C& c)
97  {
98  if(first == true)
99  first=false;
100  else
101  o << o.fill();
102 
103  o << c;
104  return *this;
105  }
106 
107  add_fill_bound& operator<<(const no_space_s&)
108  {
109  first=true;
110  return *this;
111  }
112 
113  add_fill_bound& operator<<(S& (*fptr)(S&) )
114  {
115  o << fptr;
116 
117  if(fptr == static_cast<S&(*)(S&)>(std::endl))
118  first=true;
119 
120  return *this;
121  }
122 
123  private:
124  S& o;
125  bool first;
126  };
127 
128 
129  template<class S> struct like_print_bound:public add_fill_bound<S>
130  {
131  like_print_bound(S&os)
132  :add_fill_bound<S>(os)
133  {
134  }
135 
136  ~like_print_bound()
137  {
139  }
140  };
141 
142 }
143 
144 #endif
145 
159 static struct Internal::add_fill_s add_fill;
160 
174 static struct Internal::like_print_s print;
175 
188 static struct Internal::no_space_s no_space;
189 
190 #ifndef DOXYGEN_IGNORE_INTERNAL
191 
192 template<class Char, class Traits> Internal::add_fill_bound<std::basic_ostream<Char,Traits> > operator<<(std::basic_ostream<Char,Traits>& o, const Internal::add_fill_s&)
193 {
194  return Internal::add_fill_bound<std::basic_ostream<Char,Traits> >(o);
195 }
196 
197 template<class Char, class Traits> Internal::like_print_bound<std::basic_ostream<Char,Traits> > operator<<(std::basic_ostream<Char,Traits>& o, const Internal::like_print_s&)
198 {
199  return Internal::like_print_bound<std::basic_ostream<Char,Traits> >(o);
200 }
201 
202 #endif
203 
204 #ifndef DOXYGEN_IGNORE_INTERNAL
205 namespace Internal
206 {
207  template<class A, class B> struct refpair
208  {
209  A& a;
210  B& b;
211  refpair(A& aa, B& bb)
212  :a(aa),b(bb)
213  {}
214 
215  void operator=(const std::pair<A,B>& p)
216  {
217  a=p.first;
218  b=p.second;
219  }
220  };
221 }
222 
223 #endif
224 
236 template<class A, class B> Internal::refpair<A,B> rpair(A&aa, B&bb)
237 {
238  return Internal::refpair<A,B>(aa, bb);
239 }
240 
241 
242 
243 } // namespace tag
244 
245 #endif // __PRINT_H_