Skip to content

Commit

Permalink
refactor: apply snake_case to function names (#37)
Browse files Browse the repository at this point in the history
Signed-off-by: ktro2828 <[email protected]>
  • Loading branch information
ktro2828 authored Aug 16, 2024
1 parent b182f5d commit a0e015b
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 69 deletions.
22 changes: 11 additions & 11 deletions awviz_common/include/awviz_common/display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ class Display
rclcpp::Node::SharedPtr node, std::shared_ptr<rerun::RecordingStream> stream) = 0;

/**
* @brief Set status of attributes.
* @brief Set attributes of property.
* @param topic Name of topic.
* @param entity Entity path of the record.
*/
virtual void setStatus(
virtual void set_property(
const std::string & topic,
const std::shared_ptr<std::unordered_map<std::string, std::string>> entity_roots) = 0;

Expand All @@ -69,7 +69,7 @@ class Display
* @brief Return true if the initialization is completed.
* @return bool Return the value of the private member named `is_initialized_`.
*/
virtual bool isInitialized() const { return is_initialized_; }
virtual bool is_initialized() const { return is_initialized_; }

protected:
rclcpp::Node::SharedPtr node_; //!< Node shared pointer.
Expand All @@ -90,7 +90,7 @@ class RosTopicDisplay : public Display
RosTopicDisplay()
{
const auto msg_type = rosidl_generator_traits::name<MsgType>();
property_.setType(msg_type);
property_.set_type(msg_type);
}

/**
Expand All @@ -116,12 +116,12 @@ class RosTopicDisplay : public Display
* @param topic Name of topic.
* @param entity Entity path of the record.
*/
void setStatus(
void set_property(
const std::string & topic,
const std::shared_ptr<std::unordered_map<std::string, std::string>> entity_roots) override
{
property_.setTopic(topic);
property_.setEntityRoots(entity_roots);
property_.set_topic(topic);
property_.set_entity_roots(entity_roots);
}

/**
Expand All @@ -134,7 +134,7 @@ class RosTopicDisplay : public Display
*/
void end() override { unsubscribe(); }

bool isInitialized() const override { return is_initialized_ && property_.isInitialized(); }
bool is_initialized() const override { return is_initialized_ && property_.is_initialized(); }

protected:
static constexpr const char * TIMELINE_NAME = "timestamp"; //!< Entity name of timeline record.
Expand All @@ -145,14 +145,14 @@ class RosTopicDisplay : public Display
*/
virtual void subscribe()
{
if (!isInitialized()) {
if (!is_initialized()) {
return;
}

// TODO(ktro2828): QoS setting
subscription_ = node_->create_subscription<MsgType>(
property_.topic(), rclcpp::SensorDataQoS{},
[this](const typename MsgType::ConstSharedPtr msg) { logToStream(msg); });
[this](const typename MsgType::ConstSharedPtr msg) { log_message(msg); });
};

/**
Expand All @@ -165,7 +165,7 @@ class RosTopicDisplay : public Display
* @param msg Constant shared pointer of ROS message.
* @note Currently, if the corresponding entity path doesn't exist this just logs warning as text.
*/
virtual void logToStream(typename MsgType::ConstSharedPtr msg) = 0;
virtual void log_message(typename MsgType::ConstSharedPtr msg) = 0;

protected:
typename rclcpp::Subscription<MsgType>::SharedPtr subscription_; //!< Subscription of the topic.
Expand Down
18 changes: 9 additions & 9 deletions awviz_common/include/awviz_common/display_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ class DisplayFactory : public PluginFactory<Display>
* @param lookup_name Lookup name of the class.
* @return Set of declared message types.
*/
const std::set<std::string> & getMessageTypes(const std::string & lookup_name);
const std::set<std::string> & get_message_types(const std::string & lookup_name);

/**
* @brief Get the Class Id of the corresponding message type.
*
* @param msg_type ROS message type.
* @return Return a string of lookup name if the specified type has been declared, otherwise
* returns nullptr.
* returns `nullptr`.
*/
std::optional<std::string> getClassLookupName(const std::string & msg_type) const;
std::optional<std::string> get_class_lookup_name(const std::string & msg_type) const;

public:
static constexpr const char * LIBRARY_TAG = "library"; //!< XML tag of library.
Expand All @@ -68,42 +68,42 @@ class DisplayFactory : public PluginFactory<Display>
* @param element Parsed XML element.
* @return Return true if the element has the root node.
*/
bool hasRootNode(const tinyxml2::XMLElement * element) const;
bool has_root_node(const tinyxml2::XMLElement * element) const;

/**
* @brief Check whether xml element has a library root.
* @param element Parsed XML element.
* @return Return true if the element has the library root.
*/
bool hasLibraryRoot(const tinyxml2::XMLElement * element) const;
bool has_library_root(const tinyxml2::XMLElement * element) const;

/**
* @brief Cache all classes associated with the library.
* @param library Parsed XML element for library.
*/
void cacheAllClassElements(const tinyxml2::XMLElement * library);
void cache_classes(const tinyxml2::XMLElement * library);

/**
* @brief Parse ROS message types from XML elements.
* @param element Root XML element.
* @return Set of message types.
*/
std::set<std::string> parseMsgTypes(const tinyxml2::XMLElement * element) const;
std::set<std::string> parse_message_types(const tinyxml2::XMLElement * element) const;

/**
* @brief Lookup derived class name.
* @param element Parsed XML element.
* @return Class name if the element has a `type` key, otherwise returns empty string.
*/
std::string lookupDerivedClass(const tinyxml2::XMLElement * element) const;
std::string lookup_derived_class(const tinyxml2::XMLElement * element) const;

/**
* @brief Lookup the class id.
* @param element Parsed XML element.
* @param derived Name of derived class.
* @return Class id if the element has `name` key, otherwise returns derived name.
*/
std::string lookupClassId(
std::string lookup_class_id(
const tinyxml2::XMLElement * element, const std::string & derived) const;

private:
Expand Down
13 changes: 7 additions & 6 deletions awviz_common/include/awviz_common/factory/plugin_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ class PluginFactory
* @param lookup_name Lookup name of the class.
* @return The path of the associated plugin manifest.
*/
std::string getPluginManifestPath(const std::string & lookup_name) const
std::string get_plugin_manifest_path(const std::string & lookup_name) const
{
return class_loader_->getPluginManifestPath(lookup_name);
}

/**
* @brief Return plugin information.
* @param lookup_name Lookup name of the class.
* @return PluginInfo instance.
* @return `PluginInfo` object.
*/
PluginInfo getPluginInfo(const std::string & lookup_name) const
PluginInfo get_plugin_info(const std::string & lookup_name) const
{
auto name = class_loader_->getName(lookup_name);
auto type = class_loader_->getClassType(lookup_name);
Expand All @@ -81,9 +81,10 @@ class PluginFactory
/**
* @brief Create a instance of the plugin.
* @param lookup_name Lookup name of the class.
* @return Instance of plugin. Returns `nullptr` if exception occurred.
* @return Shared pointer of a plugin. Returns `nullptr` if `pluginlib::PluginlibException`
* occurred.
*/
virtual std::shared_ptr<T> createInstance(const std::string & lookup_name) const
virtual std::shared_ptr<T> create_instance(const std::string & lookup_name) const
{
try {
return class_loader_->createSharedInstance(lookup_name);
Expand All @@ -98,7 +99,7 @@ class PluginFactory
*
* @return std::vector<std::string>
*/
std::vector<std::string> getDeclaredClasses() const
std::vector<std::string> get_declared_classes() const
{
return class_loader_->getDeclaredClasses();
}
Expand Down
8 changes: 4 additions & 4 deletions awviz_common/include/awviz_common/property.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ class RosTopicProperty
* @brief Set ROS message type name.
* @param type Name of ROS message type.
*/
void setType(const std::string & type) { type_ = type; }
void set_type(const std::string & type) { type_ = type; }

/**
* @brief Set ROS topic name.
* @param topic Name of topic.
*/
void setTopic(const std::string & topic) { topic_ = topic; }
void set_topic(const std::string & topic) { topic_ = topic; }

/**
* @brief Set entity path of record.
* @param entity Entity path of record.
*/
void setEntityRoots(
void set_entity_roots(
const std::shared_ptr<std::unordered_map<std::string, std::string>> entity_roots)
{
entity_roots_ = entity_roots;
Expand Down Expand Up @@ -99,7 +99,7 @@ class RosTopicProperty
}
}

bool isInitialized() const { return !type_.empty() && !topic_.empty() && entity_roots_; }
bool is_initialized() const { return !type_.empty() && !topic_.empty() && entity_roots_; }

private:
std::string type_; //!< Type of ROS message.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class VisualizationManager
/**
* @brief Parse topics and create new ROS subscriptions every time.
*/
void createSubscriptions();
void create_subscriptions();
};
} // namespace awviz_common
#endif // AWVIZ_COMMON__VISUALIZATION_MANAGER_HPP_
31 changes: 16 additions & 15 deletions awviz_common/src/display_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ namespace awviz_common
{
DisplayFactory::DisplayFactory() : PluginFactory<Display>("awviz_common", "awviz_common::Display")
{
for (const auto & lookup_name : getDeclaredClasses()) {
auto xml_path = getPluginManifestPath(lookup_name);
for (const auto & lookup_name : get_declared_classes()) {
auto xml_path = get_plugin_manifest_path(lookup_name);
// Initialize cache as empty.
msg_type_buffer_[lookup_name] = std::set<std::string>();
if (!xml_path.empty()) {
tinyxml2::XMLDocument document;
document.LoadFile(xml_path.c_str());
auto * config = document.RootElement();
if (!hasRootNode(config) || !hasLibraryRoot(config)) {
if (!has_root_node(config) || !has_library_root(config)) {
continue;
}

auto * library = config;
while (library) {
cacheAllClassElements(library);
cache_classes(library);
library = library->NextSiblingElement(LIBRARY_TAG);
}
}
}
}

const std::set<std::string> & DisplayFactory::getMessageTypes(const std::string & lookup_name)
const std::set<std::string> & DisplayFactory::get_message_types(const std::string & lookup_name)
{
if (msg_type_buffer_.find(lookup_name) != msg_type_buffer_.cend()) {
return msg_type_buffer_[lookup_name];
Expand All @@ -49,7 +49,7 @@ const std::set<std::string> & DisplayFactory::getMessageTypes(const std::string
return msg_type_buffer_[lookup_name];
}

std::optional<std::string> DisplayFactory::getClassLookupName(const std::string & msg_type) const
std::optional<std::string> DisplayFactory::get_class_lookup_name(const std::string & msg_type) const
{
for (const auto & [class_id, msg_types] : msg_type_buffer_) {
if (msg_types.find(msg_type) != msg_types.cend()) {
Expand All @@ -59,31 +59,32 @@ std::optional<std::string> DisplayFactory::getClassLookupName(const std::string
return {};
}

bool DisplayFactory::hasRootNode(const tinyxml2::XMLElement * element) const
bool DisplayFactory::has_root_node(const tinyxml2::XMLElement * element) const
{
return element != nullptr;
}

bool DisplayFactory::hasLibraryRoot(const tinyxml2::XMLElement * element) const
bool DisplayFactory::has_library_root(const tinyxml2::XMLElement * element) const
{
return strcmp(element->Value(), LIBRARY_TAG) == 0;
}

void DisplayFactory::cacheAllClassElements(const tinyxml2::XMLElement * library)
void DisplayFactory::cache_classes(const tinyxml2::XMLElement * library)
{
auto element = library->FirstChildElement();
while (element) {
const auto derived = lookupDerivedClass(element);
const auto current = lookupClassId(element, derived);
const auto derived = lookup_derived_class(element);
const auto current = lookup_class_id(element, derived);

msg_type_buffer_[current] = parseMsgTypes(element);
msg_type_buffer_[current] = parse_message_types(element);

// search child element
element = element->NextSiblingElement(CLASS_TAG);
}
}

std::set<std::string> DisplayFactory::parseMsgTypes(const tinyxml2::XMLElement * element) const
std::set<std::string> DisplayFactory::parse_message_types(
const tinyxml2::XMLElement * element) const
{
std::set<std::string> output;

Expand All @@ -102,12 +103,12 @@ std::set<std::string> DisplayFactory::parseMsgTypes(const tinyxml2::XMLElement *
return output;
}

std::string DisplayFactory::lookupDerivedClass(const tinyxml2::XMLElement * element) const
std::string DisplayFactory::lookup_derived_class(const tinyxml2::XMLElement * element) const
{
return element->Attribute(TYPE_ATTRIBUTE) ? element->Attribute(TYPE_ATTRIBUTE) : "";
}

std::string DisplayFactory::lookupClassId(
std::string DisplayFactory::lookup_class_id(
const tinyxml2::XMLElement * element, const std::string & derived) const
{
return element->Attribute(NAME_ATTRIBUTE) ? element->Attribute(NAME_ATTRIBUTE) : derived;
Expand Down
10 changes: 5 additions & 5 deletions awviz_common/src/visualization_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ VisualizationManager::VisualizationManager(

parallel_callback_group_ = node_->create_callback_group(rclcpp::CallbackGroupType::Reentrant);
callback_timer_ = node_->create_wall_timer(
100ms, [&]() { createSubscriptions(); }, parallel_callback_group_);
100ms, [&]() { create_subscriptions(); }, parallel_callback_group_);
}

VisualizationManager::~VisualizationManager()
Expand All @@ -44,23 +44,23 @@ VisualizationManager::~VisualizationManager()
}
}

void VisualizationManager::createSubscriptions()
void VisualizationManager::create_subscriptions()
{
for (const auto & [topic_name, topic_types] : node_->get_topic_names_and_types()) {
if (display_group_.find(topic_name) != display_group_.cend()) {
continue;
}

const auto & topic_type = topic_types.front();
const auto lookup_name = display_factory_->getClassLookupName(topic_type);
const auto lookup_name = display_factory_->get_class_lookup_name(topic_type);

if (lookup_name) {
auto display = display_factory_->createInstance(lookup_name.value());
auto display = display_factory_->create_instance(lookup_name.value());

if (display) {
display->initialize(node_, stream_);

display->setStatus(topic_name, tf_manager_->entities());
display->set_property(topic_name, tf_manager_->entities());

display->start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DetectedObjectsDisplay
DetectedObjectsDisplay();

protected:
void logToStream(autoware_perception_msgs::msg::DetectedObjects::ConstSharedPtr msg) override;
void log_message(autoware_perception_msgs::msg::DetectedObjects::ConstSharedPtr msg) override;
};
} // namespace awviz_plugin

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace awviz_plugin
* @param img Input image.
* @return Tensor shape as collection.
*/
inline rerun::Collection<rerun::TensorDimension> tensorShape(const cv::Mat & img)
inline rerun::Collection<rerun::TensorDimension> tensor_shape(const cv::Mat & img)
{
return {
static_cast<size_t>(img.rows), static_cast<size_t>(img.cols),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CompressedImageDisplay
CompressedImageDisplay();

protected:
void logToStream(sensor_msgs::msg::CompressedImage::ConstSharedPtr msg) override;
void log_message(sensor_msgs::msg::CompressedImage::ConstSharedPtr msg) override;
};
} // namespace awviz_plugin

Expand Down
Loading

0 comments on commit a0e015b

Please sign in to comment.