regIOobject.C 9.31 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2016 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

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

#include "regIOobject.H"
#include "Time.H"
#include "polyMesh.H"
29
#include "registerSwitch.H"
30
31
32

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

33
34
namespace Foam
{
35
36
    defineTypeNameAndDebug(regIOobject, 0);

37
    template<>
38
    const char* NamedEnum
39
    <
40
        regIOobject::fileCheckTypes,
41
42
        4
    >::names[] =
43
44
45
46
47
48
49
50
    {
        "timeStamp",
        "timeStampMaster",
        "inotify",
        "inotifyMaster"
    };
}

51
float Foam::regIOobject::fileModificationSkew
52
(
53
    Foam::debug::floatOptimisationSwitch("fileModificationSkew", 30)
54
55
56
57
);
registerOptSwitch
(
    "fileModificationSkew",
58
    float,
59
60
61
    Foam::regIOobject::fileModificationSkew
);

62
63
64
65
66
67
68
69
70
71
72
73
74
75

const Foam::NamedEnum<Foam::regIOobject::fileCheckTypes, 4>
    Foam::regIOobject::fileCheckTypesNames;

Foam::regIOobject::fileCheckTypes Foam::regIOobject::fileModificationChecking
(
    fileCheckTypesNames.read
    (
        debug::optimisationSwitches().lookup
        (
            "fileModificationChecking"
        )
    )
);
76
77

namespace Foam
mattijs's avatar
mattijs committed
78
{
79
80
    // Register re-reader
    class addfileModificationCheckingToOpt
mattijs's avatar
mattijs committed
81
    :
82
        public ::Foam::simpleRegIOobject
mattijs's avatar
mattijs committed
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
    public:

        addfileModificationCheckingToOpt(const char* name)
        :
            ::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject, name)
        {}

        virtual ~addfileModificationCheckingToOpt()
        {}

        virtual void readData(Foam::Istream& is)
        {
            regIOobject::fileModificationChecking =
                regIOobject::fileCheckTypesNames.read(is);
        }

        virtual void writeData(Foam::Ostream& os) const
        {
            os <<  regIOobject::fileCheckTypesNames
                [regIOobject::fileModificationChecking];
        }
    };

    addfileModificationCheckingToOpt addfileModificationCheckingToOpt_
    (
        "fileModificationChecking"
    );
}
112

113

114
115
116
bool Foam::regIOobject::masterOnlyReading = false;


117
118
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

mattijs's avatar
mattijs committed
119
Foam::regIOobject::regIOobject(const IOobject& io, const bool isTime)
120
121
122
123
:
    IOobject(io),
    registered_(false),
    ownedByRegistry_(false),
124
    watchIndex_(-1),
mattijs's avatar
mattijs committed
125
126
127
128
129
130
    eventNo_                // Do not get event for top level Time database
    (
        isTime
      ? 0
      : db().getEvent()
    ),
131
    isPtr_(nullptr)
132
133
134
135
136
137
138
139
140
{
    // Register with objectRegistry if requested
    if (registerObject())
    {
        checkIn();
    }
}


141
Foam::regIOobject::regIOobject(const regIOobject& rio)
142
143
144
145
:
    IOobject(rio),
    registered_(false),
    ownedByRegistry_(false),
146
    watchIndex_(rio.watchIndex_),
mattijs's avatar
mattijs committed
147
    eventNo_(db().getEvent()),
148
    isPtr_(nullptr)
149
150
151
152
153
{
    // Do not register copy with objectRegistry
}


154
Foam::regIOobject::regIOobject(const regIOobject& rio, bool registerCopy)
155
156
157
158
:
    IOobject(rio),
    registered_(false),
    ownedByRegistry_(false),
159
    watchIndex_(-1),
mattijs's avatar
mattijs committed
160
    eventNo_(db().getEvent()),
161
    isPtr_(nullptr)
162
163
164
165
166
167
168
169
170
{
    if (registerCopy && rio.registered_)
    {
        const_cast<regIOobject&>(rio).checkOut();
        checkIn();
    }
}


171
172
173
174
175
176
177
178
179
180
181
182
Foam::regIOobject::regIOobject
(
    const word& newName,
    const regIOobject& rio,
    bool registerCopy
)
:
    IOobject(newName, rio.instance(), rio.local(), rio.db()),
    registered_(false),
    ownedByRegistry_(false),
    watchIndex_(-1),
    eventNo_(db().getEvent()),
183
    isPtr_(nullptr)
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
{
    if (registerCopy)
    {
        checkIn();
    }
}


Foam::regIOobject::regIOobject
(
    const IOobject& io,
    const regIOobject& rio
)
:
    IOobject(io),
    registered_(false),
    ownedByRegistry_(false),
    watchIndex_(-1),
    eventNo_(db().getEvent()),
203
    isPtr_(nullptr)
204
205
206
207
208
209
210
211
{
    if (registerObject())
    {
        checkIn();
    }
}


212
213
// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

