Apollo 11.0
自动驾驶开放平台
plugin_description.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
17
18#include <map>
19#include <memory>
20#include <regex>
21#include <string>
22#include <utility>
23
24#include <tinyxml2.h>
25
27#include "cyber/common/file.h"
28#include "cyber/common/log.h"
29
30namespace apollo {
31namespace cyber {
32namespace plugin_manager {
33
35
36PluginDescription::PluginDescription(const std::string& name) : name_(name) {}
37
39 const std::string& description_index_path,
40 const std::string& description_path,
41 const std::string& actual_description_path,
42 const std::string& library_path,
43 const std::string& actual_library_path)
44 : name_(name),
45 description_index_path_(description_index_path),
46 description_path_(description_path),
47 actual_description_path_(actual_description_path),
48 library_path_(library_path),
49 actual_library_path_(actual_library_path) {}
50
51bool PluginDescription::ParseFromIndexFile(const std::string& file_path) {
52 this->description_index_path_ = file_path;
54
56 AWARN << "plugin index[" << file_path << "] name[" << this->name_
57 << "] invalid, read index file failed";
58 return false;
59 }
61}
62
63bool PluginDescription::ParseFromDescriptionFile(const std::string& file_path) {
64 if (this->description_path_.empty()) {
65 this->description_path_ = file_path;
66 }
67
69 this->description_path_, "APOLLO_PLUGIN_DESCRIPTION_PATH",
71 AWARN << "plugin index[" << file_path << "] name[" << this->name_
72 << "] invalid, description[" << this->description_path_
73 << "] file not found";
74 return false;
75 }
76
77 tinyxml2::XMLDocument doc;
78 if (doc.LoadFile(this->actual_description_path_.c_str()) !=
79 tinyxml2::XML_SUCCESS) {
80 AWARN << "plugin description[" << file_path << "] name[" << this->name_
81 << "] invalid, parse description file failed";
82 return false;
83 }
84 const tinyxml2::XMLElement* root = doc.RootElement();
85 this->library_path_ = root->Attribute("path");
86
87 std::string plugin_name =
88 std::regex_replace(this->library_path_, std::regex("/"), "__");
89 if (this->name_.empty()) {
90 this->name_ = plugin_name;
91 }
92
93 // process class name and base class name from description file, this will be
94 // used to build index fo lazy load
95 for (const tinyxml2::XMLElement* class_element =
96 root->FirstChildElement("class");
97 class_element != nullptr;
98 class_element = class_element->NextSiblingElement("class")) {
99 std::string class_name = class_element->Attribute("type");
100 std::string base_class_name = class_element->Attribute("base_class");
101
102 if (this->class_name_base_class_name_map_.find(class_name) !=
104 AWARN << "plugin description[" << file_path << "] name[" << this->name_
105 << "] invalid, class name[" << class_name << "] duplicated";
106 continue;
107 }
108
109 if (class_name.empty() || base_class_name.empty()) {
110 AWARN << "plugin description[" << file_path << "] name[" << this->name_
111 << "] invalid, class name[" << class_name << "] base class name["
112 << base_class_name << "] invalid";
113 continue;
114 }
115 this->class_name_base_class_name_map_[class_name] = base_class_name;
116 }
117
119 "APOLLO_PLUGIN_LIB_PATH",
120 &this->actual_library_path_)) {
121 AWARN << "plugin description[" << file_path << "] name[" << this->name_
122 << "] invalid, library[" << this->library_path_ << "] file not found";
123 return false;
124 }
125
126 return true;
127}
128
129} // namespace plugin_manager
130} // namespace cyber
131} // namespace apollo
bool ParseFromIndexFile(const std::string &file_path)
parse plugin description metadata from plugin index file
std::map< std::string, std::string > class_name_base_class_name_map_
bool ParseFromDescriptionFile(const std::string &file_path)
parse plugin description metadata from plugin description file
#define AWARN
Definition log.h:43
bool GetFilePathWithEnv(const std::string &path, const std::string &env_var, std::string *file_path)
get file path, judgement priority:
Definition file.cc:422
std::string GetFileName(const std::string &path, const bool remove_extension)
Definition file.cc:401
bool GetContent(const std::string &file_name, std::string *content)
Get file content as string.
Definition file.cc:167
class register implement
Definition arena_queue.h:37