Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
money_manip.cpp
Go to the documentation of this file.
1
29#include <iomanip> // for __rw_smanip
30#include <iostream> // for cout
31#include <locale> // for money_get, money_put
32
33#include <examples.h>
34
35
36// implementation class of the extraction manipulator for monetary values
37template <class moneyT>
39{
40 moneyT *pval_;
41
42 explicit money_get_manip (moneyT *pval)
43 : pval_ (pval) { }
44
45 template <class charT, class Traits>
46 void
47 operator() (std::basic_ios<charT, Traits> &strm, bool intl) const {
48 typedef std::istreambuf_iterator<charT> Iter;
49 typedef std::money_get<charT, Iter> MoneyGet;
50
51 std::ios_base::iostate err = std::ios_base::goodbit;
52 const MoneyGet &mg = std::use_facet<MoneyGet>(strm.getloc ());
53
54 // extract the monetary value from the stream
55 mg.get (Iter (strm.rdbuf ()), Iter (), intl, strm, err, *pval_);
56
57 if (std::ios_base::goodbit != err)
58 strm.setstate (err);
59 }
60};
61
62
63// implementation class of the insertion manipulator for monetary values
64template <class moneyT>
66{
67 const moneyT &val_;
68
69 explicit money_put_manip (const moneyT &val)
70 : val_ (val) { }
71
72 template <class charT, class Traits>
73 void
74 operator() (std::basic_ios<charT, Traits> &strm, bool intl) const {
75 typedef std::ostreambuf_iterator<charT> Iter;
76 typedef std::money_put<charT, Iter> MoneyPut;
77
78 const MoneyPut &tp = std::use_facet<MoneyPut>(strm.getloc ());
79
80 // insert the monetary value into the stream
81 const Iter end =
82 tp.put (Iter (strm.rdbuf ()), intl, strm, strm.fill (), val_);
83
84 if (end.failed ())
85 strm.setstate (std::ios_base::badbit);
86 }
87};
88
89
90// manipulator for the extraction of monetary long double values
91inline std::__rw_smanip<money_get_manip<long double>, bool>
92get_money (long double *pval, bool intl = false)
93{
94 // return an object of the manipulator implementation type
95 // that will store the function arguments until its function
96 // call operator is invoked by the extraction operator for
97 // std::__rw_smanip
98 typedef money_get_manip<long double> GetMoney;
99 typedef std::__rw_smanip<GetMoney, bool> Manip;
100
101 return Manip (GetMoney (pval), intl);
102}
103
104
105// manipulator for the extraction of monetary strings
106template <class charT>
107inline std::__rw_smanip<money_get_manip<std::basic_string<charT> >, bool>
108get_money (std::basic_string<charT> *pval, bool intl = false)
109{
110 // return an object of the manipulator implementation type
111 // that will store the function arguments until its function
112 // call operator is invoked by the extraction operator for
113 // std::__rw_smanip
114 typedef std::basic_string<charT> String;
115 typedef money_get_manip<String> GetMoney;
116 typedef std::__rw_smanip<GetMoney, bool> Manip;
117
118 return Manip (GetMoney (pval), intl);
119}
120
121
122// manipulator for the insertion of monetary long double values
123inline std::__rw_smanip<money_put_manip<long double>, bool>
124put_money (const long double &val, bool intl = false)
125{
126 // return an object of the manipulator implementation type
127 // that will store the function arguments until its function
128 // call operator is invoked by the insertion operator for
129 // std::__rw_smanip
130 typedef money_put_manip<long double> PutMoney;
131 typedef std::__rw_smanip<PutMoney, bool> Manip;
132
133 return Manip (PutMoney (val), intl);
134}
135
136
137// manipulator for the insertion of monetary strings
138template <class charT>
139inline std::__rw_smanip<money_put_manip<std::basic_string<charT> >, bool>
140put_money (const std::basic_string<charT> &val, bool intl = false)
141{
142 // return an object of the manipulator implementation type
143 // that will store the function arguments until its function
144 // call operator is invoked by the insertion operator for
145 // std::__rw_smanip
146 typedef std::basic_string<charT> String;
147 typedef money_put_manip<String> PutMoney;
148 typedef std::__rw_smanip<PutMoney, bool> Manip;
149
150 return Manip (PutMoney (val), intl);
151}
152
153
154int main (int argc, char *argv[])
155{
156 const std::string valstr = argc < 2 ? "0" : argv [1];
157
158 // set cin and cout's locale to the one specified in the environment
159 std::cin.imbue (std::locale (""));
160 std::cout.imbue (std::cin.getloc ());
161
162 // output the monetary value specified by the command line argument,
163 // including the currency symbol, in both the local and international
164 // formats
165 std::cout.setf (std::cout.showbase);
166 std::cout << put_money (valstr) << '\n';
167 std::cout << put_money (valstr, true) << '\n';
168
169 long double val = 0.0L;
170
171 // input a long double monetary value
172 std::cin >> get_money (&val);
173
174 // if the input was successful, output the extracted monetary value
175 // otherwise, output an error message
176 if (std::cin.good ()) {
177 std::cout << put_money (val) << '\n';
178 std::cout << put_money (val, true) << '\n';
179 }
180 else {
181 std::cerr << "get_money (long double*) failed\n";
182 return 1;
183 }
184
185 return 0;
186}
#define strm
Definition 17.names.cpp:549
#define val
Definition 17.names.cpp:570
#define showbase
const int end
#define main
std::ostream_iterator< int, char, std::char_traits< char > > Iter
Definition alg7.cpp:64
std::__rw_smanip< money_put_manip< long double >, bool > put_money(const long double &val, bool intl=false)
std::__rw_smanip< money_get_manip< long double >, bool > get_money(long double *pval, bool intl=false)
money_get_manip(moneyT *pval)
void operator()(std::basic_ios< charT, Traits > &strm, bool intl) const
money_put_manip(const moneyT &val)
const moneyT & val_
void operator()(std::basic_ios< charT, Traits > &strm, bool intl) const