00001
00002
00003
00004
00005
00006
00007
00008 #ifndef BOOST_LOCALE_COLLATOR_HPP_INCLUDED
00009 #define BOOST_LOCALE_COLLATOR_HPP_INCLUDED
00010
00011 #include <boost/locale/config.hpp>
00012 #ifdef BOOST_MSVC
00013 # pragma warning(push)
00014 # pragma warning(disable : 4275 4251 4231 4660)
00015 #endif
00016 #include <locale>
00017
00018
00019 namespace boost {
00020 namespace locale {
00021
00022 class info;
00023
00030
00034
00035 class collator_base {
00036 public:
00040 typedef enum {
00041 primary = 0,
00042 secondary = 1,
00043 tertiary = 2,
00044 quaternary = 3,
00045 identical = 4
00046 } level_type;
00047 };
00048
00055 template<typename CharType>
00056 class collator :
00057 public std::collate<CharType>,
00058 public collator_base
00059 {
00060 public:
00064 typedef CharType char_type;
00068 typedef std::basic_string<CharType> string_type;
00069
00070
00074 int compare(level_type level,
00075 char_type const *b1,char_type const *e1,
00076 char_type const *b2,char_type const *e2) const
00077 {
00078 return do_compare(level,b1,e1,b2,e2);
00079 }
00084 string_type transform(level_type level,char_type const *b,char_type const *e) const
00085 {
00086 return do_transform(level,b,e);
00087 }
00088
00092 long hash(level_type level,char_type const *b,char_type const *e) const
00093 {
00094 return do_hash(level,b,e);
00095 }
00096
00100 int compare(level_type level,string_type const &l,string_type const &r) const
00101 {
00102 return do_compare(level,l.data(),l.data()+l.size(),r.data(),r.data()+r.size());
00103 }
00104
00108
00109 long hash(level_type level,string_type const &s) const
00110 {
00111 return do_hash(level,s.data(),s.data()+s.size());
00112 }
00117 string_type transform(level_type level,string_type const &s) const
00118 {
00119 return do_transform(level,s.data(),s.data()+s.size());
00120 }
00121
00125 static collator<CharType> *create(info const &inf);
00126
00127 protected:
00128
00132 collator(size_t refs = 0) : std::collate<CharType>(refs)
00133 {
00134 }
00135
00136 virtual ~collator()
00137 {
00138 }
00139
00144 virtual int do_compare( char_type const *b1,char_type const *e1,
00145 char_type const *b2,char_type const *e2) const
00146 {
00147 return do_compare(primary,b1,e1,b2,e2);
00148 }
00153 virtual string_type do_transform(char_type const *b,char_type const *e) const
00154 {
00155 return do_transform(primary,b,e);
00156 }
00161 virtual long do_hash(char_type const *b,char_type const *e) const
00162 {
00163 return do_hash(primary,b,e);
00164 }
00165
00169 virtual int do_compare( level_type level,
00170 char_type const *b1,char_type const *e1,
00171 char_type const *b2,char_type const *e2) const = 0;
00175 virtual string_type do_transform(level_type level,char_type const *b,char_type const *e) const = 0;
00179 virtual long do_hash(level_type level,char_type const *b,char_type const *e) const = 0;
00180
00181
00182 };
00183
00185
00186 template<>
00187 BOOST_LOCALE_DECL collator<char> *collator<char>::create(info const &inf);
00188 #ifndef BOOST_NO_STD_WSTRING
00189 template<>
00190 BOOST_LOCALE_DECL collator<wchar_t> *collator<wchar_t>::create(info const &inf);
00191 #endif
00192
00193 #ifdef BOOST_HAS_CHAR16_T
00194 template<>
00195 BOOST_LOCALE_DECL collator<char16_t> *collator<char16_t>::create(info const &inf);
00196 #endif
00197
00198 #ifdef BOOST_HAS_CHAR32_T
00199 template<>
00200 BOOST_LOCALE_DECL collator<char32_t> *collator<char32_t>::create(info const &inf);
00201 #endif
00203
00216 template<typename CharType,collator_base::level_type default_level = collator_base::primary>
00217 struct comparator
00218 {
00219 public:
00223 comparator(std::locale const &l=std::locale(),collator_base::level_type level=default_level) :
00224 locale_(l),
00225 level_(level)
00226 {
00227 }
00228
00232 bool operator()(std::basic_string<CharType> const &left,std::basic_string<CharType> const &right) const
00233 {
00234 return std::use_facet<collator<CharType> >(locale_).compare(level_,left,right) < 0;
00235 }
00236 private:
00237 std::locale locale_;
00238 collator_base::level_type level_;
00239 };
00240
00241
00245
00246 }
00247 }
00248
00249 #ifdef BOOST_MSVC
00250 #pragma warning(pop)
00251 #endif
00252
00253
00254 #endif
00259 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4