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

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