KeyedI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  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 "IOstreams.H"
27 
28 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class T>
34 :
35  key_(-1)
36 {}
37 
38 
39 template<class T>
40 inline Foam::Keyed<T>::Keyed(const T& item, const label key)
41 :
42  T(item),
43  key_(key)
44 {}
45 
46 
47 template<class T>
48 inline Foam::Keyed<T>::Keyed(T&& item, const label key)
49 :
50  T(move(item)),
51  key_(key)
52 {}
53 
54 
55 template<class T>
57 {
58  is >> *this;
59 }
60 
61 
62 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
63 
64 template<class T>
66 {
67  return key_;
68 }
69 
70 template<class T>
72 {
73  return key_;
74 }
75 
76 
77 template<class T>
80 {
81  List<Keyed<T>> newList(lst.size());
82 
83  forAll(lst, elemI)
84  {
85  newList[elemI] = Keyed(lst[elemI], key);
86  }
87  return newList;
88 }
89 
90 
91 template<class T>
94 {
95  if (lst.size() != keys.size())
96  {
98  << "size mismatch adding keys to a list:" << nl
99  << "List has size " << lst.size()
100  << " and keys has size " << keys.size() << nl
101  << abort(FatalError);
102  }
103 
104  List<Keyed<T>> newList(lst.size());
105 
106  forAll(lst, elemI)
107  {
108  newList[elemI] = Keyed(lst[elemI], keys[elemI]);
109  }
110  return newList;
111 }
112 
113 
114 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
115 
116 template<class T>
118 {
119  // Read beginning of Keyed item/key pair
120  is.readBegin("Keyed<T>");
121 
122  is >> static_cast<T&>(item);
123  is >> item.key_;
124 
125  // Read end of Keyed item/key pair
126  is.readEnd("Keyed<T>");
127 
128  // Check state of Ostream
129  is.check("Istream& operator>>(Istream&, Keyed&)");
130 
131  return is;
132 }
133 
134 
135 template<class T>
136 inline Foam::Ostream& Foam::operator<<(Ostream& os, const Keyed<T>& item)
137 {
138  os << token::BEGIN_LIST
139  << static_cast<const T&>(item)
140  << token::SPACE << item.key_
141  << token::END_LIST;
142 
143  return os;
144 }
145 
146 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
147 
148 // ************************************************************************* //
Istream & readBegin(const char *funcName)
Definition: Istream.C:86
A container with an integer key attached to any item.
Definition: Keyed.H:48
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Keyed()
Construct null.
Definition: KeyedI.H:33
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
static List< Keyed< T > > createList(const List< T > &, const label key=0)
Add labels to a list of values.
Definition: KeyedI.H:79
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Istream & readEnd(const char *funcName)
Definition: Istream.C:103
Istream & operator>>(Istream &, directionInfo &)
errorManip< error > abort(error &err)
Definition: errorManip.H:131
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static const char nl
Definition: Ostream.H:260
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
label key() const
Return const access to the integer key.
Definition: KeyedI.H:65
label size() const
Return the number of elements in the UList.
Definition: UListI.H:311