• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #853

19 Dec 2022 01:01AM UTC coverage: 86.287% (+0.4%) from 85.912%
#853

push

StellarBot
Merge #6109

6109: Modernize serialization module r=hkaiser a=hkaiser

- flyby separate serialization of Boost types

working towards https://github.com/STEllAR-GROUP/hpx/issues/5497

Co-authored-by: Hartmut Kaiser <hartmut.kaiser@gmail.com>

53 of 53 new or added lines in 6 files covered. (100.0%)

173939 of 201582 relevant lines covered (86.29%)

1931657.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

13.66
/libs/core/serialization/src/exception_ptr.cpp
1
//  Copyright (c)      2020 ETH Zurich
2
//  Copyright (c) 2007-2022 Hartmut Kaiser
3
//
4
//  SPDX-License-Identifier: BSL-1.0
5
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
6
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7

8
#include <hpx/assert.hpp>
9
#include <hpx/modules/errors.hpp>
10
#include <hpx/serialization/exception_ptr.hpp>
11
#include <hpx/serialization/serialize.hpp>
12

13
#if ASIO_HAS_BOOST_THROW_EXCEPTION != 0
14
#include <boost/exception/diagnostic_information.hpp>
15
#include <boost/exception/exception.hpp>
16
#endif
17

18
#include <cstddef>
19
#include <cstdint>
20
#include <exception>
21
#include <stdexcept>
22
#include <string>
23
#include <system_error>
24
#include <typeinfo>
25

