From Jason Turner

[facets.examples]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpk08x_d_9/{from.md → to.md} +24 -5
tmp/tmpk08x_d_9/{from.md → to.md} RENAMED
@@ -3,11 +3,14 @@
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
- The locale member function templates verify its type and storage class.
 
 
 
9
 
10
  Traditional global localization is still easy:
11
 
12
  ``` cpp
13
  #include <iostream>
@@ -25,10 +28,14 @@ int main(int argc, char** argv) {
25
 
26
  return MyObject(argc, argv).doit();
27
  }
28
  ```
29
 
 
 
 
 
30
  Greater flexibility is possible:
31
 
32
  ``` cpp
33
  #include <iostream>
34
  #include <locale>
@@ -42,13 +49,17 @@ int main() {
42
  }
43
  ```
44
 
45
  In a European locale, with input `3.456,78`, output is `3456.78`.
46
 
 
 
47
  This can be important even for simple programs, which may need to write
48
  a data file in a fixed format, regardless of a user’s preference.
49
 
 
 
50
  Here is an example of the use of locales in a library interface.
51
 
52
  ``` cpp
53
  // file: Date.h
54
  #include <iosfwd>
@@ -96,15 +107,19 @@ std::istream& operator>>(std::istream& s, Date& d) {
96
  }
97
  return s;
98
  }
99
  ```
100
 
 
 
101
  A locale object may be extended with a new facet simply by constructing
102
  it with an instance of a class derived from `locale::facet`. The only
103
  member a C++program must define is the static member `id`, which
104
  identifies your class interface as a new facet.
105
 
 
 
106
  Classifying Japanese characters:
107
 
108
  ``` cpp
109
  // file: <jctype>
110
  #include <locale>
@@ -126,33 +141,36 @@ namespace My {
126
  #include "jctype" // above
127
  std::locale::id My::JCtype::id; // the static JCtype member declared above.
128
 
129
  int main() {
130
  using namespace std;
131
- typedef ctype<wchar_t> wctype;
132
  locale loc(locale(""), // the user's preferred locale ...
133
  new My::JCtype); // and a new feature ...
134
  wchar_t c = use_facet<wctype>(loc).widen('!');
135
  if (!use_facet<My::JCtype>(loc).is_kanji(c))
136
  cout << "no it isn't!" << endl;
137
- return 0;
138
  }
139
  ```
140
 
141
  The new facet is used exactly like the built-in facets.
142
 
 
 
 
 
143
  Replacing an existing facet is even easier. The code does not define a
144
  member `id` because it is reusing the `numpunct<charT>` facet interface:
145
 
146
  ``` cpp
147
  // file: my_bool.C
148
  #include <iostream>
149
  #include <locale>
150
  #include <string>
151
  namespace My {
152
  using namespace std;
153
- typedef numpunct_byname<char> cnumpunct;
154
  class BoolNames : public cnumpunct {
155
  protected:
156
  string do_truename() const { return "Oui Oui!"; }
157
  string do_falsename() const { return "Mais Non!"; }
158
  ~BoolNames() { }
@@ -165,9 +183,10 @@ int main(int argc, char** argv) {
165
  using namespace std;
166
  // make the user's preferred locale, except for...
167
  locale loc(locale(""), new My::BoolNames(""));
168
  cout.imbue(loc);
169
  cout << boolalpha << "Any arguments today? " << (argc > 1) << endl;
170
- return 0;
171
  }
172
  ```
173
 
 
 
 
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>
 
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>
 
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>
 
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>
 
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() { }
 
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
+