TinyURDF 1.0.0
A Modern C++ Library for Parsing and Visualizing URDF Model Files
Loading...
Searching...
No Matches
property_parser.h
Go to the documentation of this file.
1#ifndef TINYURDF_PROPERTYPARSER_H
2#define TINYURDF_PROPERTYPARSER_H
3
4// Copyright 2025 Wissem CHIHA
5
6#include <string>
7#include <unordered_map>
8
9#include <loguru/loguru.hpp>
10
11#include "parser_base.h"
12#include "utils.h"
13
36template<typename T>
37class PropertyParser : public ParserBase<std::unordered_map<std::string,T>>
38{
39public:
41 p_ = std::make_shared<std::unordered_map<std::string,T>>();
42 };
43
44 void print(std::ostream& os) override {
45 os << "Parsed Property-Values = [";
46 bool first = true;
47 for (const auto& pair : *p_) {
48 if (!first) os << ", ";
49 os << pair.first << "=" << pair.second;
50 first = false;
51 }
52 os << "]";
53 }
54
55 bool empty() const override {
56 return p_->empty();
57 }
58
59 void clear() override {
60 p_->clear();
61 }
62
63 const char* getTypename() override {
64 return "property_value_parser";
65 }
66
67 bool isA(const char* name) override {
68 return std::string(name) == "property_value_parser";
69 }
70
71 void parse(const tinyxml2::XMLElement* xml)
72 {
74 const tinyxml2::XMLAttribute* attr = xml->FirstAttribute();
75
76 while (attr) {
77 if constexpr (std::is_same_v<T, double>) {
78 double val;
79 str2double(attr->Value(), val);
80 (*p_)[std::string(attr->Name())] = val;
81 } else if constexpr (std::is_same_v<T, std::string>) {
82 (*p_)[std::string(attr->Name())] = std::string(attr->Value());
83 } else {
84 LOG_F(ERROR, "failed to parse attribute: unsupported type for conversion");
85 }
86 attr = attr->Next();
87 }
88 }
89
90 std::shared_ptr<std::unordered_map<std::string,T>> get() override {return p_;};
91
93
94private:
95 std::shared_ptr<std::unordered_map<std::string,T>> p_;
96};
97#endif // TINYURDF_PROPERTYVALUEPARSER_H
Definition parser_base.h:16
A basic parser for general property value definitions in XML. Parses a single XML element where a pro...
Definition property_parser.h:38
PropertyParser()
Definition property_parser.h:40
const char * getTypename() override
Definition property_parser.h:63
void parse(const tinyxml2::XMLElement *xml)
Definition property_parser.h:71
std::shared_ptr< std::unordered_map< std::string, T > > get() override
Definition property_parser.h:90
~PropertyParser()
Definition property_parser.h:92
void clear() override
Definition property_parser.h:59
bool isA(const char *name) override
Definition property_parser.h:67
void print(std::ostream &os) override
Definition property_parser.h:44
bool empty() const override
Definition property_parser.h:55
void str2double(const char *in, double &num_)
a locale-safe version of string-to-double
Definition utils.cc:14