26
///////////////////////////////////////////////////////////////////////////////
27
namespace hpx::serialization {
28
    namespace detail {
29
        ///////////////////////////////////////////////////////////////////////////
30
        // TODO: This is not scalable, and painful to update.
31
        void save(output_archive& ar, std::exception_ptr const& ep,
×
32
            unsigned int /* version */)
33
        {
34
            hpx::util::exception_type type =
×
35
                hpx::util::exception_type::unknown_exception;
36
            std::string what;
×
37
            hpx::error err_value = hpx::error::success;
×
38
            std::string err_message;
×
39

40
            std::string throw_function_;
×
41
            std::string throw_file_;
×
42
            long throw_line_ = 0;
×
43

44
            // retrieve information related to exception_info
45
            try
46
            {
47
                std::rethrow_exception(ep);
×
48
            }
×
49
            catch (exception_info const& xi)
50
            {
51
                std::string const* function =
×
52
                    xi.get<hpx::detail::throw_function>();
×
53
                if (function)
×
54
                    throw_function_ = *function;
×
55

56
                std::string const* file = xi.get<hpx::detail::throw_file>();
×
57
                if (file)
×
58
                    throw_file_ = *file;
×
59

60
                long const* line = xi.get<hpx::detail::throw_line>();
×
61
                if (line)
×
62
                    throw_line_ = *line;
×
63
            }
×
64

65
            // figure out concrete underlying exception type
66
            try
67
            {
68
                std::rethrow_exception(ep);
×
69
            }
×
70
            catch (hpx::thread_interrupted const&)
71
            {
72
                type =
×
73
                    hpx::util::exception_type::hpx_thread_interrupted_exception;
74
                what = "hpx::thread_interrupted";
×
75
                err_value = hpx::error::thread_cancelled;
×
76
            }
×
77
            catch (hpx::exception const& e)
78
            {
79
                type = hpx::util::exception_type::hpx_exception;
×
80
                what = e.what();
×
81
                err_value = e.get_error();
×
82
            }
×
83
            catch (std::system_error const& e)
84
            {
85
                type = hpx::util::exception_type::std_system_error;
×
86
                what = e.what();
×
87
                err_value = static_cast<hpx::error>(e.code().value());
×
88
                err_message = e.code().message();
×
89
            }
×
90
            catch (std::runtime_error const& e)
91
            {
92
                type = hpx::util::exception_type::std_runtime_error;
×
93
                what = e.what();
×
94
            }
×
95
            catch (std::invalid_argument const& e)
96
            {
97
                type = hpx::util::exception_type::std_invalid_argument;
×
98
                what = e.what();
×
99
            }
×
100
            catch (std::out_of_range const& e)
101
            {
102
                type = hpx::util::exception_type::std_out_of_range;
×
103
                what = e.what();
×
104
            }
×
105
            catch (std::logic_error const& e)
106
            {
107
                type = hpx::util::exception_type::std_logic_error;
×
108
                what = e.what();
×
109
            }
×
110
            catch (std::bad_alloc const& e)
111
            {
112
                type = hpx::util::exception_type::std_bad_alloc;
×
113
                what = e.what();
×
114
            }
×
115
            catch (std::bad_cast const& e)
116
            {
117
                type = hpx::util::exception_type::std_bad_cast;
×
118
                what = e.what();
×
119
            }
×
120
            catch (std::bad_typeid const& e)
121
            {
122
                type = hpx::util::exception_type::std_bad_typeid;
×
123
                what = e.what();
×
124
            }
×
125
            catch (std::bad_exception const& e)
126
            {
127
                type = hpx::util::exception_type::std_bad_exception;
×
128
                what = e.what();
×
129
            }
×
130
            catch (std::exception const& e)
131
            {
132
                type = hpx::util::exception_type::std_exception;
×
133
                what = e.what();
×
134
            }
×
135
#if BOOST_ASIO_HAS_BOOST_THROW_EXCEPTION != 0
136
            catch (boost::exception const& e)
137
            {
138
                type = hpx::util::boost_exception;
139
                what = boost::diagnostic_information(e);
140
            }
141
#endif
142
            catch (...)
143
            {
144
                type = hpx::util::exception_type::unknown_exception;
×
145
                what = "unknown exception";
×
146
            }
×
147

148
            // clang-format off
149
            ar & type & what & throw_function_ & throw_file_ & throw_line_;
×
150
            // clang-format on
151

152
            if (hpx::util::exception_type::hpx_exception == type)
×
153
            {
154
                // clang-format off
155
                ar << err_value;
×
156
                // clang-format on
157
            }
×
158
            else if (hpx::util::exception_type::boost_system_error == type ||
×
159
                hpx::util::exception_type::std_system_error == type)
×
160
            {
161
                // clang-format off
162
                ar << err_value << err_message;
×
163
                // clang-format on
164
            }
×
165
        }
×
166

167
        ///////////////////////////////////////////////////////////////////////////
168
        // TODO: This is not scalable, and painful to update.
169
        void load(input_archive& ar, std::exception_ptr& e,
×
170
            unsigned int /* version */)
171
        {
172
            hpx::util::exception_type type =
×
173
                hpx::util::exception_type::unknown_exception;
174
            std::string what;
×
175
            hpx::error err_value = hpx::error::success;
×
176
            std::string err_message;
×
177

178
            std::string throw_function_;
×
179
            std::string throw_file_;
×
180
            int throw_line_ = 0;
×
181

182
            // clang-format off
183
            ar & type & what & throw_function_ & throw_file_ & throw_line_;
×
184
            // clang-format on
185

186
            if (hpx::util::exception_type::hpx_exception == type)
×
187
            {
188
                // clang-format off
189
                ar & err_value;
×
190
                ar >> err_value;
×
191
                // clang-format on
192
            }
×
193
            else if (hpx::util::exception_type::boost_system_error == type ||
×
194
                hpx::util::exception_type::std_system_error == type)
×
195
            {
196
                // clang-format off
197
                ar & err_value& err_message;
×
198
                ar >> err_value >> err_message;
×
199
                // clang-format on
200
            }
×
201

202
            switch (type)
×
203
            {
×
204
            default:
205
            case hpx::util::exception_type::std_exception:
206
                [[fallthrough]];
207

208
            case hpx::util::exception_type::unknown_exception:
209
                e = hpx::detail::get_exception(hpx::detail::std_exception(what),
×
210
                    throw_function_, throw_file_, throw_line_);
×
211
                break;
×
212

213
            // standard exceptions
214
            case hpx::util::exception_type::std_runtime_error:
215
                e = hpx::detail::get_exception(std::runtime_error(what),
×
216
                    throw_function_, throw_file_, throw_line_);
×
217
                break;
×
218

219
            case hpx::util::exception_type::std_invalid_argument:
220
                e = hpx::detail::get_exception(std::invalid_argument(what),
×
221
                    throw_function_, throw_file_, throw_line_);
×
222
                break;
×
223

224
            case hpx::util::exception_type::std_out_of_range:
225
                e = hpx::detail::get_exception(std::out_of_range(what),
×
226
                    throw_function_, throw_file_, throw_line_);
×
227
                break;
×
228

229
            case hpx::util::exception_type::std_logic_error:
230
                e = hpx::detail::get_exception(std::logic_error(what),
×
231
                    throw_function_, throw_file_, throw_line_);
×
232
                break;
×
233

234
            case hpx::util::exception_type::std_bad_alloc:
235
                e = hpx::detail::get_exception(hpx::detail::bad_alloc(what),
×
236
                    throw_function_, throw_file_, throw_line_);
×
237
                break;
×
238

239
            case hpx::util::exception_type::std_bad_cast:
240
                e = hpx::detail::get_exception(hpx::detail::bad_cast(what),
×
241
                    throw_function_, throw_file_, throw_line_);
×
242
                break;
×
243

244
            case hpx::util::exception_type::std_bad_typeid:
245
                e = hpx::detail::get_exception(hpx::detail::bad_typeid(what),
×
246
                    throw_function_, throw_file_, throw_line_);
×
247
                break;
×
248
            case hpx::util::exception_type::std_bad_exception:
249
                e = hpx::detail::get_exception(hpx::detail::bad_exception(what),
×
250
                    throw_function_, throw_file_, throw_line_);
×
251
                break;
×
252

253
#if ASIO_HAS_BOOST_THROW_EXCEPTION != 0
254
            // boost exceptions
255
            case hpx::util::boost_exception:
256
                HPX_ASSERT(false);    // shouldn't happen
257
                break;
258
#endif
259

260
            // boost::system::system_error
261
            case hpx::util::exception_type::boost_system_error:
262
                [[fallthrough]];
263

264
            // std::system_error
265
            case hpx::util::exception_type::std_system_error:
266
                e = hpx::detail::get_exception(
×
267
                    std::system_error(static_cast<int>(err_value),
×
268
                        std::system_category(), err_message),
×
269
                    throw_function_, throw_file_, throw_line_);
×
270
                break;
×
271

272
            // hpx::exception
273
            case hpx::util::exception_type::hpx_exception:
274
                e = hpx::detail::get_exception(
×
275
                    hpx::exception(err_value, what, hpx::throwmode::rethrow),
×
276
                    throw_function_, throw_file_, throw_line_);
×
277
                break;
×
278

279
            // hpx::thread_interrupted
280
            case hpx::util::exception_type::hpx_thread_interrupted_exception:
281
                e = hpx::detail::construct_lightweight_exception(
×
282
                    hpx::thread_interrupted());
×
283
                break;
×
284
            }
285
        }
×
286

287
        save_custom_exception_handler_type& get_save_custom_exception_handler()
29,623✔
288
        {
289
            static save_custom_exception_handler_type f = save;
29,623✔
290
            return f;
29,623✔
291
        }
×
292

293
        HPX_CORE_EXPORT void set_save_custom_exception_handler(
1,219✔
294
            save_custom_exception_handler_type f)
295
        {
296
            get_save_custom_exception_handler() = HPX_MOVE(f);
1,219✔
297
        }
1,219✔
298

299
        load_custom_exception_handler_type& get_load_custom_exception_handler()
15,421✔
300
        {
301
            static load_custom_exception_handler_type f = load;
15,421✔
302
            return f;
15,421✔
303
        }
×
304

305
        HPX_CORE_EXPORT void set_load_custom_exception_handler(
1,219✔
306
            load_custom_exception_handler_type f)
307
        {
308
            get_load_custom_exception_handler() = HPX_MOVE(f);
1,219✔
309
        }
1,219✔
310
    }    // namespace detail
311

312
    ///////////////////////////////////////////////////////////////////////////
313
    template <typename Archive>
314
    void save(Archive& ar, std::exception_ptr const& ep, unsigned int version)
14,202✔
315
    {
316
        if (detail::get_save_custom_exception_handler())
14,202✔
317
        {
318
            detail::get_save_custom_exception_handler()(ar, ep, version);
14,202✔
319
        }
14,202✔
320
        else
321
        {
322
            HPX_THROW_EXCEPTION(hpx::error::invalid_status,
×
323
                "hpx::serialization::save",
324
                "Attempted to save a std::exception_ptr, but there is no "
325
                "handler installed. Set one with "
326
                "hpx::serialization::detail::set_save_custom_exception_"
327
                "handler.");
328
        }
329
    }
14,202✔
330

331
    ///////////////////////////////////////////////////////////////////////////
332
    template <typename Archive>
333
    void load(Archive& ar, std::exception_ptr& ep, unsigned int version)
7,101✔
334
    {
335
        if (detail::get_load_custom_exception_handler())
7,101✔
336
        {
337
            detail::get_load_custom_exception_handler()(ar, ep, version);
7,101✔
338
        }
7,101✔
339
        else
340
        {
341
            HPX_THROW_EXCEPTION(hpx::error::invalid_status,
×
342
                "hpx::serialization::load",
343
                "Attempted to load a std::exception_ptr, but there is no "
344
                "handler installed. Set one with "
345
                "hpx::serialization::detail::set_load_custom_exception_"
346
                "handler.");
347
        }
348
    }
7,101✔
349

350
    template HPX_CORE_EXPORT void save(hpx::serialization::output_archive&,
351
        std::exception_ptr const&, unsigned int);
352

353
    template HPX_CORE_EXPORT void load(
354
        hpx::serialization::input_archive&, std::exception_ptr&, unsigned int);
355
}    // namespace hpx::serialization
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc