FreeFOAM The Cross-Platform CFD Toolkit
Pair.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::Pair
26 
27 Description
28  An ordered pair of two objects of type <T> with first() and second()
29  elements.
30 
31 SeeAlso
32  Foam::Tuple2 for storing two objects of dissimilar types.
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef Pair_H
37 #define Pair_H
38 
39 #include <OpenFOAM/FixedList.H>
40 #include <OpenFOAM/Istream.H>
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 /*---------------------------------------------------------------------------*\
48  Class Pair Declaration
49 \*---------------------------------------------------------------------------*/
50 
51 template<class Type>
52 class Pair
53 :
54  public FixedList<Type, 2>
55 {
56 
57 public:
58 
59  // Constructors
60 
61  //- Null constructor
62  inline Pair()
63  {}
64 
65  //- Construct from components
66  inline Pair(const Type& f, const Type& s)
67  {
68  first() = f;
69  second() = s;
70  }
71 
72  //- Construct from Istream
73  inline Pair(Istream& is)
74  :
75  FixedList<Type, 2>(is)
76  {}
77 
78 
79  // Member Functions
80 
81  //- Return first
82  inline const Type& first() const
83  {
84  return this->operator[](0);
85  }
86 
87  //- Return first
88  inline Type& first()
89  {
90  return this->operator[](0);
91  }
92 
93  //- Return second
94  inline const Type& second() const
95  {
96  return this->operator[](1);
97  }
98 
99  //- Return second
100  inline Type& second()
101  {
102  return this->operator[](1);
103  }
104 
105  //- Return reverse pair
106  inline Pair<Type> reversePair() const
107  {
108  return Pair<Type>(second(), first());
109  }
110 
111  //- Return other
112  inline const Type& other(const Type& a) const
113  {
114  if (first() == second())
115  {
116  FatalErrorIn("Pair<Type>::other(const Type&) const")
117  << "Call to other only valid for Pair with differing"
118  << " elements:" << *this << abort(FatalError);
119  }
120  else if (first() == a)
121  {
122  return second();
123  }
124  else
125  {
126  if (second() != a)
127  {
128  FatalErrorIn("Pair<Type>::other(const Type&) const")
129  << "Pair " << *this
130  << " does not contain " << a << abort(FatalError);
131  }
132  return first();
133  }
134  }
135 
136 
137  //- compare Pairs
138  // - 0: different
139  // - +1: identical
140  // - -1: same pair, but reversed order
141  static inline int compare(const Pair<Type>& a, const Pair<Type>& b)
142  {
143  if (a[0] == b[0] && a[1] == b[1])
144  {
145  return 1;
146  }
147  else if (a[0] == b[1] && a[1] == b[0])
148  {
149  return -1;
150  }
151  else
152  {
153  return 0;
154  }
155  }
156 
157 
158  // Friend Operators
159 
160  friend bool operator==(const Pair<Type>& a, const Pair<Type>& b)
161  {
162  return (a.first() == b.first() && a.second() == b.second());
163  }
164 
165  friend bool operator!=(const Pair<Type>& a, const Pair<Type>& b)
166  {
167  return !(a == b);
168  }
169 };
170 
171 
172 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
173 
174 } // End namespace Foam
175 
176 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
177 
178 #endif
179 
180 // ************************ vim: set sw=4 sts=4 et: ************************ //