TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
exceptions.hpp
Go to the documentation of this file.
1// Copyright (c) 2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018-2024 Simons Foundation
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0.txt
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17// Authors: Thomas Hahn, Olivier Parcollet, Nils Wentzell
18
24#pragma once
25
26#include "./macros.hpp"
27
28#include <exception>
29#include <sstream>
30#include <string>
31
32#define NDA_RUNTIME_ERROR throw nda::runtime_error{} << "Error at " << __FILE__ << " : " << __LINE__ << "\n\n"
33
34#define NDA_ASSERT(X) \
35 if (!(X)) NDA_RUNTIME_ERROR << AS_STRING(X);
36
37#define NDA_ASSERT2(X, ...) \
38 if (!(X)) NDA_RUNTIME_ERROR << AS_STRING(X) << "\n" << __VA_ARGS__;
39
40namespace nda {
41
48 class runtime_error : public std::exception {
49 // Accumulator for the error message.
50 std::stringstream acc;
51
52 // Error message.
53 mutable std::string _what;
54
55 public:
57 runtime_error() noexcept : std::exception() {}
58
63 runtime_error(runtime_error const &err) noexcept : acc(err.acc.str()), _what(err._what) {}
64
66 ~runtime_error() noexcept override = default;
67
75 template <typename T>
76 runtime_error &operator<<(T const &x) {
77 acc << x;
78 return *this;
79 }
80
87 runtime_error &operator<<(const char *mess) {
88 (*this) << std::string(mess);
89 return *this;
90 }
91
96 const char *what() const noexcept override {
97 _what = acc.str();
98 return _what.c_str();
99 }
100 };
101
102} // namespace nda
Runtime error class used throughout the nda library.
const char * what() const noexcept override
Override the virtual function what from std::exception to retrieve the accumulated error message.
runtime_error() noexcept
Default constructor.
~runtime_error() noexcept override=default
Default destructor.
runtime_error(runtime_error const &err) noexcept
Copy constructor to copy the contents of the error message accumulator.
runtime_error & operator<<(const char *mess)
Accumulate error message.
Macros used in the nda library.