regIOobject.C 6.61 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
Mark Olesen's avatar
Mark Olesen committed
5
    \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    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 2 of the License, or (at your
    option) any later version.

    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
    along with OpenFOAM; if not, write to the Free Software Foundation,
    Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

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

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

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

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

35
int Foam::regIOobject::fileModificationSkew
36
(
37
    Foam::debug::optimisationSwitch("fileModificationSkew", 30)
38
39
40
41
42
43
);


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

// Construct from IOobject
mattijs's avatar
mattijs committed
44
Foam::regIOobject::regIOobject(const IOobject& io, const bool isTime)
45
46
47
48
49
:
    IOobject(io),
    registered_(false),
    ownedByRegistry_(false),
    lastModified_(0),
mattijs's avatar
mattijs committed
50
51
52
53
54
55
    eventNo_                // Do not get event for top level Time database
    (
        isTime
      ? 0
      : db().getEvent()
    ),
56
57
58
59
60
61
62
63
64
65
66
    isPtr_(NULL)
{
    // Register with objectRegistry if requested
    if (registerObject())
    {
        checkIn();
    }
}


// Construct as copy
67
Foam::regIOobject::regIOobject(const regIOobject& rio)
68
69
70
71
72
:
    IOobject(rio),
    registered_(false),
    ownedByRegistry_(false),
    lastModified_(rio.lastModified_),
mattijs's avatar
mattijs committed
73
    eventNo_(db().getEvent()),
74
75
76
77
78
79
80
81
    isPtr_(NULL)
{
    // Do not register copy with objectRegistry
}


// Construct as copy, and transfering objectRegistry registration to copy
// if registerCopy is true
82
Foam::regIOobject::regIOobject(const regIOobject& rio, bool registerCopy)
83
84
85
86
87
:
    IOobject(rio),
    registered_(false),
    ownedByRegistry_(false),
    lastModified_(rio.lastModified_),
mattijs's avatar
mattijs committed
88
    eventNo_(db().getEvent()),
89
90
91
92
93
94
95
96
97
98
99
100
101
    isPtr_(NULL)
{
    if (registerCopy && rio.registered_)
    {
        const_cast<regIOobject&>(rio).checkOut();
        checkIn();
    }
}


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

// Delete read stream, checkout from objectRegistry and destroy
102
Foam::regIOobject::~regIOobject()
103
104
105
106
107
108
109
110
111
112
113
114
{
    if (objectRegistry::debug)
    {
        Info<< "Destroying regIOobject called " << name()
            << " of type " << type()
            << " in directory " << path()
            << endl;
    }

    if (isPtr_)
    {
        delete isPtr_;
115
        isPtr_ = NULL;
116
117
118
119
120
121
122
123
124
125
126
127
128
    }

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

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


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

129
bool Foam::regIOobject::checkIn()
130
131
132
{
    if (!registered_)
    {
133
134
135
136
        // multiple checkin of same object is disallowed - this would mess up
        // any mapping
        registered_ = db().checkIn(*this);

137
        // check-in on defaultRegion is allowed to fail, since subsetted meshes
138
139
        // are created with the same name as their originating mesh
        if (!registered_ && debug && name() != polyMesh::defaultRegion)
140
        {
141
142
            if (debug == 2)
            {
143
144
                // for ease of finding where attempted duplicate check-in
                // originated
145
146
                FatalErrorIn("regIOobject::checkIn()")
                    << "failed to register object " << objectPath()
147
                    << " the name already exists in the objectRegistry"
148
149
150
151
152
153
154
155
156
                    << abort(FatalError);
            }
            else
            {
                WarningIn("regIOobject::checkIn()")
                    << "failed to register object " << objectPath()
                    << " the name already exists in the objectRegistry"
                    << endl;
            }
157
158
        }
    }
159
160

    return registered_;
161
162
163
}


164
bool Foam::regIOobject::checkOut()
165
166
167
168
{
    if (registered_)
    {
        registered_ = false;
169
        return db().checkOut(*this);
170
    }
171
172

    return false;
173
174
175
}


176
bool Foam::regIOobject::upToDate(const regIOobject& a) const
mattijs's avatar
mattijs committed
177
{
178
    if (a.eventNo() >= eventNo_)
mattijs's avatar
mattijs committed
179
180
181
182
183
184
185
186
187
188
    {
        return false;
    }
    else
    {
        return true;
    }
}


189
190
191
192
193
bool Foam::regIOobject::upToDate
(
    const regIOobject& a,
    const regIOobject& b
) const
mattijs's avatar
mattijs committed
194
195
196
{
    if
    (
197
198
        a.eventNo() >= eventNo_
     || b.eventNo() >= eventNo_
mattijs's avatar
mattijs committed
199
200
201
202
203
204
205
206
207
208
209
    )
    {
        return false;
    }
    else
    {
        return true;
    }
}


210
bool Foam::regIOobject::upToDate
mattijs's avatar
mattijs committed
211
(
212
213
214
    const regIOobject& a,
    const regIOobject& b,
    const regIOobject& c
mattijs's avatar
mattijs committed
215
216
217
218
) const
{
    if
    (
219
220
221
        a.eventNo() >= eventNo_
     || b.eventNo() >= eventNo_
     || c.eventNo() >= eventNo_
mattijs's avatar
mattijs committed
222
223
224
225
226
227
228
229
230
231
232
    )
    {
        return false;
    }
    else
    {
        return true;
    }
}


233
bool Foam::regIOobject::upToDate
mattijs's avatar
mattijs committed
234
(
235
236
237
238
    const regIOobject& a,
    const regIOobject& b,
    const regIOobject& c,
    const regIOobject& d
mattijs's avatar
mattijs committed
239
240
241
242
) const
{
    if
    (
243
244
245
246
        a.eventNo() >= eventNo_
     || b.eventNo() >= eventNo_
     || c.eventNo() >= eventNo_
     || d.eventNo() >= eventNo_
mattijs's avatar
mattijs committed
247
248
249
250
251
252
253
254
255
256
257
    )
    {
        return false;
    }
    else
    {
        return true;
    }
}


258
259
//- Flag me as up to date
void Foam::regIOobject::setUpToDate()
mattijs's avatar
mattijs committed
260
261
262
263
264
{
    eventNo_ = db().getEvent();
}


265
// Rename object and re-register with objectRegistry under new name
266
void Foam::regIOobject::rename(const word& newName)
267
268
269
270
271
272
{
    // Check out of objectRegistry
    checkOut();

    IOobject::rename(newName);

273
274
275
276
277
    if (registerObject())
    {
        // Re-register object with objectRegistry
        checkIn();
    }
278
279
280
281
}


// Assign to IOobject
282
void Foam::regIOobject::operator=(const IOobject& io)
283
284
285
286
287
288
289
290
291
292
293
294
{
    if (isPtr_)
    {
        delete isPtr_;
        isPtr_ = NULL;
    }

    // Check out of objectRegistry
    checkOut();

    IOobject::operator=(io);

295
296
297
298
299
    if (registerObject())
    {
        // Re-register object with objectRegistry
        checkIn();
    }
300
301
302
303
}


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