1 |
// Boost string_algo library classification.hpp header file ---------------------------// |
2 |
|
3 |
// Copyright Pavol Droba 2002-2003. Use, modification and |
4 |
// distribution is subject to the Boost Software License, Version |
5 |
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
6 |
// http://www.boost.org/LICENSE_1_0.txt) |
7 |
|
8 |
// See http://www.boost.org for updates, documentation, and revision history. |
9 |
#ifndef UTILS_PREDICATE_HPP |
10 |
#define UTILS_PREDICATE_HPP |
11 |
#include <locale> |
12 |
|
13 |
|
14 |
|
15 |
#if defined(__SUNPRO_CC) |
16 |
# define USE_FACET(Type, loc) std::use_facet(loc, static_cast<Type*>(0)) |
17 |
#else |
18 |
# define USE_FACET(Type, loc) std::use_facet< Type >(loc) |
19 |
#endif |
20 |
|
21 |
namespace OpenMD { |
22 |
|
23 |
template<typename Derived> |
24 |
struct PredFacade{}; |
25 |
|
26 |
struct CharClassification: public PredFacade<CharClassification> { |
27 |
|
28 |
CharClassification(std::ctype_base::mask type, std::locale const & loc = std::locale()) : |
29 |
type_(type), loc_(loc) {} |
30 |
|
31 |
template<typename CharT> |
32 |
bool operator()( CharT c ) const { |
33 |
return USE_FACET(std::ctype<CharT>, loc_).is( type_, c ); |
34 |
} |
35 |
|
36 |
private: |
37 |
const std::ctype_base::mask type_; |
38 |
const std::locale loc_; |
39 |
}; |
40 |
|
41 |
template<typename CharT> |
42 |
struct FromRangeFunctor : public PredFacade< FromRangeFunctor<CharT> > { |
43 |
|
44 |
FromRangeFunctor( CharT from, CharT to ) : from_(from), to_(to) {} |
45 |
|
46 |
template<typename Char2T> |
47 |
bool operator()( Char2T c ) const { |
48 |
return ( from_ <= c ) && ( c <= to_ ); |
49 |
} |
50 |
|
51 |
private: |
52 |
CharT from_; |
53 |
CharT to_; |
54 |
}; |
55 |
|
56 |
template<typename Pred1T, typename Pred2T> |
57 |
struct PredAndFunctor : public PredFacade< PredAndFunctor<Pred1T,Pred2T> > { |
58 |
public: |
59 |
|
60 |
PredAndFunctor( Pred1T pred1, Pred2T pred2 ) : pred1_(pred1), pred2_(pred2) {} |
61 |
|
62 |
template<typename CharT> |
63 |
bool operator()( CharT c ) const { |
64 |
return pred1_(c) && pred2_(c); |
65 |
} |
66 |
|
67 |
private: |
68 |
Pred1T pred1_; |
69 |
Pred2T pred2_; |
70 |
}; |
71 |
|
72 |
template<typename Pred1T, typename Pred2T> |
73 |
struct PredOrFunctor : public PredFacade< PredOrFunctor<Pred1T,Pred2T> > { |
74 |
public: |
75 |
|
76 |
PredOrFunctor( Pred1T pred1, Pred2T pred2 ) : pred1_(pred1), pred2_(pred2) {} |
77 |
|
78 |
template<typename CharT> |
79 |
bool operator()( CharT c ) const { |
80 |
return pred1_(c) || pred2_(c); |
81 |
} |
82 |
|
83 |
private: |
84 |
Pred1T pred1_; |
85 |
Pred2T pred2_; |
86 |
}; |
87 |
|
88 |
template< typename PredT > |
89 |
struct PredNotFunctor : public PredFacade< PredNotFunctor<PredT> > { |
90 |
public: |
91 |
|
92 |
PredNotFunctor( PredT pred ) : pred_(pred) {} |
93 |
|
94 |
template<typename CharT> |
95 |
bool operator()( CharT c ) const { |
96 |
return !pred_(c); |
97 |
} |
98 |
|
99 |
private: |
100 |
PredT pred_; |
101 |
}; |
102 |
|
103 |
inline CharClassification isSpace(const std::locale& loc=std::locale()){ |
104 |
return CharClassification(std::ctype_base::space, loc); |
105 |
} |
106 |
|
107 |
inline CharClassification isAlnum(const std::locale& loc=std::locale()){ |
108 |
return CharClassification(std::ctype_base::alnum, loc); |
109 |
} |
110 |
|
111 |
inline CharClassification isAlpha(const std::locale& loc=std::locale()){ |
112 |
return CharClassification(std::ctype_base::alpha, loc); |
113 |
} |
114 |
|
115 |
inline CharClassification isCntrl(const std::locale& loc=std::locale()) { |
116 |
return CharClassification(std::ctype_base::cntrl, loc); |
117 |
} |
118 |
|
119 |
inline CharClassification isDigit(const std::locale& loc=std::locale()) { |
120 |
return CharClassification(std::ctype_base::digit, loc); |
121 |
} |
122 |
|
123 |
inline CharClassification isGraph(const std::locale& loc=std::locale()) { |
124 |
return CharClassification(std::ctype_base::graph, loc); |
125 |
} |
126 |
|
127 |
inline CharClassification isLower(const std::locale& loc=std::locale()) { |
128 |
return CharClassification(std::ctype_base::lower, loc); |
129 |
} |
130 |
|
131 |
inline CharClassification isPrint(const std::locale& loc=std::locale()) { |
132 |
return CharClassification(std::ctype_base::print, loc); |
133 |
} |
134 |
|
135 |
inline CharClassification isPunct(const std::locale& loc=std::locale()) { |
136 |
return CharClassification(std::ctype_base::punct, loc); |
137 |
} |
138 |
|
139 |
inline CharClassification isUpper(const std::locale& loc=std::locale()) { |
140 |
return CharClassification(std::ctype_base::upper, loc); |
141 |
} |
142 |
|
143 |
inline CharClassification isXDigit(const std::locale& loc=std::locale()) { |
144 |
return CharClassification(std::ctype_base::xdigit, loc); |
145 |
} |
146 |
|
147 |
|
148 |
template<typename CharT> |
149 |
inline FromRangeFunctor<CharT> isFromRange(CharT from, CharT to) { |
150 |
return FromRangeFunctor<CharT>(from, to); |
151 |
} |
152 |
|
153 |
template<typename Pred1T, typename Pred2T> |
154 |
inline PredAndFunctor<Pred1T, Pred2T> |
155 |
operator&&(const PredFacade<Pred1T>& Pred1, const PredFacade<Pred2T>& Pred2) { |
156 |
// Doing the static_cast with the pointer instead of the reference |
157 |
// is a workaround for some compilers which have problems with |
158 |
// static_cast's of template references, i.e. CW8. /grafik/ |
159 |
return PredAndFunctor<Pred1T,Pred2T>( |
160 |
*static_cast<const Pred1T*>(&Pred1), |
161 |
*static_cast<const Pred2T*>(&Pred2) ); |
162 |
} |
163 |
|
164 |
|
165 |
template<typename Pred1T, typename Pred2T> |
166 |
inline PredOrFunctor<Pred1T, Pred2T> |
167 |
operator||( const PredFacade<Pred1T>& Pred1, const PredFacade<Pred2T>& Pred2 ) { |
168 |
// Doing the static_cast with the pointer instead of the reference |
169 |
// is a workaround for some compilers which have problems with |
170 |
// static_cast's of template references, i.e. CW8. /grafik/ |
171 |
return PredOrFunctor<Pred1T,Pred2T>( |
172 |
*static_cast<const Pred1T*>(&Pred1), |
173 |
*static_cast<const Pred2T*>(&Pred2)); |
174 |
} |
175 |
|
176 |
template<typename PredT> |
177 |
inline PredNotFunctor<PredT> |
178 |
operator!( const PredFacade<PredT>& Pred ) { |
179 |
// Doing the static_cast with the pointer instead of the reference |
180 |
// is a workaround for some compilers which have problems with |
181 |
// static_cast's of template references, i.e. CW8. /grafik/ |
182 |
return PredNotFunctor<PredT>(*static_cast<const PredT*>(&Pred)); |
183 |
} |
184 |
|
185 |
} |
186 |
|
187 |
#endif |