diff options
Diffstat (limited to 'src/upb_srcsink.h')
-rw-r--r-- | src/upb_srcsink.h | 83 |
1 files changed, 9 insertions, 74 deletions
diff --git a/src/upb_srcsink.h b/src/upb_srcsink.h index 4a3d1e3..8e5a09d 100644 --- a/src/upb_srcsink.h +++ b/src/upb_srcsink.h @@ -1,8 +1,6 @@ /* * upb - a minimalist implementation of protocol buffers. * - * Copyright (c) 2010 Joshua Haberman. See LICENSE for details. - * * This file defines four general-purpose interfaces for pulling/pushing either * protobuf data or bytes: * @@ -13,12 +11,16 @@ * * These interfaces are used as general-purpose glue in upb. For example, the * decoder interface works by implementing a upb_src and calling a upb_bytesrc. + * + * Copyright (c) 2010 Joshua Haberman. See LICENSE for details. + * */ #ifndef UPB_SRCSINK_H #define UPB_SRCSINK_H #include "upb_def.h" +#include "upb_srcsink_vtbl.h" #ifdef __cplusplus extern "C" { @@ -28,9 +30,6 @@ extern "C" { // TODO: decide how to handle unknown fields. -struct upb_src; -typedef struct upb_src upb_src; - // Retrieves the fielddef for the next field in the stream. Returns NULL on // error or end-of-stream. upb_fielddef *upb_src_getdef(upb_src *src); @@ -51,14 +50,12 @@ bool upb_src_startmsg(upb_src *src); // which case the rest of the submessage is skipped. bool upb_src_endmsg(upb_src *src); -// Returns the current error status for the stream. -upb_status *upb_src_status(upb_src *src); +// Returns the current error/eof status for the stream. +INLINE upb_status *upb_src_status(upb_src *src) { return &src->status; } +INLINE bool upb_src_eof(upb_src *src) { return src->eof; } /* upb_sink *******************************************************************/ -struct upb_sink; -typedef struct upb_sink upb_sink; - // Puts the given fielddef into the stream. bool upb_sink_putdef(upb_sink *sink, upb_fielddef *def); @@ -76,9 +73,6 @@ upb_status *upb_sink_status(upb_sink *sink); /* upb_bytesrc ****************************************************************/ -struct upb_bytesrc; -typedef struct upb_bytesrc upb_bytesrc; - // Returns the next string in the stream. NULL is returned on error or eof. // The string must be at least "minlen" bytes long unless the stream is eof. // @@ -94,13 +88,11 @@ void upb_bytesrc_recycle(upb_bytesrc *src, upb_string *str); bool upb_bytesrc_append(upb_bytesrc *src, upb_string *str, upb_strlen_t len); // Returns the current error status for the stream. -upb_status *upb_bytesrc_status(upb_src *src); +INLINE upb_status *upb_bytesrc_status(upb_bytesrc *src) { return &src->status; } +INLINE bool upb_bytesrc_eof(upb_bytesrc *src) { return src->eof; } /* upb_bytesink ***************************************************************/ -struct upb_bytesink; -typedef struct upb_bytesink upb_bytesink; - // Puts the given string. Returns the number of bytes that were actually, // consumed, which may be fewer than were in the string, or <0 on error. int32_t upb_bytesink_put(upb_bytesink *sink, upb_string *str); @@ -108,63 +100,6 @@ int32_t upb_bytesink_put(upb_bytesink *sink, upb_string *str); // Returns the current error status for the stream. upb_status *upb_bytesink_status(upb_bytesink *sink); -/* Dynamic Dispatch implementation for src/sink interfaces ********************/ - -// The rest of this file only concerns components that are implementing any of -// the above interfaces. To simple clients the code below should be considered -// private. - -// Typedefs for function pointers to all of the above functions. -typedef upb_fielddef (*upb_src_getdef_fptr)(upb_src *src); -typedef bool (*upb_src_getval_fptr)(upb_src *src, upb_valueptr val); -typedef bool (*upb_src_skipval_fptr)(upb_src *src); -typedef bool (*upb_src_startmsg_fptr)(upb_src *src); -typedef bool (*upb_src_endmsg_fptr)(upb_src *src); -typedef upb_status *(*upb_src_status_fptr)(upb_src *src); - -typedef bool (*upb_sink_putdef_fptr)(upb_sink *sink, upb_fielddef *def); -typedef bool (*upb_sink_putval_fptr)(upb_sink *sink, upb_value val); -typedef bool (*upb_sink_startmsg_fptr)(upb_sink *sink); -typedef bool (*upb_sink_endmsg_fptr)(upb_sink *sink); -typedef upb_status *(*upb_sink_status_fptr)(upb_sink *sink); - -typedef upb_string *(*upb_bytesrc_get_fptr)(upb_bytesrc *src); -typedef bool (*upb_bytesrc_append_fptr)( - upb_bytesrc *src, upb_string *str, upb_strlen_t len); -typedef upb_status *(*upb_bytesrc_status_fptr)(upb_src *src); - -typedef int32_t (*upb_bytesink_put_fptr)(upb_bytesink *sink, upb_string *str); -typedef upb_status *(*upb_bytesink_status_fptr)(upb_bytesink *sink); - -// Vtables for the above interfaces. -typedef struct { - upb_src_getdef_fptr getdef; - upb_src_getval_fptr getval; - upb_src_skipval_fptr skipval; - upb_src_startmsg_fptr startmsg; - upb_src_endmsg_fptr endmsg; - upb_src_status_fptr status; -} upb_src_vtable; - -// "Base Class" definitions; components that implement these interfaces should -// contain one of these structures. - -struct upb_src { - upb_src_vtable *vtbl; - upb_status status; - bool eof; -#ifndef NDEBUG - int state; // For debug-mode checking of API usage. -#endif -}; - -INLINE void upb_sink_init(upb_src *s, upb_src_vtable *vtbl) { - s->vtbl = vtbl; -#ifndef DEBUG - // TODO: initialize debug-mode checking. -#endif -} - #ifdef __cplusplus } /* extern "C" */ #endif |