Loading...
Searching...
No Matches
PtrListDictionary.H
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2015-2016 OpenFOAM Foundation
9 Copyright (C) 2024 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 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
27Class
28 Foam::PtrListDictionary
29
30Description
31 Template dictionary class which manages the storage associated with it.
32
33 It is derived from DictionaryBase instantiated on the memory managed PtrList
34 of <T> to provide ordered indexing in addition to the dictionary lookup.
35
36SourceFiles
37 PtrListDictionary.C
38
39\*---------------------------------------------------------------------------*/
40
41#ifndef Foam_PtrListDictionary_H
42#define Foam_PtrListDictionary_H
43
44#include "DictionaryBase.H"
45#include "PtrList.H"
46
47// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48
49namespace Foam
50{
51
52/*---------------------------------------------------------------------------*\
53 Class PtrListDictionary Declaration
54\*---------------------------------------------------------------------------*/
55
56template<class T>
58:
59 public DictionaryBase<PtrList<T>, T>
60{
61public:
62
63 //- The template instance used for the dictionary content
65
66
67 // Constructors
68
69 //- Default construct: empty without allocation (capacity=0).
70 PtrListDictionary() = default;
71
72 //- Construct given initial list size
73 explicit PtrListDictionary(const label len)
74 {
75 dict_type(2*len);
77 }
78
79 //- Copy construct
81
82 //- Construct from Istream using given Istream constructor class
83 template<class INew>
84 PtrListDictionary(Istream& is, const INew& inew)
85 :
86 dict_type(is, inew)
87 {}
88
89 //- Construct from Istream
90 explicit PtrListDictionary(Istream& is)
91 :
92 dict_type(is)
93 {}
94
95
96 // Member Functions
98 //- Set element to pointer provided and return old element
99 autoPtr<T> set(const label i, const word& key, T* ptr)
100 {
101 autoPtr<T> old(PtrList<T>::set(i, ptr));
102
103 if (!dict_type::addHashEntry(key, ptr))
104 {
106 << "Cannot insert '" << key << "' into hash-table"
109 return old;
110 }
111
112 //- Set element to autoPtr value provided and return old element
113 autoPtr<T> set(const label i, const word& key, autoPtr<T>& ptr)
114 {
115 return this->set(i, key, ptr.release());
116 }
117
118 //- Set element to tmp value provided and return old element
119 autoPtr<T> set(const label i, const word& key, tmp<T>& ptr)
120 {
121 return this->set(i, key, ptr.ptr()); // release or clone
122 }
123
125 // Member Operators
126
127 using PtrList<T>::operator[];
128
129 //- Find and return entry
130 const T& operator[](const word& key) const
131 {
132 return *(dict_type::lookup(key));
133 }
134
135 //- Find and return entry
136 T& operator[](const word& key)
137 {
138 return *(dict_type::lookup(key));
139 }
140};
141
142
143// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
144
145} // End namespace Foam
146
147// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
148
149#endif
150
151// ************************************************************************* //
const T * lookup(const word &keyword) const
bool addHashEntry(const word &key, T *ptr)
Add an entry to the HashTable.
friend Ostream & operator(Ostream &, const DictionaryBase< PtrList< T >, T > &)
A helper class when constructing from an Istream or dictionary.
Definition INew.H:47
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
Template dictionary class which manages the storage associated with it.
PtrListDictionary()=default
Default construct: empty without allocation (capacity=0).
PtrListDictionary(const label len)
Construct given initial list size.
autoPtr< T > set(const label i, const word &key, T *ptr)
Set element to pointer provided and return old element.
PtrListDictionary(Istream &is)
Construct from Istream.
T & operator[](const word &key)
Find and return entry.
PtrListDictionary(const PtrListDictionary &dict)=default
Copy construct.
autoPtr< T > set(const label i, const word &key, tmp< T > &ptr)
Set element to tmp value provided and return old element.
DictionaryBase< PtrList< T >, T > dict_type
The template instance used for the dictionary content.
autoPtr< T > set(const label i, const word &key, autoPtr< T > &ptr)
Set element to autoPtr value provided and return old element.
const T & operator[](const word &key) const
Find and return entry.
PtrListDictionary(Istream &is, const INew &inew)
Construct from Istream using given Istream constructor class.
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition PtrList.H:67
const T * set(const label i) const
Return const pointer to element (can be nullptr), or nullptr for out-of-range access (ie,...
Definition PtrList.H:171
void resize(const label newLen)
Adjust size of PtrList.
Definition PtrList.C:124
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
T * release() noexcept
Release ownership and return the pointer.
Definition autoPtrI.H:28
A class for managing temporary objects.
Definition tmp.H:75
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition tmpI.H:256
A class for handling words, derived from Foam::string.
Definition word.H:66
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
Namespace for OpenFOAM.
errorManip< error > abort(error &err)
Definition errorManip.H:139
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
dictionary dict