JeVois  1.8
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
UtilsImpl.H
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #pragma once
19 
20 #include <jevois/Debug/Log.H>
21 #include <jevois/Util/Demangle.H>
22 #include <errno.h>
23 #include <sys/ioctl.h>
24 #include <type_traits>
25 
26 // ####################################################################################################
27 template <typename T> inline
28 typename std::enable_if< ! std::is_integral<T>::value, T>::type jevois::from_string(std::string const & str)
29 {
30  T val; std::istringstream iss(str); iss >> val;
31 
32  if (iss.fail())
33  throw std::invalid_argument("Cannot convert [" + str + "] to value of type " + jevois::demangledName<T>());
34 
35  return val;
36 }
37 
38 // ####################################################################################################
39 template <typename T> inline
40 typename std::enable_if<std::is_integral<T>::value, T>::type jevois::from_string(std::string const & str)
41 {
42  // We need to be careful with unsigned parameters to throw if the string value is negative:
43  if (std::is_unsigned<T>::value)
44  if (str.find('-') != str.npos)
45  throw std::range_error("Cannot assign negative value [" + str +
46  "] to unsigned type " + jevois::demangledName<T>());
47 
48  // Make sure people are aware of the octal conversion if it will trigger here:
49  if (str.length() > 1 && str[0] == '0' && str[1] >= '0' && str[1] <= '9')
50  LINFO("Value [" << str << "] interpreted as Octal (base 8) because of leading 0");
51 
52  // Convert. stoll() will throw if malformed
53  return clamped_convert<T>(std::stoll(str, nullptr, 0));
54 }
55 
56 // ####################################################################################################
57 template <typename T> inline
58 std::string jevois::to_string(T const & val)
59 {
60  std::ostringstream oss; oss << val; return oss.str();
61 }
62 
63 // ####################################################################################################
64 template <typename dest_type, typename source_type> inline
65 dest_type jevois::clamped_convert(source_type source)
66 {
67 
68 
69 
70 
71  //FIXME
72 
73 
74 
75 
76  return dest_type(source);
77 }
78 
79 // ####################################################################################################
80 #define XIOCTL(dev, req, mem) \
81  do { \
82  int result = 0; \
83  do { result = ioctl(dev, req, mem); } while ((result < 0) && (errno == EINTR)); \
84  if (result < 0) PLFATAL(#req " failed"); \
85  } while (false)
86 
87 // ####################################################################################################
88 #define XIOCTL_QUIET(dev, req, mem) \
89  do { \
90  int result = 0; \
91  do { result = ioctl(dev, req, mem); } while ((result < 0) && (errno == EINTR)); \
92  if (result < 0) throw std::runtime_error(#req " failed"); \
93  } while (false)
dest_type clamped_convert(source_type source)
Clamped numerical conversion.
Definition: UtilsImpl.H:65
std::string to_string(T const &val)
Convert from type to string.
Definition: UtilsImpl.H:58
#define LINFO(msg)
Convenience macro for users to print out console or syslog messages, INFO level.
Definition: Log.H:176
std::string T from_string(std::string const &str)
Convert from string to a type.