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

Pair.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::Pair
00027 
00028 Description
00029     An ordered pair of two objects of type <T> with first() and second()
00030     elements.
00031 
00032 SeeAlso
00033     Foam::Tuple2 for storing two objects of dissimilar types.
00034 
00035 \*---------------------------------------------------------------------------*/
00036 
00037 #ifndef Pair_H
00038 #define Pair_H
00039 
00040 #include "FixedList.H"
00041 #include "Istream.H"
00042 
00043 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00044 
00045 namespace Foam
00046 {
00047 
00048 /*---------------------------------------------------------------------------*\
00049                             Class Pair Declaration
00050 \*---------------------------------------------------------------------------*/
00051 
00052 template<class Type>
00053 class Pair
00054 :
00055     public FixedList<Type, 2>
00056 {
00057 
00058 public:
00059 
00060     // Constructors
00061 
00062         //- Null constructor
00063         inline Pair()
00064         {}
00065 
00066         //- Construct from components
00067         inline Pair(const Type& f, const Type& s)
00068         {
00069             first() = f;
00070             second() = s;
00071         }
00072 
00073         //- Construct from Istream
00074         inline Pair(Istream& is)
00075         :
00076             FixedList<Type, 2>(is)
00077         {}
00078 
00079 
00080     // Member Functions
00081 
00082         //- Return first
00083         inline const Type& first() const
00084         {
00085             return this->operator[](0);
00086         }
00087 
00088         //- Return first
00089         inline Type& first()
00090         {
00091             return this->operator[](0);
00092         }
00093 
00094         //- Return second
00095         inline const Type& second() const
00096         {
00097             return this->operator[](1);
00098         }
00099 
00100         //- Return second
00101         inline Type& second()
00102         {
00103             return this->operator[](1);
00104         }
00105 
00106         //- Return reverse pair
00107         inline Pair<Type> reversePair() const
00108         {
00109             return Pair<Type>(second(), first());
00110         }
00111 
00112         //- Return other
00113         inline const Type& other(const Type& a) const
00114         {
00115             if (first() == second())
00116             {
00117                 FatalErrorIn("Pair<Type>::other(const Type&) const")
00118                     << "Call to other only valid for Pair with differing"
00119                     << " elements:" << *this << abort(FatalError);
00120             }
00121             else if (first() == a)
00122             {
00123                 return second();
00124             }
00125             else
00126             {
00127                 if (second() != a)
00128                 {
00129                     FatalErrorIn("Pair<Type>::other(const Type&) const")
00130                         << "Pair " << *this
00131                         << " does not contain " << a << abort(FatalError);
00132                 }
00133                 return first();
00134             }
00135         }
00136 
00137 
00138         //- compare Pairs
00139         //  -  0: different
00140         //  - +1: identical
00141         //  - -1: same pair, but reversed order
00142         static inline int compare(const Pair<Type>& a, const Pair<Type>& b)
00143         {
00144             if (a[0] == b[0] && a[1] == b[1])
00145             {
00146                 return 1;
00147             }
00148             else if (a[0] == b[1] && a[1] == b[0])
00149             {
00150                 return -1;
00151             }
00152             else
00153             {
00154                 return 0;
00155             }
00156         }
00157 
00158 
00159     // Friend Operators
00160 
00161         friend bool operator==(const Pair<Type>& a, const Pair<Type>& b)
00162         {
00163             return (a.first() == b.first() && a.second() == b.second());
00164         }
00165 
00166         friend bool operator!=(const Pair<Type>& a, const Pair<Type>& b)
00167         {
00168             return !(a == b);
00169         }
00170 };
00171 
00172 
00173 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00174 
00175 } // End namespace Foam
00176 
00177 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00178 
00179 #endif
00180 
00181 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd