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 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef Foam_LabelledItem_H
43 #define Foam_LabelledItem_H
44 
45 #include "label.H"
46 #include "IOstreams.H"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 // Forward Declarations
54 template<class T> class LabelledItem;
55 template<class T> Istream& operator>>(Istream&, LabelledItem<T>&);
56 template<class T> Ostream& operator<<(Ostream&, const LabelledItem<T>&);
57 
58 /*---------------------------------------------------------------------------*\
59  Class LabelledItem Declaration
60 \*---------------------------------------------------------------------------*/
61 
62 template<class T>
63 class LabelledItem
64 :
65  public T
66 {
67  // Private Data
68 
69  //- The object index
70  label index_;
71 
72 
73 public:
74 
75 
76  // Constructors
77 
78  //- Default construct item, with index = -1
79  LabelledItem()
80  :
81  T(),
82  index_(-1)
83  {}
84 
85  //- Copy construct item, with index = -1
86  explicit LabelledItem(const T& item)
87  :
88  T(item),
89  index_(-1)
90  {}
91 
92  //- Move construct item, with index = -1
93  explicit LabelledItem(T&& item)
94  :
95  T(std::move(item)),
96  index_(-1)
97  {}
98 
99  //- Construct from components
100  LabelledItem(const T& item, label idx)
101  :
102  T(item),
103  index_(idx)
104  {}
106  //- Construct from Istream
107  explicit LabelledItem(Istream& is)
108  {
109  is >> *this;
110  }
111 
112 
113  // Member Functions
115  //- Return the index
116  label index() const noexcept
117  {
118  return index_;
119  }
120 
121  //- Non-const access to the index
122  label& index() noexcept
123  {
124  return index_;
125  }
126 
127  //- Set the index
128  void setIndex(const label idx) noexcept
129  {
130  index_ = idx;
131  }
132 
134  // Member Operators
135 
136  //- Test for equality of components
137  bool operator==(const LabelledItem<T>& rhs) const
138  {
139  return
140  (
141  index_ == rhs.index_
142  && static_cast<const T&>(*this) == static_cast<const T&>(rhs)
143  );
144  }
145 
146  //- Test for inequality of components
147  bool operator!=(const LabelledItem<T>& rhs) const
148  {
149  return !(*this == rhs);
150  }
151 
153  // IOstream Operators
154 
155  friend Istream& operator>> <T>(Istream&, LabelledItem<T>&);
156  friend Ostream& operator<< <T>(Ostream&, const LabelledItem<T>&);
157 };
158 
159 
160 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
161 
162 } // End namespace Foam
163 
164 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
165 
166 template<class T>
167 inline Foam::Istream& Foam::operator>>
168 (
169  Istream& is,
170  LabelledItem<T>& item
171 )
172 {
173  is.readBegin("LabelledItem");
174  is >> static_cast<T&>(item) >> item.index();
175  is.readEnd("LabelledItem");
176 
177  is.check(FUNCTION_NAME);
178  return is;
179 }
180 
181 
182 template<class T>
183 inline Foam::Ostream& Foam::operator<<
184 (
185  Ostream& os,
186  const LabelledItem<T>& item
187 )
188 {
189  // Output like Tuple2
190  os << token::BEGIN_LIST
191  << static_cast<const T&>(item) << token::SPACE
192  << item.index()
193  << token::END_LIST;
194 
195  return os;
196 }
197 
198 
199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
200 
201 #endif
202 
203 // ************************************************************************* //
label index() const noexcept
Return the index.
Definition: LabelledItem.H:125
void setIndex(const label idx) noexcept
Set the index.
Definition: LabelledItem.H:141
LabelledItem()
Default construct item, with index = -1.
Definition: LabelledItem.H:78
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:152
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: scalarImpl.H:255
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:49
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:164
Namespace for OpenFOAM.