diff options
author | Joshua Haberman <jhaberman@gmail.com> | 2019-03-06 12:17:34 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-06 12:17:34 -0800 |
commit | ed9faae0993704b033c594b072d65e1bf19207fa (patch) | |
tree | df2777687abddf1335427d06942ba3436edf8c0b /upb/json/printer.h | |
parent | 51f30e064b8e88af8e7d429c5a54c25f28c68b02 (diff) | |
parent | 71ec01e34b03557cfed9df5f417791a189e7fca6 (diff) |
Merge pull request #141 from haberman/defcleanup
Major refactoring/cleanup of upb
Diffstat (limited to 'upb/json/printer.h')
-rw-r--r-- | upb/json/printer.h | 85 |
1 files changed, 36 insertions, 49 deletions
diff --git a/upb/json/printer.h b/upb/json/printer.h index 80644f1..85b9b12 100644 --- a/upb/json/printer.h +++ b/upb/json/printer.h @@ -12,74 +12,61 @@ #ifdef __cplusplus namespace upb { namespace json { -class Printer; +class PrinterPtr; } /* namespace json */ } /* namespace upb */ #endif -UPB_DECLARE_TYPE(upb::json::Printer, upb_json_printer) +/* upb_json_printer ***********************************************************/ +#define UPB_JSON_PRINTER_SIZE 192 -/* upb::json::Printer *********************************************************/ +struct upb_json_printer; +typedef struct upb_json_printer upb_json_printer; -#define UPB_JSON_PRINTER_SIZE 192 +#ifdef __cplusplus +extern "C" { +#endif + +/* Native C API. */ +upb_json_printer *upb_json_printer_create(upb_arena *a, const upb_handlers *h, + upb_bytessink output); +upb_sink upb_json_printer_input(upb_json_printer *p); +const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md, + bool preserve_fieldnames, + const void *owner); + +/* Lazily builds and caches handlers that will push encoded data to a bytessink. + * Any msgdef objects used with this object must outlive it. */ +upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames); #ifdef __cplusplus +} /* extern "C" */ /* Prints an incoming stream of data to a BytesSink in JSON format. */ -class upb::json::Printer { +class upb::json::PrinterPtr { public: - static Printer* Create(Environment* env, const upb::Handlers* handlers, - BytesSink* output); + PrinterPtr(upb_json_printer* ptr) : ptr_(ptr) {} - /* The input to the printer. */ - Sink* input(); + static PrinterPtr Create(Arena *arena, const upb::Handlers *handlers, + BytesSink output) { + return PrinterPtr( + upb_json_printer_create(arena->ptr(), handlers, output.sink())); + } - /* Returns handlers for printing according to the specified schema. - * If preserve_proto_fieldnames is true, the output JSON will use the - * original .proto field names (ie. {"my_field":3}) instead of using - * camelCased names, which is the default: (eg. {"myField":3}). */ - static reffed_ptr<const Handlers> NewHandlers(const upb::MessageDef* md, - bool preserve_proto_fieldnames); + /* The input to the printer. */ + Sink input() { return upb_json_printer_input(ptr_); } static const size_t kSize = UPB_JSON_PRINTER_SIZE; + static HandlerCache NewCache(bool preserve_proto_fieldnames) { + return upb_json_printer_newcache(preserve_proto_fieldnames); + } + private: - UPB_DISALLOW_POD_OPS(Printer, upb::json::Printer) + upb_json_printer* ptr_; }; -#endif - -UPB_BEGIN_EXTERN_C - -/* Native C API. */ -upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, - upb_bytessink *output); -upb_sink *upb_json_printer_input(upb_json_printer *p); -const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md, - bool preserve_fieldnames, - const void *owner); - -UPB_END_EXTERN_C - -#ifdef __cplusplus - -namespace upb { -namespace json { -inline Printer* Printer::Create(Environment* env, const upb::Handlers* handlers, - BytesSink* output) { - return upb_json_printer_create(env, handlers, output); -} -inline Sink* Printer::input() { return upb_json_printer_input(this); } -inline reffed_ptr<const Handlers> Printer::NewHandlers( - const upb::MessageDef *md, bool preserve_proto_fieldnames) { - const Handlers* h = upb_json_printer_newhandlers( - md, preserve_proto_fieldnames, &h); - return reffed_ptr<const Handlers>(h, &h); -} -} /* namespace json */ -} /* namespace upb */ - -#endif +#endif /* __cplusplus */ #endif /* UPB_JSON_TYPED_PRINTER_H_ */ |