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

label.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 Typedef
00026     Foam::label
00027 
00028 Description
00029     A label is an int/long/long long depending on the range desired.
00030 
00031     A readLabel function is defined so that label can be constructed from
00032     Istream.
00033 
00034 \*---------------------------------------------------------------------------*/
00035 
00036 #ifndef label_H
00037 #define label_H
00038 
00039 #include <climits>
00040 #include <cstdlib>
00041 
00042 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00043 
00044 
00045 #if FOAM_LABEL64
00046 #    define FOAM_LABEL_MAX 9000000000000000000
00047 #else
00048 #    define FOAM_LABEL_MAX 2000000000
00049 #endif
00050 
00051 
00052 #if INT_MAX > FOAM_LABEL_MAX
00053 
00054 // Define label as an int
00055 
00056 # undef  FOAM_LABEL_MAX
00057 # define FOAM_LABEL_MAX INT_MAX
00058 
00059 # include "int.H"
00060 
00061 namespace Foam
00062 {
00063     typedef int label;
00064 
00065     static const label labelMin = INT_MIN;
00066     static const label labelMax = INT_MAX;
00067 
00068     inline label readLabel(Istream& is)
00069     {
00070         return readInt(is);
00071     }
00072 
00073 } // End namespace Foam
00074 
00075 
00076 #elif LONG_MAX > FOAM_LABEL_MAX
00077 // Define label as a long
00078 
00079 # undef  FOAM_LABEL_MAX
00080 # define FOAM_LABEL_MAX LONG_MAX
00081 
00082 # include "int.H"
00083 # include "long.H"
00084 
00085 namespace Foam
00086 {
00087     typedef long label;
00088 
00089     static const label labelMin = LONG_MIN;
00090     static const label labelMax = LONG_MAX;
00091 
00092     inline label readLabel(Istream& is)
00093     {
00094         return readLong(is);
00095     }
00096 
00097 } // End namespace Foam
00098 
00099 
00100 #elif LLONG_MAX > FOAM_LABEL_MAX
00101 
00102 // Define label as a long long
00103 
00104 # undef  FOAM_LABEL_MAX
00105 # define FOAM_LABEL_MAX LLONG_MAX
00106 
00107 # include "int.H"
00108 # include "long.H"
00109 # include "longLong.H"
00110 
00111 namespace Foam
00112 {
00113     typedef long long label;
00114 
00115     static const label labelMin = LLONG_MIN;
00116     static const label labelMax = LLONG_MAX;
00117 
00118     inline label readLabel(Istream& is)
00119     {
00120         return readLongLong(is);
00121     }
00122 
00123 } // End namespace Foam
00124 
00125 #endif
00126 
00127 
00128 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00129 
00130 #include "pTraits.H"
00131 #include "direction.H"
00132 
00133 namespace Foam
00134 {
00135 
00136 //- template specialization for pTraits<label>
00137 template<>
00138 class pTraits<label>
00139 {
00140     label p_;
00141 
00142 public:
00143 
00144     //- Component type
00145     typedef label cmptType;
00146 
00147     // Member constants
00148 
00149         enum
00150         {
00151             dim = 3,         // Dimensionality of space
00152             rank = 0,        // Rank of label is 0
00153             nComponents = 1  // Number of components in label is 1
00154         };
00155 
00156     // Static data members
00157 
00158         static const char* const typeName;
00159         static const char* componentNames[];
00160         static const label zero;
00161         static const label one;
00162         static const label min;
00163         static const label max;
00164 
00165     // Constructors
00166 
00167         //- Construct from Istream
00168         pTraits(Istream&);
00169 
00170     // Member Functions
00171 
00172         operator label() const
00173         {
00174             return p_;
00175         }
00176 };
00177 
00178 
00179 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00180 
00181 //- Raise one label to the power of another
00182 label pow(label a, label b);
00183 
00184 //- Evaluate n! : n <= 12
00185 label factorial(label n);
00186 
00187 
00188 #define MAXMIN(retType, type1, type2)              \
00189                                                    \
00190 inline retType max(const type1 s1, const type2 s2) \
00191 {                                                  \
00192     return (s1 > s2)? s1: s2;                      \
00193 }                                                  \
00194                                                    \
00195 inline retType min(const type1 s1, const type2 s2) \
00196 {                                                  \
00197     return (s1 < s2)? s1: s2;                      \
00198 }
00199 
00200 
00201 MAXMIN(char, char, char)
00202 MAXMIN(short, short, short)
00203 MAXMIN(int, int, int)
00204 MAXMIN(long, long, long)
00205 MAXMIN(long long, long long, long long)
00206 
00207 MAXMIN(unsigned char, unsigned char, unsigned char)
00208 MAXMIN(unsigned short, unsigned short, unsigned short)
00209 MAXMIN(unsigned int, unsigned int, unsigned int)
00210 MAXMIN(unsigned long, unsigned long, unsigned long)
00211 MAXMIN(unsigned long long, unsigned long long, unsigned long long)
00212 
00213 MAXMIN(long, int, long)
00214 MAXMIN(long long, int, long long)
00215 MAXMIN(long long, long long, int)
00216 
00217 inline label& setComponent(label& l, const direction)
00218 {
00219     return l;
00220 }
00221 
00222 inline label component(const label l, const direction)
00223 {
00224     return l;
00225 }
00226 
00227 inline label mag(const label l)
00228 {
00229     return ::abs(l);
00230 }
00231 
00232 inline label sign(const label s)
00233 {
00234     return (s >= 0)? 1: -1;
00235 }
00236 
00237 inline label pos(const label s)
00238 {
00239     return (s >= 0)? 1: 0;
00240 }
00241 
00242 inline label neg(const label s)
00243 {
00244     return (s < 0)? 1: 0;
00245 }
00246 
00247 
00248 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00249 
00250 } // End namespace Foam
00251 
00252 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00253 
00254 #endif
00255 
00256 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd