feat support message for Future
This commit is contained in:
parent
00be161b61
commit
0a6b13c218
@ -60,7 +60,7 @@ struct FutureData {
|
|||||||
void SetDefaultScheduler(TaskQueueBase *scheduler) noexcept;
|
void SetDefaultScheduler(TaskQueueBase *scheduler) noexcept;
|
||||||
TaskQueueBase *GetDefaultScheduler() noexcept;
|
TaskQueueBase *GetDefaultScheduler() noexcept;
|
||||||
|
|
||||||
template<typename T, typename FailureT = std::exception>
|
template<typename T, typename FailureT = failure::DefaultException>
|
||||||
class Future {
|
class Future {
|
||||||
static_assert(!std::is_same<T, void>::value, "Future<void, _> is not allowed. Use Future<bool, _> instead");
|
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");
|
static_assert(!std::is_same<FailureT, void>::value, "Future<_, void> is not allowed. Use Future<_, bool> instead");
|
||||||
|
@ -113,6 +113,9 @@ TEST_SUITE("future")
|
|||||||
return 1;
|
return 1;
|
||||||
});
|
});
|
||||||
CHECK_FALSE(f2.IsFailed());
|
CHECK_FALSE(f2.IsFailed());
|
||||||
|
CHECK_EQ(std::string("test"), f2.FailureReason().what());
|
||||||
|
CHECK(f2.IsFailed());
|
||||||
|
|
||||||
auto f3 = sled::Future<std::string>::AsyncValue("11");
|
auto f3 = sled::Future<std::string>::AsyncValue("11");
|
||||||
CHECK_EQ(f3.Result(), "11");
|
CHECK_EQ(f3.Result(), "11");
|
||||||
}
|
}
|
||||||
|
@ -3,18 +3,38 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "sled/any.h"
|
#include "sled/any.h"
|
||||||
|
#include "sled/nonstd/string_view.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace sled {
|
namespace sled {
|
||||||
namespace failure {
|
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
|
inline FailureT
|
||||||
FailureFromString(std::string &&)
|
FailureFromString(std::string &&str)
|
||||||
{
|
{
|
||||||
return FailureT();
|
return FailureT();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename FailureT>
|
template<typename FailureT,
|
||||||
|
typename = typename std::enable_if<std::is_constructible<FailureT, const std::string &>::value>::type>
|
||||||
inline FailureT
|
inline FailureT
|
||||||
FailureFromString(const std::string &str)
|
FailureFromString(const std::string &str)
|
||||||
{
|
{
|
||||||
@ -24,9 +44,16 @@ FailureFromString(const std::string &str)
|
|||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline std::string
|
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
|
}// namespace failure
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "failure_handling.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
@ -10,7 +11,7 @@ namespace sled {
|
|||||||
template<typename T, typename FailureT>
|
template<typename T, typename FailureT>
|
||||||
class Future;
|
class Future;
|
||||||
|
|
||||||
template<typename T, typename FailureT = std::exception>
|
template<typename T, typename FailureT = failure::DefaultException>
|
||||||
class Promise final {
|
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<T, void>::value, "Promise<void, _> is not allowed. Use Promise<bool, _> instead");
|
||||||
static_assert(!std::is_same<FailureT, void>::value,
|
static_assert(!std::is_same<FailureT, void>::value,
|
||||||
|
61
src/sled/numerics/int128.h
Normal file
61
src/sled/numerics/int128.h
Normal 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
|
@ -108,7 +108,7 @@ public:
|
|||||||
} else if (IsMinusInfinity() || other.IsPlusInfinity()) {
|
} else if (IsMinusInfinity() || other.IsPlusInfinity()) {
|
||||||
return TimeDelta::MinusInfinity();
|
return TimeDelta::MinusInfinity();
|
||||||
}
|
}
|
||||||
return TimeDelta::Micros(us() - other.us());
|
return TimeDelta::Nanos(ns() - other.ns());
|
||||||
}
|
}
|
||||||
|
|
||||||
Timestamp &operator-=(const TimeDelta delta)
|
Timestamp &operator-=(const TimeDelta delta)
|
||||||
|
Loading…
Reference in New Issue
Block a user