/*
    libMakeMKV - MKV multiplexer library

    Copyright (C) 2007-2025 GuinpinSoft inc <libmkv@makemkv.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/
#ifndef CASSERT_LGPL_H_INCLUDED
#define CASSERT_LGPL_H_INCLUDED

#include <lgpl/stl.h>
#include <lgpl/stdstring.h>

#if defined (_darwin_)
// Below is a verbatim quote from Mac OS developer tools <memory> header. Thank you Apple, you are the best.

// TODO: We re-added #include <cassert> to <memory> to make the internal build
//       pass, but we should remove it as soon as all internal clients have fixed
//       their code.
#include <memory>
#if defined(assert)
#undef assert
#endif // assert
#endif // _darwin_

class mkv_error_exception_unbuffered
{
    const char* m_message;
public:
    mkv_error_exception_unbuffered(const char* Message) throw()
        : m_message(Message)
    {
    }
    ~mkv_error_exception_unbuffered() throw()
    {
    }
    const char* what() const throw()
    {
      return m_message;
    }
};

class mkv_error_exception_buffered
{
    buf::string m_message;
public:
    mkv_error_exception_buffered(const char* Message) throw()
        : m_message(Message)
    {
    }
    mkv_error_exception_buffered(const StringPointer& Message) throw()
        : m_message(Message)
    {
    }
    ~mkv_error_exception_buffered() throw()
    {
    }
    const char* what() const throw()
    {
        return strdup(m_message.c_str());
    }
};

typedef mkv_error_exception_buffered mkv_logic_error;

#define mkv_error_exception(x) mkv_error_exception_unbuffered(x "\0")

extern const char* MkvErrorText;

inline static bool MkvCheckError()
{
    return (MkvErrorText == NULL);
}

inline static void MkvClearError()
{
    MkvErrorText = NULL;
}

inline static const char* MkvGetErrorText()
{
    return MkvErrorText;
}

inline static int MkvErrorHelper(const char* Message)
{
    MkvErrorText = Message;
    return 0;
}


#define MKV_ASSERT(_Expression_) \
    (void)( (!!(_Expression_)) || (MkvErrorHelper("MKV_ASSERT: " #_Expression_ )) );

#define MKV_THROW_ERROR(_Message_) \
    (MkvErrorHelper(_Message_))


#ifdef assert
#error assert
#endif

#define assert MKV_ASSERT

#endif // CASSERT_LGPL_H_INCLUDED
