FreeFOAM The Cross-Platform CFD Toolkit
label.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 Typedef
25  Foam::label
26 
27 Description
28  A label is an int/long/long long depending on the range desired.
29 
30  A readLabel function is defined so that label can be constructed from
31  Istream.
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef label_H
36 #define label_H
37 
38 #include <climits>
39 #include <cstdlib>
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 
44 #if FOAM_LABEL64
45 # define FOAM_LABEL_MAX 9000000000000000000
46 #else
47 # define FOAM_LABEL_MAX 2000000000
48 #endif
49 
50 
51 #if INT_MAX > FOAM_LABEL_MAX
52 
53 // Define label as an int
54 
55 # undef FOAM_LABEL_MAX
56 # define FOAM_LABEL_MAX INT_MAX
57 
58 # include <OpenFOAM/int.H>
59 
60 namespace Foam
61 {
62  typedef int label;
63 
64  static const label labelMin = INT_MIN;
65  static const label labelMax = INT_MAX;
66 
67  inline label readLabel(Istream& is)
68  {
69  return readInt(is);
70  }
71 
72 } // End namespace Foam
73 
74 
75 #elif LONG_MAX > FOAM_LABEL_MAX
76 // Define label as a long
77 
78 # undef FOAM_LABEL_MAX
79 # define FOAM_LABEL_MAX LONG_MAX
80 
81 # include <OpenFOAM/int.H>
82 # include <OpenFOAM/long.H>
83 
84 namespace Foam
85 {
86  typedef long label;
87 
88  static const label labelMin = LONG_MIN;
89  static const label labelMax = LONG_MAX;
90 
91  inline label readLabel(Istream& is)
92  {
93  return readLong(is);
94  }
95 
96 } // End namespace Foam
97 
98 
99 #elif LLONG_MAX > FOAM_LABEL_MAX
100 
101 // Define label as a long long
102 
103 # undef FOAM_LABEL_MAX
104 # define FOAM_LABEL_MAX LLONG_MAX
105 
106 # include <OpenFOAM/int.H>
107 # include <OpenFOAM/long.H>
108 # include <OpenFOAM/longLong.H>
109 
110 namespace Foam
111 {
112  typedef long long label;
113 
114  static const label labelMin = LLONG_MIN;
115  static const label labelMax = LLONG_MAX;
116 
117  inline label readLabel(Istream& is)
118  {
119  return readLongLong(is);
120  }
121 
122 } // End namespace Foam
123 
124 #endif
125 
126 
127 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
128 
129 #include <OpenFOAM/pTraits.H>
130 #include <OpenFOAM/direction.H>
131 
132 namespace Foam
133 {
134 
135 //- template specialization for pTraits<label>
136 template<>
137 class pTraits<label>
138 {
139  label p_;
140 
141 public:
142 
143  //- Component type
144  typedef label cmptType;
145 
146  // Member constants
147 
148  enum
149  {
150  dim = 3, // Dimensionality of space
151  rank = 0, // Rank of label is 0
152  nComponents = 1 // Number of components in label is 1
153  };
154 
155 
156  // Static data members
158  static const char* const typeName;
159  static const char* componentNames[];
160  static const label zero;
161  static const label one;
162  static const label min;
163  static const label max;
164 
165 
166  // Constructors
167 
168  //- Construct from label
169  pTraits(const label l)
170  {
171  p_ = l;
172  }
173 
174  //- Construct from Istream
175  pTraits(Istream&);
176 
177 
178  // Member Functions
180  operator label() const
181  {
182  return p_;
183  }
185  operator label&()
186  {
187  return p_;
188  }
189 };
190 
191 
192 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
193 
194 //- Raise one label to the power of another
195 label pow(label a, label b);
196 
197 //- Evaluate n! : n <= 12
198 label factorial(label n);
199 
201 #define MAXMIN(retType, type1, type2) \
202  \
203 inline retType max(const type1 s1, const type2 s2) \
204 { \
205  return (s1 > s2)? s1: s2; \
206 } \
207  \
208 inline retType min(const type1 s1, const type2 s2) \
209 { \
210  return (s1 < s2)? s1: s2; \
211 }
212 
214 MAXMIN(char, char, char)
215 MAXMIN(short, short, short)
216 MAXMIN(int, int, int)
217 MAXMIN(long, long, long)
218 MAXMIN(long long, long long, long long)
220 MAXMIN(unsigned char, unsigned char, unsigned char)
221 MAXMIN(unsigned short, unsigned short, unsigned short)
222 MAXMIN(unsigned int, unsigned int, unsigned int)
223 MAXMIN(unsigned long, unsigned long, unsigned long)
224 MAXMIN(unsigned long long, unsigned long long, unsigned long long)
226 MAXMIN(long, int, long)
227 MAXMIN(long long, int, long long)
228 MAXMIN(long long, long long, int)
230 inline label& setComponent(label& l, const direction)
231 {
232  return l;
233 }
235 inline label component(const label l, const direction)
236 {
237  return l;
238 }
240 inline label mag(const label l)
241 {
242  return ::abs(l);
243 }
245 inline label sign(const label s)
246 {
247  return (s >= 0)? 1: -1;
248 }
250 inline label pos(const label s)
251 {
252  return (s >= 0)? 1: 0;
253 }
255 inline label neg(const label s)
256 {
257  return (s < 0)? 1: 0;
258 }
259 
260 
261 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
262 
263 } // End namespace Foam
264 
265 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
266 
267 #endif
268 
269 // ************************ vim: set sw=4 sts=4 et: ************************ //