Loading...
Searching...
No Matches
error.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003-2023, John Wiegley. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of New Artisans LLC nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
42#pragma once
43
44namespace ledger {
45
46extern std::ostringstream _desc_buffer;
47
48template <typename T>
49[[ noreturn ]] inline void throw_func(const string& message) {
50 _desc_buffer.clear();
51 _desc_buffer.str("");
52 throw T(message);
53}
54
55#define throw_(cls, msg) \
56 ((_desc_buffer << (msg)), \
57 throw_func<cls>(_desc_buffer.str()))
58
59inline void warning_func(const string& message) {
60 std::cerr << "Warning: " << message << std::endl;
61 _desc_buffer.clear();
62 _desc_buffer.str("");
63}
64
65#define warning_(msg) \
66 ((_desc_buffer << (msg)), \
67 warning_func(_desc_buffer.str()))
68
69extern std::ostringstream _ctxt_buffer;
70
71#define add_error_context(msg) \
72 ((long(_ctxt_buffer.tellp()) == 0) ? \
73 (_ctxt_buffer << (msg)) : \
74 (_ctxt_buffer << std::endl << (msg)))
75
77
78string file_context(const path& file, std::size_t line);
79string line_context(const string& line,
80 const string::size_type pos = 0,
81 const string::size_type end_pos = 0);
82
83string source_context(const path& file,
84 const std::istream::pos_type pos,
85 const std::istream::pos_type end_pos,
86 const string& prefix = "");
87
88#define DECLARE_EXCEPTION(name, kind) \
89 class name : public kind { \
90 public: \
91 explicit name(const string& why) throw() : kind(why) {} \
92 virtual ~name() throw() {} \
93 }
94
96 std::size_t count;
97 std::string message;
98 explicit error_count(std::size_t _count, std::string _msg) : count(_count), message(_msg) {}
99 const char * what() const { return message.c_str(); }
100};
101
102} // namespace ledger
void throw_func(const string &message)
Definition error.h:49
std::ostringstream _desc_buffer
void warning_func(const string &message)
Definition error.h:59
boost::filesystem::path path
Definition utils.h:68
std::ostringstream _ctxt_buffer
string line_context(const string &line, const string::size_type pos=0, const string::size_type end_pos=0)
string source_context(const path &file, const std::istream::pos_type pos, const std::istream::pos_type end_pos, const string &prefix="")
string error_context()
T & downcast(U &object)
Definition utils.h:468
string file_context(const path &file, std::size_t line)
std::size_t count
Definition error.h:96
std::string message
Definition error.h:97
const char * what() const
Definition error.h:99
error_count(std::size_t _count, std::string _msg)
Definition error.h:98