initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / primitives / Tuple2 / Tuple2.H
blobcac459587279012ae41af4cea6a53c9ddedb1c3b
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Class
26     Foam::Tuple2
28 Description
29     A 2-tuple.
31 SeeAlso
32     Foam::Pair for storing two objects of identical types.
34 \*---------------------------------------------------------------------------*/
36 #ifndef Tuple2_H
37 #define Tuple2_H
39 #include "Istream.H"
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 namespace Foam
46 // Forward declaration of friend functions and operators
48 template<class Type1, class Type2>
49 class Tuple2;
51 template<class Type1, class Type2>
52 inline bool operator==
54     const Tuple2<Type1, Type2>&,
55     const Tuple2<Type1, Type2>&
58 template<class Type1, class Type2>
59 inline bool operator!=
61     const Tuple2<Type1, Type2>&,
62     const Tuple2<Type1, Type2>&
65 template<class Type1, class Type2>
66 inline Istream& operator>>(Istream&, Tuple2<Type1, Type2>&);
68 template<class Type1, class Type2>
69 inline Ostream& operator<<(Ostream&, const Tuple2<Type1, Type2>&);
72 /*---------------------------------------------------------------------------*\
73                            class Tuple2 Declaration
74 \*---------------------------------------------------------------------------*/
76 template<class Type1, class Type2>
77 class Tuple2
79     // Private data
81         Type1 f_;
82         Type2 s_;
85 public:
87     // Constructors
89         //- Null constructor for lists
90         inline Tuple2()
91         {}
93         //- Construct from components
94         inline Tuple2(const Type1& f, const Type2& s)
95         :
96             f_(f),
97             s_(s)
98         {}
100         //- Construct from Istream
101         inline Tuple2(Istream& is)
102         {
103             is >> *this;
104         }
107     // Member Functions
109         //- Return first
110         inline const Type1& first() const
111         {
112             return f_;
113         }
115         //- Return first
116         inline Type1& first()
117         {
118             return f_;
119         }
121         //- Return second
122         inline const Type2& second() const
123         {
124             return s_;
125         }
127         //- Return second
128         inline Type2& second()
129         {
130             return s_;
131         }
133         //- Return reverse pair
134         inline Tuple2<Type2, Type1> reverseTuple2() const
135         {
136             return Tuple2<Type2, Type1>(second(), first());
137         }
140     // Friend Operators
142         friend bool operator== <Type1, Type2>
143         (
144             const Tuple2<Type1, Type2>& a,
145             const Tuple2<Type1, Type2>& b
146         );
148         friend bool operator!= <Type1, Type2>
149         (
150             const Tuple2<Type1, Type2>& a,
151             const Tuple2<Type1, Type2>& b
152         );
155     // IOstream operators
157         //- Read Tuple2 from Istream, discarding contents of existing Tuple2.
158         friend Istream& operator>> <Type1, Type2>
159         (
160             Istream& is,
161             Tuple2<Type1, Type2>& t2
162         );
164         // Write Tuple2 to Ostream.
165         friend Ostream& operator<< <Type1, Type2>
166         (
167             Ostream& os,
168             const Tuple2<Type1, Type2>& t2
169         );
173 template<class Type1, class Type2>
174 inline bool operator==
176     const Tuple2<Type1, Type2>& a,
177     const Tuple2<Type1, Type2>& b
180     return (a.first() == b.first() && a.second() == b.second());
184 template<class Type1, class Type2>
185 inline bool operator!=
187     const Tuple2<Type1, Type2>& a,
188     const Tuple2<Type1, Type2>& b
191     return !(a == b);
195 template<class Type1, class Type2>
196 inline Istream& operator>>(Istream& is, Tuple2<Type1, Type2>& t2)
198     is.readBegin("Tuple2");
199     is >> t2.f_ >> t2.s_;
200     is.readEnd("Tuple2");
202     // Check state of Istream
203     is.check("operator>>(Istream&, Tuple2<Type1, Type2>&)");
205     return is;
209 template<class Type1, class Type2>
210 inline Ostream& operator<<(Ostream& os, const Tuple2<Type1, Type2>& t2)
212     os  << token::BEGIN_LIST
213         << t2.f_ << token::SPACE << t2.s_
214         << token::END_LIST;
216     return os;
220 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
222 } // End namespace Foam
224 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
226 #endif
228 // ************************************************************************* //