_locale.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. #ifndef _STLP_INTERNAL_LOCALE_H
  22. #define _STLP_INTERNAL_LOCALE_H
  23. #ifndef _STLP_INTERNAL_CSTDLIB
  24. # include <stl/_cstdlib.h>
  25. #endif
  26. #ifndef _STLP_INTERNAL_CWCHAR
  27. # include <stl/_cwchar.h>
  28. #endif
  29. #ifndef _STLP_INTERNAL_THREADS_H
  30. # include <stl/_threads.h>
  31. #endif
  32. #ifndef _STLP_STRING_FWD_H
  33. # include <stl/_string_fwd.h>
  34. #endif
  35. #include <stl/_facets_fwd.h>
  36. _STLP_BEGIN_NAMESPACE
  37. class _Locale_impl; // Forward declaration of opaque type.
  38. class locale;
  39. template <class _CharT, class _Traits, class _Alloc>
  40. bool __locale_do_operator_call(const locale& __loc,
  41. const basic_string<_CharT, _Traits, _Alloc>& __x,
  42. const basic_string<_CharT, _Traits, _Alloc>& __y);
  43. _STLP_DECLSPEC _Locale_impl * _STLP_CALL _get_Locale_impl( _Locale_impl *locimpl );
  44. _STLP_DECLSPEC _Locale_impl * _STLP_CALL _copy_Nameless_Locale_impl( _Locale_impl *locimpl );
  45. _STLP_MOVE_TO_PRIV_NAMESPACE
  46. template <class _Facet>
  47. bool _HasFacet(const locale& __loc, const _Facet* __facet) _STLP_NOTHROW;
  48. template <class _Facet>
  49. _Facet* _UseFacet(const locale& __loc, const _Facet* __facet);
  50. template <class _Facet>
  51. void _InsertFacet(locale& __loc, _Facet* __facet);
  52. _STLP_MOVE_TO_STD_NAMESPACE
  53. #if defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND) || \
  54. defined (_STLP_SIGNAL_RUNTIME_COMPATIBILITY) || defined (_STLP_CHECK_RUNTIME_COMPATIBILITY)
  55. # define locale _STLP_NO_MEM_T_NAME(loc)
  56. #endif
  57. class _STLP_CLASS_DECLSPEC locale {
  58. public:
  59. // types:
  60. class _STLP_CLASS_DECLSPEC facet : protected _Refcount_Base {
  61. protected:
  62. /* Here we filter __init_count user value to 0 or 1 because __init_count is a
  63. * size_t instance and _Refcount_Base use __stl_atomic_t instances that might
  64. * have lower sizeof and generate roll issues. 1 is enough to keep the facet
  65. * alive when required. */
  66. explicit facet(size_t __init_count = 0) : _Refcount_Base( __init_count == 0 ? 0 : 1 ) {}
  67. virtual ~facet();
  68. friend class locale;
  69. friend class _Locale_impl;
  70. friend facet * _STLP_CALL _get_facet( facet * );
  71. friend void _STLP_CALL _release_facet( facet *& );
  72. private: // Invalidate assignment and copying.
  73. facet(const facet& ) /* : _Refcount_Base(1) {} */;
  74. void operator=(const facet&);
  75. };
  76. #if defined (__MVS__) || defined (__OS400__)
  77. struct
  78. #else
  79. class
  80. #endif
  81. _STLP_CLASS_DECLSPEC id {
  82. public:
  83. size_t _M_index;
  84. static size_t _S_max;
  85. };
  86. typedef int category;
  87. _STLP_STATIC_CONSTANT(category, none = 0x000);
  88. _STLP_STATIC_CONSTANT(category, collate = 0x010);
  89. _STLP_STATIC_CONSTANT(category, ctype = 0x020);
  90. _STLP_STATIC_CONSTANT(category, monetary = 0x040);
  91. _STLP_STATIC_CONSTANT(category, numeric = 0x100);
  92. _STLP_STATIC_CONSTANT(category, time = 0x200);
  93. _STLP_STATIC_CONSTANT(category, messages = 0x400);
  94. _STLP_STATIC_CONSTANT(category, all = collate | ctype | monetary | numeric | time | messages);
  95. // construct/copy/destroy:
  96. locale() _STLP_NOTHROW;
  97. locale(const locale&) _STLP_NOTHROW;
  98. explicit locale(const char *);
  99. locale(const locale&, const char*, category);
  100. #if defined (_STLP_MEMBER_TEMPLATES) && !defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)
  101. template <class _Facet>
  102. locale(const locale& __loc, _Facet* __f) {
  103. if ( __f != 0 ) {
  104. this->_M_impl = _get_Locale_impl( _copy_Nameless_Locale_impl( __loc._M_impl ) );
  105. _STLP_PRIV _InsertFacet(*this, __f);
  106. } else {
  107. this->_M_impl = _get_Locale_impl( __loc._M_impl );
  108. }
  109. }
  110. #endif
  111. protected:
  112. // those are for internal use
  113. locale(_Locale_impl*);
  114. public:
  115. locale(const locale&, const locale&, category);
  116. const locale& operator=(const locale&) _STLP_NOTHROW;
  117. #if defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)
  118. protected:
  119. #endif
  120. ~locale() _STLP_NOTHROW;
  121. public:
  122. #if defined (_STLP_MEMBER_TEMPLATES) && !defined (_STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS) && \
  123. !defined(_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)
  124. template <class _Facet>
  125. locale combine(const locale& __loc) const {
  126. _Facet *__facet = 0;
  127. if (!_STLP_PRIV _HasFacet(__loc, __facet))
  128. _M_throw_on_combine_error(__loc.name());
  129. return locale(*this, _STLP_PRIV _UseFacet(__loc, __facet));
  130. }
  131. #endif
  132. // locale operations:
  133. string name() const;
  134. bool operator==(const locale&) const;
  135. bool operator!=(const locale&) const;
  136. #if !defined (_STLP_MEMBER_TEMPLATES) || defined (_STLP_INLINE_MEMBER_TEMPLATES) || (defined(__MWERKS__) && __MWERKS__ <= 0x2301)
  137. bool operator()(const string& __x, const string& __y) const;
  138. # ifndef _STLP_NO_WCHAR_T
  139. bool operator()(const wstring& __x, const wstring& __y) const;
  140. # endif
  141. #elif !defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)
  142. template <class _CharT, class _Traits, class _Alloc>
  143. bool operator()(const basic_string<_CharT, _Traits, _Alloc>& __x,
  144. const basic_string<_CharT, _Traits, _Alloc>& __y) const
  145. { return __locale_do_operator_call(*this, __x, __y); }
  146. #endif
  147. // global locale objects:
  148. #if !defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)
  149. static locale _STLP_CALL global(const locale&);
  150. #else
  151. static _Locale_impl* _STLP_CALL global(const locale&);
  152. #endif
  153. static const locale& _STLP_CALL classic();
  154. //protected: // Helper functions for locale globals.
  155. facet* _M_get_facet(const id&) const;
  156. // same, but throws
  157. facet* _M_use_facet(const id&) const;
  158. static void _STLP_FUNCTION_THROWS _STLP_CALL _M_throw_on_combine_error(const string& name);
  159. static void _STLP_FUNCTION_THROWS _STLP_CALL _M_throw_on_null_name();
  160. static void _STLP_FUNCTION_THROWS _STLP_CALL _M_throw_on_creation_failure(int __err_code,
  161. const char* name, const char* facet);
  162. //protected: // More helper functions.
  163. void _M_insert(facet* __f, id& __id);
  164. // friends:
  165. friend class _Locale_impl;
  166. protected: // Data members
  167. _Locale_impl* _M_impl;
  168. _Locale_impl* _M_get_impl() const { return _M_impl; }
  169. };
  170. #if defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND) || \
  171. defined (_STLP_SIGNAL_RUNTIME_COMPATIBILITY) || defined (_STLP_CHECK_RUNTIME_COMPATIBILITY)
  172. # undef locale
  173. # define _Locale _STLP_NO_MEM_T_NAME(loc)
  174. class locale : public _Locale {
  175. public:
  176. // construct/copy/destroy:
  177. locale() _STLP_NOTHROW {
  178. #if defined (_STLP_CHECK_RUNTIME_COMPATIBILITY)
  179. _STLP_CHECK_RUNTIME_COMPATIBILITY();
  180. #endif
  181. }
  182. locale(const locale& __loc) _STLP_NOTHROW : _Locale(__loc) {}
  183. explicit locale(const char *__str) : _Locale(__str) {}
  184. locale(const locale& __loc, const char* __str, category __cat)
  185. : _Locale(__loc, __str, __cat) {}
  186. template <class _Facet>
  187. locale(const locale& __loc, _Facet* __f)
  188. : _Locale(__f != 0 ? _copy_Nameless_Locale_impl(__loc._M_impl) : __loc._M_impl) {
  189. if ( __f != 0 ) {
  190. _STLP_PRIV _InsertFacet(*this, __f);
  191. }
  192. }
  193. private:
  194. // those are for internal use
  195. locale(_Locale_impl* __impl) : _Locale(__impl) {}
  196. locale(const _Locale& __loc) : _Locale(__loc) {}
  197. public:
  198. locale(const locale& __loc1, const locale& __loc2, category __cat)
  199. : _Locale(__loc1, __loc2, __cat) {}
  200. const locale& operator=(const locale& __loc) _STLP_NOTHROW {
  201. _Locale::operator=(__loc);
  202. return *this;
  203. }
  204. template <class _Facet>
  205. locale combine(const locale& __loc) const {
  206. _Facet *__facet = 0;
  207. if (!_STLP_PRIV _HasFacet(__loc, __facet))
  208. _M_throw_on_combine_error(__loc.name());
  209. return locale(*this, _STLP_PRIV _UseFacet(__loc, __facet));
  210. }
  211. // locale operations:
  212. bool operator==(const locale& __loc) const { return _Locale::operator==(__loc); }
  213. bool operator!=(const locale& __loc) const { return _Locale::operator!=(__loc); }
  214. template <class _CharT, class _Traits, class _Alloc>
  215. bool operator()(const basic_string<_CharT, _Traits, _Alloc>& __x,
  216. const basic_string<_CharT, _Traits, _Alloc>& __y) const
  217. { return __locale_do_operator_call(*this, __x, __y); }
  218. // global locale objects:
  219. static locale _STLP_CALL global(const locale& __loc) {
  220. return _Locale::global(__loc);
  221. }
  222. static const locale& _STLP_CALL classic() {
  223. return __STATIC_CAST(const locale&, _Locale::classic());
  224. }
  225. // friends:
  226. friend class _Locale_impl;
  227. };
  228. # undef _Locale
  229. #endif
  230. //----------------------------------------------------------------------
  231. // locale globals
  232. template <class _Facet>
  233. inline const _Facet&
  234. #ifdef _STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS
  235. _Use_facet<_Facet>::operator *() const
  236. #else
  237. use_facet(const locale& __loc)
  238. #endif
  239. {
  240. _Facet *__facet = 0;
  241. return *(_STLP_PRIV _UseFacet(__loc, __facet));
  242. }
  243. template <class _Facet>
  244. #ifdef _STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS
  245. struct has_facet {
  246. const locale& __loc;
  247. has_facet(const locale& __p_loc) : __loc(__p_loc) {}
  248. operator bool() const _STLP_NOTHROW
  249. #else
  250. inline bool has_facet(const locale& __loc) _STLP_NOTHROW
  251. #endif
  252. {
  253. _Facet *__facet = 0;
  254. return _STLP_PRIV _HasFacet(__loc, __facet);
  255. }
  256. #ifdef _STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS
  257. }; // close class definition
  258. #endif
  259. _STLP_MOVE_TO_PRIV_NAMESPACE
  260. /* _GetFacetId is a helper function that allow delaying access to
  261. * facet id static instance in the library source code to avoid
  262. * the other static instances that many compilers are generating
  263. * in all dynamic library or executable when instanciating facet
  264. * template class.
  265. */
  266. template <class _Facet>
  267. inline locale::id& _GetFacetId(const _Facet*)
  268. { return _Facet::id; }
  269. _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const money_get<char, istreambuf_iterator<char, char_traits<char> > >*);
  270. _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const money_put<char, ostreambuf_iterator<char, char_traits<char> > >*);
  271. _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const num_get<char, istreambuf_iterator<char, char_traits<char> > >*);
  272. _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const num_put<char, ostreambuf_iterator<char, char_traits<char> > >*);
  273. _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const time_get<char, istreambuf_iterator<char, char_traits<char> > >*);
  274. _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const time_put<char, ostreambuf_iterator<char, char_traits<char> > >*);
  275. #ifndef _STLP_NO_WCHAR_T
  276. _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const money_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >*);
  277. _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const money_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >*);
  278. _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const num_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >*);
  279. _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const num_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >*);
  280. _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >*);
  281. _STLP_DECLSPEC locale::id& _STLP_CALL _GetFacetId(const time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >*);
  282. #endif
  283. template <class _Facet>
  284. inline bool _HasFacet(const locale& __loc, const _Facet* __facet) _STLP_NOTHROW
  285. { return (__loc._M_get_facet(_GetFacetId(__facet)) != 0); }
  286. template <class _Facet>
  287. inline _Facet* _UseFacet(const locale& __loc, const _Facet* __facet)
  288. { return __STATIC_CAST(_Facet*, __loc._M_use_facet(_GetFacetId(__facet))); }
  289. template <class _Facet>
  290. inline void _InsertFacet(locale& __loc, _Facet* __facet)
  291. { __loc._M_insert(__facet, _GetFacetId(__facet)); }
  292. _STLP_MOVE_TO_STD_NAMESPACE
  293. _STLP_END_NAMESPACE
  294. #endif /* _STLP_INTERNAL_LOCALE_H */
  295. // Local Variables:
  296. // mode:C++
  297. // End: