OSDN Git Service

d5a337f5b3716603f0ffe39482b822a38cb51d06
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / locale_init.cc
1 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
2 // Free Software Foundation, Inc.
3 //
4 // This file is part of the GNU ISO C++ Library.  This library is free
5 // software; you can redistribute it and/or modify it under the
6 // terms of the GNU General Public License as published by the
7 // Free Software Foundation; either version 2, or (at your option)
8 // any later version.
9
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14
15 // You should have received a copy of the GNU General Public License along
16 // with this library; see the file COPYING.  If not, write to the Free
17 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 // USA.
19
20 // As a special exception, you may use this file as part of a free software
21 // library without restriction.  Specifically, if other files instantiate
22 // templates or use macros or inline functions from this file, or you compile
23 // this file and link it with other files to produce an executable, this
24 // file does not by itself cause the resulting executable to be covered by
25 // the GNU General Public License.  This exception does not however
26 // invalidate any other reasons why the executable file might be covered by
27 // the GNU General Public License.
28
29 #include <clocale>
30 #include <cstring>
31 #include <cstdlib>     // For getenv, free.
32 #include <cctype>
33 #include <cwctype>     // For towupper, etc.
34 #include <locale>
35 #include <bits/atomicity.h>
36 #include <bits/concurrence.h>
37
38 namespace __gnu_internal
39 {
40   // Defined in globals.cc.
41   extern std::locale            c_locale;
42   extern std::locale::_Impl     c_locale_impl;
43
44   extern std::locale::facet* facet_vec[_GLIBCXX_NUM_FACETS];
45   extern char* name_vec[6 + _GLIBCXX_NUM_CATEGORIES];
46   extern char name_c[6 + _GLIBCXX_NUM_CATEGORIES][2];
47
48   extern std::ctype<char>                        ctype_c;
49   extern std::collate<char>                      collate_c;
50   extern std::numpunct<char>                     numpunct_c;
51   extern std::num_get<char>                      num_get_c;
52   extern std::num_put<char>                      num_put_c;  
53   extern std::codecvt<char, char, mbstate_t>     codecvt_c;
54   extern std::moneypunct<char, false>            moneypunct_cf;
55   extern std::moneypunct<char, true>             moneypunct_ct;
56   extern std::money_get<char>                    money_get_c;
57   extern std::money_put<char>                    money_put_c;
58   extern std::__timepunct<char>                  timepunct_c;
59   extern std::time_get<char>                     time_get_c;
60   extern std::time_put<char>                     time_put_c;
61   extern std::messages<char>                     messages_c;
62 #ifdef  _GLIBCXX_USE_WCHAR_T
63   extern std::ctype<wchar_t>                     ctype_w;
64   extern std::collate<wchar_t>                   collate_w;
65   extern std::numpunct<wchar_t>                  numpunct_w;
66   extern std::num_get<wchar_t>                   num_get_w;
67   extern std::num_put<wchar_t>                   num_put_w;
68   extern std::codecvt<wchar_t, char, mbstate_t>  codecvt_w;
69   extern std::moneypunct<wchar_t, false>         moneypunct_wf;
70   extern std::moneypunct<wchar_t, true>          moneypunct_wt;
71   extern std::money_get<wchar_t>                 money_get_w;
72   extern std::money_put<wchar_t>                 money_put_w;
73   extern std::__timepunct<wchar_t>               timepunct_w;
74   extern std::time_get<wchar_t>                  time_get_w;
75   extern std::time_put<wchar_t>                  time_put_w;
76   extern std::messages<wchar_t>                  messages_w;
77 #endif
78
79   // And the caches....
80   extern std::locale::facet* cache_vec[_GLIBCXX_NUM_FACETS];
81   extern std::__numpunct_cache<char>             numpunct_cache_c;
82   extern std::__moneypunct_cache<char, false>    moneypunct_cache_cf;
83   extern std::__moneypunct_cache<char, true>     moneypunct_cache_ct;
84   extern std::__timepunct_cache<char>            timepunct_cache_c;
85 #ifdef  _GLIBCXX_USE_WCHAR_T
86   extern std::__numpunct_cache<wchar_t>          numpunct_cache_w;
87   extern std::__moneypunct_cache<wchar_t, false> moneypunct_cache_wf;
88   extern std::__moneypunct_cache<wchar_t, true>  moneypunct_cache_wt;
89   extern std::__timepunct_cache<wchar_t>         timepunct_cache_w;
90 #endif
91
92   // Mutex objects for locale initialization.
93   __glibcxx_mutex_define_initialized(locale_cons_mutex);
94   __glibcxx_mutex_define_initialized(locale_global_mutex);
95 } // namespace __gnu_internal
96
97 namespace std 
98 {
99   using namespace __gnu_internal;
100
101   locale::locale() throw()
102   { 
103     _S_initialize(); 
104     __glibcxx_mutex_lock(__gnu_internal::locale_cons_mutex);
105     _S_global->_M_add_reference();
106     _M_impl = _S_global;
107     __glibcxx_mutex_unlock(__gnu_internal::locale_cons_mutex);
108   }
109
110   locale
111   locale::global(const locale& __other)
112   {
113     _S_initialize();
114     __glibcxx_mutex_lock(__gnu_internal::locale_global_mutex);
115     _Impl* __old = _S_global;
116     __other._M_impl->_M_add_reference();
117     _S_global = __other._M_impl;
118     const string __other_name = __other.name();
119     if (__other_name != "*")
120       setlocale(LC_ALL, __other_name.c_str());
121    __glibcxx_mutex_unlock(__gnu_internal::locale_global_mutex);
122
123     // Reference count sanity check: one reference removed for the
124     // subsition of __other locale, one added by return-by-value. Net
125     // difference: zero. When the returned locale object's destrutor
126     // is called, then the reference count is decremented and possibly
127     // destroyed.
128     return locale(__old);
129   }
130
131   const locale&
132   locale::classic()
133   {
134     _S_initialize();
135     return c_locale;
136   }
137
138   void
139   locale::_S_initialize_once()
140   {
141     // 2 references.
142     // One reference for _S_classic, one for _S_global
143     _S_classic = new (&c_locale_impl) _Impl(2);
144     _S_global = _S_classic;         
145     new (&c_locale) locale(_S_classic);
146   }
147
148   void  
149   locale::_S_initialize()
150   {
151 #ifdef __GTHREADS
152     if (__gthread_active_p())
153       __gthread_once(&_S_once, _S_initialize_once);
154 #endif
155     if (!_S_classic)
156       _S_initialize_once();
157   }
158
159   // Definitions for static const data members of locale::_Impl
160   const locale::id* const
161   locale::_Impl::_S_id_ctype[] =
162   {
163     &std::ctype<char>::id, 
164     &codecvt<char, char, mbstate_t>::id,
165 #ifdef _GLIBCXX_USE_WCHAR_T
166     &std::ctype<wchar_t>::id,
167     &codecvt<wchar_t, char, mbstate_t>::id,
168 #endif
169     0
170   };
171
172   const locale::id* const
173   locale::_Impl::_S_id_numeric[] =
174   {
175     &num_get<char>::id,  
176     &num_put<char>::id,  
177     &numpunct<char>::id, 
178 #ifdef _GLIBCXX_USE_WCHAR_T
179     &num_get<wchar_t>::id,
180     &num_put<wchar_t>::id,
181     &numpunct<wchar_t>::id,
182 #endif
183     0
184   };
185   
186   const locale::id* const
187   locale::_Impl::_S_id_collate[] =
188   {
189     &std::collate<char>::id,
190 #ifdef _GLIBCXX_USE_WCHAR_T
191     &std::collate<wchar_t>::id,
192 #endif
193     0
194   };
195
196   const locale::id* const
197   locale::_Impl::_S_id_time[] =
198   {
199     &__timepunct<char>::id, 
200     &time_get<char>::id, 
201     &time_put<char>::id, 
202 #ifdef _GLIBCXX_USE_WCHAR_T
203     &__timepunct<wchar_t>::id, 
204     &time_get<wchar_t>::id,
205     &time_put<wchar_t>::id,
206 #endif
207     0
208   };
209   
210   const locale::id* const
211   locale::_Impl::_S_id_monetary[] =
212   {
213     &money_get<char>::id,        
214     &money_put<char>::id,        
215     &moneypunct<char, false>::id, 
216     &moneypunct<char, true >::id, 
217 #ifdef _GLIBCXX_USE_WCHAR_T
218     &money_get<wchar_t>::id,
219     &money_put<wchar_t>::id,
220     &moneypunct<wchar_t, false>::id,
221     &moneypunct<wchar_t, true >::id,
222 #endif
223     0
224   };
225
226   const locale::id* const
227   locale::_Impl::_S_id_messages[] =
228   {
229     &std::messages<char>::id, 
230 #ifdef _GLIBCXX_USE_WCHAR_T
231     &std::messages<wchar_t>::id,
232 #endif
233     0
234   };
235   
236   const locale::id* const* const
237   locale::_Impl::_S_facet_categories[] =
238   {
239     // Order must match the decl order in class locale.
240     locale::_Impl::_S_id_ctype,
241     locale::_Impl::_S_id_numeric,
242     locale::_Impl::_S_id_collate,
243     locale::_Impl::_S_id_time,
244     locale::_Impl::_S_id_monetary,
245     locale::_Impl::_S_id_messages,
246     0
247   };
248
249   // Construct "C" _Impl.
250   locale::_Impl::
251   _Impl(size_t __refs) throw() 
252   : _M_refcount(__refs), _M_facets_size(_GLIBCXX_NUM_FACETS)
253   {
254     _M_facets = new (&facet_vec) const facet*[_M_facets_size];
255     _M_caches = new (&cache_vec) const facet*[_M_facets_size];
256     for (size_t __i = 0; __i < _M_facets_size; ++__i)
257       _M_facets[__i] = _M_caches[__i] = 0;
258
259     // Name the categories.
260     _M_names = new (&name_vec) char*[_S_categories_size];
261     _M_names[0] = new (&name_c[0]) char[2];
262     std::memcpy(_M_names[0], locale::facet::_S_get_c_name(), 2);
263     for (size_t __i = 1; __i < _S_categories_size; ++__i)
264       _M_names[__i] = 0;
265
266     // This is needed as presently the C++ version of "C" locales
267     // != data in the underlying locale model for __timepunct,
268     // numpunct, and moneypunct. Also, the "C" locales must be
269     // constructed in a way such that they are pre-allocated.
270     // NB: Set locale::facets(ref) count to one so that each individual
271     // facet is not destroyed when the locale (and thus locale::_Impl) is
272     // destroyed.
273     _M_init_facet(new (&ctype_c) std::ctype<char>(0, false, 1));
274     _M_init_facet(new (&codecvt_c) codecvt<char, char, mbstate_t>(1));
275
276     typedef __numpunct_cache<char> num_cache_c;
277     num_cache_c* __npc = new (&numpunct_cache_c) num_cache_c(2);
278     _M_init_facet(new (&numpunct_c) numpunct<char>(__npc, 1));
279
280     _M_init_facet(new (&num_get_c) num_get<char>(1));
281     _M_init_facet(new (&num_put_c) num_put<char>(1));
282     _M_init_facet(new (&collate_c) std::collate<char>(1));
283
284     typedef __moneypunct_cache<char, false> money_cache_cf;
285     typedef __moneypunct_cache<char, true> money_cache_ct;
286     money_cache_cf* __mpcf = new (&moneypunct_cache_cf) money_cache_cf(2);
287     _M_init_facet(new (&moneypunct_cf) moneypunct<char, false>(__mpcf, 1));
288     money_cache_ct* __mpct = new (&moneypunct_cache_ct) money_cache_ct(2);
289     _M_init_facet(new (&moneypunct_ct) moneypunct<char, true>(__mpct, 1));
290
291     _M_init_facet(new (&money_get_c) money_get<char>(1));
292     _M_init_facet(new (&money_put_c) money_put<char>(1));
293
294     typedef __timepunct_cache<char> time_cache_c;
295     time_cache_c* __tpc = new (&timepunct_cache_c) time_cache_c(2);
296     _M_init_facet(new (&timepunct_c) __timepunct<char>(__tpc, 1));
297
298     _M_init_facet(new (&time_get_c) time_get<char>(1));
299     _M_init_facet(new (&time_put_c) time_put<char>(1));
300     _M_init_facet(new (&messages_c) std::messages<char>(1));    
301
302 #ifdef  _GLIBCXX_USE_WCHAR_T
303     _M_init_facet(new (&ctype_w) std::ctype<wchar_t>(1));
304     _M_init_facet(new (&codecvt_w) codecvt<wchar_t, char, mbstate_t>(1));
305
306     typedef __numpunct_cache<wchar_t> num_cache_w;
307     num_cache_w* __npw = new (&numpunct_cache_w) num_cache_w(2);
308     _M_init_facet(new (&numpunct_w) numpunct<wchar_t>(__npw, 1));
309
310     _M_init_facet(new (&num_get_w) num_get<wchar_t>(1));
311     _M_init_facet(new (&num_put_w) num_put<wchar_t>(1));
312     _M_init_facet(new (&collate_w) std::collate<wchar_t>(1));
313
314     typedef __moneypunct_cache<wchar_t, false> money_cache_wf;
315     typedef __moneypunct_cache<wchar_t, true> money_cache_wt;
316     money_cache_wf* __mpwf = new (&moneypunct_cache_wf) money_cache_wf(2);
317     _M_init_facet(new (&moneypunct_wf) moneypunct<wchar_t, false>(__mpwf, 1));
318     money_cache_wt* __mpwt = new (&moneypunct_cache_wt) money_cache_wt(2);
319     _M_init_facet(new (&moneypunct_wt) moneypunct<wchar_t, true>(__mpwt, 1));
320
321     _M_init_facet(new (&money_get_w) money_get<wchar_t>(1));
322     _M_init_facet(new (&money_put_w) money_put<wchar_t>(1));
323
324     typedef __timepunct_cache<wchar_t> time_cache_w;
325     time_cache_w* __tpw = new (&timepunct_cache_w) time_cache_w(2);
326     _M_init_facet(new (&timepunct_w) __timepunct<wchar_t>(__tpw, 1));
327
328     _M_init_facet(new (&time_get_w) time_get<wchar_t>(1));
329     _M_init_facet(new (&time_put_w) time_put<wchar_t>(1));
330     _M_init_facet(new (&messages_w) std::messages<wchar_t>(1));
331 #endif 
332      
333     // This locale is safe to pre-cache, after all the facets have
334     // been created and installed.
335     _M_caches[numpunct<char>::id._M_id()] = __npc;
336     _M_caches[moneypunct<char, false>::id._M_id()] = __mpcf;
337     _M_caches[moneypunct<char, true>::id._M_id()] = __mpct;
338     _M_caches[__timepunct<char>::id._M_id()] = __tpc;
339 #ifdef  _GLIBCXX_USE_WCHAR_T
340     _M_caches[numpunct<wchar_t>::id._M_id()] = __npw;
341     _M_caches[moneypunct<wchar_t, false>::id._M_id()] = __mpwf;
342     _M_caches[moneypunct<wchar_t, true>::id._M_id()] = __mpwt;
343     _M_caches[__timepunct<wchar_t>::id._M_id()] = __tpw;
344 #endif
345   }
346 } // namespace std