Otclient  14/8/2020
cast.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 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 
33 namespace stdext {
34 
35 // cast a type to another type
36 template<typename T, typename R>
37 bool 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
45 template<typename T>
46 bool 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
54 template<>
55 inline bool cast(const std::string& in, std::string& out) {
56  out = in;
57  return true;
58 }
59 
60 // special cast from string to boolean
61 template<>
62 inline 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
73 template<>
74 inline 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
82 template<>
83 inline 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
94 template<>
95 inline 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
105 template<>
106 inline 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
120 template<>
121 inline 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
131 template<>
132 inline bool cast(const bool& in, std::string& out) {
133  out = (in ? "true" : "false");
134  return true;
135 }
136 
137 // used by safe_cast
138 class cast_exception : public exception {
139 public:
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(); }
148 private:
149  std::string m_what;
150 };
151 
152 // cast a type to another type, any error throws a cast_exception
153 template<typename R, typename T>
154 R 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
165 template<typename R, typename T>
166 R 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
stdext::cast_exception::update_what
void update_what()
Definition: cast.h:142
stdext::cast_exception
Definition: cast.h:138
stdext::cast
bool cast(const T &in, R &out)
Definition: cast.h:37
stdext::cast_exception::what
virtual const char * what() const
Definition: cast.h:147
stdext::cast_exception::~cast_exception
virtual ~cast_exception()
Definition: cast.h:140
stdext::safe_cast
R safe_cast(const T &t)
Definition: cast.h:154
demangle.h
exception.h
stdext::unsafe_cast
R unsafe_cast(const T &t, R def=R())
Definition: cast.h:166
stdext
Definition: any.h:30
stdext::exception
Definition: exception.h:31