25 #include "gen_units_class_plugin.h"
26 #include "option_extensions.pb.h"
27 #include <boost/algorithm/string.hpp>
29 #include <google/protobuf/compiler/code_generator.h>
30 #include <google/protobuf/compiler/plugin.h>
31 #include <google/protobuf/descriptor.h>
32 #include <google/protobuf/io/printer.h>
33 #include <google/protobuf/io/zero_copy_stream.h>
39 std::set<std::string> systems_to_include_;
40 std::set<std::string> base_units_to_include_;
41 std::string filename_h_;
42 std::string load_file_cpp_;
43 std::shared_ptr<std::fstream> load_file_output_;
45 std::string load_file_base_{
46 R
"DEL(#include <dccl/codec.h>
48 // DO NOT REMOVE: required by loader code
49 std::vector<const google::protobuf::Descriptor*> descriptors;
50 template<typename PB> struct DCCLLoader { DCCLLoader() { descriptors.push_back(PB::descriptor()); }};
51 // END: required by loader code
56 void dccl3_load(dccl::Codec* dccl)
58 // DO NOT REMOVE: required by loader code
59 for(auto d : descriptors)
61 // END: required by loader code
64 void dccl3_unload(dccl::Codec* dccl)
66 // DO NOT REMOVE: required by loader code
67 for(auto d : descriptors)
69 // END: required by loader code
73 // BEGIN (TO END OF FILE): AUTOGENERATED LOADERS
83 bool Generate(
const google::protobuf::FileDescriptor* file,
const std::string& parameter,
84 google::protobuf::compiler::GeneratorContext* generator_context,
85 std::string* error)
const override;
88 void generate_message(
89 const google::protobuf::Descriptor* desc,
90 google::protobuf::compiler::GeneratorContext* generator_context,
91 std::shared_ptr<std::string> message_unit_system = std::shared_ptr<std::string>())
const;
92 void generate_field(
const google::protobuf::FieldDescriptor* field,
93 google::protobuf::io::Printer* printer,
94 std::shared_ptr<std::string> message_unit_system)
const;
95 bool check_field_type(
const google::protobuf::FieldDescriptor* field)
const;
97 void generate_load_file_headers()
const;
98 void generate_load_file_message_loader(
const google::protobuf::Descriptor* desc)
const;
101 bool DCCLGenerator::check_field_type(
const google::protobuf::FieldDescriptor* field)
const
103 bool is_integer = field->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_INT32 ||
104 field->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_INT64 ||
105 field->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_UINT32 ||
106 field->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_UINT64;
108 bool is_float = field->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE ||
109 field->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_FLOAT;
111 if (!is_float && !is_integer)
113 throw std::runtime_error(
"Can only use (dccl.field).base_dimensions on numeric fields");
118 bool DCCLGenerator::Generate(
const google::protobuf::FileDescriptor* file,
119 const std::string& parameter,
120 google::protobuf::compiler::GeneratorContext* generator_context,
121 std::string* error)
const
123 std::vector<std::pair<std::string, std::string>> options;
124 google::protobuf::compiler::ParseGeneratorParameter(parameter, &options);
126 for (
const auto& option : options)
128 const auto& key = option.first;
129 const auto& value = option.second;
130 if (key ==
"dccl3_load_file")
132 load_file_cpp_ = value;
133 load_file_output_ = std::make_shared<std::fstream>(
134 load_file_cpp_, std::ios::in | std::ios::out | std::ios::app);
136 if (!load_file_output_->is_open())
138 *error =
"Failed to open dccl3_load_file: " + load_file_cpp_;
144 *error =
"Unknown parameter: " + key;
151 const std::string& filename = file->name();
152 filename_h_ = filename.substr(0, filename.find(
".proto")) +
".pb.h";
155 if (load_file_output_)
156 generate_load_file_headers();
158 for (
int message_i = 0, message_n = file->message_type_count(); message_i < message_n;
163 generate_message(file->message_type(message_i), generator_context);
165 catch (std::exception& e)
168 std::runtime_error(std::string(
"Failed to generate DCCL code: \n") + e.what()));
172 std::shared_ptr<google::protobuf::io::ZeroCopyOutputStream> include_output(
173 generator_context->OpenForInsert(filename_h_,
"includes"));
174 google::protobuf::io::Printer include_printer(include_output.get(),
'$');
175 std::stringstream includes_ss;
177 includes_ss <<
"#include <boost/units/quantity.hpp>" << std::endl;
178 includes_ss <<
"#include <boost/units/absolute.hpp>" << std::endl;
179 includes_ss <<
"#include <boost/units/dimensionless_type.hpp>" << std::endl;
180 includes_ss <<
"#include <boost/units/make_scaled_unit.hpp>" << std::endl;
182 for (
const auto& it : systems_to_include_) { include_units_headers(it, includes_ss); }
183 for (
const auto& it : base_units_to_include_)
184 { include_base_unit_headers(it, includes_ss); }
185 include_printer.Print(includes_ss.str().c_str());
189 catch (std::exception& e)
196 void DCCLGenerator::generate_message(
197 const google::protobuf::Descriptor* desc,
198 google::protobuf::compiler::GeneratorContext* generator_context,
199 std::shared_ptr<std::string> message_unit_system)
const
204 std::shared_ptr<google::protobuf::io::ZeroCopyOutputStream> output(
205 generator_context->OpenForInsert(filename_h_,
"class_scope:" + desc->full_name()));
206 google::protobuf::io::Printer printer(output.get(),
'$');
208 if (desc->options().HasExtension(dccl::msg))
210 if (desc->options().GetExtension(dccl::msg).id() != 0)
212 std::stringstream id_enum;
213 id_enum <<
"enum DCCLParameters { DCCL_ID = "
214 << desc->options().GetExtension(dccl::msg).id() <<
", "
215 <<
" DCCL_MAX_BYTES = "
216 << desc->options().GetExtension(dccl::msg).max_bytes() <<
" };\n";
217 printer.Print(id_enum.str().c_str());
219 if (load_file_output_)
220 generate_load_file_message_loader(desc);
225 desc->options().GetExtension(dccl::msg);
226 if (dccl_msg_options.has_unit_system())
228 message_unit_system.reset(
new std::string(dccl_msg_options.unit_system()));
229 systems_to_include_.insert(dccl_msg_options.unit_system());
233 for (
int field_i = 0, field_n = desc->field_count(); field_i < field_n; ++field_i)
234 { generate_field(desc->field(field_i), &printer, message_unit_system); }
236 for (
int nested_type_i = 0, nested_type_n = desc->nested_type_count();
237 nested_type_i < nested_type_n; ++nested_type_i)
238 generate_message(desc->nested_type(nested_type_i), generator_context,
239 message_unit_system);
242 catch (std::exception& e)
244 throw(std::runtime_error(std::string(
"Message: \n" + desc->full_name() +
"\n" + e.what())));
248 void DCCLGenerator::generate_field(
const google::protobuf::FieldDescriptor* field,
249 google::protobuf::io::Printer* printer,
250 std::shared_ptr<std::string> message_unit_system)
const
255 field->options().GetExtension(dccl::field);
257 if (!dccl_field_options.has_units())
262 if ((dccl_field_options.units().has_base_dimensions() &&
263 dccl_field_options.units().has_derived_dimensions()) ||
264 (dccl_field_options.units().has_base_dimensions() &&
265 dccl_field_options.units().has_unit()) ||
266 (dccl_field_options.units().has_unit() &&
267 dccl_field_options.units().has_derived_dimensions()))
269 throw(std::runtime_error(
"May define either (dccl.field).units.base_dimensions or "
270 "(dccl.field).units.derived_dimensions or "
271 "(dccl.field).units.unit, but not more than one."));
273 else if (dccl_field_options.units().has_unit())
275 std::stringstream new_methods;
277 construct_units_typedef_from_base_unit(
278 field->name(), dccl_field_options.units().unit(),
279 dccl_field_options.units().relative_temperature(),
280 dccl_field_options.units().prefix(), new_methods);
281 construct_field_class_plugin(field->name(), new_methods,
282 dccl::units::get_field_type_name(field->cpp_type()),
283 field->is_repeated());
284 printer->Print(new_methods.str().c_str());
285 base_units_to_include_.insert(dccl_field_options.units().unit());
287 else if (dccl_field_options.units().has_base_dimensions())
289 std::stringstream new_methods;
291 std::vector<double> powers;
292 std::vector<std::string> short_dimensions;
293 std::vector<std::string> dimensions;
294 if (dccl::units::parse_base_dimensions(
295 dccl_field_options.units().base_dimensions().begin(),
296 dccl_field_options.units().base_dimensions().end(), powers, short_dimensions,
299 if (!dccl_field_options.units().has_system() && !message_unit_system)
300 throw(std::runtime_error(
301 std::string(
"Field must have 'system' defined or message must have "
302 "'unit_system' defined when using 'base_dimensions'.")));
305 const std::string& unit_system =
306 (!dccl_field_options.units().has_system() && message_unit_system)
307 ? *message_unit_system
308 : dccl_field_options.units().system();
310 construct_base_dims_typedef(dimensions, powers, field->name(), unit_system,
311 dccl_field_options.units().relative_temperature(),
312 dccl_field_options.units().prefix(), new_methods);
314 construct_field_class_plugin(field->name(), new_methods,
315 dccl::units::get_field_type_name(field->cpp_type()),
316 field->is_repeated());
317 printer->Print(new_methods.str().c_str());
318 systems_to_include_.insert(unit_system);
322 throw(std::runtime_error(std::string(
"Failed to parse base_dimensions string: \"" +
323 dccl_field_options.units().base_dimensions() +
327 else if (dccl_field_options.units().has_derived_dimensions())
329 std::stringstream new_methods;
331 std::vector<std::string> operators;
332 std::vector<std::string> dimensions;
333 if (dccl::units::parse_derived_dimensions(
334 dccl_field_options.units().derived_dimensions().begin(),
335 dccl_field_options.units().derived_dimensions().end(), operators, dimensions))
337 if (!dccl_field_options.units().has_system() && !message_unit_system)
338 throw(std::runtime_error(
339 std::string(
"Field must have 'system' defined or message must have "
340 "'unit_system' defined when using 'derived_dimensions'.")));
341 const std::string& unit_system =
342 (!dccl_field_options.units().has_system() && message_unit_system)
343 ? *message_unit_system
344 : dccl_field_options.units().system();
346 construct_derived_dims_typedef(dimensions, operators, field->name(), unit_system,
347 dccl_field_options.units().relative_temperature(),
348 dccl_field_options.units().prefix(), new_methods);
350 construct_field_class_plugin(field->name(), new_methods,
351 dccl::units::get_field_type_name(field->cpp_type()),
352 field->is_repeated());
353 printer->Print(new_methods.str().c_str());
354 systems_to_include_.insert(unit_system);
358 throw(std::runtime_error(
359 std::string(
"Failed to parse derived_dimensions string: \"" +
360 dccl_field_options.units().derived_dimensions() +
"\"")));
364 catch (std::exception& e)
367 std::runtime_error(std::string(
"Field: \n" + field->DebugString() +
"\n" + e.what())));
371 void DCCLGenerator::generate_load_file_headers()
const
373 bool file_is_empty = (load_file_output_->peek() == std::ifstream::traits_type::eof());
374 load_file_output_->clear();
376 bool header_already_written =
false;
379 *load_file_output_ << load_file_base_ << std::flush;
383 for (std::string line; getline(*load_file_output_, line);)
385 if (line.find(
"\"" + filename_h_ +
"\"") != std::string::npos)
387 header_already_written =
true;
393 load_file_output_->clear();
395 load_file_output_->seekp(0, std::ios_base::end);
397 if (!header_already_written)
398 *load_file_output_ <<
"#include \"" << filename_h_ <<
"\"" << std::endl;
401 void DCCLGenerator::generate_load_file_message_loader(
402 const google::protobuf::Descriptor* desc)
const
405 load_file_output_->seekp(0, std::ios_base::beg);
408 std::string cpp_name = desc->full_name();
409 boost::algorithm::replace_all(cpp_name,
".",
"::");
412 std::string loader_name = desc->full_name() +
"_loader";
413 boost::algorithm::replace_all(loader_name,
".",
"_");
414 boost::algorithm::to_lower(loader_name);
416 bool loader_already_written =
false;
417 for (std::string line; getline(*load_file_output_, line);)
419 if (line.find(
"<" + cpp_name +
">") != std::string::npos)
421 loader_already_written =
true;
425 load_file_output_->clear();
426 load_file_output_->seekp(0, std::ios_base::end);
428 if (!loader_already_written)
429 *load_file_output_ <<
"DCCLLoader<" << cpp_name <<
"> " << loader_name <<
";" << std::endl;
432 int main(
int argc,
char* argv[])
435 return google::protobuf::compiler::PluginMain(argc, argv, &generator);