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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "IOstreams.H"
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(std::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>
65 inline Foam::label Foam::Keyed<T>::key() const
66 {
67  return key_;
68 }
69 
70 template<class T>
71 inline Foam::label& Foam::Keyed<T>::key()
72 {
73  return key_;
74 }
75 
76 
77 template<class T>
79 Foam::Keyed<T>::createList(const UList<T>& lst, const label key)
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  is.readBegin("Keyed");
120  is >> static_cast<T&>(item) >> item.key_;
121  is.readEnd("Keyed");
122 
123  is.check(FUNCTION_NAME);
124  return is;
125 }
126 
127 
128 template<class T>
129 inline Foam::Ostream& Foam::operator<<(Ostream& os, const Keyed<T>& item)
130 {
131  os << token::BEGIN_LIST
132  << static_cast<const T&>(item) << token::SPACE << item.key_
133  << token::END_LIST;
134 
135  return os;
136 }
137 
138 
139 // ************************************************************************* //
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::Keyed::key
label key() const
Return const access to the integer key.
Definition: KeyedI.H:65
Foam::Keyed
A container with an integer key attached to any item.
Definition: Keyed.H:50
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::Istream::readEnd
bool readEnd(const char *funcName)
End read of data chunk, ends with ')'.
Definition: Istream.C:127
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::Istream::readBegin
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition: Istream.C:109
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
Foam::FatalError
error FatalError
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::Keyed::Keyed
Keyed()
Construct null.
Definition: KeyedI.H:33
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::token::SPACE
Space [isspace].
Definition: token.H:117
Foam::UList
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:103
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
Foam::token::END_LIST
End list [isseparator].
Definition: token.H:123
Foam::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360
Foam::Keyed::createList
static List< Keyed< T > > createList(const UList< T > &lst, const label key=0)
Add labels to a list of values.
Definition: KeyedI.H:79
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::token::BEGIN_LIST
Begin list [isseparator].
Definition: token.H:122