JeVois  1.0
JeVois Smart Embedded Machine Vision Toolkit
Utils.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 <string>
21 #include <vector>
22 #include <type_traits> // for std::is_integral
23 
24 namespace jevois
25 {
26  /*! \defgroup utils Misc utilities
27 
28  Miscellaneous utility and helper functions. */
29 
30  /*! @{ */ // **********************************************************************
31 
32  //! Convert a V4L2 four-cc code (V4L2_PIX_FMT_...) to a 4-char string
33  std::string fccstr(unsigned int fcc);
34 
35  //! Return the number of bytes per pixel for a given V4L2_PIX_FMT_...
36  unsigned int v4l2BytesPerPix(unsigned int fcc);
37 
38  //! Return the image size in bytes for a given V4L2_PIX_FMT_..., width, height
39  unsigned int v4l2ImageSize(unsigned int fcc, unsigned int width, unsigned int height);
40 
41  //! Split string into vector of tokens using a regex to specify what to split on; default regex splits by whitespace
42  std::vector<std::string> split(std::string const & input, std::string const & regex = "\\s+");
43 
44  //! Concatenate a vector of tokens into a string
45  std::string join(std::vector<std::string> const & strings, std::string const & delimiter);
46 
47  //! Return true if str starts with prefix (including if both strings are equal)
48  /*! Note that if str is shorter than prefix, return is false (like in strncmp()). */
49  bool stringStartsWith(std::string const & str, std::string const & prefix);
50 
51  // Doxygen is not too good with enable_if, it only documents one version of the function. Here is a workaround:
52 #ifdef JEVOIS_DOXYGEN
53  //! Convert from string to a type
54  /*! For integral types, internally uses std::stoll() for that type, which supports prefixes like 0 (for octal) and 0x
55  (hex). Beware of that octal convention and do not pass leading zeros unless you mean it. For non-integral types,
56  internally uses operator>> for that type, so it works with any type that supports it. */
57  template <typename T> T from_string(std::string const & str);
58 #else
59  //! Convert from string to a type, version for non-integral types
60  /*! This internally uses operator>> for that type, so it works with any type that supports it. */
61  template <typename T>
62  typename std::enable_if< ! std::is_integral<T>::value, T>::type from_string(std::string const & str);
63 
64  //! Convert from string to a type, version for integral types
65  /*! This internally uses std::stoll() for that type, which supports prefixes like 0 (for octal) and 0x (hex). */
66  template <typename T>
67  typename std::enable_if<std::is_integral<T>::value, T>::type from_string(std::string const & str);
68 #endif
69 
70  //! Convert from type to string
71  /*! This internally uses operator>> for that type, so it works with any type that supports it. */
72  template <typename T>
73  std::string to_string(T const & val);
74 
75  //! Clamped numerical conversion
76  template <typename dest_type, typename source_type>
77  dest_type clamped_convert(source_type source);
78 
79  //! Flush the caches, may sometimes be useful when running the cemare in turbo mode
80  void flushcache();
81 
82  /*! @} */ // **********************************************************************
83 
84 } // namespace jevois
85 
86 
87 // ####################################################################################################
88 //! Helper macro to execute an ioctl, ignore interruptions, and, if error, issue a fatal message and throw
89 /*! \def XIOCTL(dev, req, mem)
90  \hideinitializer
91 
92  This macro assumes that req is an identifier (#define) for the corresponding ioctl number.
93  \ingroup utils */
94 
95 // ####################################################################################################
96 //! Helper macro to execute an ioctl, ignore interruptions, and, if error throw quietly
97 /*! \def XIOCTL_QUIET(dev, req, mem)
98  \hideinitializer
99 
100  This is useful, e.g., for V4L2 camera enumeration of formats, where one is supposed to call an ioctl
101  with increasing format number until it fails. When that happens we don't want to display any fatal error
102  message as XIOCTL() does. This macro assumes that req is an identifier for the corresponding ioctl number.
103  \ingroup utils */
104 
105 // Include implementation details
T from_string(std::string const &str)
Convert from string to a type.
dest_type clamped_convert(source_type source)
Clamped numerical conversion.
Definition: UtilsImpl.H:58
std::vector< std::string > split(std::string const &input, std::string const &regex="\\s+")
Split string into vector of tokens using a regex to specify what to split on; default regex splits by...
Definition: Utils.C:63
void flushcache()
Flush the caches, may sometimes be useful when running the cemare in turbo mode.
Definition: Utils.C:93
unsigned int v4l2BytesPerPix(unsigned int fcc)
Return the number of bytes per pixel for a given V4L2_PIX_FMT_...
Definition: Utils.C:43
std::string join(std::vector< std::string > const &strings, std::string const &delimiter)
Concatenate a vector of tokens into a string.
Definition: Utils.C:73
bool stringStartsWith(std::string const &str, std::string const &prefix)
Return true if str starts with prefix (including if both strings are equal)
Definition: Utils.C:87
std::string fccstr(unsigned int fcc)
Convert a V4L2 four-cc code (V4L2_PIX_FMT_...) to a 4-char string.
Definition: Utils.C:30
unsigned int v4l2ImageSize(unsigned int fcc, unsigned int width, unsigned int height)
Return the image size in bytes for a given V4L2_PIX_FMT_..., width, height.
Definition: Utils.C:59
std::string to_string(T const &val)
Convert from type to string.
Definition: UtilsImpl.H:51