feat support message for Future

This commit is contained in:
tqcq 2024-04-20 13:04:06 +08:00
parent 00be161b61
commit 0a6b13c218
6 changed files with 100 additions and 8 deletions

View File

@ -60,7 +60,7 @@ struct FutureData {
void SetDefaultScheduler(TaskQueueBase *scheduler) noexcept;
TaskQueueBase *GetDefaultScheduler() noexcept;
template<typename T, typename FailureT = std::exception>
template<typename T, typename FailureT = failure::DefaultException>
class Future {
static_assert(!std::is_same<T, void>::value, "Future<void, _> is not allowed. Use Future<bool, _> instead");
static_assert(!std::is_same<FailureT, void>::value, "Future<_, void> is not allowed. Use Future<_, bool> instead");

View File

@ -113,6 +113,9 @@ TEST_SUITE("future")
return 1;
});
CHECK_FALSE(f2.IsFailed());
CHECK_EQ(std::string("test"), f2.FailureReason().what());
CHECK(f2.IsFailed());
auto f3 = sled::Future<std::string>::AsyncValue("11");
CHECK_EQ(f3.Result(), "11");
}

View File

@ -3,18 +3,38 @@
#pragma once
#include "sled/any.h"
#include "sled/nonstd/string_view.h"
#include <string>
namespace sled {
namespace failure {
template<typename FailureT>
class DefaultException : std::exception {
public:
inline DefaultException() = default;
inline DefaultException(const std::string &message) : message_(message) {}
inline DefaultException(const char *message) : message_(message) {}
~DefaultException() noexcept override = default;
const char *what() const noexcept override { return message_.c_str(); }
private:
std::string message_;
};
template<typename FailureT,
typename = typename std::enable_if<!std::is_constructible<FailureT, std::string>::value>::type>
inline FailureT
FailureFromString(std::string &&)
FailureFromString(std::string &&str)
{
return FailureT();
}
template<typename FailureT>
template<typename FailureT,
typename = typename std::enable_if<std::is_constructible<FailureT, const std::string &>::value>::type>
inline FailureT
FailureFromString(const std::string &str)
{
@ -24,9 +44,16 @@ FailureFromString(const std::string &str)
template<>
inline std::string
FailureFromString<std::string>(std::string &&str)
FailureFromString<std::string>(const std::string &str)
{
return std::move(str);
return str;
}
template<>
inline DefaultException
FailureFromString<DefaultException>(const std::string &str)
{
return DefaultException(str);
}
}// namespace failure

View File

@ -3,6 +3,7 @@
#pragma once
#include "failure_handling.h"
#include <memory>
#include <type_traits>
@ -10,7 +11,7 @@ namespace sled {
template<typename T, typename FailureT>
class Future;
template<typename T, typename FailureT = std::exception>
template<typename T, typename FailureT = failure::DefaultException>
class Promise final {
static_assert(!std::is_same<T, void>::value, "Promise<void, _> is not allowed. Use Promise<bool, _> instead");
static_assert(!std::is_same<FailureT, void>::value,

View File

@ -0,0 +1,61 @@
#ifndef SLED_NUMERICS_INT128_H
#define SLED_NUMERICS_INT128_H
#include <string>
#pragma once
namespace sled {
class Int128 {
constexpr Int128(int v);
constexpr Int128(unsigned int v);
constexpr Int128(long v);
constexpr Int128(unsigned long v);
constexpr Int128(long long v);
constexpr Int128(unsigned long long v);
constexpr Int128(const Int128 &v);
Int128 &operator=(int v);
Int128 &operator=(unsigned int v);
Int128 &operator=(long v);
Int128 &operator=(unsigned long v);
Int128 &operator=(long long v);
Int128 &operator=(unsigned long long v);
Int128 &operator=(const Int128 &v);
Int128 &operator+=(const Int128 &v);
Int128 &operator-=(const Int128 &v);
Int128 &operator*=(const Int128 &v);
Int128 &operator/=(const Int128 &v);
Int128 &operator%=(const Int128 &v);
Int128 &operator++(int);
Int128 &operator--(int);
Int128 &operator&=(const Int128 &v);
Int128 &operator|=(const Int128 &v);
Int128 &operator^=(const Int128 &v);
Int128 &operator<<=(int v);
Int128 &operator>>=(int v);
constexpr explicit operator bool() const;
constexpr explicit operator char() const;
constexpr explicit operator unsigned char() const;
constexpr explicit operator short() const;
constexpr explicit operator unsigned short() const;
constexpr explicit operator int() const;
constexpr explicit operator unsigned int() const;
constexpr explicit operator long() const;
constexpr explicit operator unsigned long() const;
constexpr explicit operator long long() const;
constexpr explicit operator unsigned long long() const;
explicit operator float() const;
explicit operator double() const;
explicit operator long double() const;
private:
std::string ToString() const;
uint64_t lo_;
uint64_t hi_;
};
}// namespace sled
#endif// SLED_NUMERICS_INT128_H

View File

@ -108,7 +108,7 @@ public:
} else if (IsMinusInfinity() || other.IsPlusInfinity()) {
return TimeDelta::MinusInfinity();
}
return TimeDelta::Micros(us() - other.us());
return TimeDelta::Nanos(ns() - other.ns());
}
Timestamp &operator-=(const TimeDelta delta)