OpenFOAM logo
The Open Source CFD Toolbox
  Programmer’s C++ Doxygen 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-2007 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     Tuple2
00027 
00028 Description
00029     A 2-tuple.
00030 
00031 \*---------------------------------------------------------------------------*/
00032 
00033 #ifndef Tuple2_H
00034 #define Tuple2_H
00035 
00036 #include "Istream.H"
00037 
00038 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00039 
00040 namespace Foam
00041 {
00042 
00043 // * * * * * * Forward declaration of template friend fuctions * * * * * * * //
00044 
00045 template<class Type1, class Type2>
00046 class Tuple2;
00047 
00048 template<class Type1, class Type2>
00049 Istream& operator>>(Istream&, Tuple2<Type1, Type2>&);
00050 
00051 template<class Type1, class Type2>
00052 Ostream& operator<<(Ostream&, const Tuple2<Type1, Type2>&);
00053 
00054 
00055 /*---------------------------------------------------------------------------*\
00056                             class Tuple2 Declaration
00057 \*---------------------------------------------------------------------------*/
00058 
00059 template<class Type1, class Type2>
00060 class Tuple2
00061 {
00062     // Private data
00063 
00064         Type1 f_;
00065         Type2 s_;
00066 
00067 
00068 public:
00069 
00070     // Constructors
00071 
00072         //- Null constructor for lists
00073         inline Tuple2()
00074         {}
00075 
00076         //- Construct from components
00077         inline Tuple2(const Type1& f, const Type2& s)
00078         :
00079             f_(f),
00080             s_(s)
00081         {}
00082 
00083         //- Construct from Istream
00084         inline Tuple2(Istream& is)
00085         {
00086             operator>>(is, *this);
00087         }
00088 
00089 
00090     // Member Functions
00091 
00092         //- Return first
00093         inline const Type1& first() const
00094         {
00095             return f_;
00096         }
00097 
00098         //- Return first
00099         inline Type1& first()
00100         {
00101             return f_;
00102         }
00103 
00104         //- Return second
00105         inline const Type2& second() const
00106         {
00107             return s_;
00108         }
00109 
00110         //- Return second
00111         inline Type2& second()
00112         {
00113             return s_;
00114         }
00115 
00116         //- Return reverse pair
00117         inline Tuple2<Type2, Type1> reverseTuple2() const
00118         {
00119             return Tuple2<Type2, Type1>(second(), first());
00120         }
00121 
00122 
00123     // Friend Operators
00124 
00125         inline friend bool operator==
00126         (
00127             const Tuple2<Type1, Type2>& a,
00128             const Tuple2<Type1, Type2>& b
00129         )
00130         {
00131             return
00132             (
00133                 (a.first() == b.first()) && (a.second() == b.second())
00134             );
00135         }
00136 
00137         inline friend bool operator!=
00138         (
00139             const Tuple2<Type1, Type2>& a,
00140             const Tuple2<Type1, Type2>& b
00141         )
00142         {
00143             return (!(a == b));
00144         }
00145 
00146 
00147     // IOstream operators
00148 
00149         //- Read Tuple2 from Istream, discarding contents of existing Tuple2.
00150         friend Istream& operator>> <Type1, Type2>
00151         (
00152             Istream& is,
00153             Tuple2<Type1, Type2>& t2
00154         );
00155 
00156         // Write Tuple2 to Ostream.
00157         friend Ostream& operator<< <Type1, Type2>
00158         (
00159             Ostream& os,
00160             const Tuple2<Type1, Type2>& t2
00161         );
00162 };
00163 
00164 
00165 template<class Type1, class Type2>
00166 inline Istream& operator>>(Istream& is, Tuple2<Type1, Type2>& t2)
00167 {
00168     is.readBegin("Tuple2");
00169     is >> t2.f_ >> t2.s_;
00170     is.readEnd("Tuple2");
00171 
00172     // Check state of Istream
00173     is.check("operator>>(Istream&, Tuple2<Type1, Type2>&)");
00174 
00175     return is;
00176 }
00177 
00178 
00179 template<class Type1, class Type2>
00180 inline Ostream& operator<<(Ostream& os, const Tuple2<Type1, Type2>& t2)
00181 {
00182     os  << token::BEGIN_LIST
00183         << t2.f_ << token::SPACE << t2.s_
00184         << token::END_LIST;
00185     
00186     return os;
00187 }
00188 
00189 
00190 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00191 
00192 } // End namespace Foam
00193 
00194 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00195 
00196 #endif
00197 
00198 // ************************************************************************* //
Copyright © 2000-2007 OpenCFD Ltd