objectHit.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) 2017-2020 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::objectHit
29 
30 Description
31  This class describes a combination of target object index and success flag.
32  Behaves somewhat like std::optional
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef Foam_objectHit_H
37 #define Foam_objectHit_H
38 
39 #include "bool.H"
40 #include "label.H"
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 // Forward Declarations
48 class objectHit;
49 inline Ostream& operator<<(Ostream& os, const objectHit& obj);
50 
51 /*---------------------------------------------------------------------------*\
52  Class objectHit Declaration
53 \*---------------------------------------------------------------------------*/
54 
55 class objectHit
56 {
57  // Private Data
58 
59  //- Hit success
60  bool hit_;
61 
62  //- The hit object index
63  label index_;
64 
65 
66 public:
67 
68  // Constructors
69 
70  //- Default construct. Nothing hit and object index = -1
71  constexpr objectHit() noexcept
72  :
73  hit_(false),
74  index_(-1)
75  {}
76 
77  //- Construct from components
78  objectHit(const bool success, const label index) noexcept
79  :
80  hit_(success),
81  index_(index)
82  {}
83 
84  //- Construct from Istream
85  explicit objectHit(Istream& is)
86  :
87  hit_(readBool(is)),
88  index_(readLabel(is))
89  {}
90 
91 
92  // Member Functions
93 
94  // Access
95 
96  //- Is there a hit?
97  bool hit() const noexcept
98  {
99  return hit_;
100  }
101 
102  //- Return the hit object index
103  label index() const noexcept
104  {
105  return index_;
106  }
107 
108  //- Identical to index()
109  label hitObject() const noexcept
110  {
111  return index_;
112  }
113 
114 
115  // Edit
116 
117  //- Reset to default construct state (false, -1)
119  {
120  hit_ = false;
121  index_ = -1;
122  }
123 
124  //- Set the hit status \em on
125  void setHit() noexcept
126  {
127  hit_ = true;
128  }
130  //- Set the hit status \em off
131  void setMiss() noexcept
132  {
133  hit_ = false;
134  }
135 
136  //- Set the hit object index
137  void setIndex(const label index) noexcept
138  {
139  index_ = index;
140  }
141 
142 
143  // Ostream Operator
144 
145  inline friend Ostream& operator<<(Ostream& os, const objectHit& obj)
146  {
147  return os << obj.hit() << token::SPACE << obj.index();
148  }
149 };
150 
151 
152 // * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
153 
154 inline bool operator==(const objectHit& a, const objectHit& b)
155 {
156  return a.hit() == b.hit() && a.index() == b.index();
157 }
158 
159 
160 inline bool operator!=(const objectHit& a, const objectHit& b)
161 {
162  return !(a == b);
163 }
164 
165 
166 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
167 
168 } // End namespace Foam
169 
170 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
172 #endif
173 
174 // ************************************************************************* //
void reset() noexcept
Reset to default construct state (false, -1)
Definition: objectHit.H:129
constexpr objectHit() noexcept
Default construct. Nothing hit and object index = -1.
Definition: objectHit.H:70
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
label index() const noexcept
Return the hit object index.
Definition: objectHit.H:110
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:63
bool hit() const noexcept
Is there a hit?
Definition: objectHit.H:102
void setMiss() noexcept
Set the hit status off.
Definition: objectHit.H:146
void setHit() noexcept
Set the hit status on.
Definition: objectHit.H:138
label hitObject() const noexcept
Identical to index()
Definition: objectHit.H:118
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Space [isspace].
Definition: token.H:131
This class describes a combination of target object index and success flag. Behaves somewhat like std...
Definition: objectHit.H:48
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
bool success
OBJstream os(runTime.globalPath()/outputName)
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
void setIndex(const label index) noexcept
Set the hit object index.
Definition: objectHit.H:154
friend Ostream & operator<<(Ostream &os, const objectHit &obj)
Definition: objectHit.H:162
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:297
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
System bool.
bool readBool(Istream &is)
Read bool from stream using Foam::Switch(Istream&)
Definition: bool.C:62
Namespace for OpenFOAM.