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

labelBits.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::labelBits
00026 
00027 Description
00028     A 29bits label and 3bits direction packed into single label
00029 
00030 SourceFiles
00031 
00032 \*---------------------------------------------------------------------------*/
00033 
00034 #ifndef labelBits_H
00035 #define labelBits_H
00036 
00037 #include <OpenFOAM/label.H>
00038 #include <OpenFOAM/uLabel.H>
00039 #include <OpenFOAM/direction.H>
00040 #include <OpenFOAM/error.H>
00041 
00042 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00043 
00044 namespace Foam
00045 {
00046 
00047 
00048 /*---------------------------------------------------------------------------*\
00049                          Class labelBits Declaration
00050 \*---------------------------------------------------------------------------*/
00051 
00052 class labelBits
00053 {
00054     // Private data
00055 
00056         label data_;
00057 
00058         inline static label pack(const label val, const direction bits)
00059         {
00060 #           ifdef FULLDEBUG
00061             if (bits > 7 || (((val<<3)>>3) != val))
00062             {
00063                 FatalErrorIn
00064                 (
00065                     "labelBits::pack(const label, const direction)"
00066                 )   << "Direction " << bits << " outside range 0..7"
00067                     << abort(FatalError);
00068             }
00069 #           endif
00070 
00071             return (val<<3) | bits;
00072         }
00073 
00074 public:
00075 
00076     // Constructors
00077 
00078         //- Construct null
00079         inline labelBits()
00080         {}
00081 
00082         //- Construct from components
00083         inline labelBits(const label val, const direction bits)
00084         :
00085              data_(pack(val, bits))
00086         {}
00087 
00088         //- Construct from Istream
00089         inline labelBits(Istream& is)
00090         {
00091             is >> data_;
00092         }
00093 
00094 
00095 
00096     // Member Functions
00097 
00098         inline label val() const
00099         {
00100             return data_ >> 3;
00101         }
00102 
00103         inline direction bits() const
00104         {
00105             return data_ & 7;
00106         }
00107 
00108         inline void setVal(const label val)
00109         {
00110             data_ = pack(val, bits());
00111         }
00112 
00113         inline void setBits(const direction bits)
00114         {
00115             data_ = pack(val(), bits);
00116         }
00117 
00118 
00119     // Member Operators
00120 
00121         friend inline bool operator==(const labelBits& a, const labelBits& b)
00122         {
00123             return a.data_ == b.data_;
00124         }
00125 
00126         friend inline bool operator!=(const labelBits& a, const labelBits& b)
00127         {
00128             return !(a == b);
00129         }
00130 
00131     // IOstream Operators
00132 
00133         friend inline Istream& operator>>(Istream& is, labelBits& lb)
00134         {
00135             return is >> lb.data_;
00136         }
00137 
00138         friend inline Ostream& operator<<(Ostream& os, const labelBits& lb)
00139         {
00140             return os << lb.data_;
00141         }
00142 };
00143 
00144 
00145 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00146 
00147 } // End namespace Foam
00148 
00149 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00150 
00151 #endif
00152 
00153 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines