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

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