objectRegistryTemplates.C 6.23 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2015 OpenFOAM Foundation
6
     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
7
8
9
10
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

11
12
13
14
    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.
15
16
17
18
19
20
21

    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
22
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
23
24
25
26

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

#include "objectRegistry.H"
27
#include "stringListOps.H"
28
29
30
31

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

template<class Type>
32
Foam::wordList Foam::objectRegistry::names() const
33
34
35
36
{
    wordList objectNames(size());

    label count=0;
37
    forAllConstIter(HashTable<regIOobject*>, *this, iter)
38
39
40
41
42
43
44
45
46
47
48
49
50
    {
        if (isA<Type>(*iter()))
        {
            objectNames[count++] = iter()->name();
        }
    }

    objectNames.setSize(count);

    return objectNames;
}


51
template<class Type>
52
Foam::wordList Foam::objectRegistry::names(const wordRe& matcher) const
53
54
55
56
57
58
59
60
61
62
{
    wordList objectNames(size());

    label count = 0;
    forAllConstIter(HashTable<regIOobject*>, *this, iter)
    {
        if (isA<Type>(*iter()))
        {
            const word& objectName = iter()->name();

63
            if (matcher.match(objectName))
64
65
66
67
68
69
70
71
72
73
74
75
76
            {
                objectNames[count++] = objectName;
            }
        }
    }

    objectNames.setSize(count);

    return objectNames;
}


template<class Type>
77
Foam::wordList Foam::objectRegistry::names(const wordReList& matcher) const
78
79
80
{
    wordList names(this->names<Type>());

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    return wordList(names, findStrings(matcher, names));
}


template<class Type>
Foam::wordList Foam::objectRegistry::sortedNames() const
{
    wordList sorted(this->names<Type>());
    sort(sorted);

    return sorted;
}

template<class Type>
Foam::wordList Foam::objectRegistry::sortedNames
(
    const wordRe& match
) const
{
    wordList sorted(this->names<Type>(match));
    sort(sorted);

    return sorted;
}


template<class Type>
Foam::wordList Foam::objectRegistry::sortedNames
(
    const wordReList& matcher
) const
{
    wordList sorted(this->names<Type>(matcher));
    sort(sorted);

    return sorted;
117
118
119
}


120
template<class Type>
121
122
123
124
Foam::HashTable<const Type*> Foam::objectRegistry::lookupClass
(
    const bool strict
) const
125
126
127
{
    HashTable<const Type*> objectsOfClass(size());

128
    forAllConstIter(HashTable<regIOobject*>, *this, iter)
129
    {
Mark Olesen's avatar
Mark Olesen committed
130
        if (strict ? isType<Type>(*iter()) : isA<Type>(*iter()))
131
132
133
134
135
136
137
138
139
140
141
142
143
        {
            objectsOfClass.insert
            (
                iter()->name(),
                dynamic_cast<const Type*>(iter())
            );
        }
    }

    return objectsOfClass;
}


144
145
146
147
148
149
150
151
152
153
template<class Type>
Foam::HashTable<Type*> Foam::objectRegistry::lookupClass
(
    const bool strict
)
{
    HashTable<Type*> objectsOfClass(size());

    forAllIter(HashTable<regIOobject*>, *this, iter)
    {
Mark Olesen's avatar
Mark Olesen committed
154
        if (strict ? isType<Type>(*iter()) : isA<Type>(*iter()))
155
156
157
158
159
160
161
162
163
164
165
166
167
        {
            objectsOfClass.insert
            (
                iter()->name(),
                dynamic_cast<Type*>(iter())
            );
        }
    }

    return objectsOfClass;
}


168
template<class Type>
169
170
171
172
173
bool Foam::objectRegistry::foundObject
(
    const word& name,
    const bool recursive
) const
174
{
175
    const Type* ptr = this->lookupObjectPtr<Type>(name, recursive);
176

177
    if (ptr)
178
    {
179
        return true;
180
    }
181
    else
182
    {
183
        return false;
184
185
186
187
188
    }
}


template<class Type>
189
190
191
192
193
const Type& Foam::objectRegistry::lookupObject
(
    const word& name,
    const bool recursive
) const
194
195
196
197
198
{
    const_iterator iter = find(name);

    if (iter != end())
    {
199
        const Type* ptr = dynamic_cast<const Type*>(iter());
200

201
        if (ptr)
202
        {
203
            return *ptr;
204
205
        }

206
207
        FatalErrorInFunction
            << nl
208
209
210
211
212
213
            << "    lookup of " << name << " from objectRegistry "
            << this->name()
            << " successful\n    but it is not a " << Type::typeName
            << ", it is a " << iter()->type()
            << abort(FatalError);
    }
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
    else if (recursive && this->parentNotTime())
    {
        return parent_.lookupObject<Type>(name, recursive);
    }

    FatalErrorInFunction
        << nl
        << "    request for " << Type::typeName
        << " " << name << " from objectRegistry " << this->name()
        << " failed\n    available objects of type " << Type::typeName
        << " are" << nl
        << names<Type>()
        << abort(FatalError);

    return NullObjectRef<Type>();
}


template<class Type>
Type& Foam::objectRegistry::lookupObjectRef
(
    const word& name,
    const bool recursive
) const
{
    const Type& ref = this->lookupObject<Type>(name, recursive);
    // The above will already fail if things didn't work

    return const_cast<Type&>(ref);
}


template<class Type>
const Type* Foam::objectRegistry::lookupObjectPtr
(
    const word& name,
    const bool recursive
) const
{
    const_iterator iter = find(name);

    if (iter != end())
256
    {
257
258
259
        const Type* ptr = dynamic_cast<const Type*>(iter());

        if (ptr)
260
        {
261
            return ptr;
262
        }
263
264
265
266
    }
    else if (recursive && this->parentNotTime())
    {
        return parent_.lookupObjectPtr<Type>(name, recursive);
267
268
    }

269
270
271
272
273
274
275
276
277
278
279
280
281
282
    return nullptr;
}


template<class Type>
Type* Foam::objectRegistry::lookupObjectRefPtr
(
    const word& name,
    const bool recursive
) const
{
    const Type* ptr = this->lookupObjectPtr<Type>(name, recursive);

    return const_cast<Type*>(ptr);
283
284
285
286
}


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