OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

Tuple2.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     Foam::Tuple2
00027 
00028 Description
00029     A 2-tuple.
00030 
00031 SeeAlso
00032     Foam::Pair for storing two objects of identical types.
00033 
00034 \*---------------------------------------------------------------------------*/
00035 
00036 #ifndef Tuple2_H
00037 #define Tuple2_H
00038 
00039 #include "Istream.H"
00040 
00041 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00042 
00043 namespace Foam
00044 {
00045 
00046 // Forward declaration of friend functions and operators
00047 
00048 template<class Type1, class Type2>
00049 class Tuple2;
00050 
00051 template<class Type1, class Type2>
00052 inline bool operator==
00053 (
00054     const Tuple2<Type1, Type2>&,
00055     const Tuple2<Type1, Type2>&
00056 );
00057 
00058 template<class Type1, class Type2>
00059 inline bool operator!=
00060 (
00061     const Tuple2<Type1, Type2>&,
00062     const Tuple2<Type1, Type2>&
00063 );
00064 
00065 template<class Type1, class Type2>
00066 inline Istream& operator>>(Istream&, Tuple2<Type1, Type2>&);
00067 
00068 template<class Type1, class Type2>
00069 inline Ostream& operator<<(Ostream&, const Tuple2<Type1, Type2>&);
00070 
00071 
00072 /*---------------------------------------------------------------------------*\
00073                            class Tuple2 Declaration
00074 \*---------------------------------------------------------------------------*/
00075 
00076 template<class Type1, class Type2>
00077 class Tuple2
00078 {
00079     // Private data
00080 
00081         Type1 f_;
00082         Type2 s_;
00083 
00084 
00085 public:
00086 
00087     // Constructors
00088 
00089         //- Null constructor for lists
00090         inline Tuple2()
00091         {}
00092 
00093         //- Construct from components
00094         inline Tuple2(const Type1& f, const Type2& s)
00095         :
00096             f_(f),
00097             s_(s)
00098         {}
00099 
00100         //- Construct from Istream
00101         inline Tuple2(Istream& is)
00102         {
00103             is >> *this;
00104         }
00105 
00106 
00107     // Member Functions
00108 
00109         //- Return first
00110         inline const Type1& first() const
00111         {
00112             return f_;
00113         }
00114 
00115         //- Return first
00116         inline Type1& first()
00117         {
00118             return f_;
00119         }
00120 
00121         //- Return second
00122         inline const Type2& second() const
00123         {
00124             return s_;
00125         }
00126 
00127         //- Return second
00128         inline Type2& second()
00129         {
00130             return s_;
00131         }
00132 
00133         //- Return reverse pair
00134         inline Tuple2<Type2, Type1> reverseTuple2() const
00135         {
00136             return Tuple2<Type2, Type1>(second(), first());
00137         }
00138 
00139 
00140     // Friend Operators
00141 
00142         friend bool operator== <Type1, Type2>
00143         (
00144             const Tuple2<Type1, Type2>& a,
00145             const Tuple2<Type1, Type2>& b
00146         );
00147 
00148         friend bool operator!= <Type1, Type2>
00149         (
00150             const Tuple2<Type1, Type2>& a,
00151             const Tuple2<Type1, Type2>& b
00152         );
00153 
00154 
00155     // IOstream operators
00156 
00157         //- Read Tuple2 from Istream, discarding contents of existing Tuple2.
00158         friend Istream& operator>> <Type1, Type2>
00159         (
00160             Istream& is,
00161             Tuple2<Type1, Type2>& t2
00162         );
00163 
00164         // Write Tuple2 to Ostream.
00165         friend Ostream& operator<< <Type1, Type2>
00166         (
00167             Ostream& os,
00168             const Tuple2<Type1, Type2>& t2
00169         );
00170 };
00171 
00172 
00173 template<class Type1, class Type2>
00174 inline bool operator==
00175 (
00176     const Tuple2<Type1, Type2>& a,
00177     const Tuple2<Type1, Type2>& b
00178 )
00179 {
00180     return (a.first() == b.first() && a.second() == b.second());
00181 }
00182 
00183 
00184 template<class Type1, class Type2>
00185 inline bool operator!=
00186 (
00187     const Tuple2<Type1, Type2>& a,
00188     const Tuple2<Type1, Type2>& b
00189 )
00190 {
00191     return !(a == b);
00192 }
00193 
00194 
00195 template<class Type1, class Type2>
00196 inline Istream& operator>>(Istream& is, Tuple2<Type1, Type2>& t2)
00197 {
00198     is.readBegin("Tuple2");
00199     is >> t2.f_ >> t2.s_;
00200     is.readEnd("Tuple2");
00201 
00202     // Check state of Istream
00203     is.check("operator>>(Istream&, Tuple2<Type1, Type2>&)");
00204 
00205     return is;
00206 }
00207 
00208 
00209 template<class Type1, class Type2>
00210 inline Ostream& operator<<(Ostream& os, const Tuple2<Type1, Type2>& t2)
00211 {
00212     os  << token::BEGIN_LIST
00213         << t2.f_ << token::SPACE << t2.s_
00214         << token::END_LIST;
00215 
00216     return os;
00217 }
00218 
00219 
00220 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00221 
00222 } // End namespace Foam
00223 
00224 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00225 
00226 #endif
00227 
00228 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd