DCCL v3
dccl_native_protobuf.cpp
1 // Copyright 2009-2017 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (for 2013-)
3 // Massachusetts Institute of Technology (for 2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Dynamic Compact Control Language Library
8 // ("DCCL").
9 //
10 // DCCL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // DCCL is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
22 
23 #include "dccl_native_protobuf.h"
24 #include "dccl/codec.h"
25 
26 extern "C"
27 {
28  void dccl3_load(dccl::Codec* dccl)
29  {
30  using namespace dccl;
31  using namespace dccl::native_protobuf;
32  using google::protobuf::FieldDescriptor;
33 
34  const char* native_pb_group = "dccl.native_protobuf";
35 
36  FieldCodecManager::add<v3::DefaultMessageCodec, FieldDescriptor::TYPE_MESSAGE>(native_pb_group);
37  FieldCodecManager::add<PrimitiveTypeFieldCodec<dccl::int64, FieldDescriptor::TYPE_INT64>,
38  FieldDescriptor::TYPE_INT64>(native_pb_group);
39  FieldCodecManager::add<PrimitiveTypeFieldCodec<dccl::int32, FieldDescriptor::TYPE_INT32>,
40  FieldDescriptor::TYPE_INT32>(native_pb_group);
41 
42  FieldCodecManager::add<PrimitiveTypeFieldCodec<dccl::int64, FieldDescriptor::TYPE_SINT64>,
43  FieldDescriptor::TYPE_SINT64>(native_pb_group);
44  FieldCodecManager::add<PrimitiveTypeFieldCodec<dccl::int32, FieldDescriptor::TYPE_SINT32>,
45  FieldDescriptor::TYPE_SINT32>(native_pb_group);
46 
47  FieldCodecManager::add<PrimitiveTypeFieldCodec<dccl::uint64, FieldDescriptor::TYPE_UINT64>,
48  FieldDescriptor::TYPE_UINT64>(native_pb_group);
49  FieldCodecManager::add<PrimitiveTypeFieldCodec<dccl::uint32, FieldDescriptor::TYPE_UINT32>,
50  FieldDescriptor::TYPE_UINT32>(native_pb_group);
51 
52  FieldCodecManager::add<PrimitiveTypeFieldCodec<dccl::int64, FieldDescriptor::TYPE_SFIXED64>,
53  FieldDescriptor::TYPE_SFIXED64>(native_pb_group);
54  FieldCodecManager::add<PrimitiveTypeFieldCodec<dccl::int32, FieldDescriptor::TYPE_SFIXED32>,
55  FieldDescriptor::TYPE_SFIXED32>(native_pb_group);
56 
57  FieldCodecManager::add<PrimitiveTypeFieldCodec<dccl::uint64, FieldDescriptor::TYPE_FIXED64>,
58  FieldDescriptor::TYPE_FIXED64>(native_pb_group);
59  FieldCodecManager::add<PrimitiveTypeFieldCodec<dccl::uint32, FieldDescriptor::TYPE_FIXED32>,
60  FieldDescriptor::TYPE_FIXED32>(native_pb_group);
61 
62  FieldCodecManager::add<PrimitiveTypeFieldCodec<double, FieldDescriptor::TYPE_DOUBLE>,
63  FieldDescriptor::TYPE_DOUBLE>(native_pb_group);
64  FieldCodecManager::add<PrimitiveTypeFieldCodec<float, FieldDescriptor::TYPE_FLOAT>,
65  FieldDescriptor::TYPE_FLOAT>(native_pb_group);
66  FieldCodecManager::add<PrimitiveTypeFieldCodec<bool, FieldDescriptor::TYPE_BOOL>,
67  FieldDescriptor::TYPE_BOOL>(native_pb_group);
68 
69  FieldCodecManager::add<EnumFieldCodec, FieldDescriptor::TYPE_ENUM>(native_pb_group);
70 
71 
72  // ADD ENUM
73 
74  }
75 
76  void dccl3_unload(dccl::Codec* dccl)
77  {
78  using namespace dccl;
79  using namespace dccl::native_protobuf;
80  using google::protobuf::FieldDescriptor;
81 
82  const char* native_pb_group = "dccl.native_protobuf";
83 
84  FieldCodecManager::remove<PrimitiveTypeFieldCodec<dccl::int64, FieldDescriptor::TYPE_INT64>,
85  FieldDescriptor::TYPE_INT64>(native_pb_group);
86  FieldCodecManager::remove<PrimitiveTypeFieldCodec<dccl::int32, FieldDescriptor::TYPE_INT32>,
87  FieldDescriptor::TYPE_INT32>(native_pb_group);
88 
89  FieldCodecManager::remove<PrimitiveTypeFieldCodec<dccl::int64, FieldDescriptor::TYPE_SINT64>,
90  FieldDescriptor::TYPE_SINT64>(native_pb_group);
91  FieldCodecManager::remove<PrimitiveTypeFieldCodec<dccl::int32, FieldDescriptor::TYPE_SINT32>,
92  FieldDescriptor::TYPE_SINT32>(native_pb_group);
93 
94  FieldCodecManager::remove<PrimitiveTypeFieldCodec<dccl::uint64, FieldDescriptor::TYPE_UINT64>,
95  FieldDescriptor::TYPE_UINT64>(native_pb_group);
96  FieldCodecManager::remove<PrimitiveTypeFieldCodec<dccl::uint32, FieldDescriptor::TYPE_UINT32>,
97  FieldDescriptor::TYPE_UINT32>(native_pb_group);
98 
99  FieldCodecManager::remove<PrimitiveTypeFieldCodec<dccl::int64, FieldDescriptor::TYPE_SFIXED64>,
100  FieldDescriptor::TYPE_SFIXED64>(native_pb_group);
101  FieldCodecManager::remove<PrimitiveTypeFieldCodec<dccl::int32, FieldDescriptor::TYPE_SFIXED32>,
102  FieldDescriptor::TYPE_SFIXED32>(native_pb_group);
103 
104  FieldCodecManager::remove<PrimitiveTypeFieldCodec<dccl::uint64, FieldDescriptor::TYPE_FIXED64>,
105  FieldDescriptor::TYPE_FIXED64>(native_pb_group);
106  FieldCodecManager::remove<PrimitiveTypeFieldCodec<dccl::uint32, FieldDescriptor::TYPE_FIXED32>,
107  FieldDescriptor::TYPE_FIXED32>(native_pb_group);
108 
109  FieldCodecManager::remove<PrimitiveTypeFieldCodec<double, FieldDescriptor::TYPE_DOUBLE>,
110  FieldDescriptor::TYPE_DOUBLE>(native_pb_group);
111  FieldCodecManager::remove<PrimitiveTypeFieldCodec<float, FieldDescriptor::TYPE_FLOAT>,
112  FieldDescriptor::TYPE_FLOAT>(native_pb_group);
113  FieldCodecManager::remove<PrimitiveTypeFieldCodec<bool, FieldDescriptor::TYPE_BOOL>,
114  FieldDescriptor::TYPE_BOOL>(native_pb_group);
115 
116  FieldCodecManager::remove<EnumFieldCodec, FieldDescriptor::TYPE_ENUM>(native_pb_group);
117 
118 
119  FieldCodecManager::remove<v3::DefaultMessageCodec, FieldDescriptor::TYPE_MESSAGE>(native_pb_group);
120 
121  }
122 }
123 
124 
125 int dccl::native_protobuf::EnumFieldCodec::pre_encode(const google::protobuf::EnumValueDescriptor* const& field_value)
126 { return field_value->index(); }
127 
128 const google::protobuf::EnumValueDescriptor* dccl::native_protobuf::EnumFieldCodec::post_decode(const int& wire_value)
129 {
130  const google::protobuf::EnumDescriptor* e = this_field()->enum_type();
131 
132  if(wire_value < e->value_count())
133  {
134  const google::protobuf::EnumValueDescriptor* return_value = e->value(wire_value);
135  return return_value;
136  }
137  else
138  throw NullValueException();
139 }
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition: codec.h:56
Exception used to signal null (non-existent) value within field codecs during decode.
Definition: exception.h:40
int pre_encode(const google::protobuf::EnumValueDescriptor *const &field_value)
Convert from the FieldType representation (used in the Google Protobuf message) to the WireType repre...
Dynamic Compact Control Language namespace.
Implements the default Google Protocol Buffers encoder for a variety of numeric types.
const google::protobuf::EnumValueDescriptor * post_decode(const int &wire_value)
Convert from the WireType representation (used with encode() and decode(), i.e. "on the wire") to the...