Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:12

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