Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
27.basic.ios.tie.stdcxx-804.cpp
Go to the documentation of this file.
1
31#include <cassert>
32#include <cstddef>
33#include <cstdio>
34#include <sstream>
35
36// tie the first n streams together in a circular list
37void tie (std::ostringstream *ps, std::size_t n)
38{
39 for (std::size_t i = 0; i + 1 < n; ++i)
40 ps [i].tie (ps + i + 1);
41
42 if (n) {
43 std::ostream *plast = ps + (n - 1);
44 plast->tie (ps);
45 }
46}
47
48
49// detect a cycle in a list of tied streams
50bool is_cycle (const std::ostream *ps)
51{
52 if (0 == ps)
53 return false;
54
55 const std::ostream *p0, *p1;
56
57 for (p0 = ps, p1 = ps->tie (); p0 && p1;
58 p0 = p0->tie (), p1 = p1->tie () ? p1->tie ()->tie () : 0)
59 if (p0 == p1 || p0->rdbuf () == p1->rdbuf ())
60 return true;
61
62 return false;
63}
64
65int main ()
66{
67 std::ostringstream strm [32];
68 const std::size_t N = sizeof strm / sizeof *strm;
69
70 for (std::size_t i = 0; i != N; ++i) {
71
72 // tie the first i streams together in a circular list
73 tie (strm, i);
74
75 assert (0 == i || is_cycle (strm));
76
77 for (std::size_t j = 0; j != i; ++j) {
78
79 // make sure formatted and unformatted output for
80 // each of the tied streams works without getting
81 // into a deadlock or into infinite recursion
82 strm [j] << j;
83 strm [j].flush ();
84
85 assert (strm [j].good ());
86 }
87 }
88
89 return 0;
90}
#define N
Definition 17.names.cpp:157
#define i
Definition 17.names.cpp:126
#define j
Definition 17.names.cpp:127
#define n
Definition 17.names.cpp:130
#define p1
Definition 17.names.cpp:239
#define strm
Definition 17.names.cpp:549
bool is_cycle(const std::ostream *ps)
void tie(std::ostringstream *ps, std::size_t n)
#define assert(expr)
Definition _cassert.h:40