diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2019-12-21 13:33:50 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2019-12-21 13:33:50 +0300 |
commit | 351c5188013fff041c7217aed64478cfc7643480 (patch) | |
tree | c918b5093ac45394439f3dff30da37b809173603 /cxx/include/aesxx/error.hpp | |
parent | cmake: install libraries & their headers (diff) | |
download | aes-tools-351c5188013fff041c7217aed64478cfc7643480.tar.gz aes-tools-351c5188013fff041c7217aed64478cfc7643480.zip |
restructure the project
Diffstat (limited to 'cxx/include/aesxx/error.hpp')
-rw-r--r-- | cxx/include/aesxx/error.hpp | 104 |
1 files changed, 0 insertions, 104 deletions
diff --git a/cxx/include/aesxx/error.hpp b/cxx/include/aesxx/error.hpp deleted file mode 100644 index 4d2c783..0000000 --- a/cxx/include/aesxx/error.hpp +++ /dev/null @@ -1,104 +0,0 @@ -// Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com> -// This file is part of the "AES tools" project. -// For details, see https://github.com/egor-tensin/aes-tools. -// Distributed under the MIT License. - -#pragma once - -#include "debug.hpp" - -#include <aes/all.h> - -#include <boost/config.hpp> - -#include <cstdlib> -#include <cstring> - -#include <algorithm> -#include <functional> -#include <ostream> -#include <stdexcept> -#include <string> -#include <vector> - -namespace aes -{ - class Error : public std::runtime_error - { - public: - Error(const AES_ErrorDetails& err_details) - : std::runtime_error{format_error_message(err_details)} - { - fill_call_stack(err_details); - } - - void for_each_addr( - const std::function<void (const void*, const std::string&)>& callback) const - { - aux::CallStackFormatter formatter; - - std::for_each( - call_stack, - call_stack + call_stack_len, - [&formatter, &callback] (const void* addr) - { - callback(addr, formatter.format_address(addr)); - }); - } - - private: - static std::string format_error_message(const AES_ErrorDetails& err_details) - { - std::vector<char> buf; - buf.resize(aes_format_error(&err_details, NULL, 0)); - aes_format_error(&err_details, buf.data(), buf.size()); - return {buf.begin(), buf.end()}; - } - - void fill_call_stack(const AES_ErrorDetails& err_details) - { - call_stack_len = err_details.call_stack_len; - - if (call_stack_len > AES_MAX_CALL_STACK_LENGTH) - call_stack_len = AES_MAX_CALL_STACK_LENGTH; - - std::memcpy(call_stack, err_details.call_stack, call_stack_len * sizeof(const void*)); - } - - std::size_t call_stack_len = 0; - const void* call_stack[AES_MAX_CALL_STACK_LENGTH] = {nullptr}; - }; - - inline std::ostream& operator<<(std::ostream& os, const Error& error) - { - os << "AES error: " << error.what() << '\n'; - os << "Call stack:\n"; - error.for_each_addr([&os] (const void* addr, const std::string& name) - { - os << '\t' << addr << ' ' << name << '\n'; - }); - return os; - } - - class ErrorDetailsThrowsInDestructor - { - public: - ErrorDetailsThrowsInDestructor() - { - aes_success(get()); - } - - ~ErrorDetailsThrowsInDestructor() BOOST_NOEXCEPT_IF(false) - { - if (aes_is_error(aes_get_error_code(get()))) - throw Error(impl); - } - - AES_ErrorDetails* get() { return &impl; } - - operator AES_ErrorDetails*() { return get(); } - - private: - AES_ErrorDetails impl; - }; -} |