1 /*
2  * Copyright (c) 1999
3  * Silicon Graphics Computer Systems, Inc.
4  *
5  * Copyright (c) 1999
6  * Boris Fomitchev
7  *
8  * This material is provided "as is", with absolutely no warranty expressed
9  * or implied. Any use is at your own risk.
10  *
11  * Permission to use or copy this software for any purpose is hereby granted
12  * without fee, provided the above notices are retained on all copies.
13  * Permission to modify the code and to distribute modified code is granted,
14  * provided the above notices are retained, and a notice that the code was
15  * modified is included with the above copyright notice.
16  *
17  */
18 // WARNING: This is an internal header file, included by other C++
19 // standard library headers.  You should not attempt to use this header
20 // file directly.
21 
22 
23 #ifndef _STLP_INTERNAL_LOCALE_H
24 #define _STLP_INTERNAL_LOCALE_H
25 
26 #ifndef _STLP_INTERNAL_CSTDLIB
27 #  include <stl/_cstdlib.h>
28 #endif
29 
30 #ifndef _STLP_INTERNAL_CWCHAR
31 #  include <stl/_cwchar.h>
32 #endif
33 
34 #ifndef _STLP_INTERNAL_THREADS_H
35 #  include <stl/_threads.h>
36 #endif
37 
38 #ifndef _STLP_STRING_FWD_H
39 #  include <stl/_string_fwd.h>
40 #endif
41 
42 #include <stl/_string.h>
43 
44 #include <stl/_facets_fwd.h>
45 
46 _STLP_BEGIN_NAMESPACE
47 
48 class _Locale_impl;        // Forward declaration of opaque type.
49 class locale;
50 
51 template <class _CharT, class _Traits, class _Alloc>
52 bool __locale_do_operator_call(const locale& __loc,
53                                const basic_string<_CharT, _Traits, _Alloc>& __x,
54                                const basic_string<_CharT, _Traits, _Alloc>& __y);
55 
56 _STLP_DECLSPEC _Locale_impl * _STLP_CALL _get_Locale_impl( _Locale_impl *locimpl );
57 _STLP_DECLSPEC _Locale_impl * _STLP_CALL _copy_Nameless_Locale_impl( _Locale_impl *locimpl );
58 
59 _STLP_MOVE_TO_PRIV_NAMESPACE
60 
61 template <class _Facet>
62 bool _HasFacet(const locale& __loc, const _Facet* __facet) _STLP_NOTHROW;
63 
64 template <class _Facet>
65 _Facet* _UseFacet(const locale& __loc, const _Facet* __facet);
66 
67 template <class _Facet>
68 void _InsertFacet(locale& __loc, _Facet* __facet);
69 
70 _STLP_MOVE_TO_STD_NAMESPACE
71 
72 #if defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND) || \
73     defined (_STLP_SIGNAL_RUNTIME_COMPATIBILITY) || defined (_STLP_CHECK_RUNTIME_COMPATIBILITY)
74 #  define locale _STLP_NO_MEM_T_NAME(loc)
75 #endif
76 
77 class _STLP_CLASS_DECLSPEC locale {
78 public:
79   // types:
80   class _STLP_CLASS_DECLSPEC facet : protected _Refcount_Base {
81   protected:
82     /* Here we filter __init_count user value to 0 or 1 because __init_count is a
83      * size_t instance and _Refcount_Base use __stl_atomic_t instances that might
84      * have lower sizeof and generate roll issues. 1 is enough to keep the facet
85      * alive when required. */
86     explicit facet(size_t __init_count = 0) : _Refcount_Base( __init_count == 0 ? 0 : 1 ) {}
87     virtual ~facet();
88     friend class locale;
89     friend class _Locale_impl;
90     friend facet * _STLP_CALL _get_facet( facet * );
91     friend void _STLP_CALL _release_facet( facet *& );
92 
93   private:                        // Invalidate assignment and copying.
94     facet(const facet& ) /* : _Refcount_Base(1) {} */;
95     void operator=(const facet&);
96   };
97 
98 #if defined (__MVS__) || defined (__OS400__)
99   struct
100 #else
101   class
102 #endif
103   _STLP_CLASS_DECLSPEC id {
104   public:
105     size_t _M_index;
106     static size_t _S_max;
107   };
108 
109   typedef int category;
110   _STLP_STATIC_CONSTANT(category, none = 0x000);
111   _STLP_STATIC_CONSTANT(category, collate = 0x010);
112   _STLP_STATIC_CONSTANT(category, ctype = 0x020);
113   _STLP_STATIC_CONSTANT(category, monetary = 0x040);
114   _STLP_STATIC_CONSTANT(category, numeric = 0x100);
115   _STLP_STATIC_CONSTANT(category, time = 0x200);
116   _STLP_STATIC_CONSTANT(category, messages = 0x400);
117   _STLP_STATIC_CONSTANT(category, all = collate | ctype | monetary | numeric | time | messages);
118 
119   // construct/copy/destroy:
120   locale() _STLP_NOTHROW;
121   locale(const locale&) _STLP_NOTHROW;
122   explicit locale(const char *);
123   locale(const locale&, const char*, category);
124 
125 #if defined (_STLP_MEMBER_TEMPLATES) && !defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)
126   template <class _Facet>
locale(const locale & __loc,_Facet * __f)127   locale(const locale& __loc, _Facet* __f) {
128     if ( __f != 0 ) {
129       this->_M_impl = _get_Locale_impl( _copy_Nameless_Locale_impl( __loc._M_impl ) );
130       _STLP_PRIV _InsertFacet(*this, __f);
131     } else {
132       this->_M_impl = _get_Locale_impl( __loc._M_impl );
133     }
134   }
135 #endif
136 
137 protected:
138   // those are for internal use
139   locale(_Locale_impl*);
140 
141 public:
142   locale(const locale&, const locale&, category);
143   const locale& operator=(const locale&) _STLP_NOTHROW;
144 
145 #if defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)
146 protected:
147 #endif
148    ~locale() _STLP_NOTHROW;
149 
150 public:
151 #if defined (_STLP_MEMBER_TEMPLATES) && !defined (_STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS) && \
152    !defined(_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)
153   template <class _Facet>
combine(const locale & __loc)154   locale combine(const locale& __loc) const {
155     _Facet *__facet = 0;
156     if (!_STLP_PRIV _HasFacet(__loc, __facet))
157       _M_throw_on_combine_error(__loc.name());
158 
159     return locale(*this, _STLP_PRIV _UseFacet(__loc, __facet));
160   }
161 #endif
162 
163   // locale operations:
164   string name() const;
165 
166   bool operator==(const locale&) const;
167   bool operator!=(const locale&) const;
168 
169 #if !defined (_STLP_MEMBER_TEMPLATES) || defined (_STLP_INLINE_MEMBER_TEMPLATES) || (defined(__MWERKS__) && __MWERKS__ <= 0x2301)
170   bool operator()(const string& __x, const string& __y) const;
171 #  ifndef _STLP_NO_WCHAR_T
172   bool operator()(const wstring& __x, const wstring& __y) const;
173 #  endif
174 #elif !defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)
175   template <class _CharT, class _Traits, class _Alloc>
operator()176   bool operator()(const basic_string<_CharT, _Traits, _Alloc>& __x,
177                   const basic_string<_CharT, _Traits, _Alloc>& __y) const
178   { return __locale_do_operator_call(*this, __x, __y); }
179 #endif
180 
181   // global locale objects:
182 #if !defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)
183   static locale _STLP_CALL global(const locale&);
184 #else
185   static _Locale_impl* _STLP_CALL global(const locale&);
186 #endif
187   static const locale& _STLP_CALL classic();
188 
189 //protected:                         // Helper functions for locale globals.
190   facet* _M_get_facet(const id&) const;
191   // same, but throws
192   facet* _M_use_facet(const id&) const;
193   static void _STLP_FUNCTION_THROWS _STLP_CALL _M_throw_on_combine_error(const string& name);
194   static void _STLP_FUNCTION_THROWS _STLP_CALL _M_throw_on_null_name();
195   static void _STLP_FUNCTION_THROWS _STLP_CALL _M_throw_on_creation_failure(int __err_code,
196                                                                             const char* name, const char* facet);
197 
198 //protected:                        // More helper functions.
199   void _M_insert(facet* __f, id& __id);
200 
201   // friends:
202   friend class _Locale_impl;
203 
204 protected:                        // Data members
205   _Locale_impl* _M_impl;
_M_get_impl()206   _Locale_impl* _M_get_impl() const { return _M_impl; }
207 };
208 
209 #if defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND) || \
210     defined (_STLP_SIGNAL_RUNTIME_COMPATIBILITY) || defined (_STLP_CHECK_RUNTIME_COMPATIBILITY)
211 #  undef locale
212 #  define _Locale _STLP_NO_MEM_T_NAME(loc)
213 
214 class locale : public _Locale {
215 public:
216 
217   // construct/copy/destroy:
locale()218   locale() _STLP_NOTHROW {
219 #if defined (_STLP_CHECK_RUNTIME_COMPATIBILITY)
220     _STLP_CHECK_RUNTIME_COMPATIBILITY();
221 #endif
222   }
locale(const locale & __loc)223   locale(const locale& __loc) _STLP_NOTHROW : _Locale(__loc) {}
locale(const char * __str)224   explicit locale(const char *__str) : _Locale(__str) {}
locale(const locale & __loc,const char * __str,category __cat)225   locale(const locale& __loc, const char* __str, category __cat)
226     : _Locale(__loc, __str, __cat) {}
227 
228   template <class _Facet>
locale(const locale & __loc,_Facet * __f)229   locale(const locale& __loc, _Facet* __f)
230     : _Locale(__f != 0 ? _copy_Nameless_Locale_impl(__loc._M_impl) : __loc._M_impl) {
231     if ( __f != 0 ) {
232       _STLP_PRIV _InsertFacet(*this, __f);
233     }
234   }
235 
236 private:
237   // those are for internal use
locale(_Locale_impl * __impl)238   locale(_Locale_impl* __impl) : _Locale(__impl) {}
locale(const _Locale & __loc)239   locale(const _Locale& __loc) : _Locale(__loc) {}
240 
241 public:
242 
locale(const locale & __loc1,const locale & __loc2,category __cat)243   locale(const locale& __loc1, const locale& __loc2, category __cat)
244     : _Locale(__loc1, __loc2, __cat) {}
245 
246   const locale& operator=(const locale& __loc) _STLP_NOTHROW {
247     _Locale::operator=(__loc);
248     return *this;
249   }
250 
251   template <class _Facet>
combine(const locale & __loc)252   locale combine(const locale& __loc) const {
253     _Facet *__facet = 0;
254     if (!_STLP_PRIV _HasFacet(__loc, __facet))
255       _M_throw_on_combine_error(__loc.name());
256 
257     return locale(*this, _STLP_PRIV _UseFacet(__loc, __facet));
258   }
259 
260   // locale operations:
261   bool operator==(const locale& __loc) const { return _Locale::operator==(__loc); }
262   bool operator!=(const locale& __loc) const { return _Locale::operator!=(__loc); }
263 
264   template <class _CharT, class _Traits, class _Alloc>
operator()265   bool operator()(const basic_string<_CharT, _Traits, _Alloc>& __x,
266                   const basic_string<_CharT, _Traits, _Alloc>& __y) const
267   { return __locale_do_operator_call(*this, __x, __y); }
268 
269   // global locale objects:
global(const locale & __loc)270   static locale _STLP_CALL global(const locale& __loc) {
271     return _Locale::global(__loc);
272   }
classic()273   static const locale& _STLP_CALL classic() {
274     return __STATIC_CAST(const locale&, _Locale::classic());
275   }
276 
277   // friends:
278   friend class _Locale_impl;
279 };
280 
281 #  undef _Locale
282 #endif
283 
284 //----------------------------------------------------------------------
285 // locale globals
286 
287 template <class _Facet>
288 inline const _Facet&
289 #ifdef _STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS
290 _Use_facet<_Facet>::operator *() const
291 #else
292 use_facet(const locale& __loc)
293 #endif
294 {
295   _Facet *__facet = 0;
296   return *(_STLP_PRIV _UseFacet(__loc, __facet));
297 }
298 
299 template <class _Facet>
300 #ifdef _STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS
301 struct has_facet {
302   const locale& __loc;
has_facethas_facet303   has_facet(const locale& __p_loc) : __loc(__p_loc) {}
304   operator bool() const _STLP_NOTHROW
305 #else
306 inline bool has_facet(const locale& __loc) _STLP_NOTHROW
307 #endif
308 {
309   _Facet *__facet = 0;
310   return _STLP_PRIV _HasFacet(__loc, __facet);
311 }
312 
313 #ifdef _STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS
314 }; // close class definition
315 #endif
316 
317 _STLP_MOVE_TO_PRIV_NAMESPACE
318 
319 /* _GetFacetId is a helper function that allow delaying access to
320  * facet id static instance in the library source code to avoid
321  * the other static instances that many compilers are generating
322  * in all dynamic library or executable when instanciating facet
323  * template class.
324  */
325 template <class _Facet>
_GetFacetId(const _Facet *)326 inline locale::id& _GetFacetId(const _Facet*)
327 { return _Facet::id; }
328 
329 _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const money_get<char, istreambuf_iterator<char, char_traits<char> > >*);
330 _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const money_put<char, ostreambuf_iterator<char, char_traits<char> > >*);
331 _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const num_get<char, istreambuf_iterator<char, char_traits<char> > >*);
332 _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const num_put<char, ostreambuf_iterator<char, char_traits<char> > >*);
333 _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const time_get<char, istreambuf_iterator<char, char_traits<char> > >*);
334 _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const time_put<char, ostreambuf_iterator<char, char_traits<char> > >*);
335 
336 #ifndef _STLP_NO_WCHAR_T
337 _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const money_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >*);
338 _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const money_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >*);
339 _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const num_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >*);
340 _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const num_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >*);
341 _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >*);
342 _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >*);
343 #endif
344 
345 template <class _Facet>
_HasFacet(const locale & __loc,const _Facet * __facet)346 inline bool _HasFacet(const locale& __loc, const _Facet* __facet) _STLP_NOTHROW
347 { return (__loc._M_get_facet(_GetFacetId(__facet)) != 0); }
348 
349 template <class _Facet>
_UseFacet(const locale & __loc,const _Facet * __facet)350 inline _Facet* _UseFacet(const locale& __loc, const _Facet* __facet)
351 { return __STATIC_CAST(_Facet*, __loc._M_use_facet(_GetFacetId(__facet))); }
352 
353 template <class _Facet>
_InsertFacet(locale & __loc,_Facet * __facet)354 inline void _InsertFacet(locale& __loc, _Facet* __facet)
355 { __loc._M_insert(__facet, _GetFacetId(__facet)); }
356 
357 _STLP_MOVE_TO_STD_NAMESPACE
358 
359 _STLP_END_NAMESPACE
360 
361 #endif /* _STLP_INTERNAL_LOCALE_H */
362 
363 // Local Variables:
364 // mode:C++
365 // End:
366 
367