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

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

#include "regIOobject.H"
#include "Time.H"
#include "polyMesh.H"

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

32
defineTypeNameAndDebug(Foam::regIOobject, 0);
33

34
int Foam::regIOobject::fileModificationSkew
35
(
36
    Foam::debug::optimisationSwitch("fileModificationSkew", 30)
37
38
);

39
40
41
namespace Foam
{
    template<>
42
43
44
45
46
    const char* Foam::NamedEnum
    <
        Foam::regIOobject::fileCheckTypes,
        4
    >::names[] =
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    {
        "timeStamp",
        "timeStampMaster",
        "inotify",
        "inotifyMaster"
    };
}


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

// Default fileCheck type
Foam::regIOobject::fileCheckTypes Foam::regIOobject::fileModificationChecking
(
    fileCheckTypesNames.read
    (
        debug::optimisationSwitches().lookup
        (
            "fileModificationChecking"
        )
    )
);

71

72
73
74
bool Foam::regIOobject::masterOnlyReading = false;


75
76
77
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

// Construct from IOobject
mattijs's avatar
mattijs committed
78
Foam::regIOobject::regIOobject(const IOobject& io, const bool isTime)
79
80
81
82
:
    IOobject(io),
    registered_(false),
    ownedByRegistry_(false),
83
    watchIndex_(-1),
mattijs's avatar
mattijs committed
84
85
86
87
88
89
    eventNo_                // Do not get event for top level Time database
    (
        isTime
      ? 0
      : db().getEvent()
    ),
90
91
92
93
94
95
96
97
98
99
100
    isPtr_(NULL)
{
    // Register with objectRegistry if requested
    if (registerObject())
    {
        checkIn();
    }
}


// Construct as copy
101
Foam::regIOobject::regIOobject(const regIOobject& rio)
102
103
104
105
:
    IOobject(rio),
    registered_(false),
    ownedByRegistry_(false),
106
    watchIndex_(rio.watchIndex_),
mattijs's avatar
mattijs committed
107
    eventNo_(db().getEvent()),
108
109
110
111
112
113
114
115
    isPtr_(NULL)
{
    // Do not register copy with objectRegistry
}


// Construct as copy, and transfering objectRegistry registration to copy
// if registerCopy is true
116
Foam::regIOobject::regIOobject(const regIOobject& rio, bool registerCopy)
117
118
119
120
:
    IOobject(rio),
    registered_(false),
    ownedByRegistry_(false),
121
    watchIndex_(-1),
mattijs's avatar
mattijs committed
122
    eventNo_(db().getEvent()),
123
124
125
126
127
128
129
130
131
132
133
134
135
    isPtr_(NULL)
{
    if (registerCopy && rio.registered_)
    {
        const_cast<regIOobject&>(rio).checkOut();
        checkIn();
    }
}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

