libvisiontransfer  10.8.0
conversionhelpers.h
1 /*******************************************************************************
2  * Copyright (c) 2024 Allied Vision Technologies GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #ifndef VISIONTRANSFER_INTERNAL_CONVERSIONHELPERS_H
16 #define VISIONTRANSFER_INTERNAL_CONVERSIONHELPERS_H
17 
18 namespace visiontransfer {
19 namespace internal {
20 
22 class VT_EXPORT ConversionHelpers {
23 public:
25  template<typename T>
26  static double anyToDouble(T val);
27 
29  template<typename T>
30  static std::string anyToString(T val) {
31  std::ostringstream ss;
32  ss << std::setprecision(std::numeric_limits<double>::max_digits10 - 1) << val;
33  return ss.str();
34  }
35 
36  // Wrap anything in to_string if string is expected, or cast for numbers
37  template<typename S, typename T, typename std::enable_if<std::is_arithmetic<S>::value>::type* = nullptr >
38  static S toStringIfStringExpected(T val) {
39  return static_cast<S>(val);
40  }
41  template<typename S, typename T, typename std::enable_if<std::is_same<S, std::string>::value>::type* = nullptr >
42  static S toStringIfStringExpected(T val) {
43  return std::to_string(val);
44  }
45 };
46 
47 template<> inline double ConversionHelpers::anyToDouble(const std::string& val) { return atol(val.c_str()); }
48 template<> inline double ConversionHelpers::anyToDouble(std::string val) { return atol(val.c_str()); }
49 template<> inline double ConversionHelpers::anyToDouble(const char* val) { return atol(val); }
50 template<typename T> inline double ConversionHelpers::anyToDouble(T val) { return (double)val; }
51 
52 } // namespace internal
53 } // namespace visiontransfer
54 
55 #endif
56 
visiontransfer::internal::ConversionHelpers::anyToDouble
static double anyToDouble(T val)
Converts any type to a double.
Definition: conversionhelpers.h:74
Allied Vision