regIOobjectI.H 4.88 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-2015 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

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

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

inline bool Foam::regIOobject::ownedByRegistry() const
{
    return ownedByRegistry_;
}


37
inline bool Foam::regIOobject::store()
38
{
39 40 41 42 43 44 45 46 47 48 49
    if (checkIn())
    {
        ownedByRegistry_ = true;
    }
    else
    {
        WarningInFunction
            << "Refuse to store unregistered object: " << this->name() << nl;
    }

    return ownedByRegistry_;
50 51 52 53
}


template<class Type>
54
inline Type& Foam::regIOobject::store(Type* p)
55
{
56
    if (!p)
57
    {
58
        FatalErrorInFunction
59
            << "Object deallocated\n"
60 61 62
            << abort(FatalError);
    }

63 64 65 66 67 68 69 70 71
    const bool ok = p->regIOobject::store();

    if (!ok)
    {
        FatalErrorInFunction
            << "Failed to store pointer: " << p->regIOobject::name()
            << ". Risk of memory leakage\n"
            << abort(FatalError);
    }
72

73
    return *p;
74 75 76 77
}


template<class Type>
78
inline Type& Foam::regIOobject::store(autoPtr<Type>& ptr)
79
{
80
    // Pass management to objectRegistry
81
    return store(ptr.release());
82
}
83

84

85
template<class Type>
86
inline Type& Foam::regIOobject::store(autoPtr<Type>&& ptr)
87
{
88
    // Pass management to objectRegistry
89
    return store(ptr.release());
90 91 92 93
}


template<class Type>
94
inline Type& Foam::regIOobject::store(refPtr<Type>& ptr)
95 96 97
{
    Type* p = nullptr;

98
    if (ptr.is_pointer())
99
    {
100 101
        // Acquire ownership, pass management to objectRegistry
        p = ptr.ptr();
102 103 104

        store(p);

105 106
        // Change parameter to access the stored reference
        ptr.cref(*p);
107 108 109
    }
    else
    {
110
        // Taking ownership of reference does not make much sense.
111 112 113 114
        // - Storing the object won't actually do so, it will be removed
        //   when the original object goes out of scope.
        // - Storing a clone may not be what we want.

115
        p = ptr.get();
116 117

        WarningInFunction
118
            << "Refuse to store reference: " << p->name()
119 120 121 122 123 124 125 126
            << ". Likely indicates a coding error\n";
    }

    return *p;
}


template<class Type>
127
inline Type& Foam::regIOobject::store(refPtr<Type>&& ptr)
128
{
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    // Forward as named reference, which also does a move
    return store(ptr);
}


template<class Type>
inline Type& Foam::regIOobject::store(tmp<Type>& ptr)
{
    Type* p = nullptr;

    if (ptr.is_pointer())
    {
        // Acquire ownership, pass management to objectRegistry
        p = ptr.ptr();

        store(p);

        // Change parameter to access the stored reference
        ptr.cref(*p);
    }
    else
    {
        // Taking ownership of reference does not make much sense.
        // - Storing the object won't actually do so, it will be removed
        //   when the original object goes out of scope.
        // - Storing a clone may not be what we want.

        p = ptr.get();

        WarningInFunction
            << "Refuse to store reference: " << p->name()
            << ". Likely indicates a coding error\n";
    }

    return *p;
}


template<class Type>
inline Type& Foam::regIOobject::store(tmp<Type>&& ptr)
{
    // Forward as named reference, which also does a move
    return store(ptr);
172 173 174
}


175
inline void Foam::regIOobject::release(const bool unregister)
176 177
{
    ownedByRegistry_ = false;
178 179 180 181
    if (unregister)
    {
        registered_ = false;
    }
182 183 184
}


185
inline Foam::label Foam::regIOobject::eventNo() const
mattijs's avatar
mattijs committed
186 187 188 189
{
    return eventNo_;
}

190
inline Foam::label& Foam::regIOobject::eventNo()
mattijs's avatar
mattijs committed
191 192 193 194 195
{
    return eventNo_;
}


196
inline const Foam::labelList& Foam::regIOobject::watchIndices() const
197
{
198
    return watchIndices_;
199 200 201
}


202
inline Foam::labelList& Foam::regIOobject::watchIndices()
203
{
204
    return watchIndices_;
205 206 207
}


208
// ************************************************************************* //