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 -------------------------------------------------------------------------------
11 License
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 
27 Class
28  Foam::PtrListDictionary
29 
30 Description
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 
36 SourceFiles
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 
49 namespace Foam
50 {
51 
52 /*---------------------------------------------------------------------------*\
53  Class PtrListDictionary Declaration
54 \*---------------------------------------------------------------------------*/
55 
56 template<class T>
58 :
59  public DictionaryBase<PtrList<T>, T>
60 {
61 public:
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);
76  PtrList<T>::resize(len);
77  }
78 
79  //- Copy construct
80  PtrListDictionary(const PtrListDictionary& dict) = default;
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
97 
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"
108  }
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 // ************************************************************************* //
dictionary dict
Base dictionary class templated on both the form of doubly-linked list it uses as well as the type it...
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:600
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
PtrListDictionary()=default
Default construct: empty without allocation (capacity=0).
Template dictionary class which manages the storage associated with it.
A class for handling words, derived from Foam::string.
Definition: word.H:63
const T & operator[](const word &key) const
Find and return entry.
auto key(const Type &t) -> std::enable_if_t< std::is_enum_v< Type >, std::underlying_type_t< Type > >
Definition: foamGltfBase.H:103
errorManip< error > abort(error &err)
Definition: errorManip.H:139
DictionaryBase< PtrList< T >, T > dict_type
The template instance used for the dictionary content.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void resize(const label newLen)
Adjust size of PtrList.
Definition: PtrList.C:95
const T * lookup(const word &keyword) const
Find and return entry, FatalError on failure.
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:54
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:46
bool addHashEntry(const word &key, T *ptr)
Add an entry to the HashTable.
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
Namespace for OpenFOAM.