helper.h
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 // -- BEGIN LICENSE BLOCK ----------------------------------------------
3 // This file is part of the SCHUNK Canopen Driver suite.
4 //
5 // This program is free software licensed under the LGPL
6 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
7 // You can find a copy of this license in LICENSE folder in the top
8 // directory of the source code.
9 //
10 // © Copyright 2016 SCHUNK GmbH, Lauffen/Neckar Germany
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 // -- END LICENSE BLOCK ------------------------------------------------
13 //----------------------------------------------------------------------
21 //----------------------------------------------------------------------
22 
23 #ifndef HELPER_H
24 #define HELPER_H
25 
26 #include <boost/shared_ptr.hpp>
27 #include "CanOpenReceiveThread.h"
28 #include "Logging.h"
29 
30 namespace icl_hardware{
31 namespace canopen_schunk{
32 
36 
37 
44 std::string hexToString (const uint64_t num);
45 
53 std::string hexArrayToString( const unsigned char* msg, const uint8_t length);
54 
55 template <typename T>
56 std::string binaryString (const T num)
57 {
58  const size_t T_size = sizeof(T) * 8;
59  std::bitset<T_size> bs(num);
60  std::stringstream ss;
61  ss << "0b" << bs;
62  return ss.str();
63 }
64 
74 std::string sanitizeString(const std::string& text);
75 
76 
92 std::map<uint32_t, std::string> getErrorMapFromConfigFile(const std::string& filename,
93  const std::string& category = "error_codes");
94 
103 template <typename T>
104 inline std::vector<uint8_t> convertToCharVector(const T value)
105 {
106  std::vector<uint8_t> out_vec;
107  if (boost::is_fundamental<T>::value)
108  {
109  int32_t full_byte = 0xff;
110  size_t num_bytes = sizeof(T);
111  int32_t cast_int;
112 
113  if ( num_bytes <= 4)
114  {
115  std::memcpy(&cast_int, &value, num_bytes);
116  for (size_t i = 0; i < num_bytes; ++i)
117  {
118  int32_t unary = cast_int & full_byte;
119  out_vec.push_back(static_cast<uint8_t>( unary >> i*8));
120  full_byte = full_byte << 8;
121  }
122  }
123  else
124  {
125  LOGGING_ERROR (CanOpen, "The given data is too large. The maximum datatype size is 4 bytes." << endl);
126  throw std::bad_cast();
127  }
128  }
129  else
130  {
131  LOGGING_ERROR (CanOpen, "Only fundamental datatypes can be casted with the help of " << "this function. Fundamental types include integral, floating point and void types." << endl);
132  throw std::bad_cast();
133  }
134 
135  return out_vec;
136 }
137 
138 
146 template <typename T>
147 inline T convertFromCharVector(const std::vector<uint8_t>& vec)
148 {
149  T ret_val = 0;
150  size_t num_bytes = sizeof(T);
151  if (boost::is_fundamental<T>::value && vec.size() == num_bytes)
152  {
153  std::memcpy(&ret_val, &vec[0], num_bytes);
154  }
155  else
156  {
157  LOGGING_ERROR (CanOpen, "Only fundamental datatypes can be casted with the help of " << "this function. Fundamental types include integral, floating point and void types." << endl);
158  throw std::bad_cast();
159  }
160  return ret_val;
161 }
162 
163 }} // NS
164 
165 #endif // HELPER_H
signed int int32_t
filename
std::string hexArrayToString(const unsigned char *msg, const uint8_t length)
Transforms an array of unsigned chars into a string of Hex representations of those chars...
Definition: helper.cpp:42
std::vector< uint8_t > convertToCharVector(const T value)
This little helper transforms any datatype that has a size of at most 4 Bytes into a vector of uint8_...
Definition: helper.h:104
boost::shared_ptr< CanOpenReceiveThread > CanOpenReceiveThreadPtr
Definition: helper.h:33
#define LOGGING_ERROR(streamname, arg)
unsigned __int64 uint64_t
boost::shared_ptr< icl_hardware::can::tCanDevice > CanDevPtr
Definition: helper.h:34
unsigned char uint8_t
ThreadStream & endl(ThreadStream &stream)
T convertFromCharVector(const std::vector< uint8_t > &vec)
This converts a vector of uint8_t entries into another datatype with a matching byte length...
Definition: helper.h:147
std::string binaryString(const uint64_t num)
Definition: helper.cpp:53
std::map< uint32_t, std::string > getErrorMapFromConfigFile(const std::string &filename, const std::string &category)
Creates an error map from a given INI file.
Definition: helper.cpp:75
std::string sanitizeString(const std::string &text)
This function removes all non-graphical characters from the given string.
Definition: helper.cpp:62
std::string hexToString(const uint64_t num)
Converts a hexadecimal number into its string representation 0xXX.
Definition: helper.cpp:33
icl_hardware::can::tCanMessage CanMsg
Definition: helper.h:35


schunk_canopen_driver
Author(s): Felix Mauch , Georg Heppner
autogenerated on Mon Jun 10 2019 15:07:49