FreeFOAM The Cross-Platform CFD Toolkit
Tuple2.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 | Copyright (C) 1991-2010 OpenCFD Ltd.
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::Tuple2
26 
27 Description
28  A 2-tuple.
29 
30 SeeAlso
31  Foam::Pair for storing two objects of identical types.
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef Tuple2_H
36 #define Tuple2_H
37 
38 #include <OpenFOAM/Istream.H>
39 
40 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
41 
42 namespace Foam
43 {
44 
45 // Forward declaration of friend functions and operators
46 
47 template<class Type1, class Type2>
48 class Tuple2;
49 
50 template<class Type1, class Type2>
51 inline bool operator==
52 (
53  const Tuple2<Type1, Type2>&,
54  const Tuple2<Type1, Type2>&
55 );
56 
57 template<class Type1, class Type2>
58 inline bool operator!=
59 (
60  const Tuple2<Type1, Type2>&,
61  const Tuple2<Type1, Type2>&
62 );
63 
64 template<class Type1, class Type2>
65 inline Istream& operator>>(Istream&, Tuple2<Type1, Type2>&);
66 
67 template<class Type1, class Type2>
68 inline Ostream& operator<<(Ostream&, const Tuple2<Type1, Type2>&);
69 
70 
71 /*---------------------------------------------------------------------------*\
72  class Tuple2 Declaration
73 \*---------------------------------------------------------------------------*/
74 
75 template<class Type1, class Type2>
76 class Tuple2
77 {
78  // Private data
79 
80  Type1 f_;
81  Type2 s_;
82 
83 
84 public:
85 
86  // Constructors
87 
88  //- Null constructor for lists
89  inline Tuple2()
90  {}
91 
92  //- Construct from components
93  inline Tuple2(const Type1& f, const Type2& s)
94  :
95  f_(f),
96  s_(s)
97  {}
98 
99  //- Construct from Istream
100  inline Tuple2(Istream& is)
101  {
102  is >> *this;
103  }
104 
105 
106  // Member Functions
107 
108  //- Return first
109  inline const Type1& first() const
110  {
111  return f_;
112  }
113 
114  //- Return first
115  inline Type1& first()
116  {
117  return f_;
118  }
119 
120  //- Return second
121  inline const Type2& second() const
122  {
123  return s_;
124  }
125 
126  //- Return second
127  inline Type2& second()
128  {
129  return s_;
130  }
131 
132  //- Return reverse pair
134  {
135  return Tuple2<Type2, Type1>(second(), first());
136  }
137 
138 
139  // Friend Operators
140 
141  friend bool operator== <Type1, Type2>
142  (
143  const Tuple2<Type1, Type2>& a,
144  const Tuple2<Type1, Type2>& b
145  );
146 
147  friend bool operator!= <Type1, Type2>
148  (
149  const Tuple2<Type1, Type2>& a,
150  const Tuple2<Type1, Type2>& b
151  );
152 
153 
154  // IOstream operators
155 
156  //- Read Tuple2 from Istream, discarding contents of existing Tuple2.
157  friend Istream& operator>> <Type1, Type2>
158  (
159  Istream& is,
161  );
162 
163  // Write Tuple2 to Ostream.
164  friend Ostream& operator<< <Type1, Type2>
165  (
166  Ostream& os,
167  const Tuple2<Type1, Type2>& t2
168  );
169 };
170 
171 
172 template<class Type1, class Type2>
173 inline bool operator==
174 (
175  const Tuple2<Type1, Type2>& a,
176  const Tuple2<Type1, Type2>& b
177 )
178 {
179  return (a.first() == b.first() && a.second() == b.second());
180 }
181 
182 
183 template<class Type1, class Type2>
184 inline bool operator!=
185 (
186  const Tuple2<Type1, Type2>& a,
187  const Tuple2<Type1, Type2>& b
188 )
189 {
190  return !(a == b);
191 }
192 
193 
194 template<class Type1, class Type2>
196 {
197  is.readBegin("Tuple2");
198  is >> t2.f_ >> t2.s_;
199  is.readEnd("Tuple2");
200 
201  // Check state of Istream
202  is.check("operator>>(Istream&, Tuple2<Type1, Type2>&)");
203 
204  return is;
205 }
206 
207 
208 template<class Type1, class Type2>
209 inline Ostream& operator<<(Ostream& os, const Tuple2<Type1, Type2>& t2)
210 {
211  os << token::BEGIN_LIST
212  << t2.f_ << token::SPACE << t2.s_
213  << token::END_LIST;
214 
215  return os;
216 }
217 
218 
219 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
220 
221 } // End namespace Foam
222 
223 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
224 
225 #endif
226 
227 // ************************ vim: set sw=4 sts=4 et: ************************ //