FreeFOAM The Cross-Platform CFD Toolkit
labelBits.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 Class
25  Foam::labelBits
26 
27 Description
28  A 29bits label and 3bits direction packed into single label
29 
30 SourceFiles
31 
32 \*---------------------------------------------------------------------------*/
33 
34 #ifndef labelBits_H
35 #define labelBits_H
36 
37 #include <OpenFOAM/label.H>
38 #include <OpenFOAM/uLabel.H>
39 #include <OpenFOAM/direction.H>
40 #include <OpenFOAM/error.H>
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 
48 /*---------------------------------------------------------------------------*\
49  Class labelBits Declaration
50 \*---------------------------------------------------------------------------*/
51 
52 class labelBits
53 {
54  // Private data
55 
56  label data_;
57 
58  inline static label pack(const label val, const direction bits)
59  {
60 # ifdef FULLDEBUG
61  if (bits > 7 || (((val<<3)>>3) != val))
62  {
64  (
65  "labelBits::pack(const label, const direction)"
66  ) << "Direction " << bits << " outside range 0..7"
67  << abort(FatalError);
68  }
69 # endif
70 
71  return (val<<3) | bits;
72  }
73 
74 public:
75 
76  // Constructors
77 
78  //- Construct null
79  inline labelBits()
80  {}
81 
82  //- Construct from components
83  inline labelBits(const label val, const direction bits)
84  :
85  data_(pack(val, bits))
86  {}
87 
88  //- Construct from Istream
89  inline labelBits(Istream& is)
90  {
91  is >> data_;
92  }
93 
94 
95 
96  // Member Functions
97 
98  inline label val() const
99  {
100  return data_ >> 3;
101  }
102 
103  inline direction bits() const
104  {
105  return data_ & 7;
106  }
107 
108  inline void setVal(const label val)
109  {
110  data_ = pack(val, bits());
111  }
112 
113  inline void setBits(const direction bits)
114  {
115  data_ = pack(val(), bits);
116  }
117 
118 
119  // Member Operators
120 
121  friend inline bool operator==(const labelBits& a, const labelBits& b)
122  {
123  return a.data_ == b.data_;
124  }
125 
126  friend inline bool operator!=(const labelBits& a, const labelBits& b)
127  {
128  return !(a == b);
129  }
130 
131  // IOstream Operators
132 
133  friend inline Istream& operator>>(Istream& is, labelBits& lb)
134  {
135  return is >> lb.data_;
136  }
137 
138  friend inline Ostream& operator<<(Ostream& os, const labelBits& lb)
139  {
140  return os << lb.data_;
141  }
142 };
143 
144 
145 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
146 
147 } // End namespace Foam
148 
149 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
150 
151 #endif
152 
153 // ************************ vim: set sw=4 sts=4 et: ************************ //