dictionary.H 18.3 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2017 OpenFOAM Foundation
6
7
8
9
10
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

11
12
13
14
    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
15
16
17
18
19
20
21

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
22
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
23
24
25
26
27
28

Class
    Foam::dictionary

Description
    A list of keyword definitions, which are a keyword followed by any number
29
    of values (e.g. words and numbers). The keywords can represent patterns
mattijs's avatar
mattijs committed
30
    which are matched using Posix regular expressions. The general order for
31
    searching is as follows:
mattijs's avatar
mattijs committed
32
    - exact match
33
34
    - pattern match (in reverse order)
    - optional recursion into the enclosing (parent) dictionaries
35
36

    The dictionary class is the base class for IOdictionary.
37
38
39
    It also serves as a bootstrap dictionary for the objectRegistry data
    dictionaries since, unlike the IOdictionary class, it does not use an
    objectRegistry itself to work.
40

41
    To add - a merge() member function with a non-const dictionary parameter?
42
    This would avoid unnecessary cloning in the add(entry*, bool) method.
43
44
45
46
47
48
49
50
51
52
53
54

SourceFiles
    dictionary.C
    dictionaryIO.C

\*---------------------------------------------------------------------------*/

#ifndef dictionary_H
#define dictionary_H

#include "entry.H"
#include "IDLList.H"
mattijs's avatar
mattijs committed
55
#include "DLList.H"
56
57
58
59
60
61
62
63
64
65
66
67
#include "fileName.H"
#include "ITstream.H"
#include "HashTable.H"
#include "wordList.H"
#include "className.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{

// Forward declaration of friend functions and operators
68
class regExp;
69
class dictionary;
70
71
class SHA1Digest;

72
73
74
Istream& operator>>(Istream&, dictionary&);
Ostream& operator<<(Ostream&, const dictionary&);

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*---------------------------------------------------------------------------*\
                        Class dictionaryName Declaration
\*---------------------------------------------------------------------------*/

class dictionaryName
{
    // Private data

        fileName name_;


public:

    // Constructors

        //- Construct dictionaryName null
        dictionaryName()
        {}

        //- Construct dictionaryName as copy of the given fileName
        dictionaryName(const fileName& name)
        :
            name_(name)
        {}


    // Member functions

        //- Return the dictionary name
        const fileName& name() const
        {
            return name_;
        }

        //- Return the dictionary name
        fileName& name()
        {
            return name_;
        }
114
115
116
117
118
119

        //- Return the local dictionary name (final part of scoped name)
        const word dictName() const
        {
            const word scopedName = name_.name();

120
            string::size_type i = scopedName.rfind('.');
121
122
123
124
125
126
127
128
129
130

            if (i == scopedName.npos)
            {
                return scopedName;
            }
            else
            {
                return scopedName.substr(i + 1, scopedName.npos);
            }
        }
131
132
};

133
134
135
136
137
138
139

/*---------------------------------------------------------------------------*\
                           Class dictionary Declaration
\*---------------------------------------------------------------------------*/

class dictionary
:
140
    public dictionaryName,
