regIOobject.H 11.3 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

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"
45 46
#include "refPtr.H"
#include "tmp.H"
47
#include "typeInfo.H"
48
#include "stdFoam.H"
49 50 51 52
#include "OSspecific.H"

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

mattijs's avatar
mattijs committed
53

54 55 56
namespace Foam
{

57 58
// Forward Declarations

59 60 61 62
namespace functionEntries
{
    class codeStream;
}
63 64 65 66
namespace fileOperations
{
    class uncollatedFileOperation;
}
67

68
/*---------------------------------------------------------------------------*\
69
                         Class regIOobject Declaration
70 71 72 73 74 75
\*---------------------------------------------------------------------------*/

class regIOobject
:
    public IOobject
{
76
protected:
77

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

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

89 90 91 92
        //- To flag master-only reading of objects
        static bool masterOnlyReading;


93 94
private:

95
    // Private Data
96 97 98 99 100 101 102

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

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

103 104
        //- List of modification watch indices
        mutable labelList watchIndices_;
105

mattijs's avatar
mattijs committed
106 107 108
        //- eventNo of last update
        label eventNo_;

109
        //- Istream for reading
110
        autoPtr<ISstream> isPtr_;
111 112


113
    // Private Member Functions
114 115

        //- Return Istream
116
        Istream& readStream(const bool valid = true);
117

118 119
        //- No copy assignment
        void operator=(const regIOobject&) = delete;
120 121 122 123


public:

124
        //- Friendship with classes needing access to masterOnlyReading
125
        friend class functionEntries::codeStream;
126
        friend class fileOperations::uncollatedFileOperation;
127 128


129 130 131 132 133
    // Static data

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

134
        static float fileModificationSkew;
135

136 137
        static int maxFileModificationPolls;

138 139 140

    // Constructors

141 142 143
        //- 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);
144

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

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

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

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

159

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


164
    // Member Functions
165 166 167

        // Registration

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

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

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

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

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

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

193 194 195
            //- Transfer pointer ownership to its registry.
            //  Resets (clears) the parameter.
            //  \return reference to the stored object
196
            template<class Type>
197
            inline static Type& store(autoPtr<Type>& ptr);
198

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
            //- Transfer pointer ownership to its registry.
            //  Resets (clears) the parameter.
            //  \return reference to the stored object
            template<class Type>
            inline static Type& store(autoPtr<Type>&& ptr);

            //- Transfer pointer ownership to its registry.
            //  Changes parameter from PTR to CREF (do not rely on this).
            //  \return reference to the stored object
            template<class Type>
            inline static Type& store(refPtr<Type>& ptr);

            //- Transfer pointer ownership to its registry.
            //  Changes parameter from PTR to CREF (do not rely on this).
            //  \return reference to the stored object
214
            template<class Type>
215
            inline static Type& store(refPtr<Type>&& ptr);
216

217 218 219
            //- Transfer pointer ownership to its registry.
            //  Changes parameter from PTR to CREF (do not rely on this).
            //  \return reference to the stored object
220
            template<class Type>
221
            inline static Type& store(tmp<Type>& ptr);
222

223 224 225
            //- Transfer pointer ownership to its registry.
            //  Changes parameter from PTR to CREF (do not rely on this).
            //  \return reference to the stored object
226
            template<class Type>
227
            inline static Type& store(tmp<Type>&& ptr);
228

229
            //- Release ownership of this object from its registry
230 231
            //  \param unregister optionally set as non-registered
            inline void release(const bool unregister = false);
232 233


234
        // Dependency Checking
mattijs's avatar
mattijs committed
235 236 237 238 239 240 241

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

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

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
            //- 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
261 262
            bool upToDate
            (
263 264 265 266
                const regIOobject&,
                const regIOobject&,
                const regIOobject&,
                const regIOobject&
267 268
            ) const;

269 270

            //- Set as up-to-date
271 272
            void setUpToDate();

mattijs's avatar
mattijs committed
273

274 275 276 277 278 279 280 281
        // Edit

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


        // Reading

282 283 284 285 286 287 288 289 290
            //- 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();

291
            //- Return Istream and check object type against that given
292
            Istream& readStream(const word&, const bool valid = true);
293 294 295 296 297 298 299 300 301 302 303 304

            //- 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();

305 306
            //- Add file watch for fileName on object if not yet watched.
            //  \return index of watch
307 308 309 310
            virtual label addWatch(const fileName&);

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

312 313
            //- Return file-monitoring handles
            inline labelList& watchIndices();
314 315

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

319
            //- Read object if modified (as set by call to modified)
320 321 322 323 324
            virtual bool readIfModified();


        // Writing

Andrew Heather's avatar
Andrew Heather committed
325
            //- Pure virtual writeData function.
326 327 328
            //  Must be defined in derived types
            virtual bool writeData(Ostream&) const = 0;

329
            //- Write using stream options
330 331
            virtual bool writeObject
            (
332
                IOstreamOption streamOpt,
333
                const bool valid
334 335 336
            ) const;

            //- Write using setting from DB
337
            virtual bool write(const bool valid = true) const;
338 339


340 341 342 343 344 345 346 347 348
        // Other

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


349
    // Member Operators
350

351 352
        //- Copy assignment
        void operator=(const IOobject& io);
353 354 355 356 357 358 359 360 361 362 363 364 365


    // 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;
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
};


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

} // End namespace Foam

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

#include "regIOobjectI.H"

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

#endif

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