• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ** upb::pb::Encoder (upb_pb_encoder)
3 **
4 ** Implements a set of upb_handlers that write protobuf data to the binary wire
5 ** format.
6 **
7 ** This encoder implementation does not have any access to any out-of-band or
8 ** precomputed lengths for submessages, so it must buffer submessages internally
9 ** before it can emit the first byte.
10 */
11 
12 #ifndef UPB_ENCODER_H_
13 #define UPB_ENCODER_H_
14 
15 #include "upb/sink.h"
16 
17 #ifdef __cplusplus
18 namespace upb {
19 namespace pb {
20 class EncoderPtr;
21 }  /* namespace pb */
22 }  /* namespace upb */
23 #endif
24 
25 #define UPB_PBENCODER_MAX_NESTING 100
26 
27 /* upb_pb_encoder *************************************************************/
28 
29 /* Preallocation hint: decoder won't allocate more bytes than this when first
30  * constructed.  This hint may be an overestimate for some build configurations.
31  * But if the decoder library is upgraded without recompiling the application,
32  * it may be an underestimate. */
33 #define UPB_PB_ENCODER_SIZE 784
34 
35 struct upb_pb_encoder;
36 typedef struct upb_pb_encoder upb_pb_encoder;
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 upb_sink upb_pb_encoder_input(upb_pb_encoder *p);
43 upb_pb_encoder* upb_pb_encoder_create(upb_arena* a, const upb_handlers* h,
44                                       upb_bytessink output);
45 
46 /* Lazily builds and caches handlers that will push encoded data to a bytessink.
47  * Any msgdef objects used with this object must outlive it. */
48 upb_handlercache *upb_pb_encoder_newcache(void);
49 
50 #ifdef __cplusplus
51 }  /* extern "C" { */
52 
53 class upb::pb::EncoderPtr {
54  public:
EncoderPtr(upb_pb_encoder * ptr)55   EncoderPtr(upb_pb_encoder* ptr) : ptr_(ptr) {}
56 
ptr()57   upb_pb_encoder* ptr() { return ptr_; }
58 
59   /* Creates a new encoder in the given environment.  The Handlers must have
60    * come from NewHandlers() below. */
Create(Arena * arena,const Handlers * handlers,BytesSink output)61   static EncoderPtr Create(Arena* arena, const Handlers* handlers,
62                            BytesSink output) {
63     return EncoderPtr(
64         upb_pb_encoder_create(arena->ptr(), handlers, output.sink()));
65   }
66 
67   /* The input to the encoder. */
input()68   upb::Sink input() { return upb_pb_encoder_input(ptr()); }
69 
70   /* Creates a new set of handlers for this MessageDef. */
NewCache()71   static HandlerCache NewCache() {
72     return HandlerCache(upb_pb_encoder_newcache());
73   }
74 
75   static const size_t kSize = UPB_PB_ENCODER_SIZE;
76 
77  private:
78   upb_pb_encoder* ptr_;
79 };
80 
81 #endif  /* __cplusplus */
82 
83 #endif  /* UPB_ENCODER_H_ */
84