141
142
143
144
    public IDLList<entry>
{
    // Private data

145
146
147
148
        //- If true write optional keywords and values
        //  if not present in dictionary
        static bool writeOptionalEntries;

mattijs's avatar
mattijs committed
149
        //- HashTable of the entries held on the DL-list for quick lookup
150
151
152
153
154
        HashTable<entry*> hashedEntries_;

        //- Parent dictionary
        const dictionary& parent_;

155
        //- Entries of matching patterns
156
        DLList<entry*> patternEntries_;
mattijs's avatar
mattijs committed
157

158
        //- Patterns as precompiled regular expressions
159
        DLList<autoPtr<regExp>> patternRegexps_;
mattijs's avatar
mattijs committed
160

161

mattijs's avatar
mattijs committed
162
163
   // Private Member Functions

164
165
166
167
168
169
170
171
172
        //- Find and return an entry data stream pointer if present
        //  otherwise return nullptr. Allows scoping using '.'
        const entry* lookupScopedSubEntryPtr
        (
            const word&,
            bool recursive,
            bool patternMatch
        ) const;

173
174
        //- Search patterns table for exact match or regular expression match
        bool findInPatterns
mattijs's avatar
mattijs committed
175
        (
176
            const bool patternMatch,
mattijs's avatar
mattijs committed
177
178
            const word& Keyword,
            DLList<entry*>::const_iterator& wcLink,
179
            DLList<autoPtr<regExp>>::const_iterator& reLink
mattijs's avatar
mattijs committed
180
181
        ) const;

182
183
        //- Search patterns table for exact match or regular expression match
        bool findInPatterns
mattijs's avatar
mattijs committed
184
        (
185
            const bool patternMatch,
mattijs's avatar
mattijs committed
186
187
            const word& Keyword,
            DLList<entry*>::iterator& wcLink,
188
            DLList<autoPtr<regExp>>::iterator& reLink
mattijs's avatar
mattijs committed
189
190
        );

191

192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
public:

    //- Declare friendship with the entry class for IO
    friend class entry;


    // Declare name of the class and its debug switch
    ClassName("dictionary");


    //- Null dictionary
    static const dictionary null;


    // Constructors

        //- Construct top-level dictionary null
        dictionary();

211
212
213
214
215
        //- Construct top-level empty dictionary with given name
        dictionary(const fileName& name);

        //- Construct given the entry name, parent dictionary and Istream,
        //  reading entries until lastEntry or EOF
216
217
        dictionary
        (
218
            const fileName& name,
219
220
221
222
            const dictionary& parentDict,
            Istream&
        );

223
224
        //- Construct top-level dictionary from Istream,
        //  reading entries until EOF
225
226
        dictionary(Istream&);

227
228
229
230
        //- Construct top-level dictionary from Istream,
        //  reading entries until EOF, optionally keeping the header
        dictionary(Istream&, const bool keepHeader);

231
        //- Construct as copy given the parent dictionary
232
        dictionary(const dictionary& parentDict, const dictionary&);
233
234

        //- Construct top-level dictionary as copy
235
        dictionary(const dictionary&);
236

237
238
239
240
        //- Construct top-level dictionary as copy from pointer to dictionary.
        //  A null pointer is treated like an empty dictionary.
        dictionary(const dictionary*);

241
        //- Construct by transferring parameter contents given parent dictionary
Mark Olesen's avatar
Mark Olesen committed
242
        dictionary(const dictionary& parentDict, const Xfer<dictionary>&);
243
244

        //- Construct top-level dictionary by transferring parameter contents
Mark Olesen's avatar
Mark Olesen committed
245
        dictionary(const Xfer<dictionary>&);
246

247
        //- Construct and return clone
248
        autoPtr<dictionary> clone() const;
249
250

        //- Construct top-level dictionary on freestore from Istream
251
        static autoPtr<dictionary> New(Istream&);
252
253


254
    //- Destructor
255
    virtual ~dictionary();
256
257
258
259
260
261
262
263
264
265


    // Member functions

        //- Return the parent dictionary
        const dictionary& parent() const
        {
            return parent_;
        }

266
267
268
        //- Return the top of the tree
        const dictionary& topDict() const;

269
270
271
272
273
274
        //- Return line number of first token in dictionary
        label startLineNumber() const;

        //- Return line number of last token in dictionary
        label endLineNumber() const;

Mark Olesen's avatar
Mark Olesen committed
275
        //- Return the SHA1 digest of the dictionary contents
276
        SHA1Digest digest() const;
Mark Olesen's avatar
Mark Olesen committed
277

278
279
280
        //- Return the dictionary as a list of tokens
        tokenList tokens() const;

281
282
283
284

        // Search and lookup

            //- Search dictionary for given keyword
285
            //  If recursive, search parent dictionaries
286
287
288
289
290
291
292
            //  If patternMatch, use regular expressions
            bool found
            (
                const word&,
                bool recursive=false,
                bool patternMatch = true
            ) const;
293
294

            //- Find and return an entry data stream pointer if present
295
            //  otherwise return nullptr.
296
297
            //  If recursive, search parent dictionaries.
            //  If patternMatch, use regular expressions
298
299
            const entry* lookupEntryPtr
            (
mattijs's avatar
mattijs committed
300
301
                const word&,
                bool recursive,
302
                bool patternMatch
303
            ) const;
304

305
            //- Find and return an entry data stream pointer for manipulation
306
            //  if present otherwise return nullptr.
307
308
            //  If recursive, search parent dictionaries.
            //  If patternMatch, use regular expressions.
mattijs's avatar
mattijs committed
309
310
311
312
            entry* lookupEntryPtr
            (
                const word&,
                bool recursive,
313
                bool patternMatch
mattijs's avatar
mattijs committed
314
            );
315

316
            //- Find and return an entry data stream if present otherwise error.
317
318
            //  If recursive, search parent dictionaries.
            //  If patternMatch, use regular expressions.
mattijs's avatar
mattijs committed
319
320
321
322
            const entry& lookupEntry
            (
                const word&,
                bool recursive,
323
                bool patternMatch
mattijs's avatar
mattijs committed
324
            ) const;
325
326

            //- Find and return an entry data stream
327
328
            //  If recursive, search parent dictionaries.
            //  If patternMatch, use regular expressions.
mattijs's avatar
mattijs committed
329
330
331
332
            ITstream& lookup
            (
                const word&,
                bool recursive=false,
333
                bool patternMatch=true
mattijs's avatar
mattijs committed
334
            ) const;
335

336
            //- Find and return a T,
337
338
339
340
341
342
343
344
345
346
347
348
349
            //  if not found throw a fatal error.
            //  If recursive, search parent dictionaries.
            //  If patternMatch, use regular expressions.
            template<class T>
            T lookupType
            (
                const word&,
                bool recursive=false,
                bool patternMatch=true
            ) const;

            //- Find and return a T,
            //  if not found return the given default value.
350
351
            //  If recursive, search parent dictionaries.
            //  If patternMatch, use regular expressions.
352
            template<class T>
353
354
355
356
            T lookupOrDefault
            (
                const word&,
                const T&,
mattijs's avatar
mattijs committed
357
                bool recursive=false,
358
                bool patternMatch=true
359
            ) const;
360

361
362
            //- Find and return a T, if not found return the given
            //  default value, and add to dictionary.
363
364
            //  If recursive, search parent dictionaries.
            //  If patternMatch, use regular expressions.
365
366
367
368
369
            template<class T>
            T lookupOrAddDefault
            (
                const word&,
                const T&,
mattijs's avatar
mattijs committed
370
                bool recursive=false,
371
                bool patternMatch=true
372
373
374
            );

            //- Find an entry if present, and assign to T
375
376
377
            //  Returns true if the entry was found.
            //  If recursive, search parent dictionaries.
            //  If patternMatch, use regular expressions.
378
            template<class T>
379
            bool readIfPresent
380
381
382
            (
                const word&,
                T&,
mattijs's avatar
mattijs committed
383
                bool recursive=false,
384
                bool patternMatch=true
385
386
            ) const;

387
            //- Find and return an entry data stream pointer if present
388
389
            //  otherwise return nullptr. Allows scoping using '.'.
            //  Special handling for ':' at start of keyword and '..'.
390
391
392
393
394
395
396
            const entry* lookupScopedEntryPtr
            (
                const word&,
                bool recursive,
                bool patternMatch
            ) const;

397
398
399
400
            //- Check if entry is a sub-dictionary
            bool isDict(const word&) const;

            //- Find and return a sub-dictionary pointer if present
401
            //  otherwise return nullptr.
402
403
            const dictionary* subDictPtr(const word&) const;

404
405
406
407
            //- Find and return a sub-dictionary pointer if present
            //  otherwise return nullptr.
            dictionary* subDictPtr(const word&);

408
409
410
            //- Find and return a sub-dictionary
            const dictionary& subDict(const word&) const;

411
412
413
            //- Find and return a sub-dictionary for manipulation
            dictionary& subDict(const word&);

414
415
            //- Find and return a sub-dictionary as a copy, or
            //  return an empty dictionary if the sub-dictionary does not exist
andy's avatar
andy committed
416
417
418
419
420
            dictionary subOrEmptyDict
            (
                const word&,
                const bool mustRead = false
            ) const;
421

422
423
424
            //- Return the table of contents
            wordList toc() const;

425
426
427
            //- Return the sorted table of contents
            wordList sortedToc() const;

428
429
430
            //- Return the list of available keys or patterns
            List<keyType> keys(bool patterns=false) const;

431

432
433
        // Editing

434
435
436
437
            //- Substitute the given keyword prepended by '$' with the
            //  corresponding sub-dictionary entries
            bool substituteKeyword(const word& keyword);

438
439
440
441
            //- Substitute the given scoped keyword prepended by '$' with the
            //  corresponding sub-dictionary entries
            bool substituteScopedKeyword(const word& keyword);

442
            //- Add a new entry
Mark Olesen's avatar
Mark Olesen committed
443
444
445
            //  With the merge option, dictionaries are interwoven and
            //  primitive entries are overwritten
            bool add(entry*, bool mergeEntry=false);
446
447

            //- Add an entry
Mark Olesen's avatar
Mark Olesen committed
448
449
450
            //  With the merge option, dictionaries are interwoven and
            //  primitive entries are overwritten
            void add(const entry&, bool mergeEntry=false);
451
452

            //- Add a word entry
Mark Olesen's avatar
Mark Olesen committed
453
            //  optionally overwrite an existing entry
mattijs's avatar
mattijs committed
454
            void add(const keyType&, const word&, bool overwrite=false);
455
456

            //- Add a string entry
Mark Olesen's avatar
Mark Olesen committed
457
            //  optionally overwrite an existing entry
mattijs's avatar
mattijs committed
458
            void add(const keyType&, const string&, bool overwrite=false);
459
460

            //- Add a label entry
Mark Olesen's avatar
Mark Olesen committed
461
            //  optionally overwrite an existing entry
mattijs's avatar
mattijs committed
462
            void add(const keyType&, const label, bool overwrite=false);
463
464

            //- Add a scalar entry
Mark Olesen's avatar
Mark Olesen committed
465
            //  optionally overwrite an existing entry
466
            void add(const keyType&, const scalar, bool overwrite=false);
467

Mark Olesen's avatar
Mark Olesen committed
468
469
470
471
            //- Add a dictionary entry
            //  optionally merge with an existing sub-dictionary
            void add
            (
472
                const keyType&,
Mark Olesen's avatar
Mark Olesen committed
473
474
475
                const dictionary&,
                bool mergeEntry=false
            );
476
477

            //- Add a T entry
Mark Olesen's avatar
Mark Olesen committed
478
            //  optionally overwrite an existing entry
479
            template<class T>
480
            void add(const keyType&, const T&, bool overwrite=false);
481

Mark Olesen's avatar
Mark Olesen committed
482
483
484
485
486
487
488
            //- Assign a new entry, overwrite any existing entry
            void set(entry*);

            //- Assign a new entry, overwrite any existing entry
            void set(const entry&);

            //- Assign a dictionary entry, overwrite any existing entry
489
            void set(const keyType&, const dictionary&);
Mark Olesen's avatar
Mark Olesen committed
490
491
492

            //- Assign a T entry, overwrite any existing entry
            template<class T>
493
            void set(const keyType&, const T&);
494
495

            //- Remove an entry specified by keyword
496
            bool remove(const word&);
497
498
499
500
501

            //- Change the keyword for an entry,
            //  optionally forcing overwrite of an existing entry
            bool changeKeyword
            (
mattijs's avatar
mattijs committed
502
503
                const keyType& oldKeyword,
                const keyType& newKeyword,
504
                bool forceOverwrite=false
505
506
507
508
509
510
511
512
513
            );

            //- Merge entries from the given dictionary.
            //  Also merge sub-dictionaries as required.
            bool merge(const dictionary&);

            //- Clear the dictionary
            void clear();

Mark Olesen's avatar
Mark Olesen committed
514
            //- Transfer the contents of the argument and annul the argument.
515
516
            void transfer(dictionary&);

Mark Olesen's avatar
Mark Olesen committed
517
518
            //- Transfer contents to the Xfer container
            Xfer<dictionary> xfer();
519

520

521
522
523
524
525
        // Read

            //- Read dictionary from Istream
            bool read(Istream&);

526
527
528
            //- Read dictionary from Istream, optionally keeping the header
            bool read(Istream&, const bool keepHeader);

529

530
        // Write
531

532
533
            //- Write dictionary, normally with sub-dictionary formatting
            void write(Ostream&, const bool subDict=true) const;
534
535
536
537
538
539
540
541
542
543


    // Member Operators

        //- Find and return entry
        ITstream& operator[](const word&) const;

        void operator=(const dictionary&);

        //- Include entries from the given dictionary.
544
        //  Warn, but do not overwrite existing entries.
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
        void operator+=(const dictionary&);

        //- Conditionally include entries from the given dictionary.
        //  Do not overwrite existing entries.
        void operator|=(const dictionary&);

        //- Unconditionally include entries from the given dictionary.
        //  Overwrite existing entries.
        void operator<<=(const dictionary&);


    // IOstream operators

        //- Read dictionary from Istream
        friend Istream& operator>>(Istream&, dictionary&);

        //- Write dictionary to Ostream
        friend Ostream& operator<<(Ostream&, const dictionary&);
};


// Global Operators

568
569
//- Combine dictionaries.
//  Starting from the entries in dict1 and then including those from dict2.
Mark Olesen's avatar
Mark Olesen committed
570
//  Warn, but do not overwrite the entries from dict1.
571
dictionary operator+(const dictionary& dict1, const dictionary& dict2);
572

573
574
//- Combine dictionaries.
//  Starting from the entries in dict1 and then including those from dict2.
Mark Olesen's avatar
Mark Olesen committed
575
//  Do not overwrite the entries from dict1.
576
577
578
579
580
581
582
583
584
585
dictionary operator|(const dictionary& dict1, const dictionary& dict2);


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#ifdef NoRepository
586
    #include "dictionaryTemplates.C"
587
588
589
590
591
592
593
#endif

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#endif

// ************************************************************************* //