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

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