LabelledItem.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 OpenFOAM Foundation
9  Copyright (C) 2021-2022 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::LabelledItem
29 
30 Description
31  A container with an integer index that can be attached to any item.
32  The index may be useful for sorting or storing additional information.
33 
34 SeeAlso
35  Foam::objectHit
36  Foam::PointIndexHit
37 
38 SourceFiles
39  LabelledItemI.H
40 
41 \*---------------------------------------------------------------------------*/
42 
43 #ifndef Foam_LabelledItem_H
44 #define Foam_LabelledItem_H
45 
46 #include "label.H"
47 #include "IOstreams.H"
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Forward Declarations
55 template<class T> class LabelledItem;
56 template<class T> Istream& operator>>(Istream&, LabelledItem<T>&);
57 template<class T> Ostream& operator<<(Ostream&, const LabelledItem<T>&);
58 
59 /*---------------------------------------------------------------------------*\
60  Class LabelledItem Declaration
61 \*---------------------------------------------------------------------------*/
62 
63 template<class T>
64 class LabelledItem
65 :
66  public T
67 {
68  // Private Data
69 
70  //- The object index
71  label index_;
72 
73 
74 public:
75 
76 
77  // Constructors
78 
79  //- Default construct item, with index = -1
80  LabelledItem()
81  :
82  T(),
83  index_(-1)
84  {}
85 
86  //- Copy construct item, with index = -1
87  explicit LabelledItem(const T& item)
88  :
89  T(item),
90  index_(-1)
91  {}
92 
93  //- Move construct item, with index = -1
94  explicit LabelledItem(T&& item)
95  :
96  T(std::move(item)),
97  index_(-1)
98  {}
99 
100  //- Construct from components
101  LabelledItem(const T& item, label idx)
102  :
103  T(item),
104  index_(idx)
105  {}
107  //- Construct from Istream
108  explicit LabelledItem(Istream& is)
109  {
110  is >> *this;
111  }
112 
113 
114  // Member Functions
116  //- Return the index
117  label index() const noexcept
118  {
119  return index_;
120  }
121 
122  //- Non-const access to the index
123  label& index() noexcept
124  {
125  return index_;
126  }
127 
128  //- Set the index
129  void setIndex(const label idx) noexcept
130  {
131  index_ = idx;
132  }
133 
135  // Member Operators
136 
137  //- Test for equality of components
138  bool operator==(const LabelledItem<T>& rhs) const
139  {
140  return
141  (
142  index_ == rhs.index_
143  && static_cast<const T&>(*this) == static_cast<const T&>(rhs)
144  );
145  }
146 
147  //- Test for inequality of components
148  bool operator!=(const LabelledItem<T>& rhs) const
149  {
150  return !(*this == rhs);
151  }
152 
154  // IOstream Operators
155 
156  friend Istream& operator>> <T>(Istream&, LabelledItem<T>&);
157  friend Ostream& operator<< <T>(Ostream&, const LabelledItem<T>&);
158 };
159 
160 
161 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
162 
163 } // End namespace Foam
164 
165 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
166 
167 template<class T>
168 inline Foam::Istream& Foam::operator>>
169 (
170  Istream& is,
171  LabelledItem<T>& item
172 )
173 {
174  is.readBegin("LabelledItem");
175  is >> static_cast<T&>(item) >> item.index();
176  is.readEnd("LabelledItem");
177 
178  is.check(FUNCTION_NAME);
179  return is;
180 }
181 
182 
183 template<class T>
184 inline Foam::Ostream& Foam::operator<<
185 (
186  Ostream& os,
187  const LabelledItem<T>& item
188 )
189 {
190  // Output like Tuple2
191  os << token::BEGIN_LIST
192  << static_cast<const T&>(item) << token::SPACE
193  << item.index()
194  << token::END_LIST;
195 
196  return os;
197 }
198 
199 
200 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
201 
202 #endif
203 
204 // ************************************************************************* //
label index() const noexcept
Return the index.
Definition: LabelledItem.H:126
void setIndex(const label idx) noexcept
Set the index.
Definition: LabelledItem.H:142
LabelledItem()
Default construct item, with index = -1.
Definition: LabelledItem.H:79
bool readBegin(const char *funcName)
Begin read of data chunk, starts with &#39;(&#39;.
Definition: Istream.C:134
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
bool operator==(const LabelledItem< T > &rhs) const
Test for equality of components.
Definition: LabelledItem.H:153
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Istream & operator>>(Istream &, directionInfo &)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
A container with an integer index that can be attached to any item. The index may be useful for sorti...
Definition: LabelledItem.H:50
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const volScalarField & T
bool operator!=(const LabelledItem< T > &rhs) const
Test for inequality of components.
Definition: LabelledItem.H:165
Namespace for OpenFOAM.