DCCL v3
field_codec_manager.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 #include "field_codec_manager.h"
23 
24 std::map<google::protobuf::FieldDescriptor::Type, dccl::FieldCodecManager::InsideMap> dccl::FieldCodecManager::codecs_;
25 
26 
27 boost::shared_ptr<dccl::FieldCodecBase>
28 dccl::FieldCodecManager::__find(google::protobuf::FieldDescriptor::Type type,
29  const std::string& codec_name,
30  const std::string& type_name /* = "" */)
31 {
32  typedef InsideMap::const_iterator InsideIterator;
33  typedef std::map<google::protobuf::FieldDescriptor::Type, InsideMap>::const_iterator Iterator;
34 
35  Iterator it = codecs_.find(type);
36  if(it != codecs_.end())
37  {
38  InsideIterator inside_it = it->second.end();
39  // try specific type codec
40  inside_it = it->second.find(__mangle_name(codec_name, type_name));
41  if(inside_it != it->second.end())
42  return inside_it->second;
43 
44  // try general
45  inside_it = it->second.find(codec_name);
46  if(inside_it != it->second.end())
47  return inside_it->second;
48  }
49 
50  throw(Exception("No codec by the name `" + codec_name + "` found for type: " + internal::TypeHelper::find(type)->as_str()));
51 }
52 
53