ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ThrowAssert.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ThrowAssert.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2018 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 #include <exception>
11 #include <iostream>
12 #include <sstream>
13 #include <string>
14 
15 namespace Acts {
18 class AssertionFailureException : public std::exception {
19  public:
22  private:
23  std::ostringstream stream;
24 
25  public:
27  operator std::string() const { return stream.str(); }
28 
33  template <typename T>
35  stream << value;
36  return *this;
37  }
38  };
39 
45  AssertionFailureException(const std::string& expression,
46  const std::string& file, int line,
47  const std::string& msg) {
48  std::ostringstream os;
49 
50  if (!msg.empty()) {
51  os << msg << ": ";
52  }
53 
54  os << "Assertion '" << expression << "'";
55 
56  os << " failed in file '" << file << "' line " << line;
57  report = os.str();
58  }
59 
61  const char* what() const throw() override { return report.c_str(); }
62 
63  private:
64  std::string report;
65 };
66 
67 } // namespace Acts
68 
69 #define throw_assert(EXPRESSION, MESSAGE) \
70  if (!(EXPRESSION)) { \
71  throw Acts::AssertionFailureException( \
72  #EXPRESSION, __FILE__, __LINE__, \
73  (Acts::AssertionFailureException::StreamFormatter() << MESSAGE)); \
74  }