bitunion.hh (12491:8765e1fb564d) bitunion.hh (12625:c0cf272e0456)
1/*
2 * Copyright (c) 2007-2008 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;

--- 18 unchanged lines hidden (view full) ---

27 *
28 * Authors: Gabe Black
29 */
30
31#ifndef __BASE_BITUNION_HH__
32#define __BASE_BITUNION_HH__
33
34#include <functional>
1/*
2 * Copyright (c) 2007-2008 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;

--- 18 unchanged lines hidden (view full) ---

27 *
28 * Authors: Gabe Black
29 */
30
31#ifndef __BASE_BITUNION_HH__
32#define __BASE_BITUNION_HH__
33
34#include <functional>
35#include <iostream>
35#include <type_traits>
36#include <type_traits>
37#include <typeinfo>
36
37#include "base/bitfield.hh"
38
39// The following implements the BitUnion system of defining bitfields
40//on top of an underlying class. This is done through the pervasive use of
41//both named and unnamed unions which all contain the same actual storage.
42//Since they're unioned with each other, all of these storage locations
43//overlap. This allows all of the bitfields to manipulate the same data

--- 365 unchanged lines hidden (view full) ---

409 size_t
410 operator() (const BitUnionType<T> &val) const
411 {
412 return hash<BitUnionBaseType<T> >::operator()(val);
413 }
414 };
415}
416
38
39#include "base/bitfield.hh"
40
41// The following implements the BitUnion system of defining bitfields
42//on top of an underlying class. This is done through the pervasive use of
43//both named and unnamed unions which all contain the same actual storage.
44//Since they're unioned with each other, all of these storage locations
45//overlap. This allows all of the bitfields to manipulate the same data

--- 365 unchanged lines hidden (view full) ---

411 size_t
412 operator() (const BitUnionType<T> &val) const
413 {
414 return hash<BitUnionBaseType<T> >::operator()(val);
415 }
416 };
417}
418
419
420namespace BitfieldBackend
421{
422namespace
423{
424 template<typename T>
425 std::ostream &
426 bitfieldBackendPrinter(std::ostream &os, const T &t)
427 {
428 os << t;
429 return os;
430 }
431
432 //Since BitUnions are generally numerical values and not character codes,
433 //these specializations attempt to ensure that they get cast to integers
434 //of the appropriate type before printing.
435 template <>
436 std::ostream &
437 bitfieldBackendPrinter(std::ostream &os, const char &t)
438 {
439 os << (const int)t;
440 return os;
441 }
442
443 template <>
444 std::ostream &
445 bitfieldBackendPrinter(std::ostream &os, const unsigned char &t)
446 {
447 os << (const unsigned int)t;
448 return os;
449 }
450}
451}
452
453//A default << operator which casts a bitunion to its underlying type and
454//passes it to BitfieldBackend::bitfieldBackendPrinter.
455template <typename T>
456std::ostream &
457operator << (std::ostream &os, const BitUnionType<T> &bu)
458{
459 return BitfieldBackend::bitfieldBackendPrinter(
460 os, (BitUnionBaseType<T>)bu);
461}
462
417#endif // __BASE_BITUNION_HH__
463#endif // __BASE_BITUNION_HH__