-
Notifications
You must be signed in to change notification settings - Fork 14
/
error.hpp
116 lines (93 loc) · 4.6 KB
/
error.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (C) 2014-2015 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#ifndef FOONATHAN_STRING_ID_ERROR_HPP_INCLUDED
#define FOONATHAN_STRING_ID_ERROR_HPP_INCLUDED
#include <exception>
#include <string>
#include "config.hpp"
#include "hash.hpp"
namespace foonathan { namespace string_id
{
/// \brief The base class for all custom exception classes of this library.
class error : public std::exception
{
protected:
error() = default;
};
/// \brief The type of the collision handler.
/// \detail It will be called when a string hashing results in a collision giving it the two strings collided.
/// The default handler throws an exception of type \ref collision_error.
typedef void(*collision_handler)(hash_type hash, const char *a, const char *b);
/// \brief Exchanges the \ref collision_handler.
/// \detail This function is thread safe if \ref FOONATHAN_STRING_ID_ATOMIC_HANDLER is \c true.
collision_handler set_collision_handler(collision_handler h);
/// \brief Returns the current \ref collision_handler.
collision_handler get_collision_handler();
/// \brief The exception class thrown by the default \ref collision_handler.
class collision_error : public error
{
public:
//=== constructor/destructor ===//
/// \brief Creates a new exception, same parameter as \ref collision_handler.
collision_error(hash_type hash, const char *a, const char *b)
: a_(a), b_(b),
what_(R"(foonathan::string_id::collision_error: strings ")" + a_ + R"(" and ")" + b_ +
R"(") are both producing the value )" + std::to_string(hash)), hash_(hash) {}
~collision_error() FOONATHAN_NOEXCEPT FOONATHAN_OVERRIDE {}
//=== accessors ===//
const char* what() const FOONATHAN_NOEXCEPT FOONATHAN_OVERRIDE;
/// @{
/// \brief Returns one of the two strings that colllided.
const char* first_string() const FOONATHAN_NOEXCEPT
{
return a_.c_str();
}
const char* second_string() const FOONATHAN_NOEXCEPT
{
return b_.c_str();
}
/// @}
/// \brief Returns the hash code of the collided strings.
hash_type hash_code() const FOONATHAN_NOEXCEPT
{
return hash_;
}
private:
std::string a_, b_, what_;
hash_type hash_;
};
/// \brief The type of the generator error handler.
/// \detail It will be called when a generator would generate a \ref string_id that already was generated.
/// The generator will try again until the handler returns \c false in which case it just returns the old \c string_id.
/// It passes the number of tries, the name of the generator and the hash and string of the generated \c string_id.<br>
/// The default handler allows 8 tries and then throws an exception of type \ref generation_error.
typedef bool(*generation_error_handler)(std::size_t no, const char *generator_name,
hash_type hash, const char *str);
/// \brief Exchanges the \ref generation_error_handler.
/// \detail This function is thread safe if \ref FOONATHAN_STRING_ID_ATOMIC_HANDLER is \c true.
generation_error_handler set_generation_error_handler(generation_error_handler h);
/// \brief Returns the current \ref generation_error_handler.
generation_error_handler get_generation_error_handler();
/// \brief The exception class thrown by the default \ref generation_error_handler.
class generation_error : public error
{
public:
//=== constructor/destructor ===//
/// \brief Creates it by giving it the name of the generator.
generation_error(const char *generator_name)
: name_(generator_name), what_("foonathan::string_id::generation_error: Generator \"" + name_ +
"\" was unable to generate new string id.") {}
~generation_error() FOONATHAN_NOEXCEPT FOONATHAN_OVERRIDE {}
//=== accessors ===//
const char* what() const FOONATHAN_NOEXCEPT FOONATHAN_OVERRIDE;
/// \brief Returns the name of the generator.
const char* generator_name() const FOONATHAN_NOEXCEPT
{
return name_.c_str();
}
private:
std::string name_, what_;
};
}} // namespace foonathan::string_id
#endif // FOONATHAN_STRING_ID_ERROR_HPP_INCLUDED