1 #pragma once 2 3 #include <sstream> 4 #include <string> 5 #include <cassert> 6 #include <cstdlib> 7 #include <exception> 8 9 // Polyfill needed by Qt when building for Android with GCC 10 #if defined(__ANDROID__) && defined(__GLIBCXX__) 11 12 namespace std { 13 14 template <typename T> to_string(T value)15std::string to_string(T value) 16 { 17 std::ostringstream oss; 18 oss << value; 19 20 return oss.str(); 21 } 22 stoi(const std::string & str)23inline int stoi(const std::string &str) 24 { 25 return atoi(str.c_str()); 26 } 27 stof(const std::string & str)28inline float stof(const std::string &str) { 29 return static_cast<float>(atof(str.c_str())); 30 } 31 32 } // namespace std 33 34 #endif 35 36 namespace mbgl { 37 namespace util { 38 39 template <class T> toString(T t)40inline std::string toString(T t) { 41 return std::to_string(t); 42 } 43 toString(int8_t num)44inline std::string toString(int8_t num) { 45 return std::to_string(int(num)); 46 } 47 toString(uint8_t num)48inline std::string toString(uint8_t num) { 49 return std::to_string(unsigned(num)); 50 } 51 52 std::string toString(float); 53 std::string toString(double); 54 std::string toString(long double); 55 toString(std::exception_ptr error)56inline std::string toString(std::exception_ptr error) { 57 assert(error); 58 59 if (!error) { 60 return "(null)"; 61 } 62 63 try { 64 std::rethrow_exception(error); 65 } catch (const std::exception& ex) { 66 return ex.what(); 67 } catch (...) { 68 return "Unknown exception type"; 69 } 70 } 71 stof(const std::string & str)72inline float stof(const std::string& str) { 73 return std::stof(str); 74 } 75 76 } // namespace util 77 } // namespace mbgl 78