DCCL v4
oneof.h
1 // Copyright 2021-2023:
2 // GobySoft, LLC (2013-)
3 // Community contributors (see AUTHORS file)
4 // File authors:
5 // Toby Schneider <toby@gobysoft.org>
6 // Davide Fenucci <davfen@noc.ac.uk>
7 //
8 //
9 // This file is part of the Dynamic Compact Control Language Library
10 // ("DCCL").
11 //
12 // DCCL is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as published by
14 // the Free Software Foundation, either version 2.1 of the License, or
15 // (at your option) any later version.
16 //
17 // DCCL is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public License
23 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
24 #ifndef DCCLONEOF20210702H
25 #define DCCLONEOF20210702H
26 
27 #include "binary.h"
28 #include <google/protobuf/descriptor.h>
29 #include <google/protobuf/descriptor.pb.h>
30 
31 namespace dccl
32 {
36 inline bool is_part_of_oneof(const google::protobuf::FieldDescriptor* field_desc)
37 {
38  return field_desc->containing_oneof();
39 }
40 
45 inline int containing_oneof_index(const google::protobuf::FieldDescriptor* field_desc)
46 {
47  if (is_part_of_oneof(field_desc))
48  return field_desc->containing_oneof()->index();
49  return -1;
50 }
51 
57 inline int oneof_size(const google::protobuf::OneofDescriptor* oneof_desc)
58 {
59  return dccl::ceil_log2(oneof_desc->field_count() + 1);
60 }
61 } // namespace dccl
62 
63 #endif
dccl::containing_oneof_index
int containing_oneof_index(const google::protobuf::FieldDescriptor *field_desc)
Returns the index of the containing oneof of the given field, or -1 if the field is not part of a one...
Definition: oneof.h:45
dccl
Dynamic Compact Control Language namespace.
Definition: any.h:46
dccl::oneof_size
int oneof_size(const google::protobuf::OneofDescriptor *oneof_desc)
Returns the number of bits needed to represent the oneof cases (including the unset case).
Definition: oneof.h:57
dccl::ceil_log2
unsigned ceil_log2(dccl::uint64 v)
Definition: binary.h:178
dccl::is_part_of_oneof
bool is_part_of_oneof(const google::protobuf::FieldDescriptor *field_desc)
Checks whether a given field is part to a oneof or not.
Definition: oneof.h:36