sl@0: /*
sl@0:  * Copyright (c) 1999
sl@0:  * Silicon Graphics Computer Systems, Inc.
sl@0:  *
sl@0:  * Copyright (c) 1999
sl@0:  * Boris Fomitchev
sl@0:  *
sl@0:  * This material is provided "as is", with absolutely no warranty expressed
sl@0:  * or implied. Any use is at your own risk.
sl@0:  *
sl@0:  * Permission to use or copy this software for any purpose is hereby granted
sl@0:  * without fee, provided the above notices are retained on all copies.
sl@0:  * Permission to modify the code and to distribute modified code is granted,
sl@0:  * provided the above notices are retained, and a notice that the code was
sl@0:  * modified is included with the above copyright notice.
sl@0:  *
sl@0:  */
sl@0: #ifndef MESSAGE_FACETS_H
sl@0: #define MESSAGE_FACETS_H
sl@0: 
sl@0: #include <string>
sl@0: #include <locale>
sl@0: #include <typeinfo>
sl@0: #include <hash_map>
sl@0: 
sl@0: #include "c_locale.h"
sl@0: #include "acquire_release.h"
sl@0: 
sl@0: _STLP_BEGIN_NAMESPACE
sl@0: _STLP_MOVE_TO_PRIV_NAMESPACE
sl@0: 
sl@0: // Class _Catalog_locale_map.  The reason for this is that, internally,
sl@0: // a message string is always a char*.  We need a ctype facet to convert
sl@0: // a string to and from wchar_t, and the user is permitted to provide such
sl@0: // a facet when calling open().
sl@0: 
sl@0: struct _Catalog_locale_map {
sl@0:   _Catalog_locale_map() : M(0) {}
sl@0:   ~_Catalog_locale_map() { if (M) delete M; }
sl@0: 
sl@0:   void insert(nl_catd_type key, const locale& L);
sl@0:   locale lookup(nl_catd_type key) const;
sl@0:   void erase(nl_catd_type key);
sl@0: 
sl@0:   typedef hash_map<nl_catd_type, locale, hash<nl_catd_type>, equal_to<nl_catd_type> > map_type;
sl@0:   map_type *M;
sl@0: 
sl@0: private:                        // Invalidate copy constructor and assignment
sl@0:   _Catalog_locale_map(const _Catalog_locale_map&);
sl@0:   void operator=(const _Catalog_locale_map&);
sl@0: };
sl@0: 
sl@0: /*
sl@0:  * In glibc nl_catd type is void *, but messages_base::catalog is defined as int
sl@0:  * by ISO/IEC 14882; The int may be too short to store pointer on 64-bit platforms;
sl@0:  * Another problem, is that do_open() may return negative value to indicate that no
sl@0:  * catalog open---this case can't be represented with pointers.
sl@0:  * The class _Catalog_nl_catd_map intended to make relation between
sl@0:  * messages_base::catalog and nl_catd handler.
sl@0:  *
sl@0:  */
sl@0: 
sl@0: #if defined (_STLP_REAL_LOCALE_IMPLEMENTED) && (defined (_STLP_USE_GLIBC) && !defined (__CYGWIN__))
sl@0: #  define _STLP_USE_NL_CATD_MAPPING
sl@0: #else
sl@0: /* If no mapping a message_base::catalog entry, int typedef according C++ Standard 22.2.7.1,
sl@0:  * has to be large enough to contain a nl_catd_type value.
sl@0:  */
sl@0: _STLP_STATIC_ASSERT(sizeof(nl_catd_type) <= sizeof(int))
sl@0: #endif
sl@0: 
sl@0: class _STLP_CLASS_DECLSPEC _Catalog_nl_catd_map {
sl@0: public:
sl@0:   _Catalog_nl_catd_map()
sl@0:   {}
sl@0:   ~_Catalog_nl_catd_map()
sl@0:   {}
sl@0: 
sl@0:   typedef hash_map<messages_base::catalog, nl_catd_type, hash<messages_base::catalog>, equal_to<messages_base::catalog> > map_type;
sl@0:   typedef hash_map<nl_catd_type, messages_base::catalog, hash<nl_catd_type>, equal_to<nl_catd_type> > rmap_type;
sl@0:   // typedef map<messages_base::catalog,nl_catd_type> map_type;
sl@0:   // typedef map<nl_catd_type,messages_base::catalog> rmap_type;
sl@0: 
sl@0:   messages_base::catalog insert(nl_catd_type cat)
sl@0: #if !defined (_STLP_USE_NL_CATD_MAPPING)
sl@0:   { return (messages_base::catalog)cat; }
sl@0: #else
sl@0:   ;
sl@0: #endif
sl@0: 
sl@0:   void erase(messages_base::catalog)
sl@0: #if !defined (_STLP_USE_NL_CATD_MAPPING)
sl@0:   {}
sl@0: #else
sl@0:   ;
sl@0: #endif
sl@0: 
sl@0:   nl_catd_type operator [] ( messages_base::catalog cat ) const
sl@0: #if !defined (_STLP_USE_NL_CATD_MAPPING)
sl@0:   { return cat; }
sl@0: #else
sl@0:   { return cat < 0 ? 0 : M[cat]; }
sl@0: #endif
sl@0: 
sl@0: private:
sl@0:   _Catalog_nl_catd_map(const _Catalog_nl_catd_map&);
sl@0:   _Catalog_nl_catd_map& operator =(const _Catalog_nl_catd_map&);
sl@0: 
sl@0: #if defined (_STLP_USE_NL_CATD_MAPPING)
sl@0:   mutable map_type M;
sl@0:   mutable rmap_type Mr;
sl@0:   static _STLP_VOLATILE __stl_atomic_t _count;
sl@0: #endif
sl@0: };
sl@0: 
sl@0: class _STLP_CLASS_DECLSPEC _Messages {
sl@0: public:
sl@0:   typedef messages_base::catalog catalog;
sl@0: 
sl@0:   _Messages();
sl@0: 
sl@0:   virtual catalog do_open(const string& __fn, const locale& __loc) const;
sl@0:   virtual string do_get(catalog __c, int __set, int __msgid,
sl@0:                         const string& __dfault) const;
sl@0: #if !defined (_STLP_NO_WCHAR_T)
sl@0:   virtual wstring do_get(catalog __c, int __set, int __msgid,
sl@0:                          const wstring& __dfault) const;
sl@0: #endif
sl@0:   virtual void do_close(catalog __c) const;
sl@0:   virtual ~_Messages();
sl@0:   bool _M_delete;
sl@0: };
sl@0: 
sl@0: class _STLP_CLASS_DECLSPEC _Messages_impl : public _Messages {
sl@0: public:
sl@0:   _Messages_impl(bool, _Locale_name_hint* hint = 0);
sl@0:   _Messages_impl(bool, _Locale_messages*);
sl@0: 
sl@0:   catalog do_open(const string& __fn, const locale& __loc) const;
sl@0:   string do_get(catalog __c, int __set, int __msgid,
sl@0:                 const string& __dfault) const;
sl@0: #if !defined (_STLP_NO_WCHAR_T)
sl@0:   wstring do_get(catalog __c, int __set, int __msgid,
sl@0:                  const wstring& __dfault) const;
sl@0: #endif
sl@0:   void do_close(catalog __c) const;
sl@0: 
sl@0:   ~_Messages_impl();
sl@0: 
sl@0: private:
sl@0:   _Locale_messages* _M_message_obj;
sl@0:   _Catalog_locale_map* _M_map;
sl@0:   mutable _Catalog_nl_catd_map _M_cat;
sl@0: 
sl@0:   //private definition to avoid warning (with ICL)
sl@0:   _Messages_impl(const _Messages_impl&);
sl@0:   _Messages_impl& operator=(const _Messages_impl&);
sl@0: };
sl@0: 
sl@0: _STLP_MOVE_TO_STD_NAMESPACE
sl@0: 
sl@0: _STLP_END_NAMESPACE
sl@0: 
sl@0: #endif
sl@0: 
sl@0: // Local Variables:
sl@0: // mode:C++
sl@0: // End: