regIOobject.H 11.2 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
OpenFOAM bot's avatar
OpenFOAM bot committed
6 7
     \\/     M anipulation  |
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
8
    Copyright (C) 2011-2017 OpenFOAM Foundation
9
    Copyright (C) 2018-2020 OpenCFD Ltd.
10 11 12 13
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

14 15 16 17
    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.
18 19 20 21 22 23 24

    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
25
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

Class
    Foam::regIOobject

Description
    regIOobject is an abstract class derived from IOobject to handle
    automatic object registration with the objectRegistry.

SourceFiles
    regIOobject.C
    regIOobjectRead.C
    regIOobjectWrite.C

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

#ifndef regIOobject_H
#define regIOobject_H

#include "IOobject.H"
#include "typeInfo.H"
46
#include "stdFoam.H"
47 48 49 50
#include "OSspecific.H"

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

mattijs's avatar
mattijs committed
51

52 53 54
namespace Foam
{

55 56 57 58
namespace functionEntries
{
    class codeStream;
}
59 60 61 62 63
namespace fileOperations
{
    class uncollatedFileOperation;
    class masterUncollatedFileOperation;
}
64

65
/*---------------------------------------------------------------------------*\
66
                         Class regIOobject Declaration
67 68 69 70 71 72
\*---------------------------------------------------------------------------*/

class regIOobject
:
    public IOobject
{
73
protected:
74

75
        //- Helper: check readOpt flags and read if necessary
76 77 78 79 80
        bool readHeaderOk
        (
            const IOstream::streamFormat PstreamFormat,
            const word& typeName
        );
81

82 83 84 85
        //- Construct and return an IFstream for the object.
        //  The results is NULL if the stream construction failed
        Istream* objectStream();

86 87 88 89
        //- To flag master-only reading of objects
        static bool masterOnlyReading;


90 91
private:

92
    // Private Data
93 94 95 96 97 98 99

        //- Is this object registered with the registry
        bool registered_;

        //- Is this object owned by the registry
        bool ownedByRegistry_;

100 101
        //- List of modification watch indices
        mutable labelList watchIndices_;
102

mattijs's avatar
mattijs committed
103 104 105
        //- eventNo of last update
        label eventNo_;

106
        //- Istream for reading
107
        autoPtr<ISstream> isPtr_;
108 109


110
    // Private Member Functions
111 112

        //- Return Istream
113
        Istream& readStream(const bool valid = true);
114

115 116
        //- No copy assignment
        void operator=(const regIOobject&) = delete;
117 118 119 120


public:

121 122
        //- Declare friendship with classes that need access to
        //- masterOnlyReading
123
        friend class functionEntries::codeStream;
124 125
        friend class fileOperations::uncollatedFileOperation;
        friend class fileOperations::masterUncollatedFileOperation;
126 127


128 129 130 131 132
    // Static data

        //- Runtime type information
        TypeName("regIOobject");

133
        static float fileModificationSkew;
134

135 136
        static int maxFileModificationPolls;

137 138 139

    // Constructors

140 141 142
        //- Construct from IOobject. The optional flag adds special handling
        //- if the object is the top-level regIOobject (eg, Time).
        regIOobject(const IOobject& io, const bool isTime = false);
143

144
        //- Copy construct
145
        regIOobject(const regIOobject& rio);
146

147 148
        //- Copy construct, transferring registry registration to the copy
        //- if registerCopy is true
149
        regIOobject(const regIOobject& rio, bool registerCopy);
150

151 152
        //- Copy construct with new name, transferring registry registration
        //- to the copy f registerCopy is true
153 154
        regIOobject(const word& newName, const regIOobject&, bool registerCopy);

155 156
        //- Copy construct with new IO parameters
        regIOobject(const IOobject& io, const regIOobject& rio);
157

158

159 160
    //- Destructor
    virtual ~regIOobject();
161 162


163
    // Member Functions
164 165 166

        // Registration

167
            //- Add object to registry, if not already registered
168 169
            //  \return true if object was already registered,
            //      or was newly registered
170
            bool checkIn();
171

172 173
            //- Remove all file watches and remove object from registry
            //  \return true if object was registered and was removed
174
            bool checkOut();
175

176 177 178
            //- Add file watch on object (if registered and READ_IF_MODIFIED)
            virtual void addWatch();

179
            //- Is this object owned by the registry?
180 181
            inline bool ownedByRegistry() const;

182 183 184 185
            //- Register object with its registry
            //- and transfer ownership to the registry.
            //  \return true if now ownedByRegistry
            inline bool store();
186

187 188
            //- Register object pointer with its registry
            //- and transfer ownership to the registry.
189
            //  \return reference to the object.
190
            template<class Type>
191
            inline static Type& store(Type* p);
192

193 194 195
            //- Register object pointer with its registry
            //- and transfer ownership to the registry.
            //  Clears the autoPtr parameter.
196
            //  \return reference to the object.
197 198 199
            template<class Type>
            inline static Type& store(autoPtr<Type>& aptr);

200 201 202
            //- Register object pointer with its registry
            //- and transfer ownership to the registry.
            //  Clears the autoPtr parameter.
203
            //  \return reference to the object.
204
            template<class Type>
205
            inline static Type& store(autoPtr<Type>&& aptr);
206

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
            //- Register temporary pointer with its registry
            //- and transfer ownership of pointer to the registry.
            //  After the call, tmp parameter changes from PTR to CREF.
            //
            //  \return reference to the object.
            template<class Type>
            inline static Type& store(tmp<Type>& tptr);

            //- Register temporary pointer with its registry
            //- and transfer ownership of pointer to the registry.
            //  After the call, the tmp parameter content is \em unspecified.
            //
            //  \return reference to the object.
            template<class Type>
            inline static Type& store(tmp<Type>&& tptr);

223
            //- Release ownership of this object from its registry
224 225
            //  \param unregister optionally set as non-registered
            inline void release(const bool unregister = false);
226 227


228
        // Dependency Checking
mattijs's avatar
mattijs committed
229 230 231 232 233 234 235

            //- Event number at last update.
            inline label eventNo() const;

            //- Event number at last update.
            inline label& eventNo();

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
            //- Return true if up-to-date with respect to given object
            bool upToDate(const regIOobject&) const;

            //- Return true if up-to-date with respect to given objects
            bool upToDate
            (
                const regIOobject&,
                const regIOobject&
            ) const;

            //- Return true if up-to-date with respect to given objects
            bool upToDate
            (
                const regIOobject&,
                const regIOobject&,
                const regIOobject&
            ) const;

            //- Return true if up-to-date with respect to given objects
255 256
            bool upToDate
            (
257 258 259 260
                const regIOobject&,
                const regIOobject&,
                const regIOobject&,
                const regIOobject&
261 262
            ) const;

263 264

            //- Set as up-to-date
265 266
            void setUpToDate();

mattijs's avatar
mattijs committed
267

268 269 270 271 272 273 274 275
        // Edit

            //- Rename
            virtual void rename(const word& newName);


        // Reading

276 277 278 279 280 281 282 283 284
            //- Return complete path + object name if the file exists
            //  in the case directory otherwise null. Does not search
            //  up if parallel. Can be overridden to provide this functionality
            //  (e.g. IOdictionary)
            virtual fileName filePath() const;

            //- Read and check header info
            bool headerOk();

285
            //- Return Istream and check object type against that given
286
            Istream& readStream(const word&, const bool valid = true);
287 288 289 290 291 292 293 294 295 296 297 298

            //- Close Istream
            void close();

            //- Virtual readData function.
            //  Must be defined in derived types for which
            //  re-reading is required
            virtual bool readData(Istream&);

            //- Read object
            virtual bool read();

299 300
            //- Add file watch for fileName on object if not yet watched.
            //  \return index of watch
301 302 303 304
            virtual label addWatch(const fileName&);

            //- Return file-monitoring handles
            inline const labelList& watchIndices() const;
305

306 307
            //- Return file-monitoring handles
            inline labelList& watchIndices();
308 309

            //- Return true if the object's file (or files for objectRegistry)
310
            //- have been modified. (modified state is cached by Time)
311 312
            virtual bool modified() const;

313
            //- Read object if modified (as set by call to modified)
314 315 316 317 318
            virtual bool readIfModified();


        // Writing

Andrew Heather's avatar
Andrew Heather committed
319
            //- Pure virtual writeData function.
320 321 322
            //  Must be defined in derived types
            virtual bool writeData(Ostream&) const = 0;

323
            //- Write using stream options
324 325
            virtual bool writeObject
            (
326
                IOstreamOption streamOpt,
327
                const bool valid
328 329 330
            ) const;

            //- Write using setting from DB
331
            virtual bool write(const bool valid = true) const;
332 333


334 335 336 337 338 339 340 341 342
        // Other

            //- Is object global
            virtual bool global() const
            {
                return false;
            }


343
    // Member Operators
344

345 346
        //- Copy assignment
        void operator=(const IOobject& io);
347 348 349 350 351 352 353 354 355 356 357 358 359


    // Housekeeping

        //- Write using given format, version and compression
        FOAM_DEPRECATED_FOR(2020-02, "writeObject(IOstreamOption, bool)")
        virtual bool writeObject
        (
            IOstream::streamFormat fmt,
            IOstream::versionNumber ver,
            IOstream::compressionType comp,
            const bool valid
        ) const;
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
};


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

} // End namespace Foam

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

#include "regIOobjectI.H"

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

#endif

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