stringtree_leaf.cpp
Go to the documentation of this file.
1 /***** MIT License ****
2  *
3  * Copyright (c) 2016-2022 Davide Faconti
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
25 
26 namespace RosMsgParser
27 {
28 
30 {
31  auto node = leaf.node;
32  while (node && node->value())
33  {
34  fields.push_back(node->value());
35  node = node->parent();
36  }
37  std::reverse(fields.begin(), fields.end());
38  index_array = leaf.index_array;
39 }
40 
41 void FieldsVector::toStr(std::string& out) const
42 {
43  size_t total_size = 0;
44  for (const ROSField* field : fields)
45  {
46  total_size += field->name().size() + 1;
47  if (field->isArray())
48  {
49  total_size += (2 + 4); // super conservative (9999)
50  }
51  }
52 
53  out.resize(total_size);
54  char* buffer = static_cast<char*>(&out[0]);
55 
56  size_t array_count = 0;
57  size_t offset = 0;
58 
59  for (const ROSField* field : fields)
60  {
61  const std::string& str = field->name();
62  bool is_root = (field == fields.front());
63  if (!is_root)
64  {
65  buffer[offset++] = '/';
66  }
67  std::memcpy(&buffer[offset], str.data(), str.size());
68  offset += str.size();
69 
70  if (!is_root && field->isArray())
71  {
72  buffer[offset++] = '[';
73  if (array_count < index_array.size())
74  {
75  offset += print_number(&buffer[offset], index_array[array_count++]);
76  }
77  buffer[offset++] = ']';
78  }
79  }
80  buffer[offset] = '\0';
81  out.resize(offset);
82 }
83 
84 // void CreateStringFromTreeLeaf(const FieldTreeLeaf &leaf, bool skip_root, std::string&
85 // out)
86 //{
87 // const FieldTreeNode* leaf_node = leaf.node_ptr;
88 // if( !leaf_node ){
89 // out.clear();
90 // return ;
91 // }
92 
93 // SmallVector<const std::string*, 16> strings_chain;
94 
95 // size_t total_size = 0;
96 
97 // while(leaf_node)
98 // {
99 // const auto& str = leaf_node->value()->name();
100 // leaf_node = leaf_node->parent();
101 // if( !( leaf_node == nullptr && skip_root) )
102 // {
103 // strings_chain.emplace_back( &str );
104 // const size_t S = str.size();
105 // if( S == 1 && str[0] == '#' )
106 // {
107 // total_size += 5; // super conservative
108 // }
109 // else{
110 // total_size += S+1;
111 // }
112 // }
113 // };
114 
115 // out.resize(total_size);
116 // char* buffer = &out[0];
117 
118 // std::reverse(strings_chain.begin(), strings_chain.end() );
119 
120 // size_t array_count = 0;
121 // size_t offset = 0;
122 
123 // for( const auto& str: strings_chain)
124 // {
125 // const size_t S = str->size();
126 // if( S == 1 && (*str)[0] == '#' )
127 // {
128 // buffer[offset++] = '.';
129 // offset += print_number(&buffer[offset], leaf.index_array[ array_count++ ] );
130 // }
131 // else{
132 // if( str != strings_chain.front() ){
133 // buffer[offset++] = '/';
134 // }
135 // std::memcpy( &buffer[offset], str->data(), S );
136 // offset += S;
137 // }
138 // }
139 // out.resize(offset);
140 //}
141 
142 } // namespace RosMsgParser
llvm_vecsmall::SmallVectorTemplateCommon::size
LLVM_VECSMALL_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:212
RosMsgParser::FieldLeaf::node
const FieldTreeNode * node
Definition: stringtree_leaf.hpp:87
RosMsgParser::FieldsVector::toStr
void toStr(std::string &destination) const
Utility functions to print the entire branch.
Definition: stringtree_leaf.cpp:41
RosMsgParser::FieldsVector::index_array
SmallVector< uint16_t, 4 > index_array
Definition: stringtree_leaf.hpp:98
stringtree_leaf.hpp
RosMsgParser
Definition: builtin_types.hpp:34
RosMsgParser::FieldsVector::fields
SmallVector< const ROSField *, 8 > fields
Definition: stringtree_leaf.hpp:97
RosMsgParser::FieldLeaf::index_array
SmallVector< uint16_t, 4 > index_array
Definition: stringtree_leaf.hpp:88
field
static void field(LexState *ls, ConsControl *cc)
Definition: lparser.c:891
print_number
int print_number(char *buffer, uint16_t value)
Definition: stringtree_leaf.hpp:34
RosMsgParser::FieldsVector::FieldsVector
FieldsVector()=default
RosMsgParser::FieldLeaf
The FieldTreeLeaf is, as the name suggests, a leaf (terminal node) of a StringTree....
Definition: stringtree_leaf.hpp:85
RosMsgParser::ROSField
A ROSMessage will contain one or more ROSField(s). Each field is little more than a name / type pair.
Definition: ros_field.hpp:46


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:47