From 1a484d353bc6b48df07473e13de3da27072beaa0 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Tue, 21 Aug 2018 14:56:02 -0700 Subject: Re-add upb/bindings/stdc++/string.h, which is still used in tests. --- upb/bindings/stdc++/string.h | 64 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 upb/bindings/stdc++/string.h (limited to 'upb') diff --git a/upb/bindings/stdc++/string.h b/upb/bindings/stdc++/string.h new file mode 100644 index 0000000..99efd4f --- /dev/null +++ b/upb/bindings/stdc++/string.h @@ -0,0 +1,64 @@ + +#ifndef UPB_STDCPP_H_ +#define UPB_STDCPP_H_ + +#include "upb/sink.h" + +namespace upb { + +template +class FillStringHandler { + public: + static void SetHandler(BytesHandler* handler) { + upb_byteshandler_setstartstr(handler, &FillStringHandler::StartString, + NULL); + upb_byteshandler_setstring(handler, &FillStringHandler::StringBuf, NULL); + } + + private: + // TODO(haberman): add UpbBind/UpbMakeHandler support to BytesHandler so these + // can be prettier callbacks. + static void* StartString(void *c, const void *hd, size_t size) { + UPB_UNUSED(hd); + UPB_UNUSED(size); + + T* str = static_cast(c); + str->clear(); + return c; + } + + static size_t StringBuf(void* c, const void* hd, const char* buf, size_t n, + const BufferHandle* h) { + UPB_UNUSED(hd); + UPB_UNUSED(h); + + T* str = static_cast(c); + try { + str->append(buf, n); + return n; + } catch (const std::exception&) { + return 0; + } + } +}; + +class StringSink { + public: + template + explicit StringSink(T* target) { + // TODO(haberman): we need to avoid rebuilding a new handler every time, + // but with class globals disallowed for google3 C++ this is tricky. + FillStringHandler::SetHandler(&handler_); + input_.Reset(&handler_, target); + } + + BytesSink* input() { return &input_; } + + private: + BytesHandler handler_; + BytesSink input_; +}; + +} // namespace upb + +#endif // UPB_STDCPP_H_ -- cgit v1.2.3