00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright 2016-2017 Davide Faconti 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Willow Garage, Inc. nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 * *******************************************************************/ 00034 00035 #ifndef ROS_INTROSPECTION_STRINGTREELEAF_H 00036 #define ROS_INTROSPECTION_STRINGTREELEAF_H 00037 00038 #include <vector> 00039 #include <map> 00040 #include <iostream> 00041 #include <absl/container/inlined_vector.h> 00042 #include <absl/container/fixed_array.h> 00043 #include "ros_type_introspection/ros_message.hpp" 00044 00045 namespace RosIntrospection{ 00046 00047 // Still faster in my benchmark than absl::InlinedVector 00048 00049 template <typename T, size_t N> 00050 class InlinedVector{ 00051 public: 00052 InlinedVector(): _size(0) {} 00053 void push_back(T val) { _array[_size++] = val; } 00054 const T& back() const { return _array[_size-1]; } 00055 T& back() { return _array[_size-1]; } 00056 size_t size() const { return _size; } 00057 const T& operator[](size_t index) const { return _array[index]; } 00058 T& operator[](size_t index) { return _array[index]; } 00059 private: 00060 std::array<T,N> _array; 00061 size_t _size; 00062 }; 00063 00082 struct StringTreeLeaf{ 00083 00084 StringTreeLeaf(); 00085 00086 const StringTreeNode* node_ptr; 00087 00088 InlinedVector<uint16_t,8> index_array; 00089 00091 bool toStr(std::string &destination) const; 00092 00093 // return string length or -1 if failed 00094 int toStr(char* buffer) const; 00095 00096 std::string toStdString() const { std::string out; toStr(out); return out; } 00097 00098 constexpr static const char SEPARATOR = '/'; 00099 constexpr static const char NUM_PLACEHOLDER = '#'; 00100 00101 static const absl::string_view& num_placeholder() { 00102 constexpr static const absl::string_view nph("#"); 00103 return nph; 00104 } 00105 }; 00106 00107 //--------------------------------- 00108 00109 inline std::ostream& operator<<(std::ostream &os, const StringTreeLeaf& leaf ) 00110 { 00111 std::string dest; 00112 leaf.toStr(dest); 00113 os << dest; 00114 return os; 00115 } 00116 00117 inline StringTreeLeaf::StringTreeLeaf(): node_ptr(nullptr) 00118 { } 00119 00120 00121 inline bool StringTreeLeaf::toStr(std::string& destination) const 00122 { 00123 char buffer[256]; 00124 int offset = this->toStr(buffer); 00125 00126 if( offset < 0 ) { 00127 destination.clear(); 00128 return false; 00129 } 00130 destination.assign(buffer, offset); 00131 return true; 00132 } 00133 00134 00135 } 00136 00137 #endif // ROSTYPE_H