Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (c) 1999 |
michael@0 | 3 | * Silicon Graphics Computer Systems, Inc. |
michael@0 | 4 | * |
michael@0 | 5 | * Copyright (c) 1999 |
michael@0 | 6 | * Boris Fomitchev |
michael@0 | 7 | * |
michael@0 | 8 | * This material is provided "as is", with absolutely no warranty expressed |
michael@0 | 9 | * or implied. Any use is at your own risk. |
michael@0 | 10 | * |
michael@0 | 11 | * Permission to use or copy this software for any purpose is hereby granted |
michael@0 | 12 | * without fee, provided the above notices are retained on all copies. |
michael@0 | 13 | * Permission to modify the code and to distribute modified code is granted, |
michael@0 | 14 | * provided the above notices are retained, and a notice that the code was |
michael@0 | 15 | * modified is included with the above copyright notice. |
michael@0 | 16 | * |
michael@0 | 17 | */ |
michael@0 | 18 | #ifndef MESSAGE_FACETS_H |
michael@0 | 19 | #define MESSAGE_FACETS_H |
michael@0 | 20 | |
michael@0 | 21 | #include <string> |
michael@0 | 22 | #include <locale> |
michael@0 | 23 | #include <hash_map> |
michael@0 | 24 | |
michael@0 | 25 | #include "c_locale.h" |
michael@0 | 26 | |
michael@0 | 27 | _STLP_BEGIN_NAMESPACE |
michael@0 | 28 | _STLP_MOVE_TO_PRIV_NAMESPACE |
michael@0 | 29 | |
michael@0 | 30 | // Class _Catalog_locale_map. The reason for this is that, internally, |
michael@0 | 31 | // a message string is always a char*. We need a ctype facet to convert |
michael@0 | 32 | // a string to and from wchar_t, and the user is permitted to provide such |
michael@0 | 33 | // a facet when calling open(). |
michael@0 | 34 | |
michael@0 | 35 | struct _Catalog_locale_map { |
michael@0 | 36 | _Catalog_locale_map() : M(0) {} |
michael@0 | 37 | ~_Catalog_locale_map() { if (M) delete M; } |
michael@0 | 38 | |
michael@0 | 39 | void insert(nl_catd_type key, const locale& L); |
michael@0 | 40 | locale lookup(nl_catd_type key) const; |
michael@0 | 41 | void erase(nl_catd_type key); |
michael@0 | 42 | |
michael@0 | 43 | typedef hash_map<nl_catd_type, locale, hash<nl_catd_type>, equal_to<nl_catd_type>, |
michael@0 | 44 | allocator<pair<_STLP_CONST nl_catd_type, locale> > > map_type; |
michael@0 | 45 | map_type *M; |
michael@0 | 46 | |
michael@0 | 47 | private: // Invalidate copy constructor and assignment |
michael@0 | 48 | _Catalog_locale_map(const _Catalog_locale_map&); |
michael@0 | 49 | void operator=(const _Catalog_locale_map&); |
michael@0 | 50 | }; |
michael@0 | 51 | |
michael@0 | 52 | /* |
michael@0 | 53 | * In glibc nl_catd type is void *, but messages_base::catalog is defined as int |
michael@0 | 54 | * by ISO/IEC 14882; The int may be too short to store pointer on 64-bit platforms; |
michael@0 | 55 | * Another problem, is that do_open() may return negative value to indicate that no |
michael@0 | 56 | * catalog open---this case can't be represented with pointers. |
michael@0 | 57 | * The class _Catalog_nl_catd_map intended to make relation between |
michael@0 | 58 | * messages_base::catalog and nl_catd handler. |
michael@0 | 59 | * |
michael@0 | 60 | */ |
michael@0 | 61 | |
michael@0 | 62 | #if defined (_STLP_USE_GLIBC2_LOCALIZATION) |
michael@0 | 63 | # define _STLP_USE_NL_CATD_MAPPING |
michael@0 | 64 | #else |
michael@0 | 65 | /* If no mapping a message_base::catalog entry, int typedef according C++ Standard 22.2.7.1, |
michael@0 | 66 | * has to be large enough to contain a nl_catd_type value. |
michael@0 | 67 | */ |
michael@0 | 68 | _STLP_STATIC_ASSERT(sizeof(nl_catd_type) <= sizeof(int)) |
michael@0 | 69 | #endif |
michael@0 | 70 | |
michael@0 | 71 | class _STLP_CLASS_DECLSPEC _Catalog_nl_catd_map { |
michael@0 | 72 | public: |
michael@0 | 73 | _Catalog_nl_catd_map() |
michael@0 | 74 | {} |
michael@0 | 75 | ~_Catalog_nl_catd_map() |
michael@0 | 76 | {} |
michael@0 | 77 | |
michael@0 | 78 | typedef hash_map<messages_base::catalog, nl_catd_type, hash<messages_base::catalog>, equal_to<messages_base::catalog>, |
michael@0 | 79 | allocator<pair<_STLP_CONST messages_base::catalog, nl_catd_type> > > map_type; |
michael@0 | 80 | typedef hash_map<nl_catd_type, messages_base::catalog, hash<nl_catd_type>, equal_to<nl_catd_type>, |
michael@0 | 81 | allocator<pair<_STLP_CONST nl_catd_type, messages_base::catalog> > > rmap_type; |
michael@0 | 82 | // typedef map<messages_base::catalog,nl_catd_type> map_type; |
michael@0 | 83 | // typedef map<nl_catd_type,messages_base::catalog> rmap_type; |
michael@0 | 84 | |
michael@0 | 85 | messages_base::catalog insert(nl_catd_type cat) |
michael@0 | 86 | #if !defined (_STLP_USE_NL_CATD_MAPPING) |
michael@0 | 87 | { return (messages_base::catalog)cat; } |
michael@0 | 88 | #else |
michael@0 | 89 | ; |
michael@0 | 90 | #endif |
michael@0 | 91 | |
michael@0 | 92 | void erase(messages_base::catalog) |
michael@0 | 93 | #if !defined (_STLP_USE_NL_CATD_MAPPING) |
michael@0 | 94 | {} |
michael@0 | 95 | #else |
michael@0 | 96 | ; |
michael@0 | 97 | #endif |
michael@0 | 98 | |
michael@0 | 99 | nl_catd_type operator [] ( messages_base::catalog cat ) |
michael@0 | 100 | #if !defined (_STLP_USE_NL_CATD_MAPPING) |
michael@0 | 101 | { return cat; } |
michael@0 | 102 | #else |
michael@0 | 103 | { return cat < 0 ? 0 : M[cat]; } |
michael@0 | 104 | #endif |
michael@0 | 105 | |
michael@0 | 106 | private: |
michael@0 | 107 | _Catalog_nl_catd_map(const _Catalog_nl_catd_map&); |
michael@0 | 108 | _Catalog_nl_catd_map& operator =(const _Catalog_nl_catd_map&); |
michael@0 | 109 | |
michael@0 | 110 | #if defined (_STLP_USE_NL_CATD_MAPPING) |
michael@0 | 111 | map_type M; |
michael@0 | 112 | rmap_type Mr; |
michael@0 | 113 | static _STLP_VOLATILE __stl_atomic_t _count; |
michael@0 | 114 | #endif |
michael@0 | 115 | }; |
michael@0 | 116 | |
michael@0 | 117 | class _Messages { |
michael@0 | 118 | public: |
michael@0 | 119 | typedef messages_base::catalog catalog; |
michael@0 | 120 | |
michael@0 | 121 | _Messages(bool, const char *name); |
michael@0 | 122 | _Messages(bool, _Locale_messages*); |
michael@0 | 123 | |
michael@0 | 124 | catalog do_open(const string& __fn, const locale& __loc) const; |
michael@0 | 125 | string do_get(catalog __c, int __set, int __msgid, |
michael@0 | 126 | const string& __dfault) const; |
michael@0 | 127 | #if !defined (_STLP_NO_WCHAR_T) |
michael@0 | 128 | wstring do_get(catalog __c, int __set, int __msgid, |
michael@0 | 129 | const wstring& __dfault) const; |
michael@0 | 130 | #endif |
michael@0 | 131 | void do_close(catalog __c) const; |
michael@0 | 132 | ~_Messages(); |
michael@0 | 133 | |
michael@0 | 134 | private: |
michael@0 | 135 | _Locale_messages* _M_message_obj; |
michael@0 | 136 | _Catalog_locale_map* _M_map; |
michael@0 | 137 | mutable _Catalog_nl_catd_map _M_cat; |
michael@0 | 138 | |
michael@0 | 139 | //private definition to avoid warning (with ICL) |
michael@0 | 140 | _Messages(const _Messages&); |
michael@0 | 141 | _Messages& operator=(const _Messages&); |
michael@0 | 142 | }; |
michael@0 | 143 | |
michael@0 | 144 | _STLP_MOVE_TO_STD_NAMESPACE |
michael@0 | 145 | |
michael@0 | 146 | _STLP_END_NAMESPACE |
michael@0 | 147 | |
michael@0 | 148 | #endif |
michael@0 | 149 | |
michael@0 | 150 | // Local Variables: |
michael@0 | 151 | // mode:C++ |
michael@0 | 152 | // End: |