From 621c0cdcb5efc4f7c2382031becded018ef0b62b Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sun, 4 Sep 2011 19:29:36 -0700 Subject: Const invasion: large parts of upb made const-correct. --- bindings/cpp/upb/def.hpp | 17 ++++++++++++++--- bindings/cpp/upb/handlers.hpp | 3 --- bindings/lua/upb.c | 44 +++++++++++++++++++++---------------------- bindings/python/upb.c | 26 ++++++++++++------------- 4 files changed, 49 insertions(+), 41 deletions(-) (limited to 'bindings') diff --git a/bindings/cpp/upb/def.hpp b/bindings/cpp/upb/def.hpp index 6500243..d64625c 100644 --- a/bindings/cpp/upb/def.hpp +++ b/bindings/cpp/upb/def.hpp @@ -17,9 +17,14 @@ namespace upb { class MessageDef : public upb_msgdef { public: + // Converting from C types to C++ wrapper types. static MessageDef* Cast(upb_msgdef *md) { return (MessageDef*)md; } + static const MessageDef* Cast(const upb_msgdef *md) { + return (const MessageDef*)md; + } - void Unref() { return upb_msgdef_unref(this); } + void Ref() const { upb_msgdef_ref(this); } + void Unref() const { upb_msgdef_unref(this); } private: MessageDef(); @@ -28,14 +33,20 @@ class MessageDef : public upb_msgdef { class SymbolTable : public upb_symtab { public: + // Converting from C types to C++ wrapper types. static SymbolTable* Cast(upb_symtab *s) { return (SymbolTable*)s; } + static const SymbolTable* Cast(const upb_symtab *s) { + return (SymbolTable*)s; + } + static SymbolTable* New() { return Cast(upb_symtab_new()); } - void Unref() { return upb_symtab_unref(this); } + void Ref() const { upb_symtab_unref(this); } + void Unref() const { upb_symtab_unref(this); } // If the given name refers to a message in this symbol table, returns a new // ref to that MessageDef object, otherwise returns NULL. - MessageDef* LookupMessage(const char *name) { + const MessageDef* LookupMessage(const char *name) const { return MessageDef::Cast(upb_symtab_lookupmsg(this, name)); } diff --git a/bindings/cpp/upb/handlers.hpp b/bindings/cpp/upb/handlers.hpp index e72c0a4..07683f6 100644 --- a/bindings/cpp/upb/handlers.hpp +++ b/bindings/cpp/upb/handlers.hpp @@ -4,9 +4,6 @@ * Copyright (c) 2011 Google Inc. See LICENSE for details. * Author: Josh Haberman * - * Note! This file is a proof-of-concept for C++ wrappers and does not - * yet build. - * * upb::Handlers is a generic visitor-like interface for iterating over a * stream of protobuf data. You can register function pointers that will be * called for each message and/or field as the data is being parsed or iterated diff --git a/bindings/lua/upb.c b/bindings/lua/upb.c index fe523e7..4cce4b6 100644 --- a/bindings/lua/upb.c +++ b/bindings/lua/upb.c @@ -37,7 +37,7 @@ static uint32_t lupb_touint32(lua_State *L, int narg, const char *name) { return n; } -static void lupb_pushstring(lua_State *L, upb_strref *ref) { +static void lupb_pushstring(lua_State *L, const upb_strref *ref) { if (ref->ptr) { lua_pushlstring(L, ref->ptr, ref->len); } else { @@ -146,15 +146,15 @@ static void lupb_typecheck(lua_State *L, int narg, upb_fielddef *f) { //static void lupb_msg_getorcreate(lua_State *L, upb_msg *msg, upb_msgdef *md); static void lupb_fielddef_getorcreate(lua_State *L, upb_fielddef *f); static upb_msgdef *lupb_msgdef_check(lua_State *L, int narg); -static void lupb_msg_pushnew(lua_State *L, void *md); +static void lupb_msg_pushnew(lua_State *L, const void *md); void lupb_checkstatus(lua_State *L, upb_status *s) { if (!upb_ok(s)) { - upb_status_print(s, stderr); // Need to copy the string to the stack, so we can free it and not leak // it (since luaL_error() does not return). - char buf[strlen(s->str)+1]; - strcpy(buf, s->str); + const char *str = upb_status_getstr(s); + char buf[strlen(str)+1]; + strcpy(buf, str); upb_status_uninit(s); luaL_error(L, "%s", buf); } @@ -226,14 +226,14 @@ static lupb_def *lupb_def_check(lua_State *L, int narg) { return ldef; } -static void lupb_def_getorcreate(lua_State *L, upb_def *def, int owned) { +static void lupb_def_getorcreate(lua_State *L, const upb_def *def, int owned) { bool created = false; switch(def->type) { case UPB_DEF_MSG: - created = lupb_cache_getorcreate(L, def, "upb.msgdef"); + created = lupb_cache_getorcreate(L, (void*)def, "upb.msgdef"); break; case UPB_DEF_ENUM: - created = lupb_cache_getorcreate(L, def, "upb.enumdef"); + created = lupb_cache_getorcreate(L, (void*)def, "upb.enumdef"); break; default: luaL_error(L, "unknown deftype %d", def->type); @@ -564,7 +564,7 @@ static int lupb_symtab_gc(lua_State *L) { static int lupb_symtab_lookup(lua_State *L) { lupb_symtab *s = lupb_symtab_check(L, 1); for (int i = 2; i <= lua_gettop(L); i++) { - upb_def *def = upb_symtab_lookup(s->symtab, luaL_checkstring(L, i)); + const upb_def *def = upb_symtab_lookup(s->symtab, luaL_checkstring(L, i)); if (def) { lupb_def_getorcreate(L, def, true); } else { @@ -579,12 +579,12 @@ static int lupb_symtab_getdefs(lua_State *L) { lupb_symtab *s = lupb_symtab_check(L, 1); upb_deftype_t type = luaL_checkint(L, 2); int count; - upb_def **defs = upb_symtab_getdefs(s->symtab, &count, type); + const upb_def **defs = upb_symtab_getdefs(s->symtab, &count, type); // Create the table in which we will return the defs. lua_createtable(L, count, 0); for (int i = 0; i < count; i++) { - upb_def *def = defs[i]; + const upb_def *def = defs[i]; lupb_def_getorcreate(L, def, true); lua_rawseti(L, -2, i + 1); } @@ -616,7 +616,7 @@ static const struct luaL_Reg lupb_symtab_mm[] = { // {msgdef, } // Must pass a upb_fielddef as the pointer. -static void lupb_array_pushnew(lua_State *L, void *f); +static void lupb_array_pushnew(lua_State *L, const void *f); static void *lupb_msg_check(lua_State *L, int narg, upb_msgdef **md) { void *msg = luaL_checkudata(L, narg, "upb.msg"); @@ -632,14 +632,14 @@ static void *lupb_msg_check(lua_State *L, int narg, upb_msgdef **md) { return msg; } -static void lupb_msg_pushnew(lua_State *L, void *md) { +static void lupb_msg_pushnew(lua_State *L, const void *md) { void *msg = lua_newuserdata(L, upb_msgdef_size(md)); luaL_getmetatable(L, "upb.msg"); assert(!lua_isnil(L, -1)); // Should have been created by luaopen_upb. lua_setmetatable(L, -2); upb_msg_clear(msg, md); lua_getfenv(L, -1); - lupb_cache_getorcreate(L, md, "upb.msgdef"); + lupb_cache_getorcreate(L, (void*)md, "upb.msgdef"); lua_rawseti(L, -2, 1); lua_pop(L, 1); // Pop the fenv. } @@ -755,10 +755,10 @@ static int lupb_msgdef(lua_State *L) { // // - use thread-local storage. Convenient and efficient, but not portable. -typedef void createfunc_t(lua_State *L, void *param); +typedef void createfunc_t(lua_State *L, const void *param); -static upb_sflow_t lupb_msg_start(void *m, upb_fielddef *f, bool array, - createfunc_t *pushnew, void *param) { +static upb_sflow_t lupb_msg_start(void *m, const upb_fielddef *f, bool array, + createfunc_t *pushnew, const void *param) { lua_State *L = *(lua_State**)m; int offset = array ? lua_rawlen(L, -1) : f->offset; if (!lua_checkstack(L, 3)) luaL_error(L, "stack full"); @@ -778,7 +778,7 @@ static upb_sflow_t lupb_msg_start(void *m, upb_fielddef *f, bool array, static upb_flow_t lupb_msg_string(void *m, upb_value fval, upb_value val, bool array) { // Could add lazy materialization of strings here. - upb_fielddef *f = upb_value_getfielddef(fval); + const upb_fielddef *f = upb_value_getfielddef(fval); lua_State *L = *(lua_State**)m; int offset = array ? lua_rawlen(L, -1) : f->offset; if (!lua_checkstack(L, 1)) luaL_error(L, "stack full"); @@ -788,17 +788,17 @@ static upb_flow_t lupb_msg_string(void *m, upb_value fval, upb_value val, } static upb_sflow_t lupb_msg_startseq(void *m, upb_value fval) { - upb_fielddef *f = upb_value_getfielddef(fval); + const upb_fielddef *f = upb_value_getfielddef(fval); return lupb_msg_start(m, f, false, lupb_array_pushnew, f); } static upb_sflow_t lupb_msg_startsubmsg(void *m, upb_value fval) { - upb_fielddef *f = upb_value_getfielddef(fval); + const upb_fielddef *f = upb_value_getfielddef(fval); return lupb_msg_start(m, f, false, lupb_msg_pushnew, upb_fielddef_subdef(f)); } static upb_sflow_t lupb_msg_startsubmsg_r(void *a, upb_value fval) { - upb_fielddef *f = upb_value_getfielddef(fval); + const upb_fielddef *f = upb_value_getfielddef(fval); return lupb_msg_start(a, f, true, lupb_msg_pushnew, upb_fielddef_subdef(f)); } @@ -877,7 +877,7 @@ static void lupb_array_check(lua_State *L, int narg) { luaL_typerror(L, narg, "upb array"); } -static void lupb_array_pushnew(lua_State *L, void *f) { +static void lupb_array_pushnew(lua_State *L, const void *f) { (void)L; (void)f; } diff --git a/bindings/python/upb.c b/bindings/python/upb.c index ffb3d52..497074b 100644 --- a/bindings/python/upb.c +++ b/bindings/python/upb.c @@ -36,8 +36,8 @@ static upb_accessor_vtbl *PyUpb_AccessorForField(upb_fielddef *f); // For objects that are just wrappers around a C object pointer, we keep a // cache mapping C pointer -> wrapper object. This allows us to consistently // vend the same Python object given the same C object. This prevents us from -// creating too many Python objects unnecessarily. More importantly, it provides -// the expected semantics: +// creating too many Python objects unnecessarily. Just as importantly, it +// provides the expected semantics: // // if field.subdef is field.subdef: // print "Sanity prevails." @@ -66,7 +66,7 @@ static PyObject *weakref_callback = NULL; // Utility functions for manipulating Python dictionaries keyed by pointer. -static PyObject *PyUpb_StringForPointer(void *ptr) { +static PyObject *PyUpb_StringForPointer(const void *ptr) { PyObject *o = PyString_FromStringAndSize((const char *)&ptr, sizeof(void*)); assert(o); return o; @@ -86,7 +86,7 @@ static PyObject *PyUpb_ObjCacheDeleteCallback(PyObject *self, PyObject *ref) { return Py_None; } -static PyObject *PyUpb_ObjCacheGet(void *obj, PyTypeObject *type) { +static PyObject *PyUpb_ObjCacheGet(const void *obj, PyTypeObject *type) { PyObject *kv = PyUpb_StringForPointer(obj); PyObject *ref = PyDict_GetItem(obj_cache, kv); PyObject *ret; @@ -96,7 +96,7 @@ static PyObject *PyUpb_ObjCacheGet(void *obj, PyTypeObject *type) { Py_INCREF(ret); } else { PyUpb_ObjWrapper *wrapper = (PyUpb_ObjWrapper*)type->tp_alloc(type, 0); - wrapper->obj = obj; + wrapper->obj = (void*)obj; wrapper->weakreflist = NULL; ret = (PyObject*)wrapper; ref = PyWeakref_NewRef(ret, weakref_callback); @@ -113,7 +113,7 @@ static PyObject *PyUpb_ObjCacheGet(void *obj, PyTypeObject *type) { /* PyUpb_Def ******************************************************************/ -static PyTypeObject *PyUpb_TypeForDef(upb_def *def); +static PyTypeObject *PyUpb_TypeForDef(const upb_def *def); static void PyUpb_Def_dealloc(PyObject *obj) { PyUpb_ObjWrapper *wrapper = (void*)obj; @@ -121,7 +121,7 @@ static void PyUpb_Def_dealloc(PyObject *obj) { obj->ob_type->tp_free(obj); } -PyObject *PyUpb_Def_GetOrCreate(upb_def *def) { +PyObject *PyUpb_Def_GetOrCreate(const upb_def *def) { return def ? PyUpb_ObjCacheGet(def, PyUpb_TypeForDef(def)) : Py_None; } @@ -142,7 +142,7 @@ static int PyUpb_FieldDef_setattro(PyObject *o, PyObject *key, PyObject *val); } \ } while(0) -static PyObject *PyUpb_FieldDef_GetOrCreate(upb_fielddef *f) { +static PyObject *PyUpb_FieldDef_GetOrCreate(const upb_fielddef *f) { return PyUpb_ObjCacheGet(f, &PyUpb_FieldDefType); } @@ -424,7 +424,7 @@ static PyTypeObject PyUpb_MessageDefType = { }; -static PyTypeObject *PyUpb_TypeForDef(upb_def *def) { +static PyTypeObject *PyUpb_TypeForDef(const upb_def *def) { switch(def->type) { case UPB_DEF_MSG: return &PyUpb_MessageDefType; default: return NULL; @@ -499,7 +499,7 @@ static PyObject *PyUpb_SymbolTable_add_def(PyObject *o, PyObject *def) { static PyObject *PyUpb_SymbolTable_defs(PyObject *o, PyObject *none) { upb_symtab *s = Check_SymbolTable(o, NULL); int count; - upb_def **defs = upb_symtab_getdefs(s, &count, UPB_DEF_ANY); + const upb_def **defs = upb_symtab_getdefs(s, &count, UPB_DEF_ANY); PyObject *ret = PyList_New(count); int i; for(i = 0; i < count; i++) @@ -510,7 +510,7 @@ static PyObject *PyUpb_SymbolTable_defs(PyObject *o, PyObject *none) { static PyObject *PyUpb_SymbolTable_lookup(PyObject *o, PyObject *arg) { upb_symtab *s = Check_SymbolTable(o, NULL); const char *name = PyString_AsString(arg); - upb_def *def = upb_symtab_lookup(s, name); + const upb_def *def = upb_symtab_lookup(s, name); return PyUpb_Def_GetOrCreate(def); } @@ -581,7 +581,7 @@ typedef struct { PyObject **PyUpb_Accessor_GetPtr(PyObject *_m, upb_value fval) { PyUpb_Message *m = (PyUpb_Message*)_m; - upb_fielddef *f = upb_value_getfielddef(fval); + const upb_fielddef *f = upb_value_getfielddef(fval); return (PyObject**)&m->data[f->offset]; } @@ -611,7 +611,7 @@ static upb_sflow_t PyUpb_Message_StartRepeatedSubmessage(void *a, upb_value fval static upb_flow_t PyUpb_Message_StringValue(void *m, upb_value fval, upb_value val) { PyObject **str = PyUpb_Accessor_GetPtr(m, fval); - if (*str) Py_DECREF(*str); + if (*str) { Py_DECREF(*str); } *str = PyString_FromStringAndSize(NULL, upb_value_getstrref(val)->len); upb_strref_read(upb_value_getstrref(val), PyString_AsString(*str)); upb_stdmsg_sethas(m, fval); -- cgit v1.2.3