1/*
2 * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient>
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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#ifndef STDEXT_CAST_H
24#define STDEXT_CAST_H
25
26#include "exception.h"
27#include "demangle.h"
28
29#include <sstream>
30#include <iostream>
31#include <cstdlib>
32
33namespace stdext {
34
35// cast a type to another type
36template<typename T, typename R>
37bool cast(const T& in, R& out) {
38 std::stringstream ss;
39 ss << in;
40 ss >> out;
41 return !!ss && ss.eof();
42}
43
44// cast a type to string
45template<typename T>
46bool cast(const T& in, std::string& out) {
47 std::stringstream ss;
48 ss << in;
49 out = ss.str();
50 return true;
51}
52
53// cast string to string
54template<>
55inline bool cast(const std::string& in, std::string& out) {
56 out = in;
57 return true;
58}
59
60// special cast from string to boolean
61template<>
62inline bool cast(const std::string& in, bool& b) {
63 if(in == "true")
64 b = true;
65 else if(in == "false")
66 b = false;
67 else
68 return false;
69 return true;
70}
71
72// special cast from string to char
73template<>
74inline bool cast(const std::string& in, char& c) {
75 if(in.length() != 1)
76 return false;
77 c = in[0];
78 return true;
79}
80
81// special cast from string to long
82template<>
83inline bool cast(const std::string& in, long& l) {
84 if(in.find_first_not_of("-0123456789") != std::string::npos)
85 return false;
86 std::size_t t = in.find_last_of('-');
87 if(t != std::string::npos && t != 0)
88 return false;
89 l = atol(in.c_str());
90 return true;
91}
92
93// special cast from string to int
94template<>
95inline bool cast(const std::string& in, int& i) {
96 long l;
97 if(cast(in, l)) {
98 i=l;
99 return true;
100 }
101 return false;
102}
103
104// special cast from string to double
105template<>
106inline bool cast(const std::string& in, double& d) {
107 if(in.find_first_not_of("-0123456789.") != std::string::npos)
108 return false;
109 std::size_t t = in.find_last_of('-');
110 if(t != std::string::npos && t != 0)
111 return false;
112 t = in.find_first_of('.');
113 if(t != std::string::npos && (t == 0 || t == in.length()-1 || in.find_first_of('.', t+1) != std::string::npos))
114 return false;
115 d = atof(in.c_str());
116 return true;
117}
118
119// special cast from string to float
120template<>
121inline bool cast(const std::string& in, float& f) {
122 double d;
123 if(cast(in, d)) {
124 f=(float)d;
125 return true;
126 }
127 return false;
128}
129
130// special cast from boolean to string
131template<>
132inline bool cast(const bool& in, std::string& out) {
133 out = (in ? "true" : "false");
134 return true;
135}
136
137// used by safe_cast
138class cast_exception : public exception {
139public:
140 virtual ~cast_exception() throw() { }
141 template<class T, class R>
142 void update_what() {
143 std::stringstream ss;
144 ss << "failed to cast value of type '" << demangle_type<T>() << "' to type '" << demangle_type<R>() << "'";
145 m_what = ss.str();
146 }
147 virtual const char* what() const throw() { return m_what.c_str(); }
148private:
149 std::string m_what;
150};
151
152// cast a type to another type, any error throws a cast_exception
153template<typename R, typename T>
154R safe_cast(const T& t) {
155 R r;
156 if(!cast(t, r)) {
157 cast_exception e;
158 e.update_what<T,R>();
159 throw e;
160 }
161 return r;
162}
163
164// cast a type to another type, cast errors are ignored
165template<typename R, typename T>
166R unsafe_cast(const T& t, R def = R()) {
167 try {
168 return safe_cast<R,T>(t);
169 } catch(cast_exception& e) {
170 std::cerr << "CAST ERROR: " << e.what() << std::endl;
171 return def;
172 }
173}
174
175}
176
177#endif
178