Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
fmtflags_manip.cpp
Go to the documentation of this file.
1
29#include <ios> // for hex
30#include <iomanip> // for __rw_smanip
31#include <iostream> // for cout
32
33#include <examples.h>
34
35
36// implementation class of the fmtflags manipulator that saves
37// a stream object's fmtflags state before optionally setting
38// their new value and restores the saved state when destroyed
39// uses ios::basefield as an invalid flags value (it's a valid
40// mask but not a valid flags value)
42{
43 std::ios_base *strm_;
44 std::ios_base::fmtflags saved_;
45
46public:
47 fmtflags_manip (): strm_ (), saved_ (std::ios_base::basefield) { }
48
49 void operator() (std::ios_base &strm,
50 std::ios_base::fmtflags flags) const {
51
52 const_cast<fmtflags_manip*>(this)->strm_ = &strm;
53 const_cast<fmtflags_manip*>(this)->saved_ = strm.flags ();
54
55 if (flags != std::ios_base::basefield)
56 strm.flags (flags);
57 }
58
60 if (strm_)
61 strm_->flags (saved_);
62 }
63};
64
65
66// saveflags manipulator to temporarily set formatting flags and
67// restore their initial value at the end of an insertion statement
68inline std::__rw_smanip<fmtflags_manip, std::ios_base::fmtflags>
69saveflags (std::ios_base::fmtflags flags = std::ios_base::basefield)
70{
71 typedef std::__rw_smanip<fmtflags_manip, std::ios_base::fmtflags> Manip;
72
73 return Manip (flags);
74}
75
76
77// dummy class whose insertion operator always throws
78struct BadClass { };
79
80
81inline std::ostream&
82operator<< (std::ostream &strm, BadClass&)
83{
84 // inserter that always throws an exception
85 return throw "exception", strm;
86
87}
88
89
90int main ()
91{
92 try {
93 BadClass throw_exception;
94
95 // use the saveflags manipulator to set the formatting flags
96 // for the rest of this insertion statement only and have it
97 // restore the previous flags at the end of the statement
98 // or on exception
99 std::cout << saveflags (std::cout.hex)
100 << std::showbase
101 << "hex 1234: " << 0x1234 << '\n'
102 << "hex 2345: " << 0x2345 << '\n'
103 << throw_exception // inserting will throw
104 << "never output!"; // this string is never output
105 }
106 catch (...) {
107 // show that default formatting flags (decimal output and no
108 // showbase) have been restored during stack unwinding above
109 std::cout << "dec 3456: " << 3456 << '\n'
110 << std::oct
111 << "oct 4567: " << 04567 << '\n';
112 }
113
114 // use ordinary manipulators to set hex output with base prefix
115 std::cout << std::hex << std::showbase
116 << "hex 5678: " << 0x5678 << '\n'
117 << "hex 6789: " << 0x6789 << '\n';
118
119 // the same flags set above are still in effect
120 std::cout << "hex 789a: " << 0x789a << '\n';
121
122 return 0;
123}
#define strm
Definition 17.names.cpp:549
#define hex
#define basefield
#define flags
void operator()(std::ios_base &strm, std::ios_base::fmtflags flags) const
std::ostream & operator<<(std::ostream &strm, BadClass &)
std::__rw_smanip< fmtflags_manip, std::ios_base::fmtflags > saveflags(std::ios_base::fmtflags flags=std::ios_base::basefield)
int main()
Definition json.h:5363