From db512df98e0fac208a716c7807d037f0b0d309f1 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 4 Jan 2011 15:47:25 -0800 Subject: A bunch of work on upb_def and upb_value. --- core/upb.h | 51 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 13 deletions(-) (limited to 'core/upb.h') diff --git a/core/upb.h b/core/upb.h index 6ecc2a0..7bed779 100644 --- a/core/upb.h +++ b/core/upb.h @@ -130,21 +130,46 @@ typedef uint32_t upb_strlen_t; // A single .proto value. The owner must have an out-of-band way of knowing // the type, so that it knows which union member to use. -typedef union { - double _double; - float _float; - int32_t int32; - int64_t int64; - uint32_t uint32; - uint64_t uint64; - bool _bool; - upb_string *str; - upb_msg *msg; - upb_array *arr; - upb_atomic_refcount_t *refcount; - void *_void; +typedef struct { + union { + double _double; + float _float; + int32_t int32; + int64_t int64; + uint32_t uint32; + uint64_t uint64; + bool _bool; + upb_string *str; + upb_msg *msg; + upb_array *arr; + upb_atomic_refcount_t *refcount; + void *_void; + } val; + + // In debug mode we carry the value type around also so we can check accesses + // to be sure the right member is being read. +#ifndef NDEBUG + upb_valuetype_t type; +#endif } upb_value; +#define UPB_VALUE_ACCESSORS(name, membername, ctype, proto_type) \ + ctype upb_value_get ## name(upb_value val) { \ + assert(val.type == UPB_TYPE(proto_type)); \ + return val.membername; \ + } \ + void upb_value_ ## name(upb_value *val, ctype cval) { \ + val.type = UPB_TYPE(proto_type); \ + val.membername = cval; \ + } +UPB_VALUE_ACCESSORS(double, _double, double, DOUBLE); +UPB_VALUE_ACCESSORS(float, _float, float, FLOAT); +UPB_VALUE_ACCESSORS(int32, int32, int32_t, INT32); +UPB_VALUE_ACCESSORS(int64, int64, int64_t, INT64); +UPB_VALUE_ACCESSORS(uint32, uint32, uint32_t, UINT32); +UPB_VALUE_ACCESSORS(uint64, uint64, uint64_t, UINT64); +UPB_VALUE_ACCESSORS(bool, _bool, bool, BOOL); + // A pointer to a .proto value. The owner must have an out-of-band way of // knowing the type, so it knows which union member to use. typedef union { -- cgit v1.2.3