• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef MESSAGE_FACETS_H
19 #define MESSAGE_FACETS_H
20 
21 #include <string>
22 #include <locale>
23 #include <hash_map>
24 
25 #include "c_locale.h"
26 
27 _STLP_BEGIN_NAMESPACE
28 _STLP_MOVE_TO_PRIV_NAMESPACE
29 
30 // Class _Catalog_locale_map.  The reason for this is that, internally,
31 // a message string is always a char*.  We need a ctype facet to convert
32 // a string to and from wchar_t, and the user is permitted to provide such
33 // a facet when calling open().
34 
35 struct _Catalog_locale_map {
_Catalog_locale_map_Catalog_locale_map36   _Catalog_locale_map() : M(0) {}
~_Catalog_locale_map_Catalog_locale_map37   ~_Catalog_locale_map() { if (M) delete M; }
38 
39   void insert(nl_catd_type key, const locale& L);
40   locale lookup(nl_catd_type key) const;
41   void erase(nl_catd_type key);
42 
43   typedef hash_map<nl_catd_type, locale, hash<nl_catd_type>, equal_to<nl_catd_type>,
44                    allocator<pair<_STLP_CONST nl_catd_type, locale> > > map_type;
45   map_type *M;
46 
47 private:                        // Invalidate copy constructor and assignment
48   _Catalog_locale_map(const _Catalog_locale_map&);
49   void operator=(const _Catalog_locale_map&);
50 };
51 
52 /*
53  * In glibc nl_catd type is void *, but messages_base::catalog is defined as int
54  * by ISO/IEC 14882; The int may be too short to store pointer on 64-bit platforms;
55  * Another problem, is that do_open() may return negative value to indicate that no
56  * catalog open---this case can't be represented with pointers.
57  * The class _Catalog_nl_catd_map intended to make relation between
58  * messages_base::catalog and nl_catd handler.
59  *
60  */
61 
62 #if defined (_STLP_USE_GLIBC2_LOCALIZATION)
63 #  define _STLP_USE_NL_CATD_MAPPING
64 #else
65 /* If no mapping a message_base::catalog entry, int typedef according C++ Standard 22.2.7.1,
66  * has to be large enough to contain a nl_catd_type value.
67  */
68 _STLP_STATIC_ASSERT(sizeof(nl_catd_type) <= sizeof(int))
69 #endif
70 
71 class _STLP_CLASS_DECLSPEC _Catalog_nl_catd_map {
72 public:
_Catalog_nl_catd_map()73   _Catalog_nl_catd_map()
74   {}
~_Catalog_nl_catd_map()75   ~_Catalog_nl_catd_map()
76   {}
77 
78   typedef hash_map<messages_base::catalog, nl_catd_type, hash<messages_base::catalog>, equal_to<messages_base::catalog>,
79                    allocator<pair<_STLP_CONST messages_base::catalog, nl_catd_type> > > map_type;
80   typedef hash_map<nl_catd_type, messages_base::catalog, hash<nl_catd_type>, equal_to<nl_catd_type>,
81                    allocator<pair<_STLP_CONST nl_catd_type, messages_base::catalog> > > rmap_type;
82   // typedef map<messages_base::catalog,nl_catd_type> map_type;
83   // typedef map<nl_catd_type,messages_base::catalog> rmap_type;
84 
insert(nl_catd_type cat)85   messages_base::catalog insert(nl_catd_type cat)
86 #if !defined (_STLP_USE_NL_CATD_MAPPING)
87   { return (messages_base::catalog)cat; }
88 #else
89   ;
90 #endif
91 
erase(messages_base::catalog)92   void erase(messages_base::catalog)
93 #if !defined (_STLP_USE_NL_CATD_MAPPING)
94   {}
95 #else
96   ;
97 #endif
98 
99   nl_catd_type operator [] ( messages_base::catalog cat )
100 #if !defined (_STLP_USE_NL_CATD_MAPPING)
101   { return cat; }
102 #else
103   { return cat < 0 ? 0 : M[cat]; }
104 #endif
105 
106 private:
107   _Catalog_nl_catd_map(const _Catalog_nl_catd_map&);
108   _Catalog_nl_catd_map& operator =(const _Catalog_nl_catd_map&);
109 
110 #if defined (_STLP_USE_NL_CATD_MAPPING)
111   map_type M;
112   rmap_type Mr;
113   static _STLP_VOLATILE __stl_atomic_t _count;
114 #endif
115 };
116 
117 class _Messages {
118 public:
119   typedef messages_base::catalog catalog;
120 
121   _Messages(bool, const char *name);
122   _Messages(bool, _Locale_messages*);
123 
124   catalog do_open(const string& __fn, const locale& __loc) const;
125   string do_get(catalog __c, int __set, int __msgid,
126                 const string& __dfault) const;
127 #if !defined (_STLP_NO_WCHAR_T)
128   wstring do_get(catalog __c, int __set, int __msgid,
129                  const wstring& __dfault) const;
130 #endif
131   void do_close(catalog __c) const;
132   ~_Messages();
133 
134 private:
135   _Locale_messages* _M_message_obj;
136   _Catalog_locale_map* _M_map;
137   mutable _Catalog_nl_catd_map _M_cat;
138 
139   //private definition to avoid warning (with ICL)
140   _Messages(const _Messages&);
141   _Messages& operator=(const _Messages&);
142 };
143 
144 _STLP_MOVE_TO_STD_NAMESPACE
145 
146 _STLP_END_NAMESPACE
147 
148 #endif
149 
150 // Local Variables:
151 // mode:C++
152 // End:
153