1 /*
2 ** upb::pb::TextPrinter (upb_textprinter)
3 **
4 ** Handlers for writing to protobuf text format.
5 */
6 
7 #ifndef UPB_TEXT_H_
8 #define UPB_TEXT_H_
9 
10 #include "upb/sink.h"
11 
12 #ifdef __cplusplus
13 namespace upb {
14 namespace pb {
15 class TextPrinterPtr;
16 }  /* namespace pb */
17 }  /* namespace upb */
18 #endif
19 
20 /* upb_textprinter ************************************************************/
21 
22 struct upb_textprinter;
23 typedef struct upb_textprinter upb_textprinter;
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /* C API. */
30 upb_textprinter *upb_textprinter_create(upb_arena *arena, const upb_handlers *h,
31                                         upb_bytessink output);
32 void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line);
33 upb_sink upb_textprinter_input(upb_textprinter *p);
34 upb_handlercache *upb_textprinter_newcache(void);
35 
36 #ifdef __cplusplus
37 }  /* extern "C" */
38 
39 class upb::pb::TextPrinterPtr {
40  public:
TextPrinterPtr(upb_textprinter * ptr)41   TextPrinterPtr(upb_textprinter* ptr) : ptr_(ptr) {}
42 
43   /* The given handlers must have come from NewHandlers().  It must outlive the
44    * TextPrinter. */
Create(Arena * arena,upb::HandlersPtr * handlers,BytesSink output)45   static TextPrinterPtr Create(Arena *arena, upb::HandlersPtr *handlers,
46                                BytesSink output) {
47     return TextPrinterPtr(
48         upb_textprinter_create(arena->ptr(), handlers->ptr(), output.sink()));
49   }
50 
SetSingleLineMode(bool single_line)51   void SetSingleLineMode(bool single_line) {
52     upb_textprinter_setsingleline(ptr_, single_line);
53   }
54 
input()55   Sink input() { return upb_textprinter_input(ptr_); }
56 
57   /* If handler caching becomes a requirement we can add a code cache as in
58    * decoder.h */
NewCache()59   static HandlerCache NewCache() {
60     return HandlerCache(upb_textprinter_newcache());
61   }
62 
63  private:
64   upb_textprinter* ptr_;
65 };
66 
67 #endif
68 
69 #endif  /* UPB_TEXT_H_ */
70