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-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 Class
28  Foam::word
29 
30 Description
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 
36 SourceFiles
37  wordI.H
38  word.C
39  wordIO.C
40 
41 \*---------------------------------------------------------------------------*/
42 
43 #ifndef word_H
44 #define word_H
45 
46 #include "string.H"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 // Forward Declarations
54 class word;
55 Istream& operator>>(Istream& is, word& val);
56 Ostream& operator<<(Ostream& os, const word& val);
57 
58 //- Hashing for word
59 template<> struct Hash<word> : string::hasher {};
60 
61 
62 /*---------------------------------------------------------------------------*\
63  Class word Declaration
64 \*---------------------------------------------------------------------------*/
65 
66 class word
67 :
68  public string
69 {
70 public:
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
216 Istream& operator>>(Istream& is, word& val);
217 
218 //- Write operator
219 Ostream& 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.
226 word 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)
233 word name(const void* ptr);
234 
235 
236 //- Extract name (as a word) from an object, typically using its name() method.
237 template<class T>
238 struct nameOp
239 {
240  word operator()(const T& obj) const
241  {
242  return obj.name();
243  }
244 };
245 
246 
247 //- Extract type (as a word) from an object, typically using its type() method.
248 template<class T>
249 struct typeOp
250 {
251  word operator()(const T& obj) const
252  {
253  return obj.type();
254  }
255 };
256 
257 
258 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
259 
260 } // End namespace Foam
261 
262 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
263 
264 #include "wordI.H"
265 
266 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
267 
268 #endif
269 
270 // ************************************************************************* //
Foam::word::lessExt
word lessExt() const
Return word without extension (part before last .)
Definition: word.C:113
Foam::word::operator=
word & operator=(const word &s)
Copy assignment, no character validation required.
Definition: wordI.H:166
Foam::string::hasExt
bool hasExt() const
Return true if it has an extension or simply ends with a '.'.
Definition: stringI.H:56
Foam::typeOp::operator()
word operator()(const T &obj) const
Definition: word.H:250
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
s
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))
Definition: gmvOutputSpray.H:25
Foam::string::removeExt
bool removeExt()
Remove extension, returning true if string changed.
Definition: stringI.H:96
Foam::operator&
tmp< GeometricField< Type, fvPatchField, volMesh > > operator&(const fvMatrix< Type > &, const DimensionedField< Type, volMesh > &)
Foam::word::validate
static word validate(const std::string &s, const bool prefix=false)
Construct validated word (no invalid characters).
Definition: word.C:45
string.H
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
Foam::word::ext
word ext() const
Return file name extension (part after last .)
Definition: word.C:126
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::Hash
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:53
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::nameOp
Extract name (as a word) from an object, typically using its name() method.
Definition: word.H:237
Foam::word::stripInvalid
void stripInvalid()
Strip invalid characters from this word.
Definition: wordI.H:144
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:76
Foam::nameOp::operator()
word operator()(const T &obj) const
Definition: word.H:239
Foam::word::valid
static bool valid(char c)
Is this character valid for a word?
Definition: wordI.H:59
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::word::typeName
static const char *const typeName
The typeName.
Definition: word.H:74
wordI.H
Foam::word::debug
static int debug
Debugging.
Definition: word.H:77
Foam::word::word
word()=default
Default construct.
Foam::string::hasher
Hashing functor for string and derived string classes.
Definition: string.H:147
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::word::printf
static word printf(const char *fmt, const PrimitiveType &val)
Use a printf-style formatter for a primitive.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::typeOp
Extract type (as a word) from an object, typically using its type() method.
Definition: word.H:248