forked from sony/nmos-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_utils.h
More file actions
66 lines (57 loc) · 1.83 KB
/
basic_utils.h
File metadata and controls
66 lines (57 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef CPPREST_BASIC_UTILS_H
#define CPPREST_BASIC_UTILS_H
#include "cpprest/asyncrt_utils.h" // for cpprest/details/basic_types.h and utility::conversions
namespace utility
{
namespace conversions
{
namespace details
{
// non-throwing overload of function in cpprest/asyncrt_utils.h
template <typename Source>
utility::string_t print_string(const Source& val, const utility::string_t& default_str)
{
utility::ostringstream_t oss;
oss.imbue(std::locale::classic());
oss << val;
return !oss.fail() ? oss.str() : default_str;
}
// non-throwing overload of function in cpprest/asyncrt_utils.h
template <typename Target>
Target scan_string(const utility::string_t& str, const Target& default_val)
{
Target t;
utility::istringstream_t iss(str);
iss.imbue(std::locale::classic());
iss >> t;
return !iss.fail() ? t : default_val;
}
}
}
}
#ifndef _TURN_OFF_PLATFORM_STRING
#define US(x) utility::string_t{_XPLATSTR(x)}
#endif
// more convenient utility functions dependent on utility::char_t
namespace utility
{
inline std::string us2s(const string_t& us)
{
return conversions::to_utf8string(us);
}
inline string_t s2us(const std::string& s)
{
return conversions::to_string_t(s);
}
template <typename T>
inline string_t ostringstreamed(const T& value)
{
return conversions::details::print_string(value, {});
}
template <typename T>
inline T istringstreamed(const string_t& value, const T& default_value = {})
{
return conversions::details::scan_string(value, default_value);
}
}
#endif