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

Tuple.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::Tuple
00027 
00028 Description
00029     A 2 Tuple. Differs from Tuple in that the two elements can be different
00030     type.
00031 
00032 
00033 \*---------------------------------------------------------------------------*/
00034 
00035 #ifndef Tuple_H
00036 #define Tuple_H
00037 
00038 #include "Istream.H"
00039 #include "Ostream.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 Tuple;
00050 
00051 template<class Type1, class Type2>
00052 Istream& operator>>(Istream&, Tuple<Type1, Type2>&);
00053 
00054 template<class Type1, class Type2>
00055 Ostream& operator<<(Ostream&, const Tuple<Type1, Type2>&);
00056 
00057 /*---------------------------------------------------------------------------*\
00058                             Class Tuple Declaration
00059 \*---------------------------------------------------------------------------*/
00060 
00061 template<class Type1, class Type2>
00062 class Tuple
00063 {
00064     // Private data
00065 
00066        Type1 first_;
00067        Type2 second_;
00068 
00069 
00070 public:
00071 
00072     // Constructors
00073 
00074         //- Null constructor for lists
00075         inline Tuple()
00076         {}
00077 
00078         //- Construct from components
00079         inline Tuple(const Type1& first, const Type2& second)
00080         :
00081             first_(first),
00082             second_(second)
00083         {}
00084 
00085         //- Construct from Istream
00086         inline Tuple(Istream& is)
00087         {
00088             // Read beginning of pair
00089             is.readBegin("pair");
00090 
00091             is >> first_ >> second_;
00092 
00093             // Read end of pair
00094             is.readEnd("pair");
00095 
00096             // Check state of Istream
00097             is.check("Tuple::Tuple(Istream&)");
00098         }
00099 
00100 
00101     // Member Functions
00102 
00103         //- Return first
00104         inline Type1 first() const
00105         {
00106             return first_;
00107         }
00108 
00109         //- Return first
00110         inline Type1& first()
00111         {
00112             return first_;
00113         }
00114 
00115         //- Return second
00116         inline Type2 second() const
00117         {
00118             return second_;
00119         }
00120 
00121         //- Return second
00122         inline Type2& second()
00123         {
00124             return second_;
00125         }
00126 
00127         //- Return reverse pair
00128         inline Tuple<Type1, Type2> reverseTuple() const
00129         {
00130             return Tuple<Type1, Type2>(second_, first_);
00131         }
00132 
00133 
00134     // Friend Operators
00135 
00136         inline friend bool operator==
00137         (
00138             const Tuple<Type1, Type2>& a,
00139             const Tuple<Type1, Type2>& b
00140         )
00141         {
00142             return
00143             (
00144                 (a.first_ == b.first_) && (a.second_ == b.second_)
00145             );
00146         }
00147 
00148         inline friend bool operator!=
00149         (
00150             const Tuple<Type1, Type2>& a,
00151             const Tuple<Type1, Type2>& b
00152         )
00153         {
00154             return (!(a == b));
00155         }
00156 
00157 
00158     // IOstream Operators
00159 
00160         friend Istream& operator>> <Type1, Type2>
00161         (
00162             Istream& is,
00163             Tuple<Type1, Type2>& p
00164         );
00165         friend Ostream& operator<< <Type1, Type2>
00166         (
00167             Ostream& os,
00168             const Tuple<Type1, Type2>& p
00169         );
00170 };
00171 
00172 
00173 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00174 
00175 template<class Type1, class Type2>
00176 Istream& operator>>(Istream& is, Tuple<Type1, Type2>& p)
00177 {
00178     // Read beginning of Tuple<Type, Type>
00179     is.readBegin("Tuple<Type, Type>");
00180 
00181     is >> p.first_ >> p.second_;
00182 
00183     // Read end of Tuple<Type, Type>
00184     is.readEnd("Tuple<Type, Type>");
00185 
00186     // Check state of Ostream
00187     is.check("Istream& operator>>(Istream&, Tuple<Type, Type>&)");
00188 
00189     return is;
00190 }
00191 
00192 template<class Type1, class Type2>
00193 Ostream& operator<<(Ostream& os, const Tuple<Type1, Type2>& p)
00194 {
00195     os  << token::BEGIN_LIST
00196         << p.first_ << token::SPACE
00197         << p.second_
00198         << token::END_LIST;
00199 
00200     // Check state of Ostream
00201     os.check("Ostream& operator<<(Ostream&, const Tuple<Type, Type>&)");
00202 
00203     return os;
00204 }
00205 
00206 
00207 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00208 
00209 } // End namespace Foam
00210 
00211 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00212 
00213 #endif
00214 
00215 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd