DCCL v3
dynamic_protobuf_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 <sstream>
23 
24 #include "dynamic_protobuf_manager.h"
25 #include "logger.h"
26 #include "exception.h"
27 
28 boost::shared_ptr<dccl::DynamicProtobufManager> dccl::DynamicProtobufManager::inst_;
29 
30 const google::protobuf::FileDescriptor* dccl::DynamicProtobufManager::add_protobuf_file(const google::protobuf::FileDescriptorProto& proto)
31 {
32  simple_database().Add(proto);
33 
34  const google::protobuf::FileDescriptor* return_desc = user_descriptor_pool().FindFileByName(proto.name());
35  return return_desc;
36 }
37 
38 void dccl::DynamicProtobufManager::enable_disk_source_database()
39 {
40  if(disk_source_tree_)
41  return;
42 
43  disk_source_tree_.reset(new google::protobuf::compiler::DiskSourceTree);
44  source_database_.reset(new google::protobuf::compiler::SourceTreeDescriptorDatabase(disk_source_tree_.get()));
45  error_collector_.reset(new DLogMultiFileErrorCollector);
46 
47  source_database_->RecordErrorsTo(error_collector_.get());
48  disk_source_tree_->MapPath("/", "/");
49  disk_source_tree_->MapPath("", "");
50  add_database(source_database_);
51 }
52 
53 
59 const google::protobuf::FileDescriptor*
60 dccl::DynamicProtobufManager::load_from_proto_file(const std::string& protofile_absolute_path)
61 {
62  if(!get_instance()->source_database_)
63  throw(dccl::Exception("Must called enable_compilation() before loading proto files directly"));
64 
65  return user_descriptor_pool().FindFileByName(protofile_absolute_path);
66 }
67 
68 
69 // DLogMultiFileErrorCollector
70 void dccl::DynamicProtobufManager::DLogMultiFileErrorCollector::AddError(const std::string & filename, int line, int column,
71  const std::string & message)
72 {
73  std::stringstream ss;
74  ss << "File: " << filename
75  << " has error (line: " << line << ", column: "
76  << column << "):" << message;
77 
78  throw(dccl::Exception(ss.str()));
79 
80 }
81 
82 
static const google::protobuf::FileDescriptor * add_protobuf_file(const google::protobuf::FileDescriptorProto &proto)
Add a protobuf file defined in a google::protobuf::FileDescriptorProto.
Exception class for DCCL.
Definition: exception.h:31
static void add_database(boost::shared_ptr< google::protobuf::DescriptorDatabase > database)
Add a Google Protobuf DescriptorDatabase to the set of databases searched for Message Descriptors...
static const google::protobuf::FileDescriptor * load_from_proto_file(const std::string &protofile_absolute_path)
Load a message from a .proto file on the disk. enable_compilation() must be called first...