1 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 // Free Software Foundation, Inc.
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)
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.
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,
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.
32 #include <cstdlib> // For getenv
34 #include <cwctype> // For towupper, etc.
36 #include <ext/concurrence.h>
41 get_locale_cache_mutex()
43 static __gnu_cxx::__mutex locale_cache_mutex;
44 return locale_cache_mutex;
46 } // anonymous namespace
48 // XXX GLIBCXX_ABI Deprecated
49 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
50 # define _GLIBCXX_LOC_ID(mangled) extern std::locale::id mangled
51 _GLIBCXX_LOC_ID (_ZNSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
52 _GLIBCXX_LOC_ID (_ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
53 _GLIBCXX_LOC_ID (_ZNSt9money_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
54 _GLIBCXX_LOC_ID (_ZNSt9money_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
55 # ifdef _GLIBCXX_USE_WCHAR_T
56 _GLIBCXX_LOC_ID (_ZNSt7num_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
57 _GLIBCXX_LOC_ID (_ZNSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
58 _GLIBCXX_LOC_ID (_ZNSt9money_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
59 _GLIBCXX_LOC_ID (_ZNSt9money_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
63 _GLIBCXX_BEGIN_NAMESPACE(std)
65 // Definitions for static const data members of locale.
66 const locale::category locale::none;
67 const locale::category locale::ctype;
68 const locale::category locale::numeric;
69 const locale::category locale::collate;
70 const locale::category locale::time;
71 const locale::category locale::monetary;
72 const locale::category locale::messages;
73 const locale::category locale::all;
75 // These are no longer exported.
76 locale::_Impl* locale::_S_classic;
77 locale::_Impl* locale::_S_global;
80 __gthread_once_t locale::_S_once = __GTHREAD_ONCE_INIT;
83 locale::locale(const locale& __other) throw()
84 : _M_impl(__other._M_impl)
85 { _M_impl->_M_add_reference(); }
87 // This is used to initialize global and classic locales, and
88 // assumes that the _Impl objects are constructed correctly.
89 // The lack of a reference increment is intentional.
90 locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
93 locale::~locale() throw()
94 { _M_impl->_M_remove_reference(); }
97 locale::operator==(const locale& __rhs) const throw()
99 // Deal first with the common cases, fast to process: refcopies,
100 // unnamed (i.e., !_M_names[0]), "simple" (!_M_names[1] => all the
101 // categories same name, i.e., _M_names[0]). Otherwise fall back
102 // to the general locale::name().
104 if (_M_impl == __rhs._M_impl)
106 else if (!_M_impl->_M_names[0] || !__rhs._M_impl->_M_names[0]
107 || std::strcmp(_M_impl->_M_names[0],
108 __rhs._M_impl->_M_names[0]) != 0)
110 else if (!_M_impl->_M_names[1] && !__rhs._M_impl->_M_names[1])
113 __ret = this->name() == __rhs.name();
118 locale::operator=(const locale& __other) throw()
120 __other._M_impl->_M_add_reference();
121 _M_impl->_M_remove_reference();
122 _M_impl = __other._M_impl;
130 if (!_M_impl->_M_names[0])
132 else if (_M_impl->_M_check_same_name())
133 __ret = _M_impl->_M_names[0];
137 __ret += _S_categories[0];
139 __ret += _M_impl->_M_names[0];
140 for (size_t __i = 1; __i < _S_categories_size; ++__i)
143 __ret += _S_categories[__i];
145 __ret += _M_impl->_M_names[__i];
152 locale::_S_normalize_category(category __cat)
155 if (__cat == none || ((__cat & all) && !(__cat & ~all)))
159 // NB: May be a C-style "LC_ALL" category; convert.
177 #ifdef _GLIBCXX_HAVE_LC_MESSAGES
186 __throw_runtime_error(__N("locale::_S_normalize_category "
187 "category not found"));
194 __c_locale locale::facet::_S_c_locale;
196 const char locale::facet::_S_c_name[2] = "C";
199 __gthread_once_t locale::facet::_S_once = __GTHREAD_ONCE_INIT;
203 locale::facet::_S_initialize_once()
205 // Initialize the underlying locale model.
206 _S_create_c_locale(_S_c_locale, _S_c_name);
210 locale::facet::_S_get_c_locale()
213 if (__gthread_active_p())
214 __gthread_once(&_S_once, _S_initialize_once);
219 _S_initialize_once();
225 locale::facet::_S_get_c_name()
226 { return _S_c_name; }
236 for (size_t __i = 0; __i < _M_facets_size; ++__i)
238 _M_facets[__i]->_M_remove_reference();
242 for (size_t __i = 0; __i < _M_facets_size; ++__i)
244 _M_caches[__i]->_M_remove_reference();
248 for (size_t __i = 0; __i < _S_categories_size; ++__i)
249 delete [] _M_names[__i];
253 // Clone existing _Impl object.
255 _Impl(const _Impl& __imp, size_t __refs)
256 : _M_refcount(__refs), _M_facets(0), _M_facets_size(__imp._M_facets_size),
257 _M_caches(0), _M_names(0)
261 _M_facets = new const facet*[_M_facets_size];
262 for (size_t __i = 0; __i < _M_facets_size; ++__i)
264 _M_facets[__i] = __imp._M_facets[__i];
266 _M_facets[__i]->_M_add_reference();
268 _M_caches = new const facet*[_M_facets_size];
269 for (size_t __j = 0; __j < _M_facets_size; ++__j)
271 _M_caches[__j] = __imp._M_caches[__j];
273 _M_caches[__j]->_M_add_reference();
275 _M_names = new char*[_S_categories_size];
276 for (size_t __k = 0; __k < _S_categories_size; ++__k)
279 // Name the categories.
280 for (size_t __l = 0; (__l < _S_categories_size
281 && __imp._M_names[__l]); ++__l)
283 const size_t __len = std::strlen(__imp._M_names[__l]) + 1;
284 _M_names[__l] = new char[__len];
285 std::memcpy(_M_names[__l], __imp._M_names[__l], __len);
291 __throw_exception_again;
297 _M_replace_category(const _Impl* __imp,
298 const locale::id* const* __idpp)
300 for (; *__idpp; ++__idpp)
301 _M_replace_facet(__imp, *__idpp);
306 _M_replace_facet(const _Impl* __imp, const locale::id* __idp)
308 size_t __index = __idp->_M_id();
309 if ((__index > (__imp->_M_facets_size - 1))
310 || !__imp->_M_facets[__index])
311 __throw_runtime_error(__N("locale::_Impl::_M_replace_facet"));
312 _M_install_facet(__idp, __imp->_M_facets[__index]);
317 _M_install_facet(const locale::id* __idp, const facet* __fp)
321 size_t __index = __idp->_M_id();
323 // Check size of facet vector to ensure adequate room.
324 if (__index > _M_facets_size - 1)
326 const size_t __new_size = __index + 4;
329 const facet** __oldf = _M_facets;
330 const facet** __newf;
331 __newf = new const facet*[__new_size];
332 for (size_t __i = 0; __i < _M_facets_size; ++__i)
333 __newf[__i] = _M_facets[__i];
334 for (size_t __l = _M_facets_size; __l < __new_size; ++__l)
338 const facet** __oldc = _M_caches;
339 const facet** __newc;
342 __newc = new const facet*[__new_size];
347 __throw_exception_again;
349 for (size_t __j = 0; __j < _M_facets_size; ++__j)
350 __newc[__j] = _M_caches[__j];
351 for (size_t __k = _M_facets_size; __k < __new_size; ++__k)
354 _M_facets_size = __new_size;
361 __fp->_M_add_reference();
362 const facet*& __fpr = _M_facets[__index];
365 // Replacing an existing facet. Order matters.
366 __fpr->_M_remove_reference();
371 // Installing a newly created facet into an empty
372 // _M_facets container, say a newly-constructed,
373 // swanky-fresh _Impl.
374 _M_facets[__index] = __fp;
377 // Ideally, it would be nice to only remove the caches that
378 // are now incorrect. However, some of the caches depend on
379 // multiple facets, and we only know about one facet
380 // here. It's no great loss: the first use of the new facet
381 // will create a new, correctly cached facet anyway.
382 for (size_t __i = 0; __i < _M_facets_size; ++__i)
384 const facet* __cpr = _M_caches[__i];
387 __cpr->_M_remove_reference();
396 _M_install_cache(const facet* __cache, size_t __index)
398 __gnu_cxx::__scoped_lock sentry(get_locale_cache_mutex());
399 if (_M_caches[__index] != 0)
401 // Some other thread got in first.
406 __cache->_M_add_reference();
407 _M_caches[__index] = __cache;
412 // Definitions for static const data members of locale::id
413 _Atomic_word locale::id::_S_refcount; // init'd to 0 by linker
416 locale::id::_M_id() const
420 // XXX GLIBCXX_ABI Deprecated
421 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
423 # define _GLIBCXX_SYNC_ID(facet, mangled) \
424 if (this == &::mangled) \
426 _GLIBCXX_SYNC_ID (num_get<char>, _ZNSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
427 _GLIBCXX_SYNC_ID (num_put<char>, _ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
428 _GLIBCXX_SYNC_ID (money_get<char>, _ZNSt9money_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
429 _GLIBCXX_SYNC_ID (money_put<char>, _ZNSt9money_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
430 # ifdef _GLIBCXX_USE_WCHAR_T
431 _GLIBCXX_SYNC_ID (num_get<wchar_t>, _ZNSt7num_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
432 _GLIBCXX_SYNC_ID (num_put<wchar_t>, _ZNSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
433 _GLIBCXX_SYNC_ID (money_get<wchar_t>, _ZNSt9money_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
434 _GLIBCXX_SYNC_ID (money_put<wchar_t>, _ZNSt9money_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
437 _M_index = 1 + f->_M_id();
440 _M_index = 1 + __gnu_cxx::__exchange_and_add_dispatch(&_S_refcount,
446 _GLIBCXX_END_NAMESPACE