• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #ifndef RUBY_PROTOBUF_DEFS_H_
9 #define RUBY_PROTOBUF_DEFS_H_
10 
11 #include "protobuf.h"
12 #include "ruby-upb.h"
13 
14 // -----------------------------------------------------------------------------
15 // TypeInfo
16 // -----------------------------------------------------------------------------
17 
18 // This bundles a upb_CType and msgdef/enumdef when appropriate. This is
19 // convenient for functions that need type information but cannot necessarily
20 // assume a upb_FieldDef will be available.
21 //
22 // For example, Google::Protobuf::Map and Google::Protobuf::RepeatedField can
23 // be constructed with type information alone:
24 //
25 //   # RepeatedField will internally store the type information in a TypeInfo.
26 //   Google::Protobuf::RepeatedField.new(:message, FooMessage)
27 
28 typedef struct {
29   upb_CType type;
30   union {
31     const upb_MessageDef* msgdef;  // When type == kUpb_CType_Message
32     const upb_EnumDef* enumdef;    // When type == kUpb_CType_Enum
33   } def;
34 } TypeInfo;
35 
TypeInfo_get(const upb_FieldDef * f)36 static inline TypeInfo TypeInfo_get(const upb_FieldDef* f) {
37   TypeInfo ret = {upb_FieldDef_CType(f), {NULL}};
38   switch (ret.type) {
39     case kUpb_CType_Message:
40       ret.def.msgdef = upb_FieldDef_MessageSubDef(f);
41       break;
42     case kUpb_CType_Enum:
43       ret.def.enumdef = upb_FieldDef_EnumSubDef(f);
44       break;
45     default:
46       break;
47   }
48   return ret;
49 }
50 
51 TypeInfo TypeInfo_FromClass(int argc, VALUE* argv, int skip_arg,
52                             VALUE* type_class, VALUE* init_arg);
53 
TypeInfo_from_type(upb_CType type)54 static inline TypeInfo TypeInfo_from_type(upb_CType type) {
55   TypeInfo ret = {type};
56   assert(type != kUpb_CType_Message && type != kUpb_CType_Enum);
57   return ret;
58 }
59 
60 // -----------------------------------------------------------------------------
61 // Other utilities
62 // -----------------------------------------------------------------------------
63 
64 VALUE Descriptor_DefToClass(const upb_MessageDef* m);
65 
66 // Returns the underlying msgdef, enumdef, or symtab (respectively) for the
67 // given Descriptor, EnumDescriptor, or DescriptorPool Ruby object.
68 const upb_EnumDef* EnumDescriptor_GetEnumDef(VALUE enum_desc_rb);
69 const upb_DefPool* DescriptorPool_GetSymtab(VALUE desc_pool_rb);
70 const upb_MessageDef* Descriptor_GetMsgDef(VALUE desc_rb);
71 
72 // Returns a upb field type for the given Ruby symbol
73 // (eg. :float => kUpb_CType_Float).
74 upb_CType ruby_to_fieldtype(VALUE type);
75 
76 // The singleton generated pool (a DescriptorPool object).
77 extern VALUE generated_pool;
78 
79 // Call at startup to register all types in this module.
80 void Defs_register(VALUE module);
81 
82 #endif  // RUBY_PROTOBUF_DEFS_H_
83