KeyedI.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 | Copyright (C) 2011 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(const Xfer<T>& item, const label key)
49 :
50  T(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  (
99  "Foam::Keyed<T>::createList(const List<T>&, const labelUList&)"
100  )
101  << "size mismatch adding keys to a list:" << nl
102  << "List has size " << lst.size()
103  << " and keys has size " << keys.size() << nl
104  << abort(FatalError);
105  }
106 
107  List<Keyed<T> > newList(lst.size());
108 
109  forAll(lst, elemI)
110  {
111  newList[elemI] = Keyed(lst[elemI], keys[elemI]);
112  }
113  return newList;
114 }
115 
116 
117 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
118 
119 template<class T>
121 {
122  // Read beginning of Keyed item/key pair
123  is.readBegin("Keyed<T>");
124 
125  is >> static_cast<T&>(item);
126  is >> item.key_;
127 
128  // Read end of Keyed item/key pair
129  is.readEnd("Keyed<T>");
130 
131  // Check state of Ostream
132  is.check("Istream& operator>>(Istream&, Keyed&)");
133 
134  return is;
135 }
136 
137 
138 template<class T>
139 inline Foam::Ostream& Foam::operator<<(Ostream& os, const Keyed<T>& item)
140 {
141  os << token::BEGIN_LIST
142  << static_cast<const T&>(item)
143  << token::SPACE << item.key_
144  << token::END_LIST;
145 
146  return os;
147 }
148 
149 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
150 
151 // ************************************************************************* //
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
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
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
static const char nl
Definition: Ostream.H:260
#define forAll(list, i)
Definition: UList.H:421
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Istream & readBegin(const char *funcName)
Definition: Istream.C:88
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
error FatalError
A container with an integer key attached to any item.
Definition: Keyed.H:48
Keyed()
Construct null.
Definition: KeyedI.H:33
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
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:144
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static List< Keyed< T > > createList(const List< T > &, const label key=0)
Add labels to a list of values.
Definition: KeyedI.H:79
label key() const
Return const access to the integer key.
Definition: KeyedI.H:65
Istream & readEnd(const char *funcName)
Definition: Istream.C:105