// Delete read stream, checkout from objectRegistry and destroy
136
Foam::regIOobject::~regIOobject()
137
138
139
140
141
142
143
144
145
146
147
148
{
    if (objectRegistry::debug)
    {
        Info<< "Destroying regIOobject called " << name()
            << " of type " << type()
            << " in directory " << path()
            << endl;
    }

    if (isPtr_)
    {
        delete isPtr_;
149
        isPtr_ = NULL;
150
151
152
153
154
155
156
157
158
159
160
161
162
    }

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

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


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

163
bool Foam::regIOobject::checkIn()
164
165
166
{
    if (!registered_)
    {
167
168
169
170
        // multiple checkin of same object is disallowed - this would mess up
        // any mapping
        registered_ = db().checkIn(*this);

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
        if
        (
            registered_
         && readOpt() == MUST_READ_IF_MODIFIED
         && time().runTimeModifiable()
        )
        {
            if (watchIndex_ != -1)
            {
                FatalErrorIn("regIOobject::checkIn()")
                    << "Object " << objectPath()
                    << " already watched with index " << watchIndex_
                    << abort(FatalError);
            }

            fileName f = filePath();
187
            if (!f.size())
188
            {
189
190
191
                // We don't have this file but would like to re-read it.
                // Possibly if master-only reading mode.
                f = objectPath();
192
            }
193
            watchIndex_ = time().addWatch(f);
194
195
        }

196
        // check-in on defaultRegion is allowed to fail, since subsetted meshes
197
198
        // are created with the same name as their originating mesh
        if (!registered_ && debug && name() != polyMesh::defaultRegion)
199
        {
200
201
            if (debug == 2)
            {
202
203
                // for ease of finding where attempted duplicate check-in
                // originated
204
205
                FatalErrorIn("regIOobject::checkIn()")
                    << "failed to register object " << objectPath()
206
                    << " the name already exists in the objectRegistry"
207
208
209
210
211
212
213
214
215
                    << abort(FatalError);
            }
            else
            {
                WarningIn("regIOobject::checkIn()")
                    << "failed to register object " << objectPath()
                    << " the name already exists in the objectRegistry"
                    << endl;
            }
216
217
        }
    }
218
219

    return registered_;
220
221
222
}


223
bool Foam::regIOobject::checkOut()
224
225
226
227
{
    if (registered_)
    {
        registered_ = false;
228
229
230
231
232
233

        if (watchIndex_ != -1)
        {
            time().removeWatch(watchIndex_);
            watchIndex_ = -1;
        }
234
        return db().checkOut(*this);
235
    }
236
237

    return false;
238
239
240
}


241
bool Foam::regIOobject::upToDate(const regIOobject& a) const
mattijs's avatar
mattijs committed
242
{
243
    if (a.eventNo() >= eventNo_)
mattijs's avatar
mattijs committed
244
245
246
247
248
249
250
251
252
253
    {
        return false;
    }
    else
    {
        return true;
    }
}


254
255
256
257
258
bool Foam::regIOobject::upToDate
(
    const regIOobject& a,
    const regIOobject& b
) const
mattijs's avatar
mattijs committed
259
260
261
{
    if
    (
262
263
        a.eventNo() >= eventNo_
     || b.eventNo() >= eventNo_
mattijs's avatar
mattijs committed
264
265
266
267
268
269
270
271
272
273
274
    )
    {
        return false;
    }
    else
    {
        return true;
    }
}


275
bool Foam::regIOobject::upToDate
mattijs's avatar
mattijs committed
276
(
277
278
279
    const regIOobject& a,
    const regIOobject& b,
    const regIOobject& c
mattijs's avatar
mattijs committed
280
281
282
283
) const
{
    if
    (
284
285
286
        a.eventNo() >= eventNo_
     || b.eventNo() >= eventNo_
     || c.eventNo() >= eventNo_
mattijs's avatar
mattijs committed
287
288
289
290
291
292
293
294
295
296
297
    )
    {
        return false;
    }
    else
    {
        return true;
    }
}


298
bool Foam::regIOobject::upToDate
mattijs's avatar
mattijs committed
299
(
300
301
302
303
    const regIOobject& a,
    const regIOobject& b,
    const regIOobject& c,
    const regIOobject& d
mattijs's avatar
mattijs committed
304
305
306
307
) const
{
    if
    (
308
309
310
311
        a.eventNo() >= eventNo_
     || b.eventNo() >= eventNo_
     || c.eventNo() >= eventNo_
     || d.eventNo() >= eventNo_
mattijs's avatar
mattijs committed
312
313
314
315
316
317
318
319
320
321
322
    )
    {
        return false;
    }
    else
    {
        return true;
    }
}


323
324
//- Flag me as up to date
void Foam::regIOobject::setUpToDate()
mattijs's avatar
mattijs committed
325
326
327
328
329
{
    eventNo_ = db().getEvent();
}


330
// Rename object and re-register with objectRegistry under new name
331
void Foam::regIOobject::rename(const word& newName)
332
333
334
335
336
337
{
    // Check out of objectRegistry
    checkOut();

    IOobject::rename(newName);

338
339
340
341
342
    if (registerObject())
    {
        // Re-register object with objectRegistry
        checkIn();
    }
343
344
345
346
}


// Assign to IOobject
347
void Foam::regIOobject::operator=(const IOobject& io)
348
349
350
351
352
353
354
355
356
357
358
359
{
    if (isPtr_)
    {
        delete isPtr_;
        isPtr_ = NULL;
    }

    // Check out of objectRegistry
    checkOut();

    IOobject::operator=(io);

360
361
362
363
364
    if (registerObject())
    {
        // Re-register object with objectRegistry
        checkIn();
    }
365
366
367
368
}


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