214
Foam::regIOobject::~regIOobject()
215
216
217
218
219
220
221
222
223
224
225
226
{
    if (objectRegistry::debug)
    {
        Info<< "Destroying regIOobject called " << name()
            << " of type " << type()
            << " in directory " << path()
            << endl;
    }

    if (isPtr_)
    {
        delete isPtr_;
227
        isPtr_ = nullptr;
228
229
230
231
232
233
234
235
236
237
238
239
240
    }

    // Check out of objectRegistry if not owned by the registry

    if (!ownedByRegistry_)
    {
        checkOut();
    }
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

241
bool Foam::regIOobject::checkIn()
242
243
244
{
    if (!registered_)
    {
245
246
247
248
        // multiple checkin of same object is disallowed - this would mess up
        // any mapping
        registered_ = db().checkIn(*this);

249
250
251
252
253
254
255
256
257
        if
        (
            registered_
         && readOpt() == MUST_READ_IF_MODIFIED
         && time().runTimeModifiable()
        )
        {
            if (watchIndex_ != -1)
            {
258
                FatalErrorInFunction
259
260
261
262
263
264
                    << "Object " << objectPath()
                    << " already watched with index " << watchIndex_
                    << abort(FatalError);
            }

            fileName f = filePath();
265
            if (!f.size())
266
            {
267
268
269
                // We don't have this file but would like to re-read it.
                // Possibly if master-only reading mode.
                f = objectPath();
270
            }
271
            watchIndex_ = time().addWatch(f);
272
273
        }

274
        // check-in on defaultRegion is allowed to fail, since subsetted meshes
275
276
        // are created with the same name as their originating mesh
        if (!registered_ && debug && name() != polyMesh::defaultRegion)
277
        {
278
279
            if (debug == 2)
            {
280
281
                // for ease of finding where attempted duplicate check-in
                // originated
282
                FatalErrorInFunction
283
                    << "failed to register object " << objectPath()
284
285
                    << " the name already exists in the objectRegistry" << endl
                    << "Contents:" << db().sortedToc()
286
287
288
289
                    << abort(FatalError);
            }
            else
            {
290
                WarningInFunction
291
292
293
294
                    << "failed to register object " << objectPath()
                    << " the name already exists in the objectRegistry"
                    << endl;
            }
295
296
        }
    }
297
298

    return registered_;
299
300
301
}


302
bool Foam::regIOobject::checkOut()
303
304
305
306
{
    if (registered_)
    {
        registered_ = false;
307
308
309
310
311
312

        if (watchIndex_ != -1)
        {
            time().removeWatch(watchIndex_);
            watchIndex_ = -1;
        }
313
        return db().checkOut(*this);
314
    }
315
316

    return false;
317
318
319
}


320
bool Foam::regIOobject::upToDate(const regIOobject& a) const
mattijs's avatar
mattijs committed
321
{
322
    if (a.eventNo() >= eventNo_)
mattijs's avatar
mattijs committed
323
324
325
326
327
328
329
330
331
332
    {
        return false;
    }
    else
    {
        return true;
    }
}


333
334
335
336
337
bool Foam::regIOobject::upToDate
(
    const regIOobject& a,
    const regIOobject& b
) const
mattijs's avatar
mattijs committed
338
339
340
{
    if
    (
341
342
        a.eventNo() >= eventNo_
     || b.eventNo() >= eventNo_
mattijs's avatar
mattijs committed
343
344
345
346
347
348
349
350
351
352
353
    )
    {
        return false;
    }
    else
    {
        return true;
    }
}


354
bool Foam::regIOobject::upToDate
mattijs's avatar
mattijs committed
355
(
356
357
358
    const regIOobject& a,
    const regIOobject& b,
    const regIOobject& c
mattijs's avatar
mattijs committed
359
360
361
362
) const
{
    if
    (
363
364
365
        a.eventNo() >= eventNo_
     || b.eventNo() >= eventNo_
     || c.eventNo() >= eventNo_
mattijs's avatar
mattijs committed
366
367
368
369
370
371
372
373
374
375
376
    )
    {
        return false;
    }
    else
    {
        return true;
    }
}


377
bool Foam::regIOobject::upToDate
mattijs's avatar
mattijs committed
378
(
379
380
381
382
    const regIOobject& a,
    const regIOobject& b,
    const regIOobject& c,
    const regIOobject& d
mattijs's avatar
mattijs committed
383
384
385
386
) const
{
    if
    (
387
388
389
390
        a.eventNo() >= eventNo_
     || b.eventNo() >= eventNo_
     || c.eventNo() >= eventNo_
     || d.eventNo() >= eventNo_
mattijs's avatar
mattijs committed
391
392
393
394
395
396
397
398
399
400
401
    )
    {
        return false;
    }
    else
    {
        return true;
    }
}


402
void Foam::regIOobject::setUpToDate()
mattijs's avatar
mattijs committed
403
404
405
406
407
{
    eventNo_ = db().getEvent();
}


408
void Foam::regIOobject::rename(const word& newName)
409
410
411
412
413
414
{
    // Check out of objectRegistry
    checkOut();

    IOobject::rename(newName);

415
416
417
418
419
    if (registerObject())
    {
        // Re-register object with objectRegistry
        checkIn();
    }
420
421
422
}


423
void Foam::regIOobject::operator=(const IOobject& io)
424
425
426
427
{
    if (isPtr_)
    {
        delete isPtr_;
428
        isPtr_ = nullptr;
429
430
431
432
433
434
435
    }

    // Check out of objectRegistry
    checkOut();

    IOobject::operator=(io);

436
437
438
439
440
    if (registerObject())
    {
        // Re-register object with objectRegistry
        checkIn();
    }
441
442
443
444
}


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