/usr/include/loki/SafeBits.h is in libloki-dev 0.1.7-3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 | ////////////////////////////////////////////////////////////////////////////////
// The Loki Library
// Copyright (c) 2009 by Fedor Pikus & Rich Sposato
// The copyright on this file is protected under the terms of the MIT license.
//
// Permission to use, copy, modify, distribute and sell this software for any
// purpose is hereby granted without fee, provided that the above copyright
// notice appear in all copies and that both that copyright notice and this
// permission notice appear in supporting documentation.
//
// The author makes no claims about the suitability of this software for any
// purpose. It is provided "as is" without express or implied warranty.
////////////////////////////////////////////////////////////////////////////////
// $Id$
#ifndef LOKI_INCLUDED_SAFE_BIT_FIELDS_H
#define LOKI_INCLUDED_SAFE_BIT_FIELDS_H
#include <cstdlib>
#include <assert.h>
#include <loki/static_check.h>
namespace Loki
{
/*
==========================================================================================================================================
SafeBitField - type-safe class for bit fields.
SafeBitConst - type-safe class for bit constants.
SafeBitField is designed to be a [almost] drop-in replacement for integer flags and bit fields where individual bits are set and checked
using symbolic names for flags:
typedef unsigned long Labels_t;
Labels_t labels;
const Labels_t Label_A = 0x00000001;
const Labels_t Label_B = 0x00000002;
...
labels |= Label_B;
if ( labels & Label_A ) { ... }
Such code offers no protection against mismatching bit constants and bit fields:
typedef unsigned long Kinds_t;
Kinds_t kinds;
const Kinds_t Kind_A = 0x00000004;
...
if ( kinds & Label_A ) { ... } // Error but compiles
SafeBitField is a drop-in replacement which generates a unique type for each bit field. Bit fields of different types cannot be applied
to each other:
LOKI_BIT_FIELD( unsigned long ) Labels_t;
Labels_t labels;
LOKI_BIT_CONST( Labels_t, Label_A, 1 ); // 0x0001 - 1st bit is set
LOKI_BIT_CONST( Labels_t, Label_B, 2 ); // 0x0002 - 1st bit is set
...
LOKI_BIT_FIELD( unsigned long ) Kinds_t;
Kinds_t kinds;
LOKI_BIT_CONST( Kinds_t, Kind_A, 3 ); // 0x0004 - 1st bit is set
...
if ( kinds & Label_A ) { ... } // Does not compile
Several other kinds of bit field misuse are caught by safe bit fields:
if ( kinds & Kind_A == 0 ) { ... }
if ( kinds && Kind_A ) { ... }
There are few cases where drop-in replacement does not work:
1. Operations involving bit fields and unnamed integers. Usually the integer in question is 0:
Labels_t labels = 0; // No longer compiles
if ( ( labels & Label_A ) == 0 ) { ... } // Also does not compile
The solution is to use named bit constants, including the one for 0:
LOKI_BIT_CONST( Labels_t, Label_None, 0 ); // 0x0000 - No bit is set
Labels_t labels = Label_None; // Or just Labels_t labels; - constructor initializes to 0
if ( ( labels & Label_A ) == Label_None ) { ... } // // Or just if ( labels & Label_A ) { ... }
2. I/O and other operations which require integer variables and cannot be modified:
void write_to_db( unsigned int word );
Labels_t labels;
write_to_db( labels ); // No longer compiles
This problem is solved by reinterpreting the bit fields as an integer, the user is responsible for using the right
type of integer:
write_to_db( *((Labels_t::bit_word_t*)(&labels)) );
==========================================================================================================================================
*/
/// @par Non-Templated Initialization.
/// Not all compilers support template member functions where the template
/// arguments are not deduced but explicitly specified. For these broken
/// compilers, a non-template make_bit_const() function is provided instead of
/// the template one. The only downside is that instead of compile-time checking
/// of the index argument, it does runtime checking.
#if defined(__SUNPRO_CC) || ( defined(__GNUC__) && (__GNUC__ < 3) )
#define LOKI_BIT_FIELD_NONTEMPLATE_INIT
#endif
/// @par Forbidding Conversions.
/// This incomplete type prevents compilers from instantiating templates for
/// type conversions which should not happen. This incomplete type must be a
/// template: if the type is incomplete at the point of template definition,
/// the template is illegal (although the standard allows compilers to accept
/// or reject such code, ยง14.6/, so some compilers will not issue diagnostics
/// unless template is instantiated). The standard-compliant way is to defer
/// binding to the point of instantiation by making the incomplete type itself
/// a template.
template < typename > struct Forbidden_conversion; // This struct must not be defined!
/// Forward declaration of the field type.
template <
unsigned int unique_index,
typename word_t = unsigned long
> class SafeBitField;
////////////////////////////////////////////////////////////////////////////////
/// \class SafeBitConst Bit constants.
/// This class defines a bit-field constant - a collection of unchanging bits
/// used to compare to bit-fields. Instances of this class are intended to act
/// as labels for bit-fields.
///
/// \par Safety
/// - This class provides operations used for comparisons and conversions, but
/// no operations which may modify the value.
/// - As a templated class, it provides type-safety so bit values and constants
/// used for different reasons may not be unknowingly compared to each other.
/// - The unique_index template parameter insures the unique type of each bit
/// bit-field. It shares the unique_index with a similar SafeBitField.
/// - Its operations only allow comparisons to other bit-constants and
/// bit-fields of the same type.
////////////////////////////////////////////////////////////////////////////////
template
<
unsigned int unique_index,
typename word_t = unsigned long
>
class SafeBitConst
{
public:
/// Type of the bit field is available if needed.
typedef word_t bit_word_t;
/// Corresponding field type.
typedef SafeBitField< unique_index, word_t > field_t;
/// Typedef is not allowed in friendship declaration.
friend class SafeBitField< unique_index, word_t >;
// Static factory constructor, creates a bit constant with one bit set. The position of the bit is given by the template parameter,
// bit 1 is the junior bit, i.e. make_bit_const<1>() returns 1. Bit index 0 is a special case and returns 0.
// This function should be used only to initialize the static bit constant objects.
// This function will not compile if the bit index is outside the vaild range.
// There is also a compile-time assert to make sure the size of the class is the same as the size of the underlaying integer type.
// This assert could go into the constructor, but aCC does not seem to understand sizeof(SafeBitConst) in the constructor.
//
#ifndef LOKI_BIT_FIELD_NONTEMPLATE_INIT
template < unsigned int i > static SafeBitConst make_bit_const()
{
LOKI_STATIC_CHECK( i <= ( 8 * sizeof(word_t) ), Index_is_beyond_size_of_data );
LOKI_STATIC_CHECK( sizeof(SafeBitConst) == sizeof(word_t), Object_size_does_not_match_data_size );
// Why check for ( i > 0 ) again inside the shift if the shift
// can never be evaluated for i == 0? Some compilers see shift by ( i - 1 )
// and complain that for i == 0 the number is invalid, without
// checking that shift needs evaluating.
return SafeBitConst( ( i > 0 ) ? ( word_t(1) << ( ( i > 0 ) ? ( i - 1 ) : 0 ) ) : 0 );
}
#else
static SafeBitConst make_bit_const( unsigned int i )
{
LOKI_STATIC_CHECK( sizeof(SafeBitConst) == sizeof(word_t), Object_size_does_not_match_data_size );
assert( i <= ( 8 * sizeof(word_t) ) ); // Index is beyond size of data.
// Why check for ( i > 0 ) again inside the shift if the shift
// can never be evaluated for i == 0? Some compilers see shift by ( i - 1 )
// and complain that for i == 0 the number is invalid, without
// checking that shift needs evaluating.
return SafeBitConst( ( i > 0 ) ? ( word_t(1) << ( ( i > 0 ) ? ( i - 1 ) : 0 ) ) : 0 );
}
#endif
/// Default constructor allows client code to construct bit fields on the stack.
SafeBitConst() : word( 0 ) {}
/// Copy constructor.
SafeBitConst( const SafeBitConst& rhs ) : word( rhs.word ) {}
/// Comparison operators which take a constant bit value.
bool operator == ( const SafeBitConst & rhs ) const { return word == rhs.word; }
bool operator != ( const SafeBitConst & rhs ) const { return word != rhs.word; }
bool operator < ( const SafeBitConst & rhs ) const { return word < rhs.word; }
bool operator > ( const SafeBitConst & rhs ) const { return word > rhs.word; }
bool operator <= ( const SafeBitConst & rhs ) const { return word <= rhs.word; }
bool operator >= ( const SafeBitConst & rhs ) const { return word >= rhs.word; }
/// Comparision operators for mutable bit fields.
bool operator == ( const field_t & rhs ) const { return word == rhs.word; }
bool operator != ( const field_t & rhs ) const { return word != rhs.word; }
bool operator < ( const field_t & rhs ) const { return word < rhs.word; }
bool operator > ( const field_t & rhs ) const { return word > rhs.word; }
bool operator <= ( const field_t & rhs ) const { return word <= rhs.word; }
bool operator >= ( const field_t & rhs ) const { return word >= rhs.word; }
/// Bitwise operations. Operation-assignment operators are not needed,
/// since bit constants cannot be changed after they are initialized.
const SafeBitConst operator | ( const SafeBitConst & rhs ) const { return SafeBitConst( word | rhs.word ); }
const SafeBitConst operator & ( const SafeBitConst & rhs ) const { return SafeBitConst( word & rhs.word ); }
const SafeBitConst operator ^ ( const SafeBitConst & rhs ) const { return SafeBitConst( word ^ rhs.word ); }
const SafeBitConst operator ~ ( void ) const { return SafeBitConst( ~word ); }
/// These bitwise operators return a bit-field instead of a bit-const.
field_t operator | ( const field_t & rhs ) const { return field_t( word | rhs.word ); }
field_t operator & ( const field_t & rhs ) const { return field_t( word & rhs.word ); }
field_t operator ^ ( const field_t & rhs ) const { return field_t( word ^ rhs.word ); }
/// The shift operators move bits inside the bit field. These are useful in
/// loops which act over bit fields and increment them.
const SafeBitConst operator << ( unsigned int s ) const { return SafeBitConst( word << s ); }
const SafeBitConst operator >> ( unsigned int s ) const { return SafeBitConst( word >> s ); }
/// Word size is also the maximum number of different bit fields for a given word type.
static size_t size() { return ( 8 * sizeof( word_t ) ); }
private:
/// Copy-assignment operator is not implemented since it does not make sense
/// for a constant object.
SafeBitConst operator = ( const SafeBitConst & rhs );
// Private constructor from an integer type.
explicit SafeBitConst( word_t init ) : word( init ) {}
/// This data stores a single bit value. It is declared const to enforce
// constness for all functions of this class.
const word_t word;
// Here comes the interesting stuff: all the operators designed to
// trap unintended conversions and make them not compile.
// Operators below handle code like this:
// SafeBitField<1> label1;
// SafeBitField<2> label2;
// if ( label1 & label2 ) { ... }
// These operators are private, and will not instantiate in any
// event because of the incomplete Forbidden_conversion struct.
template < typename T > SafeBitConst operator|( T ) const { Forbidden_conversion< T > wrong; return *this; }
template < typename T > SafeBitConst operator&( T ) const { Forbidden_conversion< T > wrong; return *this; }
template < typename T > SafeBitConst operator^( T ) const { Forbidden_conversion< T > wrong; return *this; }
template < typename T > SafeBitConst operator|=( T ) const { Forbidden_conversion< T > wrong; return *this; }
template < typename T > SafeBitConst operator&=( T ) const { Forbidden_conversion< T > wrong; return *this; }
template < typename T > SafeBitConst operator^=( T ) const { Forbidden_conversion< T > wrong; return *this; }
// And the same thing for comparisons: private and unusable.
// if ( label1 == label2 ) { ... }
template < typename T > bool operator==( const T ) const { Forbidden_conversion< T > wrong; return true; }
template < typename T > bool operator!=( const T ) const { Forbidden_conversion< T > wrong; return true; }
template < typename T > bool operator<( const T ) const { Forbidden_conversion< T > wrong; return true; }
template < typename T > bool operator>( const T ) const { Forbidden_conversion< T > wrong; return true; }
template < typename T > bool operator<=( const T ) const { Forbidden_conversion< T > wrong; return true; }
template < typename T > bool operator>=( const T ) const { Forbidden_conversion< T > wrong; return true; }
};
////////////////////////////////////////////////////////////////////////////////
/// \class SafeBitConst Bit constants.
/// This class defines a bit-field constant - a collection of unchanging bits
/// used to compare to bit-fields. Instances of this class are intended to
/// store bit values.
///
/// \par Safety
/// - This class provides operations used for comparisons and conversions, and
/// also operations which may safely modify the value.
/// - As a templated class, it provides type-safety so bit values and constants
/// used for different reasons may not be unknowingly compared to each other.
/// - The unique_index template parameter insures the unique type of each bit
/// bit-field. It shares the unique_index with a similar SafeBitConst.
/// - Its operations only allow comparisons to other bit-constants and
/// bit-fields of the same type.
////////////////////////////////////////////////////////////////////////////////
template
<
unsigned int unique_index,
typename word_t
>
class SafeBitField
{
public:
/// Type of the bit field is available if needed.
typedef word_t bit_word_t;
/// Corresponding field type.
typedef SafeBitConst< unique_index, word_t > const_t;
/// Typedef is not allowed in friendship declaration.
friend class SafeBitConst<unique_index, word_t>;
/// Default constructor allows client code to construct bit fields on the stack.
SafeBitField() : word( 0 ) {}
/// Copy constructor and assignment operators.
SafeBitField( const SafeBitField & rhs ) : word( rhs.word ) {}
SafeBitField & operator = ( const SafeBitField & rhs ) { word = rhs.word; return *this; }
/// Copy constructor and assignment operators from constant bit fields.
SafeBitField( const const_t & rhs ) : word( rhs.word ) {}
SafeBitField & operator = ( const const_t & rhs ) { word = rhs.word; return *this; }
/// These comparison operators act on bit-fields of the same type.
bool operator == ( const SafeBitField & rhs ) const { return word == rhs.word; }
bool operator != ( const SafeBitField & rhs ) const { return word != rhs.word; }
bool operator < ( const SafeBitField & rhs ) const { return word < rhs.word; }
bool operator > ( const SafeBitField & rhs ) const { return word > rhs.word; }
bool operator <= ( const SafeBitField & rhs ) const { return word <= rhs.word; }
bool operator >= ( const SafeBitField & rhs ) const { return word >= rhs.word; }
/// These comparison operators act on bit-constants of a similar type.
bool operator == ( const const_t & rhs ) const { return word == rhs.word; }
bool operator != ( const const_t & rhs ) const { return word != rhs.word; }
bool operator < ( const const_t & rhs ) const { return word < rhs.word; }
bool operator > ( const const_t & rhs ) const { return word > rhs.word; }
bool operator <= ( const const_t & rhs ) const { return word <= rhs.word; }
bool operator >= ( const const_t & rhs ) const { return word >= rhs.word; }
/// Bitwise operations that use bit-fields.
SafeBitField operator | ( const SafeBitField & rhs ) const { return SafeBitField( word | rhs.word ); }
SafeBitField operator & ( const SafeBitField & rhs ) const { return SafeBitField( word & rhs.word ); }
SafeBitField operator ^ ( const SafeBitField & rhs ) const { return SafeBitField( word ^ rhs.word ); }
SafeBitField operator ~ ( void ) const { return SafeBitField( ~word ); }
SafeBitField operator |= ( const SafeBitField & rhs ) { word |= rhs.word; return SafeBitField( *this ); }
SafeBitField operator &= ( const SafeBitField & rhs ) { word &= rhs.word; return SafeBitField( *this ); }
SafeBitField operator ^= ( const SafeBitField & rhs ) { word ^= rhs.word; return SafeBitField( *this ); }
/// Bitwise operators that use bit-constants.
SafeBitField operator | ( const_t rhs ) const { return SafeBitField( word | rhs.word ); }
SafeBitField operator & ( const_t rhs ) const { return SafeBitField( word & rhs.word ); }
SafeBitField operator ^ ( const_t rhs ) const { return SafeBitField( word ^ rhs.word ); }
SafeBitField operator |= ( const_t rhs ) { word |= rhs.word; return SafeBitField( *this ); }
SafeBitField operator &= ( const_t rhs ) { word &= rhs.word; return SafeBitField( *this ); }
SafeBitField operator ^= ( const_t rhs ) { word ^= rhs.word; return SafeBitField( *this ); }
// Conversion to bool.
// This is a major source of headaches, but it's required to support code like this:
// const static SafeBitConst<1> Label_value = SafeBitConst<1>::make_bit_const<1>();
// SafeBitField<1> label;
// if ( label & Label_value ) { ... } // Nice...
//
// The downside is that this allows all sorts of nasty conversions. Without additional precautions, bit fields of different types
// can be converted to bool and then compared or operated on:
// SafeBitField<1> label1;
// SafeBitField<2> label2;
// if ( label1 == label2 ) { ... } // Yuck!
// if ( label1 & label2 ) { ... } // Blech!
//
// It is somewhat safer to convert to a pointer, at least pointers to different types cannot be readilly compared, and there are no
// bitwise operations on pointers, but the conversion from word_t to a pointer can have run-time cost if they are of different size.
//
operator const bool() const { return ( 0 != word ); }
// Shift operators shift bits inside the bit field. Does not make
// sense, most of the time, except perhaps to loop over labels and
// increment them.
SafeBitField operator << ( unsigned int s ) { return SafeBitField( word << s ); }
SafeBitField operator >> ( unsigned int s ) { return SafeBitField( word >> s ); }
SafeBitField operator <<= ( unsigned int s ) { word <<= s; return *this; }
SafeBitField operator >>= ( unsigned int s ) { word >>= s; return *this; }
// Word size is also the maximum number of different bit fields for
// a given word type.
static size_t size( void ) { return ( 8 * sizeof( word_t ) ); }
private:
/// Private constructor from an integer type. Don't put too much stock into
/// explicit declaration, it's better than nothing but does not solve all
/// problems with undesired conversions because SafeBitField coverts to bool.
explicit SafeBitField( word_t init ) : word( init ) {}
/// This stores the bits.
word_t word;
// Here comes the interesting stuff: all the operators designed to
// trap unintended conversions and make them not compile.
// Operators below handle code like this:
// SafeBitField<1> label1;
// SafeBitField<2> label2;
// if ( label1 & label2 ) { ... }
// These operators are private, and will not instantiate in any
// event because of the incomplete Forbidden_conversion struct.
template < typename T > SafeBitField operator | ( T ) const { Forbidden_conversion< T > wrong; return *this; }
template < typename T > SafeBitField operator & ( T ) const { Forbidden_conversion< T > wrong; return *this; }
template < typename T > SafeBitField operator ^ ( T ) const { Forbidden_conversion< T > wrong; return *this; }
template < typename T > SafeBitField operator |= ( T ) const { Forbidden_conversion< T > wrong; return *this; }
template < typename T > SafeBitField operator &= ( T ) const { Forbidden_conversion< T > wrong; return *this; }
template < typename T > SafeBitField operator ^= ( T ) const { Forbidden_conversion< T > wrong; return *this; }
// And the same thing for comparisons:
// if ( label1 == label2 ) { ... }
template < typename T > bool operator == ( const T ) const { Forbidden_conversion< T > wrong; return true; }
template < typename T > bool operator != ( const T ) const { Forbidden_conversion< T > wrong; return true; }
template < typename T > bool operator < ( const T ) const { Forbidden_conversion< T > wrong; return true; }
template < typename T > bool operator > ( const T ) const { Forbidden_conversion< T > wrong; return true; }
template < typename T > bool operator <= ( const T ) const { Forbidden_conversion< T > wrong; return true; }
template < typename T > bool operator >= ( const T ) const { Forbidden_conversion< T > wrong; return true; }
};
// The above template member operators catch errors when the first
// argument to a binary operator is a label, but they don't work when
// the first argument is an integer and the second one is a label: the
// label converts to bool and the operator is performed on two integers.
// These operators catch errors like this:
// SafeBitField<1> label1;
// SafeBitField<2> label2;
// if ( !label1 & label2 ) { ... }
// where the first label is converted to bool (these errors cannot be
// caught by member operators of SafeBitField class because the first
// argument is not SafeBitField but bool.
//
// If used, these operators will not instantiate because of the
// incomplete Forbidden_conversion struct.
template < unsigned int unique_index, typename word_t >
inline SafeBitField< unique_index, word_t > operator & ( bool, SafeBitField< unique_index, word_t > rhs )
{
Forbidden_conversion<word_t> wrong;
return rhs;
}
template < unsigned int unique_index, typename word_t >
inline SafeBitField< unique_index, word_t > operator | ( bool, SafeBitField< unique_index, word_t > rhs )
{
Forbidden_conversion< word_t > wrong;
return rhs;
}
template < unsigned int unique_index, typename word_t >
inline SafeBitField< unique_index, word_t > operator ^ ( bool, SafeBitField< unique_index, word_t > rhs )
{
Forbidden_conversion< word_t > wrong;
return rhs;
}
template < unsigned int unique_index, typename word_t >
inline SafeBitField< unique_index, word_t > operator == ( bool, SafeBitField< unique_index, word_t > rhs )
{
Forbidden_conversion< word_t > wrong;
return rhs;
}
template < unsigned int unique_index, typename word_t >
inline SafeBitField< unique_index, word_t > operator != ( bool, SafeBitField< unique_index, word_t > rhs )
{
Forbidden_conversion< word_t > wrong;
return rhs;
}
// Finally, few macros. All macros are conditionally defined to use the SafeBitField classes if LOKI_SAFE_BIT_FIELD is defined. Otherwise,
// the macros fall back on the use of typedefs and integer constants. This provides no addititonal safety but allows the code to support the
// mixture of compilers which are broken to different degrees.
#define LOKI_SAFE_BIT_FIELD
// The first macro helps to declare new bit field types:
// LOKI_BIT_FIELD( ulong ) field_t;
// This creates a typedef field_t for SafeBitField<unique_index, ulong> where index is the current line number. Since line numbers __LINE__ are counted
// separately for all header files, this ends up being the same type in all files using the header which defines field_t.
#ifdef LOKI_SAFE_BIT_FIELD
#define LOKI_BIT_FIELD( word_t ) typedef SafeBitField<__LINE__, word_t>
#else
#define LOKI_BIT_FIELD( word_t ) typedef word_t
#endif // LOKI_SAFE_BIT_FIELD
// The second macro helps to declare static bit constants:
// LOKI_BIT_CONST( field_t, Label_1, 1 );
// creates new bit field object named Label_1 of type field_t which represents the field with the 1st (junior) bit set.
#ifdef LOKI_SAFE_BIT_FIELD
#ifndef LOKI_BIT_FIELD_NONTEMPLATE_INIT
#define LOKI_BIT_CONST( field_t, label, bit_index ) \
static const field_t::const_t label = field_t::const_t::make_bit_const<bit_index>()
#else
#define LOKI_BIT_CONST( field_t, label, bit_index ) \
static const field_t::const_t label = field_t::const_t::make_bit_const( bit_index )
#endif // LOKI_BIT_FIELD_NONTEMPLATE_INIT
#else
inline size_t make_bit_const( size_t i ) { return ( i > 0 ) ? ( size_t(1) << ( ( i > 0 ) ? ( i - 1 ) : 0 ) ) : 0; }
#define LOKI_BIT_CONST( field_t, label, bit_index ) static const field_t label = make_bit_const( bit_index )
#endif // LOKI_SAFE_BIT_FIELD
// The third macro helps to declare complex bit constants which are combination of several bits:
// LOKI_BIT_CONSTS( field_t, Label12 ) = Label_1 | Label_2;
#ifdef LOKI_SAFE_BIT_FIELD
#define LOKI_BIT_CONSTS( field_t, label ) static const field_t::const_t label
#else
#define LOKI_BIT_CONSTS( field_t, label ) static const field_t label
#endif // LOKI_SAFE_BIT_FIELD
// The fourth macro helps to declare the maximum number of bit constants for a given type:
// static const size_t count = LOKI_BIT_FIELD_COUNT( field_t );
// declared a variable "count" initialized to field_t::size()
#ifdef LOKI_SAFE_BIT_FIELD
#define LOKI_BIT_FIELD_COUNT( field_t ) field_t::size()
#else
#define LOKI_BIT_FIELD_COUNT( field_t ) ( 8 * sizeof(field_t) )
#endif // LOKI_SAFE_BIT_FIELD
} // namespace Loki
#endif // LOKI_INCLUDED_SAFE_BIT_FIELDS_H
|