FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 Class
00025     Foam::Tuple2
00026 
00027 Description
00028     A 2-tuple.
00029 
00030 SeeAlso
00031     Foam::Pair for storing two objects of identical types.
00032 
00033 \*---------------------------------------------------------------------------*/
00034 
00035 #ifndef Tuple2_H
00036 #define Tuple2_H
00037 
00038 #include <OpenFOAM/Istream.H>
00039 
00040 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00041 
00042 namespace Foam
00043 {
00044 
00045 // Forward declaration of friend functions and operators
00046 
00047 template<class Type1, class Type2>
00048 class Tuple2;
00049 
00050 template<class Type1, class Type2>
00051 inline bool operator==
00052 (
00053     const Tuple2<Type1, Type2>&,
00054     const Tuple2<Type1, Type2>&
00055 );
00056 
00057 template<class Type1, class Type2>
00058 inline bool operator!=
00059 (
00060     const Tuple2<Type1, Type2>&,
00061     const Tuple2<Type1, Type2>&
00062 );
00063 
00064 template<class Type1, class Type2>
00065 inline Istream& operator>>(Istream&, Tuple2<Type1, Type2>&);
00066 
00067 template<class Type1, class Type2>
00068 inline Ostream& operator<<(Ostream&, const Tuple2<Type1, Type2>&);
00069 
00070 
00071 /*---------------------------------------------------------------------------*\
00072                            class Tuple2 Declaration
00073 \*---------------------------------------------------------------------------*/
00074 
00075 template<class Type1, class Type2>
00076 class Tuple2
00077 {
00078     // Private data
00079 
00080         Type1 f_;
00081         Type2 s_;
00082 
00083 
00084 public:
00085 
00086     // Constructors
00087 
00088         //- Null constructor for lists
00089         inline Tuple2()
00090         {}
00091 
00092         //- Construct from components
00093         inline Tuple2(const Type1& f, const Type2& s)
00094         :
00095             f_(f),
00096             s_(s)
00097         {}
00098 
00099         //- Construct from Istream
00100         inline Tuple2(Istream& is)
00101         {
00102             is >> *this;
00103         }
00104 
00105 
00106     // Member Functions
00107 
00108         //- Return first
00109         inline const Type1& first() const
00110         {
00111             return f_;
00112         }
00113 
00114         //- Return first
00115         inline Type1& first()
00116         {
00117             return f_;
00118         }
00119 
00120         //- Return second
00121         inline const Type2& second() const
00122         {
00123             return s_;
00124         }
00125 
00126         //- Return second
00127         inline Type2& second()
00128         {
00129             return s_;
00130         }
00131 
00132         //- Return reverse pair
00133         inline Tuple2<Type2, Type1> reverseTuple2() const
00134         {
00135             return Tuple2<Type2, Type1>(second(), first());
00136         }
00137 
00138 
00139     // Friend Operators
00140 
00141         friend bool operator== <Type1, Type2>
00142         (
00143             const Tuple2<Type1, Type2>& a,
00144             const Tuple2<Type1, Type2>& b
00145         );
00146 
00147         friend bool operator!= <Type1, Type2>
00148         (
00149             const Tuple2<Type1, Type2>& a,
00150             const Tuple2<Type1, Type2>& b
00151         );
00152 
00153 
00154     // IOstream operators
00155 
00156         //- Read Tuple2 from Istream, discarding contents of existing Tuple2.
00157         friend Istream& operator>> <Type1, Type2>
00158         (
00159             Istream& is,
00160             Tuple2<Type1, Type2>& t2
00161         );
00162 
00163         // Write Tuple2 to Ostream.
00164         friend Ostream& operator<< <Type1, Type2>
00165         (
00166             Ostream& os,
00167             const Tuple2<Type1, Type2>& t2
00168         );
00169 };
00170 
00171 
00172 template<class Type1, class Type2>
00173 inline bool operator==
00174 (
00175     const Tuple2<Type1, Type2>& a,
00176     const Tuple2<Type1, Type2>& b
00177 )
00178 {
00179     return (a.first() == b.first() && a.second() == b.second());
00180 }
00181 
00182 
00183 template<class Type1, class Type2>
00184 inline bool operator!=
00185 (
00186     const Tuple2<Type1, Type2>& a,
00187     const Tuple2<Type1, Type2>& b
00188 )
00189 {
00190     return !(a == b);
00191 }
00192 
00193 
00194 template<class Type1, class Type2>
00195 inline Istream& operator>>(Istream& is, Tuple2<Type1, Type2>& t2)
00196 {
00197     is.readBegin("Tuple2");
00198     is >> t2.f_ >> t2.s_;
00199     is.readEnd("Tuple2");
00200 
00201     // Check state of Istream
00202     is.check("operator>>(Istream&, Tuple2<Type1, Type2>&)");
00203 
00204     return is;
00205 }
00206 
00207 
00208 template<class Type1, class Type2>
00209 inline Ostream& operator<<(Ostream& os, const Tuple2<Type1, Type2>& t2)
00210 {
00211     os  << token::BEGIN_LIST
00212         << t2.f_ << token::SPACE << t2.s_
00213         << token::END_LIST;
00214 
00215     return os;
00216 }
00217 
00218 
00219 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00220 
00221 } // End namespace Foam
00222 
00223 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00224 
00225 #endif
00226 
00227 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines