CppCMS
collator.h
1 //
2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOSTER_LOCALE_COLLATOR_H_INCLUDED
9 #define BOOSTER_LOCALE_COLLATOR_H_INCLUDED
10 
11 #include <booster/config.h>
12 #ifdef BOOSTER_MSVC
13 # pragma warning(push)
14 # pragma warning(disable : 4275 4251 4231 4660)
15 #endif
16 #include <locale>
17 
18 
19 namespace booster {
20 namespace locale {
21 
22  class info;
23 
30 
34 
35  class collator_base {
36  public:
40  typedef enum {
41  primary = 0,
42  secondary = 1,
43  tertiary = 2,
44  quaternary = 3,
45  identical = 4
46  } level_type;
47  };
48 
55  template<typename CharType>
56  class collator :
57  public std::collate<CharType>,
58  public collator_base
59  {
60  public:
64  typedef CharType char_type;
68  typedef std::basic_string<CharType> string_type;
69 
70 
77  int compare(level_type level,
78  char_type const *b1,char_type const *e1,
79  char_type const *b2,char_type const *e2) const
80  {
81  return do_compare(level,b1,e1,b2,e2);
82  }
94  string_type transform(level_type level,char_type const *b,char_type const *e) const
95  {
96  return do_transform(level,b,e);
97  }
98 
106  long hash(level_type level,char_type const *b,char_type const *e) const
107  {
108  return do_hash(level,b,e);
109  }
110 
118  int compare(level_type level,string_type const &l,string_type const &r) const
119  {
120  return do_compare(level,l.data(),l.data()+l.size(),r.data(),r.data()+r.size());
121  }
122 
128 
129  long hash(level_type level,string_type const &s) const
130  {
131  return do_hash(level,s.data(),s.data()+s.size());
132  }
142  string_type transform(level_type level,string_type const &s) const
143  {
144  return do_transform(level,s.data(),s.data()+s.size());
145  }
146 
147  protected:
148 
152  collator(size_t refs = 0) : std::collate<CharType>(refs)
153  {
154  }
155 
156  virtual ~collator()
157  {
158  }
159 
164  virtual int do_compare( char_type const *b1,char_type const *e1,
165  char_type const *b2,char_type const *e2) const
166  {
167  return do_compare(identical,b1,e1,b2,e2);
168  }
173  virtual string_type do_transform(char_type const *b,char_type const *e) const
174  {
175  return do_transform(identical,b,e);
176  }
181  virtual long do_hash(char_type const *b,char_type const *e) const
182  {
183  return do_hash(identical,b,e);
184  }
185 
189  virtual int do_compare( level_type level,
190  char_type const *b1,char_type const *e1,
191  char_type const *b2,char_type const *e2) const = 0;
195  virtual string_type do_transform(level_type level,char_type const *b,char_type const *e) const = 0;
199  virtual long do_hash(level_type level,char_type const *b,char_type const *e) const = 0;
200 
201 
202  };
203 
216  template<typename CharType,collator_base::level_type default_level = collator_base::identical>
217  struct comparator
218  {
219  public:
225  comparator(std::locale const &l=std::locale(),collator_base::level_type level=default_level) :
226  locale_(l),
227  level_(level)
228  {
229  }
230 
234  bool operator()(std::basic_string<CharType> const &left,std::basic_string<CharType> const &right) const
235  {
236  return std::use_facet<collator<CharType> >(locale_).compare(level_,left,right) < 0;
237  }
238  private:
239  std::locale locale_;
241  };
242 
243 
247 
248  } // locale
249 } // boost
250 
251 #ifdef BOOSTER_MSVC
252 #pragma warning(pop)
253 #endif
254 
255 
256 #endif
257 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
a base class that includes collation level flags
Definition: collator.h:35
virtual int do_compare(char_type const *b1, char_type const *e1, char_type const *b2, char_type const *e2) const
Definition: collator.h:164
4th collation level: letters, accents, case and punctuation
Definition: collator.h:44
2nd collation level: letters and accents
Definition: collator.h:42
identical collation level: include code-point comparison
Definition: collator.h:45
long hash(level_type level, string_type const &s) const
Definition: collator.h:129
comparator(std::locale const &l=std::locale(), collator_base::level_type level=default_level)
Definition: collator.h:225
CharType char_type
Definition: collator.h:64
int compare(level_type level, char_type const *b1, char_type const *e1, char_type const *b2, char_type const *e2) const
Definition: collator.h:77
string_type transform(level_type level, string_type const &s) const
Definition: collator.h:142
1st collation level: base letters
Definition: collator.h:41
std::basic_string< CharType > string_type
Definition: collator.h:68
This is the main namespace that encloses all localization classes.
Definition: locale_fwd.h:14
Collation facet.
Definition: collator.h:56
3rd collation level: letters, accents and case
Definition: collator.h:43
string_type transform(level_type level, char_type const *b, char_type const *e) const
Definition: collator.h:94
virtual string_type do_transform(char_type const *b, char_type const *e) const
Definition: collator.h:173
This class can be used in STL algorithms and containers for comparison of strings with a level other ...
Definition: collator.h:217
collator(size_t refs=0)
Definition: collator.h:152
bool operator()(std::basic_string< CharType > const &left, std::basic_string< CharType > const &right) const
Definition: collator.h:234
int compare(level_type level, string_type const &l, string_type const &r) const
Definition: collator.h:118
Booster library namespace. The library that implements Boost Like API in ABI backward compatible way...
Definition: application.h:23
long hash(level_type level, char_type const *b, char_type const *e) const
Definition: collator.h:106
virtual long do_hash(char_type const *b, char_type const *e) const
Definition: collator.h:181
level_type
Definition: collator.h:40