/*
 * Copyright © 2007-2026 Dynare Team
 *
 * This file is part of Dynare.
 *
 * Dynare is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dynare is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Dynare.  If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef ERROR_HANDLING_HH
#define ERROR_HANDLING_HH

#include <cmath>
#include <format>
#include <sstream>
#include <string>

using namespace std;

struct GeneralException
{
  const string message;
};

struct FloatingPointException : public GeneralException
{
  const string location;
  FloatingPointException(const string& details, string location_arg) :
      GeneralException {"Floating point error: " + details}, location {move(location_arg)}
  {
  }
};

struct UnaryOpException : public FloatingPointException
{
  UnaryOpException(const string& op, double value, string location_arg) :
      FloatingPointException {std::format("{}(X) with X={}", op, value), move(location_arg)}
  {
  }
};

struct DivideException : public FloatingPointException
{
  DivideException(double v1, double v2, string location_arg) :
      FloatingPointException {std::format("a/X with a={} and X={}", v1, v2), move(location_arg)}
  {
  }
};

struct PowException : public FloatingPointException
{
  PowException(double base, double exponent, string location_arg) :
      FloatingPointException {std::format("X^a with X={} and a={}", base, exponent),
                              move(location_arg)}
  {
  }
};

struct UserException : public GeneralException
{
  UserException() : GeneralException {"User break"}
  {
  }
};

struct FatalException : public GeneralException
{
  template<typename... Args>
  FatalException(const format_string<Args...>& fmt, Args&&... args) :
      GeneralException {"Fatal error: " + format(fmt, forward<Args>(args)...)}
  {
  }
};

inline void
test_mxMalloc(void* z, int line, const string& file, const string& func, int amount)
{
  if (!z && amount > 0)
    throw FatalException {
        "mxMalloc: out of memory, {} bytes required at line {} in function {} (file {})", amount,
        line, func, file};
}

#ifdef MATLAB_MEX_FILE
extern "C" bool utIsInterruptPending();
#else
# include <octave/quit.h>
#endif

#endif
