summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabe Black <gabeblack@google.com>2018-03-27 16:55:18 -0700
committerGabe Black <gabeblack@google.com>2018-03-28 20:24:16 +0000
commitab46e32b36810bec329a015d794f0aafff8d4b6f (patch)
tree48f77289e951e0a7b7d46a44a21a3339b321e133 /src
parenta5f933d9af64b8625785fb3eea469d1e98442fdd (diff)
downloadgem5-ab46e32b36810bec329a015d794f0aafff8d4b6f.tar.xz
base: Add a default output function for bitunion types.
This way printing bitunions with, for instance, DPRINTF actually prints something useful. More specialized overloads will still allow printing particular bitunion types in ways that might make more sense for that particular type. Change-Id: I92beb0ce07683ba8b318cf25aa73e0057e4a60ef Reviewed-on: https://gem5-review.googlesource.com/9461 Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com> Maintainer: Gabe Black <gabeblack@google.com>
Diffstat (limited to 'src')
-rw-r--r--src/base/bitunion.hh46
-rw-r--r--src/base/bituniontest.cc14
2 files changed, 60 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__
diff --git a/src/base/bituniontest.cc b/src/base/bituniontest.cc
index 8781d2d5e..d7ed95bb8 100644
--- a/src/base/bituniontest.cc
+++ b/src/base/bituniontest.cc
@@ -270,3 +270,17 @@ TEST_F(BitUnionData, Templating)
is64 = std::is_same<BitUnionBaseType<Dummy32>, uint64_t>::value;
EXPECT_FALSE(is64);
}
+
+TEST_F(BitUnionData, Output)
+{
+ sixtyFour = 1234567812345678;
+ std::stringstream ss;
+ ss << sixtyFour;
+ EXPECT_EQ(ss.str(), "1234567812345678");
+ ss.str("");
+
+ EmptyEight eight = 65;
+ ss << eight;
+ EXPECT_EQ(ss.str(), "65");
+ ss.str("");
+}