mirror of
https://github.com/edubart/otclient.git
synced 2025-10-19 05:53:26 +02:00
add clock, change exceptions, add network exceptions, fix some crashes
This commit is contained in:
19
src/framework/util/exception.h
Normal file
19
src/framework/util/exception.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef EXCEPTION_H
|
||||
#define EXCEPTION_H
|
||||
|
||||
#include <exception>
|
||||
|
||||
class Exception : public std::exception
|
||||
{
|
||||
public:
|
||||
Exception() { }
|
||||
Exception(const std::string& what) : m_what(what) { }
|
||||
virtual ~Exception() throw() { };
|
||||
|
||||
virtual const char* what() const throw() { return m_what.c_str(); }
|
||||
|
||||
protected:
|
||||
std::string m_what;
|
||||
};
|
||||
|
||||
#endif
|
@@ -29,22 +29,11 @@
|
||||
#include <sstream>
|
||||
#include <exception>
|
||||
#include <cxxabi.h>
|
||||
#include <chrono>
|
||||
#include <unistd.h>
|
||||
#include "types.h"
|
||||
#include "exception.h"
|
||||
|
||||
namespace Fw {
|
||||
|
||||
inline int getTicks() {
|
||||
static auto firstTick = std::chrono::high_resolution_clock::now();
|
||||
auto tickNow = std::chrono::high_resolution_clock::now();
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(tickNow - firstTick).count();
|
||||
}
|
||||
|
||||
inline void sleep(int ms) {
|
||||
usleep(ms);
|
||||
}
|
||||
|
||||
// read utilities for istream
|
||||
inline uint8 getU8(std::istream& in) {
|
||||
uint8 tmp;
|
||||
@@ -197,9 +186,9 @@ inline bool cast(const bool& in, std::string& out) {
|
||||
}
|
||||
|
||||
// used by safe_cast
|
||||
class BadCast : public std::bad_cast {
|
||||
class CastException : public Exception {
|
||||
public:
|
||||
virtual ~BadCast() throw() { }
|
||||
virtual ~CastException() throw() { }
|
||||
template<class T, class R>
|
||||
void setWhat() {
|
||||
m_what = mkstr("failed to cast value of type '", demangleType<T>(),
|
||||
@@ -217,7 +206,7 @@ template<typename R, typename T>
|
||||
R safeCast(const T& t) {
|
||||
R r;
|
||||
if(!cast(t, r)) {
|
||||
BadCast e;
|
||||
CastException e;
|
||||
e.setWhat<T,R>();
|
||||
throw e;
|
||||
}
|
||||
@@ -231,7 +220,7 @@ template<typename R, typename T>
|
||||
R unsafeCast(const T& t, R def = R()) {
|
||||
try {
|
||||
return safeCast<R,T>(t);
|
||||
} catch(BadCast& e) {
|
||||
} catch(CastException& e) {
|
||||
println("CAST ERROR: ", e.what());
|
||||
return def;
|
||||
}
|
||||
@@ -268,6 +257,11 @@ inline std::string ip2str(uint32 ip) {
|
||||
return std::string(host);
|
||||
}
|
||||
|
||||
template<typename... T>
|
||||
void throwException(const T&... args) {
|
||||
throw Exception(Fw::mkstr(args...));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// shortcut for Fw::dump
|
||||
|
Reference in New Issue
Block a user