Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:09:30

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2018 CERN for the benefit of the Acts project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 #include <exception>
0011 #include <iostream>
0012 #include <sstream>
0013 #include <string>
0014 
0015 namespace Acts {
0016 /// @brief Exception type for assertion failures
0017 /// This class captures the information available to the throw_assert macro
0018 class AssertionFailureException : public std::exception {
0019  public:
0020   /// @brief Class which allows to use the << operator to assemble a string
0021   class StreamFormatter {
0022    private:
0023     std::ostringstream stream;
0024 
0025    public:
0026     /// @brief Converts to string
0027     operator std::string() const { return stream.str(); }
0028 
0029     /// @brief Stream operator which takes everything and forwards
0030     ///        it to the stringstream.
0031     /// @tparam T type of anything
0032     /// @param value const ref to anything
0033     template <typename T>
0034     StreamFormatter& operator<<(const T& value) {
0035       stream << value;
0036       return *this;
0037     }
0038   };
0039 
0040   /// @brief Construct an assertion failure exception, captures macro info
0041   /// @param expression The expression being asserted
0042   /// @param file The current file
0043   /// @param line The current line
0044   /// @param msg The message to print if assertion fails
0045   AssertionFailureException(const std::string& expression,
0046                             const std::string& file, int line,
0047                             const std::string& msg) {
0048     std::ostringstream os;
0049 
0050     if (!msg.empty()) {
0051       os << msg << ": ";
0052     }
0053 
0054     os << "Assertion '" << expression << "'";
0055 
0056     os << " failed in file '" << file << "' line " << line;
0057     report = os.str();
0058   }
0059 
0060   /// The assertion message
0061   const char* what() const throw() override { return report.c_str(); }
0062 
0063  private:
0064   std::string report;
0065 };
0066 
0067 }  // namespace Acts
0068 
0069 #define throw_assert(EXPRESSION, MESSAGE)                                 \
0070   if (!(EXPRESSION)) {                                                    \
0071     throw Acts::AssertionFailureException(                                \
0072         #EXPRESSION, __FILE__, __LINE__,                                  \
0073         (Acts::AssertionFailureException::StreamFormatter() << MESSAGE)); \
0074   }