FreeFOAM The Cross-Platform CFD Toolkit
Tuple.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::Tuple
26 
27 Description
28  A 2 Tuple. Differs from Tuple in that the two elements can be different
29  type.
30 
31 
32 \*---------------------------------------------------------------------------*/
33 
34 #ifndef Tuple_H
35 #define Tuple_H
36 
37 #include <OpenFOAM/Istream.H>
38 #include <OpenFOAM/Ostream.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 Tuple;
49 
50 template<class Type1, class Type2>
51 Istream& operator>>(Istream&, Tuple<Type1, Type2>&);
52 
53 template<class Type1, class Type2>
54 Ostream& operator<<(Ostream&, const Tuple<Type1, Type2>&);
55 
56 /*---------------------------------------------------------------------------*\
57  Class Tuple Declaration
58 \*---------------------------------------------------------------------------*/
59 
60 template<class Type1, class Type2>
61 class Tuple
62 {
63  // Private data
64 
65  Type1 first_;
66  Type2 second_;
67 
68 
69 public:
70 
71  // Constructors
72 
73  //- Null constructor for lists
74  inline Tuple()
75  {}
76 
77  //- Construct from components
78  inline Tuple(const Type1& first, const Type2& second)
79  :
80  first_(first),
81  second_(second)
82  {}
83 
84  //- Construct from Istream
85  inline Tuple(Istream& is)
86  {
87  // Read beginning of pair
88  is.readBegin("pair");
89 
90  is >> first_ >> second_;
91 
92  // Read end of pair
93  is.readEnd("pair");
94 
95  // Check state of Istream
96  is.check("Tuple::Tuple(Istream&)");
97  }
98 
99 
100  // Member Functions
101 
102  //- Return first
103  inline Type1 first() const
104  {
105  return first_;
106  }
107 
108  //- Return first
109  inline Type1& first()
110  {
111  return first_;
112  }
113 
114  //- Return second
115  inline Type2 second() const
116  {
117  return second_;
118  }
119 
120  //- Return second
121  inline Type2& second()
122  {
123  return second_;
124  }
125 
126  //- Return reverse pair
128  {
129  return Tuple<Type1, Type2>(second_, first_);
130  }
131 
132 
133  // Friend Operators
134 
135  inline friend bool operator==
136  (
137  const Tuple<Type1, Type2>& a,
138  const Tuple<Type1, Type2>& b
139  )
140  {
141  return
142  (
143  (a.first_ == b.first_) && (a.second_ == b.second_)
144  );
145  }
146 
147  inline friend bool operator!=
148  (
149  const Tuple<Type1, Type2>& a,
150  const Tuple<Type1, Type2>& b
151  )
152  {
153  return (!(a == b));
154  }
155 
156 
157  // IOstream Operators
158 
159  friend Istream& operator>> <Type1, Type2>
160  (
161  Istream& is,
163  );
164  friend Ostream& operator<< <Type1, Type2>
165  (
166  Ostream& os,
167  const Tuple<Type1, Type2>& p
168  );
169 };
170 
171 
172 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
173 
174 template<class Type1, class Type2>
176 {
177  // Read beginning of Tuple<Type, Type>
178  is.readBegin("Tuple<Type, Type>");
179 
180  is >> p.first_ >> p.second_;
181 
182  // Read end of Tuple<Type, Type>
183  is.readEnd("Tuple<Type, Type>");
184 
185  // Check state of Ostream
186  is.check("Istream& operator>>(Istream&, Tuple<Type, Type>&)");
187 
188  return is;
189 }
190 
191 template<class Type1, class Type2>
192 Ostream& operator<<(Ostream& os, const Tuple<Type1, Type2>& p)
193 {
194  os << token::BEGIN_LIST
195  << p.first_ << token::SPACE
196  << p.second_
197  << token::END_LIST;
198 
199  // Check state of Ostream
200  os.check("Ostream& operator<<(Ostream&, const Tuple<Type, Type>&)");
201 
202  return os;
203 }
204 
205 
206 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
207 
208 } // End namespace Foam
209 
210 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
211 
212 #endif
213 
214 // ************************ vim: set sw=4 sts=4 et: ************************ //