JeVois  1.3
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 
25 // ####################################################################################################
26 template <typename T> inline
27 typename std::enable_if< ! std::is_integral<T>::value, T>::type jevois::from_string(std::string const & str)
28 {
29  T val; std::istringstream iss(str); iss >> val;
30 
31  if (iss.fail())
32  throw std::invalid_argument("Cannot convert [" + str + "] to value of type " + jevois::demangledName<T>());
33 
34  return val;
35 }
36 
37 // ####################################################################################################
38 template <typename T> inline
39 typename std::enable_if<std::is_integral<T>::value, T>::type jevois::from_string(std::string const & str)
40 {
41  // Make sure people are aware of the octal conversion if it will trigger here:
42  if (str.length() > 1 && str[0] == '0' && str[1] >= '0' && str[1] <= '9')
43  LINFO("Value [" << str << "] interpreted as Octal (base 8) because of leading 0");
44 
45  // Convert. stoll() will throw if malformed
46  return clamped_convert<T>(std::stoll(str, nullptr, 0));
47 }
48 
49 // ####################################################################################################
50 template <typename T> inline
51 std::string jevois::to_string(T const & val)
52 {
53  std::ostringstream oss; oss << val; return oss.str();
54 }
55 
56 // ####################################################################################################
57 template <typename dest_type, typename source_type> inline
58 dest_type jevois::clamped_convert(source_type source)
59 {
60 
61 
62 
63 
64  //FIXME
65 
66 
67 
68 
69  return dest_type(source);
70 }
71 
72 // ####################################################################################################
73 #define XIOCTL(dev, req, mem) \
74  do { \
75  int result = 0; \
76  do { result = ioctl(dev, req, mem); } while ((result < 0) && (errno == EINTR)); \
77  if (result < 0) PLFATAL(#req " failed"); \
78  } while (false)
79 
80 // ####################################################################################################
81 #define XIOCTL_QUIET(dev, req, mem) \
82  do { \
83  int result = 0; \
84  do { result = ioctl(dev, req, mem); } while ((result < 0) && (errno == EINTR)); \
85  if (result < 0) throw std::runtime_error(#req " failed"); \
86  } while (false)
dest_type clamped_convert(source_type source)
Clamped numerical conversion.
Definition: UtilsImpl.H:58
std::string to_string(T const &val)
Convert from type to string.
Definition: UtilsImpl.H:51
#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.