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

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