DynamicID.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 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 Class
25  Foam::DynamicID
26 
27 Description
28  A class that holds the data needed to identify things (zones, patches)
29  in a dynamic mesh.
30 
31  The thing is identified by name.
32  Its indices are updated if the mesh has changed.
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef DynamicID_H
37 #define DynamicID_H
38 
39 #include "wordRe.H"
40 #include "labelList.H"
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 // Forward declaration of friend functions and operators
48 template<class> class DynamicID;
49 
50 template<class ObjectType>
51 Ostream& operator<<(Ostream&, const DynamicID<ObjectType>&);
52 
53 /*---------------------------------------------------------------------------*\
54  Class DynamicID Declaration
55 \*---------------------------------------------------------------------------*/
56 
57 template<class ObjectType>
58 class DynamicID
59 {
60  // Private Data
61 
62  //- Zone name
63  wordRe key_;
64 
65  //- Zone indices
66  labelList indices_;
67 
68 
69 public:
70 
71  // Constructors
72 
73  //- Construct from name
74  DynamicID(const wordRe& key, const ObjectType& obj)
75  :
76  key_(key),
77  indices_(obj.findIndices(key_))
78  {}
79 
80  //- Construct from Istream
81  DynamicID(Istream& is, const ObjectType& obj)
82  :
83  key_(is),
84  indices_(obj.findIndices(key_))
85  {}
86 
87 
88  // Destructor - default
89 
90 
91  // Member Functions
92 
93  // Access
94 
95  //- Return name
96  const wordRe& name() const
97  {
98  return key_;
99  }
100 
101  //- Return indices of matching zones
102  const labelList& indices() const
103  {
104  return indices_;
105  }
106 
107  //- Return index of first matching zone
108  label index() const
109  {
110  return indices_.empty() ? -1 : indices_[0];
111  }
112 
113  //- Has the zone been found
114  bool active() const
115  {
116  return !indices_.empty();
117  }
118 
119 
120  // Edit
121 
122  //- Update
123  void update(const ObjectType& obj)
124  {
125  indices_ = obj.findIndices(key_);
126  }
127 
128 
129  // IOstream Operators
130 
131  friend Ostream& operator<< <ObjectType>
132  (Ostream&, const DynamicID<ObjectType>&);
133 };
134 
135 
136 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
137 
138 template<class ObjectType>
139 Ostream& operator<<(Ostream& os, const DynamicID<ObjectType>& dynId)
140 {
141  os << token::BEGIN_LIST
142  << dynId.name() << token::SPACE << dynId.index()
143  << token::END_LIST;
144 
145  // Check state of Ostream
146  os.check("Ostream& operator<<(Ostream&, const DynamicID<ObjectType>&)");
147 
148  return os;
149 }
150 
151 
152 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
153 
154 } // End namespace Foam
155 
156 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
157 
158 #endif
159 
160 // ************************************************************************* //
void update(const ObjectType &obj)
Update.
Definition: DynamicID.H:122
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:325
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
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A class that holds the data needed to identify things (zones, patches) in a dynamic mesh...
Definition: DynamicID.H:47
labelList findIndices(const ListType &, typename ListType::const_reference, const label start=0)
Find all occurrences of given element. Linear search.
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:74
DynamicID(const wordRe &key, const ObjectType &obj)
Construct from name.
Definition: DynamicID.H:73
label index() const
Return index of first matching zone.
Definition: DynamicID.H:107
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
bool active() const
Has the zone been found.
Definition: DynamicID.H:113
const wordRe & name() const
Return name.
Definition: DynamicID.H:95
const labelList & indices() const
Return indices of matching zones.
Definition: DynamicID.H:101
Namespace for OpenFOAM.