summaryrefslogtreecommitdiff
path: root/src/base/bitunion.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/base/bitunion.hh')
-rw-r--r--src/base/bitunion.hh46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/base/bitunion.hh b/src/base/bitunion.hh
index 569d65031..b2a2ba806 100644
--- a/src/base/bitunion.hh
+++ b/src/base/bitunion.hh
@@ -32,7 +32,9 @@
#define __BASE_BITUNION_HH__
#include <functional>
+#include <iostream>
#include <type_traits>
+#include <typeinfo>
#include "base/bitfield.hh"
@@ -414,4 +416,48 @@ namespace std
};
}
+
+namespace BitfieldBackend
+{
+namespace
+{
+ template<typename T>
+ std::ostream &
+ bitfieldBackendPrinter(std::ostream &os, const T &t)
+ {
+ os << t;
+ return os;
+ }
+
+ //Since BitUnions are generally numerical values and not character codes,
+ //these specializations attempt to ensure that they get cast to integers
+ //of the appropriate type before printing.
+ template <>
+ std::ostream &
+ bitfieldBackendPrinter(std::ostream &os, const char &t)
+ {
+ os << (const int)t;
+ return os;
+ }
+
+ template <>
+ std::ostream &
+ bitfieldBackendPrinter(std::ostream &os, const unsigned char &t)
+ {
+ os << (const unsigned int)t;
+ return os;
+ }
+}
+}
+
+//A default << operator which casts a bitunion to its underlying type and
+//passes it to BitfieldBackend::bitfieldBackendPrinter.
+template <typename T>
+std::ostream &
+operator << (std::ostream &os, const BitUnionType<T> &bu)
+{
+ return BitfieldBackend::bitfieldBackendPrinter(
+ os, (BitUnionBaseType<T>)bu);
+}
+
#endif // __BASE_BITUNION_HH__