tmp/tmp_hvflusd/{from.md → to.md}
RENAMED
|
@@ -1,192 +0,0 @@
|
|
| 1 |
-
### Program-defined facets <a id="facets.examples">[[facets.examples]]</a>
|
| 2 |
-
|
| 3 |
-
A C++program may define facets to be added to a locale and used
|
| 4 |
-
identically as the built-in facets. To create a new facet interface,
|
| 5 |
-
C++programs simply derive from `locale::facet` a class containing a
|
| 6 |
-
static member: `static locale::id id`.
|
| 7 |
-
|
| 8 |
-
[*Note 1*: The locale member function templates verify its type and
|
| 9 |
-
storage class. — *end note*]
|
| 10 |
-
|
| 11 |
-
[*Example 1*:
|
| 12 |
-
|
| 13 |
-
Traditional global localization is still easy:
|
| 14 |
-
|
| 15 |
-
``` cpp
|
| 16 |
-
#include <iostream>
|
| 17 |
-
#include <locale>
|
| 18 |
-
int main(int argc, char** argv) {
|
| 19 |
-
using namespace std;
|
| 20 |
-
locale::global(locale("")); // set the global locale
|
| 21 |
-
// imbue it on all the std streams
|
| 22 |
-
cin.imbue(locale());
|
| 23 |
-
cout.imbue(locale());
|
| 24 |
-
cerr.imbue(locale());
|
| 25 |
-
wcin.imbue(locale());
|
| 26 |
-
wcout.imbue(locale());
|
| 27 |
-
wcerr.imbue(locale());
|
| 28 |
-
|
| 29 |
-
return MyObject(argc, argv).doit();
|
| 30 |
-
}
|
| 31 |
-
```
|
| 32 |
-
|
| 33 |
-
— *end example*]
|
| 34 |
-
|
| 35 |
-
[*Example 2*:
|
| 36 |
-
|
| 37 |
-
Greater flexibility is possible:
|
| 38 |
-
|
| 39 |
-
``` cpp
|
| 40 |
-
#include <iostream>
|
| 41 |
-
#include <locale>
|
| 42 |
-
int main() {
|
| 43 |
-
using namespace std;
|
| 44 |
-
cin.imbue(locale("")); // the user's preferred locale
|
| 45 |
-
cout.imbue(locale::classic());
|
| 46 |
-
double f;
|
| 47 |
-
while (cin >> f) cout << f << endl;
|
| 48 |
-
return (cin.fail() != 0);
|
| 49 |
-
}
|
| 50 |
-
```
|
| 51 |
-
|
| 52 |
-
In a European locale, with input `3.456,78`, output is `3456.78`.
|
| 53 |
-
|
| 54 |
-
— *end example*]
|
| 55 |
-
|
| 56 |
-
This can be important even for simple programs, which may need to write
|
| 57 |
-
a data file in a fixed format, regardless of a user’s preference.
|
| 58 |
-
|
| 59 |
-
[*Example 3*:
|
| 60 |
-
|
| 61 |
-
Here is an example of the use of locales in a library interface.
|
| 62 |
-
|
| 63 |
-
``` cpp
|
| 64 |
-
// file: Date.h
|
| 65 |
-
#include <iosfwd>
|
| 66 |
-
#include <string>
|
| 67 |
-
#include <locale>
|
| 68 |
-
|
| 69 |
-
class Date {
|
| 70 |
-
public:
|
| 71 |
-
Date(unsigned day, unsigned month, unsigned year);
|
| 72 |
-
std::string asString(const std::locale& = std::locale());
|
| 73 |
-
};
|
| 74 |
-
|
| 75 |
-
std::istream& operator>>(std::istream& s, Date& d);
|
| 76 |
-
std::ostream& operator<<(std::ostream& s, Date d);
|
| 77 |
-
```
|
| 78 |
-
|
| 79 |
-
This example illustrates two architectural uses of class `locale`.
|
| 80 |
-
|
| 81 |
-
The first is as a default argument in `Date::asString()`, where the
|
| 82 |
-
default is the global (presumably user-preferred) locale.
|
| 83 |
-
|
| 84 |
-
The second is in the operators `<<` and `>>`, where a locale
|
| 85 |
-
“hitchhikes” on another object, in this case a stream, to the point
|
| 86 |
-
where it is needed.
|
| 87 |
-
|
| 88 |
-
``` cpp
|
| 89 |
-
// file: Date.C
|
| 90 |
-
#include "Date" // includes <ctime>
|
| 91 |
-
#include <sstream>
|
| 92 |
-
std::string Date::asString(const std::locale& l) {
|
| 93 |
-
using namespace std;
|
| 94 |
-
ostringstream s; s.imbue(l);
|
| 95 |
-
s << *this; return s.str();
|
| 96 |
-
}
|
| 97 |
-
|
| 98 |
-
std::istream& operator>>(std::istream& s, Date& d) {
|
| 99 |
-
using namespace std;
|
| 100 |
-
istream::sentry cerberos(s);
|
| 101 |
-
if (cerberos) {
|
| 102 |
-
ios_base::iostate err = goodbit;
|
| 103 |
-
struct tm t;
|
| 104 |
-
use_facet<time_get<char>>(s.getloc()).get_date(s, 0, s, err, &t);
|
| 105 |
-
if (!err) d = Date(t.tm_day, t.tm_mon + 1, t.tm_year + 1900);
|
| 106 |
-
s.setstate(err);
|
| 107 |
-
}
|
| 108 |
-
return s;
|
| 109 |
-
}
|
| 110 |
-
```
|
| 111 |
-
|
| 112 |
-
— *end example*]
|
| 113 |
-
|
| 114 |
-
A locale object may be extended with a new facet simply by constructing
|
| 115 |
-
it with an instance of a class derived from `locale::facet`. The only
|
| 116 |
-
member a C++program must define is the static member `id`, which
|
| 117 |
-
identifies your class interface as a new facet.
|
| 118 |
-
|
| 119 |
-
[*Example 4*:
|
| 120 |
-
|
| 121 |
-
Classifying Japanese characters:
|
| 122 |
-
|
| 123 |
-
``` cpp
|
| 124 |
-
// file: <jctype>
|
| 125 |
-
#include <locale>
|
| 126 |
-
namespace My {
|
| 127 |
-
using namespace std;
|
| 128 |
-
class JCtype : public locale::facet {
|
| 129 |
-
public:
|
| 130 |
-
static locale::id id; // required for use as a new locale facet
|
| 131 |
-
bool is_kanji (wchar_t c) const;
|
| 132 |
-
JCtype() { }
|
| 133 |
-
protected:
|
| 134 |
-
~JCtype() { }
|
| 135 |
-
};
|
| 136 |
-
}
|
| 137 |
-
|
| 138 |
-
// file: filt.C
|
| 139 |
-
#include <iostream>
|
| 140 |
-
#include <locale>
|
| 141 |
-
#include "jctype" // above
|
| 142 |
-
std::locale::id My::JCtype::id; // the static JCtype member declared above.
|
| 143 |
-
|
| 144 |
-
int main() {
|
| 145 |
-
using namespace std;
|
| 146 |
-
using wctype = ctype<wchar_t>;
|
| 147 |
-
locale loc(locale(""), // the user's preferred locale ...
|
| 148 |
-
new My::JCtype); // and a new feature ...
|
| 149 |
-
wchar_t c = use_facet<wctype>(loc).widen('!');
|
| 150 |
-
if (!use_facet<My::JCtype>(loc).is_kanji(c))
|
| 151 |
-
cout << "no it isn't!" << endl;
|
| 152 |
-
}
|
| 153 |
-
```
|
| 154 |
-
|
| 155 |
-
The new facet is used exactly like the built-in facets.
|
| 156 |
-
|
| 157 |
-
— *end example*]
|
| 158 |
-
|
| 159 |
-
[*Example 5*:
|
| 160 |
-
|
| 161 |
-
Replacing an existing facet is even easier. The code does not define a
|
| 162 |
-
member `id` because it is reusing the `numpunct<charT>` facet interface:
|
| 163 |
-
|
| 164 |
-
``` cpp
|
| 165 |
-
// file: my_bool.C
|
| 166 |
-
#include <iostream>
|
| 167 |
-
#include <locale>
|
| 168 |
-
#include <string>
|
| 169 |
-
namespace My {
|
| 170 |
-
using namespace std;
|
| 171 |
-
using cnumpunct = numpunct_byname<char>;
|
| 172 |
-
class BoolNames : public cnumpunct {
|
| 173 |
-
protected:
|
| 174 |
-
string do_truename() const { return "Oui Oui!"; }
|
| 175 |
-
string do_falsename() const { return "Mais Non!"; }
|
| 176 |
-
~BoolNames() { }
|
| 177 |
-
public:
|
| 178 |
-
BoolNames(const char* name) : cnumpunct(name) { }
|
| 179 |
-
};
|
| 180 |
-
}
|
| 181 |
-
|
| 182 |
-
int main(int argc, char** argv) {
|
| 183 |
-
using namespace std;
|
| 184 |
-
// make the user's preferred locale, except for...
|
| 185 |
-
locale loc(locale(""), new My::BoolNames(""));
|
| 186 |
-
cout.imbue(loc);
|
| 187 |
-
cout << boolalpha << "Any arguments today? " << (argc > 1) << endl;
|
| 188 |
-
}
|
| 189 |
-
```
|
| 190 |
-
|
| 191 |
-
— *end example*]
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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