keyTypeI.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 -------------------------------------------------------------------------------
11 License
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 
27 \*---------------------------------------------------------------------------*/
28 
29 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
30 
31 inline bool Foam::keyType::valid(const char c)
32 {
33  // Also accept '{' and '}' (for regex grouping?)
34  return (word::valid(c) || c == '{' || c == '}');
35 }
36 
37 
38 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
39 
41 :
42  word(),
43  type_(option::LITERAL)
44 {}
45 
46 
47 inline Foam::keyType::keyType(const word& str)
48 :
49  word(str),
50  type_(option::LITERAL)
51 {}
52 
53 
55 :
56  word(std::move(str)),
57  type_(option::LITERAL)
58 {}
59 
60 
61 inline Foam::keyType::keyType(const string& str)
62 :
63  word(str, false), // No stripping
64  type_(option::REGEX)
65 {}
66 
67 
68 inline Foam::keyType::keyType(string&& str)
69 :
70  word(std::move(str), false), // No stripping
71  type_(option::REGEX)
72 {}
73 
74 
75 inline Foam::keyType::keyType(const std::string& str, option opt)
76 :
77  word(str, false), // No stripping
78  type_(option(opt & 0x0F))
79 {}
80 
81 
82 inline Foam::keyType::keyType(std::string&& str, option opt)
83 :
84  word(std::move(str), false), // No stripping
85  type_(option(opt & 0x0F))
86 {}
87 
88 
89 inline Foam::keyType::keyType(const char* str, option opt)
90 :
91  word(str, false), // No stripping
92  type_(option(opt & 0x0F))
93 {}
94 
95 
96 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
97 
98 inline bool Foam::keyType::isLiteral() const noexcept
99 {
100  return !(type_ & option::REGEX);
101 }
102 
103 
104 inline bool Foam::keyType::isPattern() const noexcept
105 {
106  return (type_ & option::REGEX);
107 }
108 
109 
110 inline void Foam::keyType::setType(option opt, bool adjust)
111 {
112  opt = option(opt & 0x0F);
113 
114  if (type_ != opt)
115  {
116  // Only strip when debug is active (potentially costly operation)
117  if (adjust && isPattern() && word::debug)
118  {
119  string::stripInvalid<word>(*this);
120  }
121 
122  type_ = opt;
123  }
124 }
125 
126 
127 inline bool Foam::keyType::compile() noexcept
128 {
129  type_ = option::REGEX;
130  return true;
131 }
132 
133 
134 inline void Foam::keyType::uncompile() noexcept
135 {
136  type_ = option::LITERAL;
137 }
138 
139 
140 inline void Foam::keyType::uncompile(bool adjust)
141 {
142  // Only strip when debug is active (potentially costly operation)
143  if (adjust && isPattern() && word::debug)
144  {
145  string::stripInvalid<word>(*this);
146  }
147 
148  type_ = option::LITERAL;
149 }
150 
151 
152 inline void Foam::keyType::clear()
153 {
154  word::clear();
155  type_ = option::LITERAL;
156 }
157 
158 
159 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
160 
161 inline bool Foam::keyType::operator()(const std::string& text) const
162 {
163  return match(text); // Use smart match
164 }
165 
166 
167 inline void Foam::keyType::operator=(const keyType& str)
168 {
169  if (this == &str)
170  {
171  return; // Self-assignment is a no-op
172  }
173 
174  assign(str); // Bypasses char checking
175  type_ = str.type_;
176 }
177 
178 
180 {
181  if (this == &str)
182  {
183  return; // Self-assignment is a no-op
184  }
185 
186  clear();
187  swap(str);
188 }
189 
190 
191 inline void Foam::keyType::operator=(const char* str)
192 {
193  assign(str); // Bypasses char checking
194  type_ = option::LITERAL;
195 }
196 
197 
198 inline void Foam::keyType::operator=(const word& str)
199 {
200  assign(str); // Bypasses char checking
201  type_ = option::LITERAL;
202 }
203 
204 
205 inline void Foam::keyType::operator=(const string& str)
206 {
207  assign(str); // Bypasses char checking
208  type_ = option::REGEX;
209 }
210 
211 
212 // ************************************************************************* //
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::keyType::isPattern
bool isPattern() const noexcept
The keyType is treated as a pattern, not as literal string.
Definition: keyTypeI.H:104
Foam::keyType::keyType
keyType()
Default construct, empty literal.
Definition: keyTypeI.H:40
Foam::keyType::clear
void clear()
Clear string and set as literal.
Definition: keyTypeI.H:152
Foam::keyType::valid
static bool valid(const char c)
Test for valid keyType character?
Definition: keyTypeI.H:31
Foam::keyType::operator()
bool operator()(const std::string &text) const
Perform smart match on text, as per match()
Definition: keyTypeI.H:161
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:68
Foam::keyType::operator=
void operator=(const std::string &)=delete
No assignment where type could be indeterminate.
Foam::keyType::uncompile
void uncompile() noexcept
Mark as literal string.
Definition: keyTypeI.H:134
Foam::word::valid
static bool valid(char c)
Is this character valid for a word?
Definition: wordI.H:59
Foam::word::debug
static int debug
Debugging.
Definition: word.H:77
Foam::keyType::setType
void setType(option opt, bool adjust=false)
Definition: keyTypeI.H:110
Foam::stringOps::match
bool match(const UList< wordRe > &patterns, const std::string &text)
Return true if text matches one of the regular expressions.
Definition: stringOps.H:76
clear
patchWriters clear()
Foam::keyType::compile
bool compile() noexcept
Mark as regular expression.
Definition: keyTypeI.H:127
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::FieldOps::assign
void assign(Field< Tout > &result, const Field< T1 > &a, const UnaryOp &op)
Populate a field as the result of a unary operation on an input.
Definition: FieldOps.C:35
Foam::keyType::isLiteral
bool isLiteral() const noexcept
The keyType is treated as literal, not as pattern.
Definition: keyTypeI.H:98
Foam::keyType::option
option
Enumeration for the data type and search/match modes (bitmask)
Definition: keyType.H:78