word.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-2022 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::word
29
30Description
31 A class for handling words, derived from Foam::string.
32
33 A word is a string of characters without whitespace, quotes, slashes,
34 semicolons or brace brackets. Words are delimited by whitespace.
35
36SourceFiles
37 wordI.H
38 word.C
39 wordIO.C
40
41\*---------------------------------------------------------------------------*/
42
43#ifndef Foam_word_H
44#define Foam_word_H
45
46#include "string.H"
47
48// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49
50namespace Foam
51{
52
53// Forward Declarations
54class word;
55Istream& operator>>(Istream& is, word& val);
56Ostream& operator<<(Ostream& os, const word& val);
57
58//- Hashing for word
59template<> struct Hash<word> : string::hasher {};
60
61
62/*---------------------------------------------------------------------------*\
63 Class word Declaration
64\*---------------------------------------------------------------------------*/
66class word
67:
68 public string
69{
70public:
71
72 // Static Data Members
73
74 //- The typeName
75 static const char* const typeName;
76
77 //- Debugging
78 static int debug;
79
80 //- An empty word
81 static const word null;
82
83
84 // Constructors
85
86 //- Default construct
87 word() = default;
88
89 //- Copy construct
90 word(const word&) = default;
91
92 //- Move construct
93 word(word&& w) = default;
94
95 //- Copy construct from Foam::string
96 inline word(const string& s, bool doStrip=true);
97
98 //- Move construct from Foam::string
99 inline word(string&& s, bool doStrip=true);
100
101 //- Copy construct from std::string
102 inline word(const std::string& s, bool doStrip=true);
103
104 //- Move construct from std::string
105 inline word(std::string&& s, bool doStrip=true);
106
107 //- Copy from character array
108 inline word(const char* s, bool doStrip=true);
109
110 //- Copy from buffer for a maximum number of characters
111 inline word(const char* s, size_type len, bool doStrip);
112
113 //- Construct from Istream
114 explicit word(Istream& is);
115
116
117 // Member Functions
118
119 //- Use a printf-style formatter for a primitive.
120 // The representation is not checked for valid characters -
121 // it is assumed that the caller knows what they are doing
122 template<class PrimitiveType>
123 inline static word printf
124 (
125 const char* fmt,
126 const PrimitiveType& val
127 );
128
129 //- Use a printf-style formatter for a primitive.
130 // The representation is not checked for valid characters -
131 // it is assumed that the caller knows what they are doing
132 template<class PrimitiveType>
133 inline static word printf
134 (
135 const std::string& fmt,
136 const PrimitiveType& val
137 );
138
139 //- Is this character valid for a word?
140 inline static bool valid(char c);
141
142 //- Construct validated word (no invalid characters).
143 // Optionally prefix any leading digit with '_' to have words
144 // that work nicely as dictionary keywords.
145 static word validate(const std::string& s, const bool prefix=false);
146
147 //- Construct validated word (no invalid characters) from a sequence
148 //- of characters in the range [first,last),
149 // Optionally prefix any leading digit with '_'.
150 static word validate
151 (
152 const char* first,
153 const char* last,
154 const bool prefix=false
155 );
156
157 //- Strip invalid characters from this word
158 // Trips an abort on invalid characters for debug 2 or greater
159 inline void stripInvalid();
160
161
162 // File-like Functions
163
164 //- Return word without extension (part before last .)
165 word lessExt() const;
166
167 //- Return file name extension (part after last .)
168 word ext() const;
169
170 //- Append a '.' and the ending, and return the object.
171 // The '.' and ending will not be added when the ending is empty,
172 // or when the file name is empty or ended with a '/'.
173 word& ext(const word& ending);
174
175 //- Various checks for extensions
176 using string::hasExt;
177
178 //- Remove extension, returning true if string changed.
179 using string::removeExt;
180
181
182 // Member Operators
183
184 // Assignment
185
186 //- Copy assignment, no character validation required.
187 // Self-assignment is a no-op
188 inline word& operator=(const word& s);
189
190 //- Move assignment, no character validation required
191 // Self-assignment is a no-op
192 inline word& operator=(word&& s);
193
194 //- Copy assignment from Foam::string, stripping invalid characters
195 inline word& operator=(const string& s);
196
197 //- Move assignment from Foam::string, stripping invalid characters
198 inline word& operator=(string&& s);
199
200 //- Copy assignment from std::string, stripping invalid characters
201 inline word& operator=(const std::string& s);
202
203 //- Move assignment from std::string, stripping invalid characters
204 inline word& operator=(std::string&& s);
205
206 //- Copy, stripping invalid characters
207 inline word& operator=(const char* s);
208};
209
210
211// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
212
213// IOstream Operators
214
215//- Read operator
216Istream& operator>>(Istream& is, word& val);
217
218//- Write operator
219Ostream& operator<<(Ostream& os, const word& val);
220
221
222// * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
223
224//- Join words as camelCase, capitalizing the first letter of b.
225// No effect if either argument is empty.
226word operator&(const word& a, const word& b);
227
228
229// * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
230
231//- A word representation of a memory address as hexadecimal.
232// No special handling of nullptr (renders as 0x0)
233word name(const void* ptr);
234
235
236//- Extract name (as a word) from an object, typically using its name() method.
237template<class T>
238struct nameOp
240 word operator()(const T& obj) const
241 {
242 return obj.name();
243 }
244
245 //- Less-compare two objects by their name() method - for sorting
246 bool operator()(const T& a, const T& b) const
247 {
248 return (a.name() < b.name());
249 }
250};
251
252
253//- Extract type (as a word) from an object, typically using its type() method.
254template<class T>
255struct typeOp
257 word operator()(const T& obj) const
258 {
259 return obj.type();
260 }
261
262 //- Less-compare two objects by their type() method - for sorting
263 bool operator()(const T& a, const T& b) const
264 {
265 return (a.type() < b.type());
266 }
267};
268
269
270// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271
272} // End namespace Foam
273
274// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
275
276#include "wordI.H"
277
278// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
279
280#endif
281
282// ************************************************************************* //
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:75
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:65
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A class for handling character strings derived from std::string.
Definition: string.H:79
bool removeExt()
Remove extension, returning true if string changed.
Definition: stringI.H:96
bool hasExt() const
Return true if it has an extension or simply ends with a '.'.
Definition: stringI.H:56
A class for handling words, derived from Foam::string.
Definition: word.H:68
static word validate(const std::string &s, const bool prefix=false)
Construct validated word (no invalid characters).
Definition: word.C:45
static word printf(const char *fmt, const PrimitiveType &val)
Use a printf-style formatter for a primitive.
static const word null
An empty word.
Definition: word.H:80
word(const word &)=default
Copy construct.
word(word &&w)=default
Move construct.
word()=default
Default construct.
static bool valid(char c)
Is this character valid for a word?
Definition: wordI.H:59
word ext() const
Return file name extension (part after last .)
Definition: word.C:126
word & operator=(const word &s)
Copy assignment, no character validation required.
Definition: wordI.H:166
static int debug
Debugging.
Definition: word.H:77
void stripInvalid()
Strip invalid characters from this word.
Definition: wordI.H:144
word lessExt() const
Return word without extension (part before last .)
Definition: word.C:113
static word printf(const std::string &fmt, const PrimitiveType &val)
Use a printf-style formatter for a primitive.
static const char *const typeName
The typeName.
Definition: word.H:74
const volScalarField & T
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.
tmp< GeometricField< Type, fvPatchField, volMesh > > operator&(const fvMatrix< Type > &, const DimensionedField< Type, volMesh > &)
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Istream & operator>>(Istream &, directionInfo &)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
volScalarField & b
Definition: createFields.H:27
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:54
Extract name (as a word) from an object, typically using its name() method.
Definition: word.H:238
word operator()(const T &obj) const
Definition: word.H:239
bool operator()(const T &a, const T &b) const
Less-compare two objects by their name() method - for sorting.
Definition: word.H:245
Hashing functor for string and derived string classes.
Definition: string.H:148
Extract type (as a word) from an object, typically using its type() method.
Definition: word.H:255
word operator()(const T &obj) const
Definition: word.H:256
bool operator()(const T &a, const T &b) const
Less-compare two objects by their type() method - for sorting.
Definition: word.H:262