|
| 1 | +// Copyright 2022, Foxglove Technologies. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef ROSBAG2_CPP__MESSAGE_DEFINITIONS__LOCAL_MESSAGE_DEFINITION_SOURCE_HPP_ |
| 16 | +#define ROSBAG2_CPP__MESSAGE_DEFINITIONS__LOCAL_MESSAGE_DEFINITION_SOURCE_HPP_ |
| 17 | + |
| 18 | +#ifndef ROSBAG2_CPP_LOCAL_MESSAGE_DEFINITION_SOURCE_MAX_RECURSION_DEPTH |
| 19 | +#define ROSBAG2_CPP_LOCAL_MESSAGE_DEFINITION_SOURCE_MAX_RECURSION_DEPTH 50 |
| 20 | +#endif |
| 21 | + |
| 22 | +#include <set> |
| 23 | +#include <string> |
| 24 | +#include <unordered_map> |
| 25 | +#include <unordered_set> |
| 26 | +#include <utility> |
| 27 | + |
| 28 | +#include "rosbag2_cpp/visibility_control.hpp" |
| 29 | +#include "rosbag2_storage/message_definition.hpp" |
| 30 | + |
| 31 | +// This is necessary because of using stl types here. It is completely safe, because |
| 32 | +// a) the member is not accessible from the outside |
| 33 | +// b) there are no inline functions. |
| 34 | +#ifdef _WIN32 |
| 35 | +# pragma warning(push) |
| 36 | +# pragma warning(disable:4251) |
| 37 | +#endif |
| 38 | + |
| 39 | +namespace rosbag2_cpp |
| 40 | +{ |
| 41 | + |
| 42 | +class DefinitionNotFoundError : public std::exception |
| 43 | +{ |
| 44 | +private: |
| 45 | + std::string name_; |
| 46 | + |
| 47 | +public: |
| 48 | + explicit DefinitionNotFoundError(std::string name) |
| 49 | + : name_(std::move(name)) |
| 50 | + { |
| 51 | + } |
| 52 | + |
| 53 | + const char * what() const noexcept override |
| 54 | + { |
| 55 | + return name_.c_str(); |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +class ROSBAG2_CPP_PUBLIC LocalMessageDefinitionSource final |
| 60 | +{ |
| 61 | +public: |
| 62 | + /** |
| 63 | + * Concatenate the message definition with its dependencies into a self-contained schema. |
| 64 | + * The format is different for MSG and IDL definitions, and is described fully in |
| 65 | + * docs/message_definition_encoding.md |
| 66 | + * Throws DefinitionNotFoundError if one or more definition files are missing for the given |
| 67 | + * package resource name. |
| 68 | + */ |
| 69 | + rosbag2_storage::MessageDefinition get_full_text(const std::string & root_topic_type); |
| 70 | + |
| 71 | + enum struct Format |
| 72 | + { |
| 73 | + UNKNOWN = 0, |
| 74 | + MSG = 1, |
| 75 | + IDL = 2, |
| 76 | + }; |
| 77 | + |
| 78 | + explicit LocalMessageDefinitionSource() = default; |
| 79 | + |
| 80 | + LocalMessageDefinitionSource(const LocalMessageDefinitionSource &) = delete; |
| 81 | + LocalMessageDefinitionSource(const LocalMessageDefinitionSource &&) = delete; |
| 82 | + |
| 83 | +private: |
| 84 | + struct MessageSpec |
| 85 | + { |
| 86 | + MessageSpec(Format format, std::string text, const std::string & package_context); |
| 87 | + const std::set<std::string> dependencies; |
| 88 | + const std::string text; |
| 89 | + Format format{Format::UNKNOWN}; |
| 90 | + }; |
| 91 | + |
| 92 | + struct DefinitionIdentifier |
| 93 | + { |
| 94 | + DefinitionIdentifier() = delete; |
| 95 | + DefinitionIdentifier(const std::string & topic_type, Format format) |
| 96 | + : topic_type_(topic_type) |
| 97 | + , format_(format) |
| 98 | + { |
| 99 | + size_t h1 = std::hash<Format>()(format_); |
| 100 | + size_t h2 = std::hash<std::string>()(topic_type_); |
| 101 | + hash_ = h1 ^ h2; |
| 102 | + } |
| 103 | + bool operator==(const DefinitionIdentifier & di) const |
| 104 | + { |
| 105 | + return (format_ == di.format_) && (topic_type_ == di.topic_type_); |
| 106 | + } |
| 107 | + |
| 108 | + size_t hash() const |
| 109 | + { |
| 110 | + return hash_; |
| 111 | + } |
| 112 | + |
| 113 | + Format format() const |
| 114 | + { |
| 115 | + return format_; |
| 116 | + } |
| 117 | + |
| 118 | + std::string topic_type() const |
| 119 | + { |
| 120 | + return topic_type_; |
| 121 | + } |
| 122 | + |
| 123 | +private: |
| 124 | + std::string topic_type_; |
| 125 | + Format format_; |
| 126 | + size_t hash_; |
| 127 | + }; |
| 128 | + |
| 129 | + struct DefinitionIdentifierHash |
| 130 | + { |
| 131 | + size_t operator()(const DefinitionIdentifier & di) const |
| 132 | + { |
| 133 | + return di.hash(); |
| 134 | + } |
| 135 | + }; |
| 136 | + |
| 137 | + /** |
| 138 | + * Load and parse the message file referenced by the given datatype, or return it from |
| 139 | + * msg_specs_by_datatype |
| 140 | + */ |
| 141 | + const MessageSpec & load_message_spec(const DefinitionIdentifier & definition_identifier); |
| 142 | + |
| 143 | + static std::string delimiter(const DefinitionIdentifier & definition_identifier); |
| 144 | + |
| 145 | + std::unordered_map<DefinitionIdentifier, |
| 146 | + MessageSpec, DefinitionIdentifierHash> msg_specs_by_definition_identifier_; |
| 147 | +}; |
| 148 | + |
| 149 | +ROSBAG2_CPP_PUBLIC |
| 150 | +std::set<std::string> parse_definition_dependencies( |
| 151 | + LocalMessageDefinitionSource::Format format, |
| 152 | + const std::string & text, |
| 153 | + const std::string & package_context); |
| 154 | + |
| 155 | +} // namespace rosbag2_cpp |
| 156 | + |
| 157 | +#ifdef _WIN32 |
| 158 | +# pragma warning(pop) |
| 159 | +#endif |
| 160 | + |
| 161 | +#endif // ROSBAG2_CPP__MESSAGE_DEFINITIONS__LOCAL_MESSAGE_DEFINITION_SOURCE_HPP_ |
0 commit comments