OSDN Git Service

gcc/
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / locale_init.cc
1 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 
2 // 2009
3 // Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 #include <clocale>
31 #include <cstring>
32 #include <cstdlib>     // For getenv, free.
33 #include <cctype>
34 #include <cwctype>     // For towupper, etc.
35 #include <locale>
36 #include <ext/concurrence.h>
37
38 namespace 
39 {
40   __gnu_cxx::__mutex&
41   get_locale_mutex()
42   {
43     static __gnu_cxx::__mutex locale_mutex;
44     return locale_mutex;
45   }
46
47   using namespace std;
48
49   typedef char fake_locale_Impl[sizeof(locale::_Impl)]
50   __attribute__ ((aligned(__alignof__(locale::_Impl))));
51   fake_locale_Impl c_locale_impl;
52
53   typedef char fake_locale[sizeof(locale)]
54   __attribute__ ((aligned(__alignof__(locale))));
55   fake_locale c_locale;
56
57   typedef char fake_name_vec[sizeof(char*)]
58   __attribute__ ((aligned(__alignof__(char*))));
59   fake_name_vec name_vec[6 + _GLIBCXX_NUM_CATEGORIES];
60
61   typedef char fake_names[sizeof(char[2])]
62   __attribute__ ((aligned(__alignof__(char[2]))));
63   fake_names name_c[6 + _GLIBCXX_NUM_CATEGORIES];
64
65   typedef char fake_facet_vec[sizeof(locale::facet*)]
66   __attribute__ ((aligned(__alignof__(locale::facet*))));
67   fake_facet_vec facet_vec[_GLIBCXX_NUM_FACETS];
68
69   typedef char fake_cache_vec[sizeof(locale::facet*)]
70   __attribute__ ((aligned(__alignof__(locale::facet*))));
71   fake_cache_vec cache_vec[_GLIBCXX_NUM_FACETS];
72
73   typedef char fake_ctype_c[sizeof(std::ctype<char>)]
74   __attribute__ ((aligned(__alignof__(std::ctype<char>))));
75   fake_ctype_c ctype_c;
76
77   typedef char fake_collate_c[sizeof(std::collate<char>)]
78   __attribute__ ((aligned(__alignof__(std::collate<char>))));
79   fake_collate_c collate_c;
80
81   typedef char fake_numpunct_c[sizeof(numpunct<char>)]
82   __attribute__ ((aligned(__alignof__(numpunct<char>))));
83   fake_numpunct_c numpunct_c;
84
85   typedef char fake_num_get_c[sizeof(num_get<char>)]
86   __attribute__ ((aligned(__alignof__(num_get<char>))));
87   fake_num_get_c num_get_c;
88
89   typedef char fake_num_put_c[sizeof(num_put<char>)]
90   __attribute__ ((aligned(__alignof__(num_put<char>))));
91   fake_num_put_c num_put_c;
92
93   typedef char fake_codecvt_c[sizeof(codecvt<char, char, mbstate_t>)]
94   __attribute__ ((aligned(__alignof__(codecvt<char, char, mbstate_t>))));
95   fake_codecvt_c codecvt_c;
96
97   typedef char fake_moneypunct_c[sizeof(moneypunct<char, true>)]
98   __attribute__ ((aligned(__alignof__(moneypunct<char, true>))));
99   fake_moneypunct_c moneypunct_ct;
100   fake_moneypunct_c moneypunct_cf;
101
102   typedef char fake_money_get_c[sizeof(money_get<char>)]
103   __attribute__ ((aligned(__alignof__(money_get<char>))));
104   fake_money_get_c money_get_c;
105   
106   typedef char fake_money_put_c[sizeof(money_put<char>)]
107   __attribute__ ((aligned(__alignof__(money_put<char>))));
108   fake_money_put_c money_put_c;
109
110   typedef char fake_timepunct_c[sizeof(__timepunct<char>)]
111   __attribute__ ((aligned(__alignof__(__timepunct<char>))));
112   fake_timepunct_c timepunct_c;
113
114   typedef char fake_time_get_c[sizeof(time_get<char>)]
115   __attribute__ ((aligned(__alignof__(time_get<char>))));
116   fake_time_get_c time_get_c;
117
118   typedef char fake_time_put_c[sizeof(time_put<char>)]
119   __attribute__ ((aligned(__alignof__(time_put<char>))));
120   fake_time_put_c time_put_c;
121
122   typedef char fake_messages_c[sizeof(messages<char>)]
123   __attribute__ ((aligned(__alignof__(messages<char>))));
124   fake_messages_c messages_c;
125
126 #ifdef  _GLIBCXX_USE_WCHAR_T
127   typedef char fake_wtype_w[sizeof(std::ctype<wchar_t>)]
128   __attribute__ ((aligned(__alignof__(std::ctype<wchar_t>))));
129   fake_wtype_w ctype_w;
130
131   typedef char fake_wollate_w[sizeof(std::collate<wchar_t>)]
132   __attribute__ ((aligned(__alignof__(std::collate<wchar_t>))));
133   fake_wollate_w collate_w;
134
135   typedef char fake_numpunct_w[sizeof(numpunct<wchar_t>)]
136   __attribute__ ((aligned(__alignof__(numpunct<wchar_t>))));
137   fake_numpunct_w numpunct_w;
138
139   typedef char fake_num_get_w[sizeof(num_get<wchar_t>)]
140   __attribute__ ((aligned(__alignof__(num_get<wchar_t>))));
141   fake_num_get_w num_get_w;
142
143   typedef char fake_num_put_w[sizeof(num_put<wchar_t>)]
144   __attribute__ ((aligned(__alignof__(num_put<wchar_t>))));
145   fake_num_put_w num_put_w;
146
147   typedef char fake_wodecvt_w[sizeof(codecvt<wchar_t, char, mbstate_t>)]
148   __attribute__ ((aligned(__alignof__(codecvt<wchar_t, char, mbstate_t>))));
149   fake_wodecvt_w codecvt_w;
150
151   typedef char fake_moneypunct_w[sizeof(moneypunct<wchar_t, true>)]
152   __attribute__ ((aligned(__alignof__(moneypunct<wchar_t, true>))));
153   fake_moneypunct_w moneypunct_wt;
154   fake_moneypunct_w moneypunct_wf;
155
156   typedef char fake_money_get_w[sizeof(money_get<wchar_t>)]
157   __attribute__ ((aligned(__alignof__(money_get<wchar_t>))));
158   fake_money_get_w money_get_w;
159   
160   typedef char fake_money_put_w[sizeof(money_put<wchar_t>)]
161   __attribute__ ((aligned(__alignof__(money_put<wchar_t>))));
162   fake_money_put_w money_put_w;
163
164   typedef char fake_timepunct_w[sizeof(__timepunct<wchar_t>)]
165   __attribute__ ((aligned(__alignof__(__timepunct<wchar_t>))));
166   fake_timepunct_w timepunct_w;
167
168   typedef char fake_time_get_w[sizeof(time_get<wchar_t>)]
169   __attribute__ ((aligned(__alignof__(time_get<wchar_t>))));
170   fake_time_get_w time_get_w;
171
172   typedef char fake_time_put_w[sizeof(time_put<wchar_t>)]
173   __attribute__ ((aligned(__alignof__(time_put<wchar_t>))));
174   fake_time_put_w time_put_w;
175
176   typedef char fake_messages_w[sizeof(messages<wchar_t>)]
177   __attribute__ ((aligned(__alignof__(messages<wchar_t>))));
178   fake_messages_w messages_w;
179 #endif
180
181   // Storage for "C" locale caches.
182   typedef char fake_num_cache_c[sizeof(std::__numpunct_cache<char>)]
183   __attribute__ ((aligned(__alignof__(std::__numpunct_cache<char>))));
184   fake_num_cache_c numpunct_cache_c;
185
186   typedef char fake_money_cache_c[sizeof(std::__moneypunct_cache<char, true>)]
187   __attribute__ ((aligned(__alignof__(std::__moneypunct_cache<char, true>))));
188   fake_money_cache_c moneypunct_cache_ct;
189   fake_money_cache_c moneypunct_cache_cf;
190
191   typedef char fake_time_cache_c[sizeof(std::__timepunct_cache<char>)]
192   __attribute__ ((aligned(__alignof__(std::__timepunct_cache<char>))));
193   fake_time_cache_c timepunct_cache_c;
194
195 #ifdef _GLIBCXX_USE_WCHAR_T
196   typedef char fake_num_cache_w[sizeof(std::__numpunct_cache<wchar_t>)]
197   __attribute__ ((aligned(__alignof__(std::__numpunct_cache<wchar_t>))));
198   fake_num_cache_w numpunct_cache_w;
199
200   typedef char fake_money_cache_w[sizeof(std::__moneypunct_cache<wchar_t,true>)]
201   __attribute__ ((aligned(__alignof__(std::__moneypunct_cache<wchar_t,true>))));
202   fake_money_cache_w moneypunct_cache_wt;
203   fake_money_cache_w moneypunct_cache_wf;
204
205   typedef char fake_time_cache_w[sizeof(std::__timepunct_cache<wchar_t>)]
206   __attribute__ ((aligned(__alignof__(std::__timepunct_cache<wchar_t>))));
207   fake_time_cache_w timepunct_cache_w;
208 #endif
209 } // anonymous namespace
210
211 _GLIBCXX_BEGIN_NAMESPACE(std)
212
213   locale::locale() throw() : _M_impl(0)
214   { 
215     _S_initialize();
216     __gnu_cxx::__scoped_lock sentry(get_locale_mutex());
217     _S_global->_M_add_reference();
218     _M_impl = _S_global;
219   }
220
221   locale
222   locale::global(const locale& __other)
223   {
224     _S_initialize();
225     _Impl* __old;
226     {
227       __gnu_cxx::__scoped_lock sentry(get_locale_mutex());
228       __old = _S_global;
229       __other._M_impl->_M_add_reference();
230       _S_global = __other._M_impl;
231       const string __other_name = __other.name();
232       if (__other_name != "*")
233         setlocale(LC_ALL, __other_name.c_str());
234     }
235
236     // Reference count sanity check: one reference removed for the
237     // subsition of __other locale, one added by return-by-value. Net
238     // difference: zero. When the returned locale object's destrutor
239     // is called, then the reference count is decremented and possibly
240     // destroyed.
241     return locale(__old);
242   }
243
244   const locale&
245   locale::classic()
246   { 
247     _S_initialize();
248     return *(new (&c_locale) locale(_S_classic));
249   }
250
251   void
252   locale::_S_initialize_once()
253   {
254     // 2 references.
255     // One reference for _S_classic, one for _S_global
256     _S_classic = new (&c_locale_impl) _Impl(2);
257     _S_global = _S_classic;         
258   }
259
260   void  
261   locale::_S_initialize()
262   {
263 #ifdef __GTHREADS
264     if (__gthread_active_p())
265       __gthread_once(&_S_once, _S_initialize_once);
266 #endif
267     if (!_S_classic)
268       _S_initialize_once();
269   }
270
271   // Definitions for static const data members of locale::_Impl
272   const locale::id* const
273   locale::_Impl::_S_id_ctype[] =
274   {
275     &std::ctype<char>::id, 
276     &codecvt<char, char, mbstate_t>::id,
277 #ifdef _GLIBCXX_USE_WCHAR_T
278     &std::ctype<wchar_t>::id,
279     &codecvt<wchar_t, char, mbstate_t>::id,
280 #endif
281     0
282   };
283
284   const locale::id* const
285   locale::_Impl::_S_id_numeric[] =
286   {
287     &num_get<char>::id,  
288     &num_put<char>::id,  
289     &numpunct<char>::id, 
290 #ifdef _GLIBCXX_USE_WCHAR_T
291     &num_get<wchar_t>::id,
292     &num_put<wchar_t>::id,
293     &numpunct<wchar_t>::id,
294 #endif
295     0
296   };
297   
298   const locale::id* const
299   locale::_Impl::_S_id_collate[] =
300   {
301     &std::collate<char>::id,
302 #ifdef _GLIBCXX_USE_WCHAR_T
303     &std::collate<wchar_t>::id,
304 #endif
305     0
306   };
307
308   const locale::id* const
309   locale::_Impl::_S_id_time[] =
310   {
311     &__timepunct<char>::id, 
312     &time_get<char>::id, 
313     &time_put<char>::id, 
314 #ifdef _GLIBCXX_USE_WCHAR_T
315     &__timepunct<wchar_t>::id, 
316     &time_get<wchar_t>::id,
317     &time_put<wchar_t>::id,
318 #endif
319     0
320   };
321   
322   const locale::id* const
323   locale::_Impl::_S_id_monetary[] =
324   {
325     &money_get<char>::id,        
326     &money_put<char>::id,        
327     &moneypunct<char, false>::id, 
328     &moneypunct<char, true >::id, 
329 #ifdef _GLIBCXX_USE_WCHAR_T
330     &money_get<wchar_t>::id,
331     &money_put<wchar_t>::id,
332     &moneypunct<wchar_t, false>::id,
333     &moneypunct<wchar_t, true >::id,
334 #endif
335     0
336   };
337
338   const locale::id* const
339   locale::_Impl::_S_id_messages[] =
340   {
341     &std::messages<char>::id, 
342 #ifdef _GLIBCXX_USE_WCHAR_T
343     &std::messages<wchar_t>::id,
344 #endif
345     0
346   };
347   
348   const locale::id* const* const
349   locale::_Impl::_S_facet_categories[] =
350   {
351     // Order must match the decl order in class locale.
352     locale::_Impl::_S_id_ctype,
353     locale::_Impl::_S_id_numeric,
354     locale::_Impl::_S_id_collate,
355     locale::_Impl::_S_id_time,
356     locale::_Impl::_S_id_monetary,
357     locale::_Impl::_S_id_messages,
358     0
359   };
360
361   // Construct "C" _Impl.
362   locale::_Impl::
363   _Impl(size_t __refs) throw() 
364   : _M_refcount(__refs), _M_facets(0), _M_facets_size(_GLIBCXX_NUM_FACETS),
365   _M_caches(0), _M_names(0)    
366   {
367     _M_facets = new (&facet_vec) const facet*[_M_facets_size];
368     _M_caches = new (&cache_vec) const facet*[_M_facets_size];
369     for (size_t __i = 0; __i < _M_facets_size; ++__i)
370       _M_facets[__i] = _M_caches[__i] = 0;
371
372     // Name the categories.
373     _M_names = new (&name_vec) char*[_S_categories_size];
374     _M_names[0] = new (&name_c[0]) char[2];
375     std::memcpy(_M_names[0], locale::facet::_S_get_c_name(), 2);
376     for (size_t __j = 1; __j < _S_categories_size; ++__j)
377       _M_names[__j] = 0;
378
379     // This is needed as presently the C++ version of "C" locales
380     // != data in the underlying locale model for __timepunct,
381     // numpunct, and moneypunct. Also, the "C" locales must be
382     // constructed in a way such that they are pre-allocated.
383     // NB: Set locale::facets(ref) count to one so that each individual
384     // facet is not destroyed when the locale (and thus locale::_Impl) is
385     // destroyed.
386     _M_init_facet(new (&ctype_c) std::ctype<char>(0, false, 1));
387     _M_init_facet(new (&codecvt_c) codecvt<char, char, mbstate_t>(1));
388
389     typedef __numpunct_cache<char> num_cache_c;
390     num_cache_c* __npc = new (&numpunct_cache_c) num_cache_c(2);
391     _M_init_facet(new (&numpunct_c) numpunct<char>(__npc, 1));
392
393     _M_init_facet(new (&num_get_c) num_get<char>(1));
394     _M_init_facet(new (&num_put_c) num_put<char>(1));
395     _M_init_facet(new (&collate_c) std::collate<char>(1));
396
397     typedef __moneypunct_cache<char, false> money_cache_cf;
398     typedef __moneypunct_cache<char, true> money_cache_ct;
399     money_cache_cf* __mpcf = new (&moneypunct_cache_cf) money_cache_cf(2);
400     _M_init_facet(new (&moneypunct_cf) moneypunct<char, false>(__mpcf, 1));
401     money_cache_ct* __mpct = new (&moneypunct_cache_ct) money_cache_ct(2);
402     _M_init_facet(new (&moneypunct_ct) moneypunct<char, true>(__mpct, 1));
403
404     _M_init_facet(new (&money_get_c) money_get<char>(1));
405     _M_init_facet(new (&money_put_c) money_put<char>(1));
406
407     typedef __timepunct_cache<char> time_cache_c;
408     time_cache_c* __tpc = new (&timepunct_cache_c) time_cache_c(2);
409     _M_init_facet(new (&timepunct_c) __timepunct<char>(__tpc, 1));
410
411     _M_init_facet(new (&time_get_c) time_get<char>(1));
412     _M_init_facet(new (&time_put_c) time_put<char>(1));
413     _M_init_facet(new (&messages_c) std::messages<char>(1));    
414
415 #ifdef  _GLIBCXX_USE_WCHAR_T
416     _M_init_facet(new (&ctype_w) std::ctype<wchar_t>(1));
417     _M_init_facet(new (&codecvt_w) codecvt<wchar_t, char, mbstate_t>(1));
418
419     typedef __numpunct_cache<wchar_t> num_cache_w;
420     num_cache_w* __npw = new (&numpunct_cache_w) num_cache_w(2);
421     _M_init_facet(new (&numpunct_w) numpunct<wchar_t>(__npw, 1));
422
423     _M_init_facet(new (&num_get_w) num_get<wchar_t>(1));
424     _M_init_facet(new (&num_put_w) num_put<wchar_t>(1));
425     _M_init_facet(new (&collate_w) std::collate<wchar_t>(1));
426
427     typedef __moneypunct_cache<wchar_t, false> money_cache_wf;
428     typedef __moneypunct_cache<wchar_t, true> money_cache_wt;
429     money_cache_wf* __mpwf = new (&moneypunct_cache_wf) money_cache_wf(2);
430     _M_init_facet(new (&moneypunct_wf) moneypunct<wchar_t, false>(__mpwf, 1));
431     money_cache_wt* __mpwt = new (&moneypunct_cache_wt) money_cache_wt(2);
432     _M_init_facet(new (&moneypunct_wt) moneypunct<wchar_t, true>(__mpwt, 1));
433
434     _M_init_facet(new (&money_get_w) money_get<wchar_t>(1));
435     _M_init_facet(new (&money_put_w) money_put<wchar_t>(1));
436
437     typedef __timepunct_cache<wchar_t> time_cache_w;
438     time_cache_w* __tpw = new (&timepunct_cache_w) time_cache_w(2);
439     _M_init_facet(new (&timepunct_w) __timepunct<wchar_t>(__tpw, 1));
440
441     _M_init_facet(new (&time_get_w) time_get<wchar_t>(1));
442     _M_init_facet(new (&time_put_w) time_put<wchar_t>(1));
443     _M_init_facet(new (&messages_w) std::messages<wchar_t>(1));
444 #endif 
445      
446     // This locale is safe to pre-cache, after all the facets have
447     // been created and installed.
448     _M_caches[numpunct<char>::id._M_id()] = __npc;
449     _M_caches[moneypunct<char, false>::id._M_id()] = __mpcf;
450     _M_caches[moneypunct<char, true>::id._M_id()] = __mpct;
451     _M_caches[__timepunct<char>::id._M_id()] = __tpc;
452 #ifdef  _GLIBCXX_USE_WCHAR_T
453     _M_caches[numpunct<wchar_t>::id._M_id()] = __npw;
454     _M_caches[moneypunct<wchar_t, false>::id._M_id()] = __mpwf;
455     _M_caches[moneypunct<wchar_t, true>::id._M_id()] = __mpwt;
456     _M_caches[__timepunct<wchar_t>::id._M_id()] = __tpw;
457 #endif
458   }
459
460 _GLIBCXX_END_NAMESPACE