message_facets.h 4.5 KB

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