Skip to content

Commit

Permalink
replace exceptions with big obvious error boxes
Browse files Browse the repository at this point in the history
as a bonus you can now compile without exceptions
  • Loading branch information
Ryan-rsm-McKenzie committed Aug 21, 2020
1 parent 70b438b commit 66e1e3c
Show file tree
Hide file tree
Showing 15 changed files with 865 additions and 883 deletions.
6 changes: 3 additions & 3 deletions include/RE/BSCore/BSTArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ namespace RE
{
const auto mem = malloc(a_size);
if (!mem) {
throw std::bad_alloc();
stl::report_and_fail("out of memory"sv);
} else {
std::memset(mem, 0, a_size);
return mem;
Expand Down Expand Up @@ -198,7 +198,7 @@ namespace RE
if (a_size > N) {
const auto mem = malloc(a_size);
if (!mem) {
throw std::bad_alloc();
stl::report_and_fail("out of memory"sv);
} else {
std::memset(mem, 0, a_size);
return mem;
Expand Down Expand Up @@ -241,7 +241,7 @@ namespace RE
if (!local()) {
const auto mem = malloc(capacity());
if (!mem) {
throw std::bad_alloc();
stl::report_and_fail("out of memory"sv);
} else {
_data.heap = mem;
}
Expand Down
2 changes: 1 addition & 1 deletion include/RE/BSCore/BSTHashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ namespace RE
{
if (!get_entries() || !_freeCount) {
if (!grow()) {
return std::make_pair(end(), false); // maybe throw?
return std::make_pair(end(), false);
}
}

Expand Down
6 changes: 0 additions & 6 deletions include/RE/BSScript/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ namespace RE

~Array();

[[nodiscard]] reference at(size_type a_pos);
[[nodiscard]] const_reference at(size_type a_pos) const;

[[nodiscard]] reference operator[](size_type a_pos);
[[nodiscard]] const_reference operator[](size_type a_pos) const;

Expand Down Expand Up @@ -71,9 +68,6 @@ namespace RE
[[nodiscard]] TypeInfo::RawType type() const;

private:
void out_of_range(const char* a_fnName, size_type a_pos) const;


static inline constexpr size_type MAX_SIZE = 128;

// members
Expand Down
1 change: 0 additions & 1 deletion include/RE/BSScript/PackUnpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ namespace RE
return GetRawTypeFromVMType(static_cast<VMTypeID>(unwrapped_type_t<T>::FORMTYPE)) + TypeInfo::RawType::kObject;
} else {
static_assert(false);
throw std::exception;
}
}

Expand Down
20 changes: 10 additions & 10 deletions include/RE/Memory/MemoryManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ namespace RE
{
auto heap = MemoryManager::GetSingleton();
return heap ?
heap->Allocate(a_size, 0, false) :
nullptr;
heap->Allocate(a_size, 0, false) :
nullptr;
}


Expand All @@ -93,8 +93,8 @@ namespace RE
{
auto heap = MemoryManager::GetSingleton();
return heap ?
heap->Allocate(a_size, static_cast<std::int32_t>(a_alignment), true) :
nullptr;
heap->Allocate(a_size, static_cast<std::int32_t>(a_alignment), true) :
nullptr;
}


Expand Down Expand Up @@ -136,8 +136,8 @@ namespace RE
{
auto heap = MemoryManager::GetSingleton();
return heap ?
heap->Reallocate(a_ptr, a_newSize, 0, false) :
nullptr;
heap->Reallocate(a_ptr, a_newSize, 0, false) :
nullptr;
}


Expand All @@ -152,8 +152,8 @@ namespace RE
{
auto heap = MemoryManager::GetSingleton();
return heap ?
heap->Reallocate(a_ptr, a_newSize, static_cast<std::int32_t>(a_alignment), true) :
nullptr;
heap->Reallocate(a_ptr, a_newSize, static_cast<std::int32_t>(a_alignment), true) :
nullptr;
}


Expand Down Expand Up @@ -190,7 +190,7 @@ namespace RE
if (mem) { \
return mem; \
} else { \
throw std::bad_alloc(); \
stl::report_and_fail("out of memory"sv); \
} \
} \
\
Expand All @@ -200,7 +200,7 @@ namespace RE
if (mem) { \
return mem; \
} else { \
throw std::bad_alloc(); \
stl::report_and_fail("out of memory"sv); \
} \
} \
\
Expand Down
38 changes: 12 additions & 26 deletions include/RE/RTTI.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,9 @@ namespace RE
}


template <class T = PVOID>
inline T RTDynamicCast(PVOID a_inptr, LONG a_vfDelta, PVOID a_srcType, PVOID a_targetType, BOOL a_isReference)
inline void* RTDynamicCast(void* a_inptr, std::int32_t a_vfDelta, void* a_srcType, void* a_targetType, std::int32_t a_isReference)
{
using func_t = decltype(&RTDynamicCast<T>);
using func_t = decltype(&RTDynamicCast);
REL::Relocation<func_t> func{ REL::ID(102238) };
return func(a_inptr, a_vfDelta, a_srcType, a_targetType, a_isReference);
}
Expand All @@ -227,27 +226,14 @@ template <
int> = 0>
inline To skyrim_cast(const From* a_from)
{
REL::Relocation<PVOID> from(RE::SK_Impl::remove_cvpr_t<From>::RTTI);
REL::Relocation<PVOID> to(RE::SK_Impl::remove_cvpr_t<To>::RTTI);
return RE::RTDynamicCast<To>((PVOID)a_from, 0, from.type(), to.type(), false);
}


template <
class To,
class From,
std::enable_if_t<
RE::SK_Impl::cast_is_valid_v<
To,
const From&>,
int> = 0>
inline To skyrim_cast(const From& a_from) // throw(std::bad_cast)
{
try {
REL::Relocation<PVOID> from(RE::SK_Impl::remove_cvpr_t<From>::RTTI);
REL::Relocation<PVOID> to(RE::SK_Impl::remove_cvpr_t<To>::RTTI);
return RE::RTDynamicCast<To>((PVOID)std::addressof(a_from), 0, from.type(), to.type(), true);
} catch (...) {
throw std::bad_cast();
}
REL::Relocation<void*> from{ RE::SK_Impl::remove_cvpr_t<From>::RTTI };
REL::Relocation<void*> to{ RE::SK_Impl::remove_cvpr_t<To>::RTTI };
return static_cast<To>(
RE::RTDynamicCast(
const_cast<void*>(
static_cast<const volatile void*>(a_from)),
0,
from.get(),
to.get(),
false));
}
1 change: 0 additions & 1 deletion include/RE/Scaleform/FxGameDelegate/FxDelegateArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace RE
FxDelegateArgs(GFxValue a_responseID, FxDelegateHandler* a_handler, GFxMovieView* a_movie, const GFxValue* a_vals, std::uint32_t a_numArgs);

void Respond(FxResponseArgsBase& a_params) const;
const GFxValue& At(UPInt a_pos) const;
const GFxValue& operator[](UPInt a_pos) const;
FxDelegateHandler* GetHandler() const;
GFxMovieView* GetMovie() const;
Expand Down
32 changes: 16 additions & 16 deletions include/REL/Relocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@
REL_MAKE_MEMBER_FUNCTION_NON_POD_TYPE_HELPER(&, ##__VA_ARGS__) \
REL_MAKE_MEMBER_FUNCTION_NON_POD_TYPE_HELPER(&&, ##__VA_ARGS__)

#define REL_THROW_EXCEPTION(a_what) \
#define REL_FAILURE(a_what) \
{ \
const auto src = stl::source_location::current(); \
throw std::runtime_error( \
stl::report_and_fail( \
fmt::format( \
FMT_STRING("{}({}): {}"), \
src.file_name(), \
Expand Down Expand Up @@ -478,7 +478,7 @@ namespace REL
{
auto handle = GetModuleHandleW(FILENAME.data());
if (handle == nullptr) {
REL_THROW_EXCEPTION("failed to obtain module handle");
REL_FAILURE("failed to obtain module handle");
}
_base = reinterpret_cast<std::uintptr_t>(handle);
_natvis = _base;
Expand Down Expand Up @@ -516,17 +516,17 @@ namespace REL
DWORD dummy;
std::vector<char> buf(GetFileVersionInfoSizeW(FILENAME.data(), std::addressof(dummy)));
if (buf.size() == 0) {
REL_THROW_EXCEPTION("failed to obtain file version info size");
REL_FAILURE("failed to obtain file version info size");
}

if (!GetFileVersionInfoW(FILENAME.data(), 0, static_cast<DWORD>(buf.size()), buf.data())) {
REL_THROW_EXCEPTION("failed to obtain file version info");
REL_FAILURE("failed to obtain file version info");
}

LPVOID verBuf;
UINT verLen;
if (!VerQueryValueW(buf.data(), L"\\StringFileInfo\\040904B0\\ProductVersion", std::addressof(verBuf), std::addressof(verLen))) {
REL_THROW_EXCEPTION("failed to query value");
REL_FAILURE("failed to query value");
}

std::wistringstream ss(
Expand Down Expand Up @@ -570,7 +570,7 @@ namespace REL
[[nodiscard]] inline std::size_t id2offset(std::uint64_t a_id) const
{
if (_id2offset.empty()) {
REL_THROW_EXCEPTION("data is empty");
REL_FAILURE("data is empty");
}

mapping_t elem{ a_id, 0 };
Expand All @@ -582,7 +582,7 @@ namespace REL
return a_lhs.id < a_rhs.id;
});
if (it == _id2offset.end()) {
REL_THROW_EXCEPTION("id not found");
REL_FAILURE("id not found");
}

return static_cast<std::size_t>(it->offset);
Expand All @@ -592,7 +592,7 @@ namespace REL
[[nodiscard]] inline std::uint64_t offset2id(std::size_t a_offset) const
{
if (_offset2id.empty()) {
REL_THROW_EXCEPTION("data is empty");
REL_FAILURE("data is empty");
}

mapping_t elem{ 0, a_offset };
Expand All @@ -604,7 +604,7 @@ namespace REL
return a_lhs.offset < a_rhs.offset;
});
if (it == _offset2id.end()) {
REL_THROW_EXCEPTION("offset not found");
REL_FAILURE("offset not found");
}

return it->id;
Expand All @@ -625,7 +625,7 @@ namespace REL
_stream(a_filename.data(), a_mode)
{
if (!_stream.is_open()) {
REL_THROW_EXCEPTION("failed to open file");
REL_FAILURE("failed to open file");
}

_stream.exceptions(std::ios::badbit | std::ios::failbit | std::ios::eofbit);
Expand Down Expand Up @@ -663,7 +663,7 @@ namespace REL
std::int32_t format{};
a_input.readin(format);
if (format != 1) {
REL_THROW_EXCEPTION("unexpected format");
REL_FAILURE("unexpected format");
}

std::int32_t version[4]{};
Expand Down Expand Up @@ -721,15 +721,15 @@ namespace REL
header_t header;
header.read(input);
if (header.version() != a_version) {
REL_THROW_EXCEPTION("version mismatch");
REL_FAILURE("version mismatch");
}

auto mapname = L"CommonLibSSEOffsets-v2-"s;
mapname += a_version.wstring();
const auto byteSize = static_cast<std::size_t>(header.address_count()) * sizeof(mapping_t);
if (!_mmap.open(mapname, byteSize) &&
!_mmap.create(mapname, byteSize)) {
REL_THROW_EXCEPTION("failed to create shared mapping");
REL_FAILURE("failed to create shared mapping");
}

_id2offset = { static_cast<mapping_t*>(_mmap.data()), header.address_count() };
Expand Down Expand Up @@ -793,7 +793,7 @@ namespace REL
id = a_input.readout<std::uint32_t>();
break;
default:
REL_THROW_EXCEPTION("unhandled type");
REL_FAILURE("unhandled type");
}

const std::uint64_t tmp = (hi & 8) != 0 ? (prevOffset / a_header.pointer_size()) : prevOffset;
Expand Down Expand Up @@ -824,7 +824,7 @@ namespace REL
offset = a_input.readout<std::uint32_t>();
break;
default:
REL_THROW_EXCEPTION("unhandled type");
REL_FAILURE("unhandled type");
}

if ((hi & 8) != 0) {
Expand Down
4 changes: 2 additions & 2 deletions include/SKSE/API.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace SKSE
{
bool Init(const LoadInterface* a_intfc) noexcept;
void Init(const LoadInterface* a_intfc) noexcept;
void RegisterForAPIInitEvent(std::function<void()> a_fn);

PluginHandle GetPluginHandle() noexcept;
Expand All @@ -38,5 +38,5 @@ namespace SKSE
const SKSEPersistentObjectStorage* GetPersistentObjectStorage() noexcept;

Trampoline& GetTrampoline();
bool AllocTrampoline(std::size_t a_size, bool a_trySKSEReserve = true);
void AllocTrampoline(std::size_t a_size, bool a_trySKSEReserve = true);
}

0 comments on commit 66e1e3c

Please sign in to comment.