Program Listing for File Error.hpp

Return to documentation for file (pennylane_lightning/core/src/utils/Error.hpp)

// Copyright 2018-2023 Xanadu Quantum Technologies Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//     http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once

#include <exception>
#include <sstream>
#include <string>

// NOLINTBEGIN(cppcoreguidelines-macro-usage)
#define PL_ABORT(message)                                                      \
    Pennylane::Util::Abort((message), __FILE__, __LINE__, __func__)
#define PL_ABORT_IF(expression, message)                                       \
    if ((expression)) {                                                        \
        PL_ABORT(message);                                                     \
    }
#define PL_ABORT_IF_NOT(expression, message)                                   \
    if (!(expression)) {                                                       \
        PL_ABORT(message);                                                     \
    }

#define PL_ASSERT(expression)                                                  \
    PL_ABORT_IF_NOT(expression, "Assertion failed: " #expression)

// NOLINTEND(cppcoreguidelines-macro-usage)

namespace Pennylane::Util {
class LightningException : public std::exception {
  private:
    std::string err_msg;

  public:
    explicit LightningException(std::string err_msg) noexcept
        : err_msg{std::move(err_msg)} {}

    LightningException(const LightningException &) = default;
    LightningException(LightningException &&) noexcept = default;

    auto operator=(const LightningException &) -> LightningException & = delete;
    auto operator=(LightningException &&) -> LightningException & = delete;

    ~LightningException() override = default;

    [[nodiscard]] auto what() const noexcept -> const char * override {
        return err_msg.c_str();
    }
};

[[noreturn]] inline void Abort(const char *message, const char *file_name,
                               int line, const char *function_name) {
    std::stringstream err_msg;
    err_msg << "[" << file_name << "][Line:" << line
            << "][Method:" << function_name
            << "]: Error in PennyLane Lightning: " << message;
    throw LightningException(err_msg.str());
}
[[noreturn]] inline void Abort(const std::string &message,
                               const char *file_name, int line,
                               const char *function_name) {
    std::stringstream err_msg;
    err_msg << "[" << file_name << "][Line:" << line
            << "][Method:" << function_name
            << "]: Error in PennyLane Lightning: " << message;
    throw LightningException(err_msg.str());
}

} // namespace Pennylane::Util