File: defs.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (82 lines) | stat: -rw-r--r-- 2,816 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

#ifndef RUBY_PROTOBUF_DEFS_H_
#define RUBY_PROTOBUF_DEFS_H_

#include "protobuf.h"
#include "ruby-upb.h"

// -----------------------------------------------------------------------------
// TypeInfo
// -----------------------------------------------------------------------------

// This bundles a upb_CType and msgdef/enumdef when appropriate. This is
// convenient for functions that need type information but cannot necessarily
// assume a upb_FieldDef will be available.
//
// For example, Google::Protobuf::Map and Google::Protobuf::RepeatedField can
// be constructed with type information alone:
//
//   # RepeatedField will internally store the type information in a TypeInfo.
//   Google::Protobuf::RepeatedField.new(:message, FooMessage)

typedef struct {
  upb_CType type;
  union {
    const upb_MessageDef* msgdef;  // When type == kUpb_CType_Message
    const upb_EnumDef* enumdef;    // When type == kUpb_CType_Enum
  } def;
} TypeInfo;

static inline TypeInfo TypeInfo_get(const upb_FieldDef* f) {
  TypeInfo ret = {upb_FieldDef_CType(f), {NULL}};
  switch (ret.type) {
    case kUpb_CType_Message:
      ret.def.msgdef = upb_FieldDef_MessageSubDef(f);
      break;
    case kUpb_CType_Enum:
      ret.def.enumdef = upb_FieldDef_EnumSubDef(f);
      break;
    default:
      break;
  }
  return ret;
}

TypeInfo TypeInfo_FromClass(int argc, VALUE* argv, int skip_arg,
                            VALUE* type_class, VALUE* init_arg);

static inline TypeInfo TypeInfo_from_type(upb_CType type) {
  TypeInfo ret = {type};
  assert(type != kUpb_CType_Message && type != kUpb_CType_Enum);
  return ret;
}

// -----------------------------------------------------------------------------
// Other utilities
// -----------------------------------------------------------------------------

VALUE Descriptor_DefToClass(const upb_MessageDef* m);

// Returns the underlying msgdef, enumdef, or symtab (respectively) for the
// given Descriptor, EnumDescriptor, or DescriptorPool Ruby object.
const upb_EnumDef* EnumDescriptor_GetEnumDef(VALUE enum_desc_rb);
const upb_DefPool* DescriptorPool_GetSymtab(VALUE desc_pool_rb);
const upb_MessageDef* Descriptor_GetMsgDef(VALUE desc_rb);

// Returns a upb field type for the given Ruby symbol
// (eg. :float => kUpb_CType_Float).
upb_CType ruby_to_fieldtype(VALUE type);

// The singleton generated pool (a DescriptorPool object).
extern VALUE generated_pool;

// Call at startup to register all types in this module.
void Defs_register(VALUE module);

#endif  // RUBY_PROTOBUF_DEFS_H_