-
Notifications
You must be signed in to change notification settings - Fork 14
/
error.cpp
93 lines (79 loc) · 2.33 KB
/
error.cpp
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
// 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.
#include "error.hpp"
#include <atomic>
#include <sstream>
namespace sid = foonathan::string_id;
namespace
{
void default_collision_handler(sid::hash_type hash, const char *a, const char *b)
{
throw sid::collision_error(hash, a, b);
}
#if FOONATHAN_STRING_ID_ATOMIC_HANDLER
std::atomic<sid::collision_handler> collision_h(default_collision_handler);
#else
sid::collision_handler collision_h(default_collision_handler);
#endif
}
sid::collision_handler sid::set_collision_handler(collision_handler h)
{
#if FOONATHAN_STRING_ID_ATOMIC_HANDLER
return collision_h.exchange(h);
#else
auto val = collision_h;
collision_h = h;
return val;
#endif
}
sid::collision_handler sid::get_collision_handler()
{
return collision_h;
}
const char* sid::collision_error::what() const FOONATHAN_NOEXCEPT try
{
return what_.c_str();
}
catch (...)
{
return "foonathan::string_id::collision_error: two different strings are producing the same value";
}
namespace
{
FOONATHAN_CONSTEXPR auto no_tries_generation = 8u;
bool default_generation_error_handler(std::size_t no, const char *generator_name,
sid::hash_type, const char *)
{
if (no >= no_tries_generation)
throw sid::generation_error(generator_name);
return true;
}
#if FOONATHAN_STRING_ID_ATOMIC_HANDLER
std::atomic<sid::generation_error_handler> generation_error_h(default_generation_error_handler);
#else
sid::generation_error_handler generation_error_h(default_generation_error_handler);
#endif
}
sid::generation_error_handler sid::set_generation_error_handler(generation_error_handler h)
{
#if FOONATHAN_STRING_ID_ATOMIC_HANDLER
return generation_error_h.exchange(h);
#else
auto val = generation_error_h;
generation_error_h = h;
return val;
#endif
}
sid::generation_error_handler sid::get_generation_error_handler()
{
return generation_error_h;
}
const char* sid::generation_error::what() const FOONATHAN_NOEXCEPT try
{
return what_.c_str();
}
catch (...)
{
return "foonathan::string_id::generation_error: unable to generate new string id.";
}