c++: CowException for handling errors/poisoned cows

error_handling
Avril 3 years ago
parent a293133f29
commit 8f8d738e0e
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -5,7 +5,7 @@ PROJECT=cow
AUTHOR=Avril (Flanchan) <flanchan@cumallover.me>
VERSION_MAJOR=0
VERSION_MINOR=2.0r0
VERSION_MINOR=2.0r1
VERSION=$(VERSION_MAJOR).$(VERSION_MINOR)
ifeq ($(PREFIX),)

@ -3,9 +3,20 @@
#include "cow.h"
#include <memory>
#include <exception>
#include "cow/slice.hpp"
struct CowException : public std::exception
{
inline CowException(cow_err_kind k) : kind(k){}
const char* what() const noexcept override;
inline ~CowException(){}
const cow_err_kind kind;
};
struct Cow : public _cow_util::Span<unsigned char> {
struct Fake;
Cow() = delete;

@ -29,11 +29,11 @@ Cow::_inner::~_inner() {
}
Cow::_inner::_inner(size_t sz) : cow(_cow_create_unboxed(sz)){
//TODO: Real exception type?
if(UNLIKELY(cow.poisoned)) throw "POISONED";
if(UNLIKELY(cow.poisoned)) throw CowException(cow_err());
}
Cow::_inner::_inner(cow_t* ptr) : cow(*ptr)
{
if(UNLIKELY(cow.poisoned)) throw "POISONED";
if(UNLIKELY(cow.poisoned)) throw CowException(cow_err());
free(ptr);
}
@ -66,4 +66,10 @@ Cow::Fake Cow::Fake::Fake::from_real(const Cow& real) { return Fake(real); }
Cow::Fake Cow::Fake::clone() const { return Fake(*static_cast<const Fake*>(this)); }
cow_t* Cow::Fake::get_raw() const { return fake; }
// Error
const char* CowException::what() const noexcept {
auto str = cow_err_msg(kind);
if(str && *str) return *str;
else return "Unknown error";
}

@ -217,9 +217,12 @@ int main()
read_fake(clone); //clone still functions because of refcount on origin.
printf("Last error: %d, %s\n", cow_err(), *cow_err_msg(cow_err()));
Cow should_fail(SIZE_MAX);
printf("Last error: %d, %s\n", cow_err(), *cow_err_msg(cow_err()));
Cow::Fake should_fail_clone = should_fail;
try {
Cow should_fail(SIZE_MAX);
Cow::Fake should_fail_clone = should_fail;
} catch (CowException& cw) {
printf("Error! (%d) %s\n", cw.kind, cw.what());
}
printf("Last error: %d, %s\n", cow_err(), *cow_err_msg(cow_err()));
return 0;
}

Loading…
Cancel
Save