keyType.H
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2017-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27Class
28 Foam::keyType
29
30Description
31 A class for handling keywords in dictionaries.
32
33 A keyType is the keyword of a dictionary.
34 It differs from word in that it also accepts patterns (regular expressions).
35 It is very similar to wordRe, but doesn't store a regular expression.
36
37SourceFiles
38 keyType.C
39 keyTypeI.H
40
41\*---------------------------------------------------------------------------*/
42
43#ifndef keyType_H
44#define keyType_H
45
46#include "word.H"
47#include "wordRe.H"
48#include "stdFoam.H"
49
50// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51
52namespace Foam
53{
54
55// Forward Declarations
56class keyType;
57class token;
58Istream& operator>>(Istream& is, keyType& val);
59Ostream& operator<<(Ostream& os, const keyType& val);
60
61//- Hashing for keyType
62template<> struct Hash<keyType> : string::hasher {};
63
64
65/*---------------------------------------------------------------------------*\
66 Class keyType Declaration
67\*---------------------------------------------------------------------------*/
69class keyType
70:
71 public word
72{
73public:
74
75 // Public Data Types
76
77 //- Enumeration for the data type and search/match modes (bitmask)
78 // eg, (keyType::REGEX | keyType::RECURSIVE)
79 enum option : unsigned char
80 {
81 // Base stored types
82 LITERAL = 0,
83 REGEX = 1,
84
85 // Variants for search/match only
86 RECURSIVE = 0x80,
89 };
90
91
92private:
93
94 // Private Data
95
96 //- Treat keyType as literal, regex etc.
97 // Never contains RECURSIVE values.
98 option type_;
99
100
101public:
102
103 // Static Data Members
104
105 //- An empty keyType
106 static const keyType null;
107
108
109 // Constructors
110
111 //- Default construct, empty literal
112 inline keyType();
113
114 //- Copy construct
115 keyType(const keyType&) = default;
116
117 //- Move construct
118 keyType(keyType&&) = default;
119
120 //- Implicit copy construct from word, treat as LITERAL
121 inline keyType(const word& str);
122
123 //- Implicit move construct from word, treat as LITERAL
124 inline keyType(word&& str);
125
126 //- Implicit copy construct from Foam::string, treat as REGEX
127 inline keyType(const string& str);
128
129 //- Implicit move construct from Foam::string, treat as REGEX
130 inline keyType(string&& str);
131
132 //- Copy construct from std::string with specified treatment
133 inline keyType(const std::string& str, option opt);
134
135 //- Move construct from std::string with specified treatment
136 inline keyType(std::string&& str, option opt);
137
138 //- Implicit construct from character array,
139 //- with specified compile option (default is LITERAL)
140 inline keyType(const char* str, option opt = option::LITERAL);
141
142 //- Construct from Istream by reading a token
143 // Treat as regular expression if surrounded by quotation marks.
144 explicit keyType(Istream& is);
145
146
147 // Member Functions
148
149 //- Test for valid keyType character?
150 // Like Foam::word, but with brace-brackets,
151 // which are valid for some regexs.
152 inline static bool valid(const char c);
153
154
155 // Access
156
157 //- The keyType is treated as literal, not as pattern.
158 inline bool isLiteral() const noexcept;
159
160 //- The keyType is treated as a pattern, not as literal string.
161 inline bool isPattern() const noexcept;
162
163
164 // Infrastructure
165
166 //- Inherit all regular string assign() methods
167 using word::assign;
168
169 //- Assign from word or string token.
170 // Words are treated as literals, strings as regex
171 // \return false if the token was the incorrect type
172 bool assign(const token& tok);
173
174 //- Change the representation, optionally stripping invalid word
175 //- characters when changing to a literal
176 inline void setType(option opt, bool adjust = false);
177
178 //- Mark as regular expression
179 inline bool compile() noexcept;
180
181 //- Mark as literal string
182 inline void uncompile() noexcept;
183
184 //- Mark as literal string, optionally strip invalid word
185 //- characters when changing to a literal
186 inline void uncompile(bool adjust);
187
188
189 // Editing
190
191 //- Clear string and set as literal
192 inline void clear();
193
194 //- Swap contents. Self-swapping is a no-op.
195 void swap(keyType& rhs);
196
197
198 // Matching/Searching
199
200 //- Smart match as regular expression or as a string.
201 // Optionally force a literal match only
202 bool match(const std::string& text, bool literal=false) const;
203
204
205 // Member Operators
206
207 //- Perform smart match on text, as per match()
208 // Allows use as a predicate.
209 inline bool operator()(const std::string& text) const;
210
211 //- No assignment where type could be indeterminate
212 void operator=(const std::string&) = delete;
213
214 //- Copy assignment, retaining type (literal or regex)
215 // Self-assignment is a no-op.
216 inline void operator=(const keyType& str);
217
218 //- Move assignment, retaining type (literal or regex)
219 // Self-assignment is a no-op.
220 inline void operator=(keyType&& str);
221
222 //- Assign from word, treat as literal
223 inline void operator=(const word& str);
224
225 //- Assign from Foam::string, treat as regular expression
226 inline void operator=(const string& str);
227
228 //- Assign from character array, treat as literal
229 inline void operator=(const char* str);
230
231
232 // Housekeeping
233
234 //- Deprecated(2019-08) construct as literal/regex
235 // \deprecated(2019-08) - use Construct with option
236 FOAM_DEPRECATED_FOR(2019-08, "Construct with option")
237 keyType(const std::string& s, bool isPattern)
238 :
240 {}
241};
242
243
244// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
245
246// IOstream Operators
247
248//- Read operator
249Istream& operator>>(Istream& is, keyType& val);
250
251//- Write operator
252Ostream& operator<<(Ostream& os, const keyType& val);
253
254
255// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
256
257} // End namespace Foam
258
259// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
260
261#include "keyTypeI.H"
262
263// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
264
265#endif
266
267// ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
A class for handling keywords in dictionaries.
Definition: keyType.H:71
bool assign(const token &tok)
Assign from word or string token.
Definition: keyType.C:73
option
Enumeration for the data type and search/match modes (bitmask)
Definition: keyType.H:79
@ LITERAL
String literal.
Definition: keyType.H:81
@ REGEX_RECURSIVE
Definition: keyType.H:87
@ REGEX
Regular expression.
Definition: keyType.H:82
@ LITERAL_RECURSIVE
Definition: keyType.H:86
@ RECURSIVE
Recursive search (eg, in dictionary)
Definition: keyType.H:85
keyType(const keyType &)=default
Copy construct.
bool isLiteral() const noexcept
The keyType is treated as literal, not as pattern.
Definition: keyTypeI.H:98
static const keyType null
An empty keyType.
Definition: keyType.H:105
bool compile() noexcept
Mark as regular expression.
Definition: keyTypeI.H:127
static bool valid(const char c)
Test for valid keyType character?
Definition: keyTypeI.H:31
keyType(keyType &&)=default
Move construct.
void uncompile() noexcept
Mark as literal string.
Definition: keyTypeI.H:134
keyType()
Default construct, empty literal.
Definition: keyTypeI.H:40
void swap(keyType &rhs)
Swap contents. Self-swapping is a no-op.
Definition: keyType.C:50
bool isPattern() const noexcept
The keyType is treated as a pattern, not as literal string.
Definition: keyTypeI.H:104
void setType(option opt, bool adjust=false)
Definition: keyTypeI.H:110
void clear()
Clear string and set as literal.
Definition: keyTypeI.H:152
bool match(const std::string &text, bool literal=false) const
Smart match as regular expression or as a string.
Definition: keyType.C:62
A token holds an item read from Istream.
Definition: token.H:69
A class for handling words, derived from Foam::string.
Definition: word.H:68
OBJstream os(runTime.globalPath()/outputName)
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Namespace for OpenFOAM.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Istream & operator>>(Istream &, directionInfo &)
const direction noexcept
Definition: Scalar.H:223
Includes some standard C++ headers, defines global macros and templates used in multiple places by Op...
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition: stdFoam.H:52
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:54
Hashing functor for string and derived string classes.
Definition: string.H:148