THRIFT-922. cpp: Fix C++ compilation when using list<bool>
The STL specializes vector<bool> to store the values as individual bits, rather
than bools. Therefore, when using a Thrift list<bool>, readBool() gets invoked
not with a bool&, but with a std::vector<bool>::reference.
TProtocol does provide a readBool(std::vector<bool>::reference) implementation.
However, almost all TProtocol subclasses defined only readBool(bool&), which
hides the other overloaded versions of readBool(). As a result, the code
worked only when accessing TProtocol objects via a "TProtocol*", and not
directly via the subclass type. When using C++ templates, protocol objects do
get invoked via pointers to the subclass type, causing compile failures when
std::vector<bool> is used.
This change updates the various TProtocol implementations to also provide
readBool(std::vector<bool>::reference).
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@1005137 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/protocol/TVirtualProtocol.h b/lib/cpp/src/protocol/TVirtualProtocol.h
index 7f01570..9133dbe 100644
--- a/lib/cpp/src/protocol/TVirtualProtocol.h
+++ b/lib/cpp/src/protocol/TVirtualProtocol.h
@@ -108,6 +108,11 @@
"this protocol does not support reading (yet).");
}
+ uint32_t readBool(std::vector<bool>::reference value) {
+ throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
+ "this protocol does not support reading (yet).");
+ }
+
uint32_t readByte(int8_t& byte) {
throw TProtocolException(TProtocolException::NOT_IMPLEMENTED,
"this protocol does not support reading (yet).");
@@ -437,6 +442,10 @@
return static_cast<Protocol_*>(this)->readBool(value);
}
+ virtual uint32_t readBool_virt(std::vector<bool>::reference value) {
+ return static_cast<Protocol_*>(this)->readBool(value);
+ }
+
virtual uint32_t readByte_virt(int8_t& byte) {
return static_cast<Protocol_*>(this)->readByte(byte);
}
@@ -484,6 +493,21 @@
return ::apache::thrift::protocol::skip(*prot, type);
}
+ /*
+ * Provide a default readBool() implementation for use with
+ * std::vector<bool>, that behaves the same as reading into a normal bool.
+ *
+ * Subclasses can override this if desired, but there normally shouldn't
+ * be a need to.
+ */
+ uint32_t readBool(std::vector<bool>::reference value) {
+ bool b = false;
+ uint32_t ret = static_cast<Protocol_*>(this)->readBool(b);
+ value = b;
+ return ret;
+ }
+ using Super_::readBool; // so we don't hide readBool(bool&)
+
protected:
TVirtualProtocol(boost::shared_ptr<TTransport> ptrans)
: Super_(ptrans)