stringtree.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright 2016-2017 Davide Faconti
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  * *******************************************************************/
34 
36 
37 namespace Ros2Introspection
38 {
39 namespace details
40 {
41 // Brutally faster for numbers below 100
42 inline int printNumber(char* buffer, uint16_t value)
43 {
44  const char DIGITS[] = "00010203040506070809"
45  "10111213141516171819"
46  "20212223242526272829"
47  "30313233343536373839"
48  "40414243444546474849"
49  "50515253545556575859"
50  "60616263646566676869"
51  "70717273747576777879"
52  "80818283848586878889"
53  "90919293949596979899";
54  if (value < 10)
55  {
56  buffer[0] = static_cast<char>('0' + value);
57  return 1;
58  }
59  else if (value < 100)
60  {
61  value *= 2;
62  buffer[0] = DIGITS[value];
63  buffer[1] = DIGITS[value + 1];
64  return 2;
65  }
66  else
67  {
68  return sprintf(buffer, "%d", value);
69  }
70 }
71 } // end namespace details
72 
73 int StringTreeLeaf::toStr(std::string& buffer_str) const
74 {
75  const StringTreeNode* leaf_node = this->node_ptr;
76  if (!leaf_node)
77  {
78  return -1;
79  }
80 
81  boost::container::static_vector<const std::string*, 16> strings_chain;
82  size_t num_bytes = 2;
83 
84  while (leaf_node)
85  {
86  const auto& str = leaf_node->value();
87  const size_t S = str.size();
88  if (S == 1 && str[0] == NUM_PLACEHOLDER)
89  {
90  num_bytes += 5; // space for up to 9999
91  }
92  else
93  {
94  num_bytes += S + 1;
95  }
96  strings_chain.push_back(&str);
97  leaf_node = leaf_node->parent();
98  };
99 
100  std::reverse(strings_chain.begin(), strings_chain.end());
101 
102  size_t array_count = 0;
103  size_t offset = 0;
104 
105  buffer_str.resize(num_bytes); // expand the buffer
106  char* buffer = &buffer_str.front();
107 
108  for (const auto& str : strings_chain)
109  {
110  const size_t S = str->size();
111  if (S == 1 && (*str)[0] == NUM_PLACEHOLDER)
112  {
113  buffer[offset++] = '.';
114  offset += details::printNumber(&buffer[offset], this->index_array[array_count++]);
115  }
116  else
117  {
118  if (offset != 0)
119  {
120  buffer[offset++] = SEPARATOR;
121  }
122  std::memcpy(&buffer[offset], str->data(), S);
123  offset += S;
124  }
125  }
126 
127  buffer_str.resize(offset);
128  return int(offset);
129 }
130 
131 } // namespace Ros2Introspection
auto sprintf(const S &fmt, const T &... args) -> std::basic_string< Char >
#define S(x)
Element of the tree. it has a single parent and N >= 0 children.
int printNumber(char *buffer, uint16_t value)
Definition: stringtree.cpp:42
int toStr(std::string &destination) const
Utility functions to print the entire branch.
Definition: stringtree.cpp:73


plotjuggler_ros
Author(s): Davide Faconti
autogenerated on Fri Jun 23 2023 02:28:04