regIOobject.H 10.9 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 46 47 48 49

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"
#include "OSspecific.H"

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

mattijs's avatar
mattijs committed
50

51 52 53
namespace Foam
{

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

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

class regIOobject
:
    public IOobject
{
72
protected:
73

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

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

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


89 90
private:

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

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

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

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

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

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


109
    // Private Member Functions
110 111

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

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


public:

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


127 128 129 130 131
    // Static data

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

132
        static float fileModificationSkew;
133

134 135
        static int maxFileModificationPolls;

136 137 138

    // Constructors

139 140 141
        //- 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);
142

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

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

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

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

157

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


162
    // Member Functions
163 164 165

        // Registration

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

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

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

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

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

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

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

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

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
            //- 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);

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


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

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

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

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
            //- 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
254 255
            bool upToDate
            (
256 257 258 259
                const regIOobject&,
                const regIOobject&,
                const regIOobject&,
                const regIOobject&
260 261
            ) const;

262 263

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

mattijs's avatar
mattijs committed
266

267 268 269 270 271 272 273 274
        // Edit

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


        // Reading

275 276 277 278 279 280 281 282 283
            //- 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();

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

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

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

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

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

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

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


        // Writing

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

            //- Write using given format, version and compression
            virtual bool writeObject
            (
325 326
                IOstream::streamFormat,
                IOstream::versionNumber,
327 328
                IOstream::compressionType,
                const bool valid
329 330 331
            ) const;

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


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

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


344
    // Member Operators
345

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


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

} // End namespace Foam

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

#include "regIOobjectI.H"

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

#endif

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