ISDataMappings.h
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright (c) 2014-2020 Inertial Sense, Inc. - http://inertialsense.com
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
7 
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #ifndef __ISDATAMAPPINGS_H_
14 #define __ISDATAMAPPINGS_H_
15 
16 #include <string>
17 #include <map>
18 #include <inttypes.h>
19 #include "com_manager.h"
20 #include "DataCSV.h"
21 
22 #ifndef CHAR_BIT
23 #define CHAR_BIT 8
24 #endif
25 
26 using namespace std;
27 
33 uint32_t GetDataTypeSize(eDataType dataType);
34 
35 #if !PLATFOM_IS_EMBEDDED
36 
37 extern const unsigned char g_asciiToLowerMap[256];
38 
39 #endif
40 
45 {
47  {
48  bool operator() (const unsigned char& c1, const unsigned char& c2) const
49  {
50  return g_asciiToLowerMap[c1] < g_asciiToLowerMap[c2];
51  }
52  };
53 
54  /*
55  size_t operator()(const std::string s) const
56  {
57  size_t hashCode = 5381;
58  char c;
59  const char* ptr = s.c_str();
60  const char* ptrEnd = ptr + s.size();
61  for (; ptr < ptrEnd; ptr++)
62  {
63  c = g_asciiToLowerMap[*ptr];
64  hashCode = ((hashCode << 5) + hashCode) + c;
65  }
66  return hashCode;
67  }
68  */
69 
70  // less than, not equal
71  bool operator() (const std::string& s1, const std::string& s2) const
72  {
73  // return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), nocase_compare());
74 
75  // we don't need unicode or fancy language handling here, and we do not want branching
76  // so we have hand-coded a highly performant ASCII case insensitive compare here.
77  // this custom code is 3x speed of lexicographical_compare
78  char c1, c2;
79  const char* ptr1 = s1.c_str();
80  const char* ptr2 = s2.c_str();
81  size_t size1 = s1.size();
82  size_t size2 = s2.size();
83  int sizeDiff = (int)size1 - (int)size2;
84 
85  // branchless min
86  // y + ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1)));
87  size_t minSize = size2 + (sizeDiff & (sizeDiff >> (sizeof(int) * CHAR_BIT - 1)));
88 
89  for (size_t i = 0; i < minSize; i++)
90  {
91  c1 = g_asciiToLowerMap[(int)ptr1[i]];
92  c2 = g_asciiToLowerMap[(int)ptr2[i]];
93  if (c1 != c2)
94  {
95  return (c1 < c2);
96  }
97  }
98  return (s1.size() < s2.size());
99  }
100 };
101 
102 /*
103 template <>
104 struct equal_to<std::string> : public unary_function<std::string, bool>
105 {
106  bool operator()(const std::string& s1, const std::string& s2) const
107  {
108  // we don't need unicode or fancy language handling here, and we do not want branching
109  // so we have hand-coded a highly performant ASCII case insensitive compare here.
110  // this custom code is 3x speed of lexicographical_compare
111  if (s1.size() != s2.size())
112  {
113  return false;
114  }
115 
116  char c1, c2;
117  const char* ptr1 = s1.c_str();
118  const char* ptr2 = s2.c_str();
119 
120  for (size_t i = 0; i < s1.size(); i++)
121  {
122  c1 = g_asciiToLowerMap[ptr1[i]];
123  c2 = g_asciiToLowerMap[ptr2[i]];
124  if (c1 != c2)
125  {
126  return false;
127  }
128  }
129  return true;
130  }
131 };
132 */
133 
134 // map of field name to data info
135 typedef map<string, data_info_t, sCaseInsensitiveCompare> map_name_to_info_t;
137 
139 {
140 public:
144  virtual ~cISDataMappings();
145 
151  static const char* GetDataSetName(uint32_t dataId);
152 
157  static const map_name_to_info_t* GetMapInfo(uint32_t dataId);
158 
164  static uint32_t GetSize(uint32_t dataId);
165 
177  static bool StringToData(const char* stringBuffer, int stringLength, const p_data_hdr_t* hdr, uint8_t* dataBuffer, const data_info_t& info, int radix = 10, bool json = false);
178 
188  static bool DataToString(const data_info_t& info, const p_data_hdr_t* hdr, const uint8_t* dataBuffer, data_mapping_string_t stringBuffer, bool json = false);
189 
196  static double GetTimestamp(const p_data_hdr_t* hdr, const uint8_t* buf);
197 
206  static bool CanGetFieldData(const data_info_t& info, const p_data_hdr_t* hdr, const uint8_t* buf, const uint8_t*& ptr);
207 
208 private:
209  cISDataMappings();
210 
211  uint32_t m_lookupSize[DID_COUNT];
212  const data_info_t* m_timestampFields[DID_COUNT];
214 
215 #if PLATFORM_IS_EMBEDDED
216 
217  // on embedded we cannot new up C++ runtime until after free rtos has started
218  static cISDataMappings* s_map;
219 
220 #else
221 
223 
224 #endif
225 
226 };
227 
228 #endif // __ISDATAMAPPINGS_H_
229 
#define DID_COUNT
Definition: data_sets.h:138
static cISDataMappings s_map
char data_mapping_string_t[IS_DATA_MAPPING_MAX_STRING_LENGTH]
const unsigned char g_asciiToLowerMap[256]
#define IS_DATA_MAPPING_MAX_STRING_LENGTH
Definition: DataCSV.h:24
map< string, data_info_t, sCaseInsensitiveCompare > map_name_to_info_t
#define CHAR_BIT
uint32_t GetDataTypeSize(eDataType dataType)
eDataType
Definition: DataCSV.h:26


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:04