DCCL v4
field_codec_manager.cpp
1 // Copyright 2011-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 "field_codec_manager.h"
25 
26 dccl::FieldCodecManagerLocal::FieldCodecManagerLocal()
27  : deprecated_names_{{"_static", "dccl.static"}, {"_time", "dccl.time"}}
28 {
29 }
30 
31 dccl::FieldCodecManagerLocal::~FieldCodecManagerLocal() = default;
32 
33 std::shared_ptr<dccl::FieldCodecBase>
34 dccl::FieldCodecManagerLocal::__find(google::protobuf::FieldDescriptor::Type type,
35  int codec_version, const std::string& codec_name,
36  const std::string& type_name) const
37 {
38  check_deprecated(codec_name);
39 
40  auto it = codecs_.find(type);
41 
42  std::vector<std::string> codec_names_to_try;
43  if (it != codecs_.end())
44  {
45  auto inside_it = it->second.end();
46 
47  // try appending codec_version first
48  if (!std::isdigit(codec_name.back()))
49  codec_names_to_try.push_back(codec_name + std::to_string(codec_version));
50 
51  codec_names_to_try.push_back(codec_name);
52 
53  for (const std::string& c_name : codec_names_to_try)
54  {
55  // try specific type codec
56  inside_it = it->second.find(__mangle_name(c_name, type_name));
57  if (inside_it != it->second.end())
58  return inside_it->second;
59 
60  // try general
61  inside_it = it->second.find(c_name);
62  if (inside_it != it->second.end())
63  return inside_it->second;
64  }
65  }
66 
67  std::stringstream err_ss;
68  err_ss << "No codec by the name `" << codec_name
69  << "` found for type: " << type_helper().find(type)->as_str() << " (tried names:";
70  for (const std::string& c_name : codec_names_to_try) err_ss << " `" << c_name << "`";
71  err_ss << ")";
72  throw(Exception(err_ss.str()));
73 }
74 
75 void dccl::FieldCodecManagerLocal::check_deprecated(const std::string& codec_name) const
76 {
77  auto it = deprecated_names_.find(codec_name);
78  if (it != deprecated_names_.end())
79  {
80  dlog.is(dccl::logger::DEBUG1) && dlog << "Codec name \"" << it->first
81  << "\" is deprecated: use \"" << it->second
82  << "\" instead " << std::endl;
83  }
84 }
dccl::Logger::is
bool is(logger::Verbosity verbosity, logger::Group group=logger::GENERAL)
Indicates the verbosity of the Logger until the next std::flush or std::endl. The boolean return is u...
Definition: logger.h:192