Beispiel #1
0
// Returns an ordering of all fields based on:
// 1. required/optional (required fields first).
// 2. field number
static int upb_fielddef_cmphasbit(const void *_f1, const void *_f2) {
  upb_fielddef *f1 = *(void**)_f1;
  upb_fielddef *f2 = *(void**)_f2;
  size_t req1 = f1->label == UPB_LABEL(REQUIRED);
  size_t req2 = f2->label == UPB_LABEL(REQUIRED);
  if (req1 != req2) return req1 - req2;
  // Otherwise return in number order.
  return f1->number - f2->number;
}
Beispiel #2
0
PyMODINIT_FUNC initupb(void) {
  PyObject *mod = Py_InitModule("upb", methods);

  PyUpb_AddType(mod, "FieldDef", &PyUpb_FieldDefType);
  PyUpb_AddType(mod, "MessageDef", &PyUpb_MessageDefType);
  PyUpb_AddType(mod, "SymbolTable", &PyUpb_SymbolTableType);

  PyModule_AddIntConstant(mod, "LABEL_OPTIONAL", UPB_LABEL(OPTIONAL));
  PyModule_AddIntConstant(mod, "LABEL_REQUIRED", UPB_LABEL(REQUIRED));
  PyModule_AddIntConstant(mod, "LABEL_REPEATED", UPB_LABEL(REPEATED));

  PyModule_AddIntConstant(mod, "TYPE_DOUBLE", UPB_TYPE(DOUBLE));
  PyModule_AddIntConstant(mod, "TYPE_FLOAT", UPB_TYPE(FLOAT));
  PyModule_AddIntConstant(mod, "TYPE_INT64", UPB_TYPE(INT64));
  PyModule_AddIntConstant(mod, "TYPE_UINT64", UPB_TYPE(UINT64));
  PyModule_AddIntConstant(mod, "TYPE_INT32", UPB_TYPE(INT32));
  PyModule_AddIntConstant(mod, "TYPE_FIXED64", UPB_TYPE(FIXED64));
  PyModule_AddIntConstant(mod, "TYPE_FIXED32", UPB_TYPE(FIXED32));
  PyModule_AddIntConstant(mod, "TYPE_BOOL", UPB_TYPE(BOOL));
  PyModule_AddIntConstant(mod, "TYPE_STRING", UPB_TYPE(STRING));
  PyModule_AddIntConstant(mod, "TYPE_GROUP", UPB_TYPE(GROUP));
  PyModule_AddIntConstant(mod, "TYPE_MESSAGE", UPB_TYPE(MESSAGE));
  PyModule_AddIntConstant(mod, "TYPE_BYTES", UPB_TYPE(BYTES));
  PyModule_AddIntConstant(mod, "TYPE_UINT32", UPB_TYPE(UINT32));
  PyModule_AddIntConstant(mod, "TYPE_ENUM", UPB_TYPE(ENUM));
  PyModule_AddIntConstant(mod, "TYPE_SFIXED32", UPB_TYPE(SFIXED32));
  PyModule_AddIntConstant(mod, "TYPE_SFIXED64", UPB_TYPE(SFIXED64));
  PyModule_AddIntConstant(mod, "TYPE_SINT32", UPB_TYPE(SINT32));
  PyModule_AddIntConstant(mod, "TYPE_SINT64", UPB_TYPE(SINT64));

  obj_cache = PyDict_New();
  reverse_cache = PyDict_New();
  static PyMethodDef method = {
        "WeakRefCallback", &PyUpb_ObjCacheDeleteCallback, METH_O, NULL};
  PyObject *pyname = PyString_FromString(method.ml_name);
  weakref_callback = PyCFunction_NewEx(&method, NULL, pyname);
  Py_DECREF(pyname);
}
Beispiel #3
0
upb_fielddef *upb_fielddef_new() {
  upb_fielddef *f = malloc(sizeof(*f));
  f->msgdef = NULL;
  f->def = NULL;
  upb_atomic_init(&f->refcount, 1);
  f->finalized = false;
  f->label = UPB_LABEL(OPTIONAL);
  f->hasbit = -1;
  f->offset = 0;
  f->accessor = NULL;
  upb_value_setfielddef(&f->fval, f);

  // These are initialized to be invalid; the user must set them explicitly.
  // Could relax this later if it's convenient and non-confusing to have a
  // defaults for them.
  f->name = NULL;
  f->type = 0;
  f->number = 0;

  upb_fielddef_init_default(f);
  return f;
}
Beispiel #4
0
upb_fielddef *upb_fielddef_new(const void *owner) {
  static const struct upb_refcounted_vtbl vtbl = {visitfield, freefield};
  upb_fielddef *f = malloc(sizeof(*f));
  if (!f) return NULL;
  if (!upb_def_init(upb_upcast(f), UPB_DEF_FIELD, &vtbl, owner)) {
    free(f);
    return NULL;
  }
  f->msgdef = NULL;
  f->sub.def = NULL;
  f->subdef_is_symbolic = false;
  f->subdef_is_owned = false;
  f->label_ = UPB_LABEL(OPTIONAL);

  // These are initialized to be invalid; the user must set them explicitly.
  // Could relax this later if it's convenient and non-confusing to have a
  // defaults for them.
  f->type_ = UPB_TYPE_NONE;
  f->number_ = 0;

  upb_fielddef_init_default(f);
  return f;
}