DCCL v4
dccl_native_protobuf.cpp
1 // Copyright 2012-2023:
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 // File authors:
6 // Toby Schneider <toby@gobysoft.org>
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 #include "dccl_native_protobuf.h"
25 #include "../codec.h"
26 #include "../codecs4/field_codec_default_message.h"
27 
28 extern "C"
29 {
30  void dccl3_load(dccl::Codec* dccl)
31  {
32  using namespace dccl;
33  using namespace dccl::native_protobuf;
34  using google::protobuf::FieldDescriptor;
35 
36  const char* native_pb_group = "dccl.native_protobuf";
37 
38  dccl->manager().add<v4::DefaultMessageCodec, FieldDescriptor::TYPE_MESSAGE>(
39  native_pb_group);
40  dccl->manager()
42  FieldDescriptor::TYPE_INT64>(native_pb_group);
43  dccl->manager()
45  FieldDescriptor::TYPE_INT32>(native_pb_group);
46 
47  dccl->manager()
49  FieldDescriptor::TYPE_SINT64>(native_pb_group);
50  dccl->manager()
52  FieldDescriptor::TYPE_SINT32>(native_pb_group);
53 
54  dccl->manager()
56  FieldDescriptor::TYPE_UINT64>(native_pb_group);
57  dccl->manager()
59  FieldDescriptor::TYPE_UINT32>(native_pb_group);
60 
61  dccl->manager()
63  FieldDescriptor::TYPE_SFIXED64>(native_pb_group);
64  dccl->manager()
66  FieldDescriptor::TYPE_SFIXED32>(native_pb_group);
67 
68  dccl->manager()
70  FieldDescriptor::TYPE_FIXED64>(native_pb_group);
71  dccl->manager()
73  FieldDescriptor::TYPE_FIXED32>(native_pb_group);
74 
75  dccl->manager()
77  FieldDescriptor::TYPE_DOUBLE>(native_pb_group);
78  dccl->manager()
80  FieldDescriptor::TYPE_FLOAT>(native_pb_group);
81  dccl->manager()
83  FieldDescriptor::TYPE_BOOL>(native_pb_group);
84 
85  dccl->manager().add<EnumFieldCodec, FieldDescriptor::TYPE_ENUM>(native_pb_group);
86 
87  // ADD ENUM
88  }
89 
90  void dccl3_unload(dccl::Codec* dccl)
91  {
92  using namespace dccl;
93  using namespace dccl::native_protobuf;
94  using google::protobuf::FieldDescriptor;
95 
96  const char* native_pb_group = "dccl.native_protobuf";
97 
98  dccl->manager()
100  FieldDescriptor::TYPE_INT64>(native_pb_group);
101  dccl->manager()
103  FieldDescriptor::TYPE_INT32>(native_pb_group);
104 
105  dccl->manager()
107  FieldDescriptor::TYPE_SINT64>(native_pb_group);
108  dccl->manager()
110  FieldDescriptor::TYPE_SINT32>(native_pb_group);
111 
112  dccl->manager()
114  FieldDescriptor::TYPE_UINT64>(native_pb_group);
115  dccl->manager()
117  FieldDescriptor::TYPE_UINT32>(native_pb_group);
118 
119  dccl->manager()
121  FieldDescriptor::TYPE_SFIXED64>(native_pb_group);
122  dccl->manager()
124  FieldDescriptor::TYPE_SFIXED32>(native_pb_group);
125 
126  dccl->manager()
128  FieldDescriptor::TYPE_FIXED64>(native_pb_group);
129  dccl->manager()
131  FieldDescriptor::TYPE_FIXED32>(native_pb_group);
132 
133  dccl->manager()
135  FieldDescriptor::TYPE_DOUBLE>(native_pb_group);
136  dccl->manager()
138  FieldDescriptor::TYPE_FLOAT>(native_pb_group);
139  dccl->manager()
141  FieldDescriptor::TYPE_BOOL>(native_pb_group);
142 
143  dccl->manager().remove<EnumFieldCodec, FieldDescriptor::TYPE_ENUM>(native_pb_group);
144 
145  dccl->manager().remove<v4::DefaultMessageCodec, FieldDescriptor::TYPE_MESSAGE>(
146  native_pb_group);
147  }
148 }
149 
151  const google::protobuf::EnumValueDescriptor* const& field_value)
152 {
153  return field_value->index();
154 }
155 
156 const google::protobuf::EnumValueDescriptor*
158 {
159  const google::protobuf::EnumDescriptor* e = this_field()->enum_type();
160 
161  if (wire_value < e->value_count())
162  {
163  const google::protobuf::EnumValueDescriptor* return_value = e->value(wire_value);
164  return return_value;
165  }
166  else
167  throw NullValueException();
168 }
dccl::v4::DefaultMessageCodec
Provides the default codec for encoding a base Google Protobuf message or an embedded message by call...
Definition: field_codec_default_message.h:40
dccl
Dynamic Compact Control Language namespace.
Definition: any.h:46
dccl::native_protobuf::PrimitiveTypeFieldCodec
Definition: dccl_native_protobuf.h:300
dccl::Codec
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition: codec.h:62
dccl::native_protobuf::EnumFieldCodec::pre_encode
int pre_encode(const google::protobuf::EnumValueDescriptor *const &field_value) override
Convert from the FieldType representation (used in the Google Protobuf message) to the WireType repre...
Definition: dccl_native_protobuf.cpp:150
dccl::native_protobuf
Implements the default Google Protocol Buffers encoder for a variety of numeric types.
Definition: dccl_native_protobuf.h:43
dccl::NullValueException
Exception used to signal null (non-existent) value within field codecs during decode.
Definition: exception.h:61
dccl::native_protobuf::EnumFieldCodec
Definition: dccl_native_protobuf.h:393
dccl::native_protobuf::EnumFieldCodec::post_decode
const google::protobuf::EnumValueDescriptor * post_decode(const int &wire_value) override
Convert from the WireType representation (used with encode() and decode(), i.e. "on the wire") to the...
Definition: dccl_native_protobuf.cpp:157