2023-11-20 23:25:36 -06:00
#pragma region License
2023-11-14 18:11:32 -06:00
/*
License (OLC-3)
~~~~~~~~~~~~~~~
2024-01-02 00:46:32 -06:00
Copyright 2024 Joshua Sigona <sigonasr2@gmail.com>
2023-11-14 18:11:32 -06:00
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions or derivations of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions or derivative works in binary form must reproduce the above
copyright notice. This list of conditions and the following disclaimer must be
reproduced in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
2023-11-29 00:50:00 -06:00
2024-01-30 14:48:49 +00:00
Portions of this software are copyright © 2024 The FreeType
2023-11-29 00:50:00 -06:00
Project (www.freetype.org). Please see LICENSE_FT.txt for more information.
All rights reserved.
2023-11-14 18:11:32 -06:00
*/
2023-11-20 23:25:36 -06:00
#pragma endregion
2023-11-11 00:54:50 -06:00
#pragma once
2023-11-16 18:54:52 -06:00
#include <iostream>
#include <sstream>
2023-12-27 20:40:01 -06:00
#include <format>
#include <any>
2024-01-16 00:20:30 -06:00
#include <memory>
2024-01-27 04:51:14 -06:00
#include <source_location>
2024-03-25 14:15:37 -05:00
#include <fstream>
inline std :: ofstream debugLogger ;
2023-11-11 00:54:50 -06:00
#ifdef _DEBUG
2024-05-24 22:37:20 -05:00
#ifndef __EMSCRIPTEN__
#ifndef __linux__
#define NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
// Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the
// allocations to be of _CLIENT_BLOCK type
#endif
#endif
2024-01-29 19:01:51 +00:00
#endif
2024-05-24 22:37:20 -05:00
#ifndef NEW //For everything else.
2024-01-29 19:01:51 +00:00
#define NEW new
#endif
2023-11-11 00:54:50 -06:00
#undef ERR //Stupid Windows
#ifndef __EMSCRIPTEN__
2023-11-16 18:54:52 -06:00
//WARNING! err accepts a stream of data using << operators. If you want to concatenate strings via the + operator, you must wrap the entire operation in ()
2023-11-11 00:54:50 -06:00
#define ERR(err) { \
std::stringstream errStream; \
errStream<<err; \
Error::log(errStream,std::source_location::current());}
2024-03-25 14:15:37 -05:00
#define LOG(msg) { \
std::stringstream debugStream; \
debugStream<<msg; \
Error::debug(debugStream,std::source_location::current());}
2023-11-11 00:54:50 -06:00
class Error {
public :
2024-03-25 14:15:37 -05:00
inline static void debug ( std :: stringstream & str , std :: source_location loc ){
debugLogger << loc . file_name () << "(" << loc . line () << ":" << loc . column () << ") " << loc . function_name () << ": " << str . str () << std :: endl ;
std :: cout << loc . file_name () << "(" << loc . line () << ":" << loc . column () << ") " << loc . function_name () << ": " << str . str () << std :: endl ;
}
2023-11-11 00:54:50 -06:00
inline static void log ( std :: stringstream & str , std :: source_location loc ){
2024-03-25 14:15:37 -05:00
debugLogger << loc . file_name () << "(" << loc . line () << ":" << loc . column () << ") " << loc . function_name () << ": " << str . str () << std :: endl ;
2023-11-11 00:54:50 -06:00
std :: cout << loc . file_name () << "(" << loc . line () << ":" << loc . column () << ") " << loc . function_name () << ": " << str . str () << std :: endl ;
2024-07-07 10:07:13 -05:00
throw std :: exception { std :: format ( "{}({}:{}) {}: {}" , loc . file_name (), loc . line (), loc . column (), loc . function_name (), str . str ()). c_str ()};
2023-11-11 00:54:50 -06:00
}
};
#else
#define ERR(err) { \
std::stringstream errStream; \
errStream<<err; \
2024-01-27 04:51:14 -06:00
Error::log(errStream,std::source_location::current());}
2024-03-26 22:19:17 -05:00
#define LOG(err) { \
std::stringstream errStream; \
errStream<<err; \
Error::log(errStream,std::source_location::current());}
2024-01-27 04:51:14 -06:00
class Error {
public :
inline static void log ( std :: stringstream & str , std :: source_location loc ){
2024-06-18 15:35:58 -05:00
std :: cout << loc . file_name () << "(" << loc . line () << ":" << loc . column () << ") " << loc . function_name () << ": " << str . str () << std :: endl ;
2024-01-27 04:51:14 -06:00
#ifdef __DEBUG__
throw ;
#endif
}
};
2023-11-11 00:54:50 -06:00
#define _CrtDumpMemoryLeaks() ((int)0)
2023-12-27 20:40:01 -06:00
#endif
template < typename type >
type DYNAMIC_CAST ( auto variable ){
type pointer = dynamic_cast < type > ( variable );
if ( pointer == nullptr ) ERR ( "Could not dynamic cast to type " << typeid ( variable ). name () << "!" );
return pointer ;
2024-01-16 00:20:30 -06:00
}
2024-01-16 21:03:46 +00:00
template < typename T , typename U >
std :: shared_ptr < T > DYNAMIC_POINTER_CAST ( const std :: shared_ptr < U >& variable ){
std :: shared_ptr < T > newVariable = dynamic_pointer_cast < T > ( variable );
2024-01-19 00:07:29 -06:00
if ( ! newVariable ) ERR ( "Could not dynamic cast to pointer type " << typeid ( newVariable ). name () << "!" );
2024-01-16 21:03:46 +00:00
return newVariable ;
}
template < typename T , typename U >
std :: shared_ptr < T > DYNAMIC_POINTER_CAST ( const std :: weak_ptr < U >& variable ){
std :: shared_ptr < T > newVariable = dynamic_pointer_cast < T > ( variable . lock ());
2024-01-19 00:07:29 -06:00
if ( ! newVariable ) ERR ( "Could not dynamic cast to pointer type " << typeid ( newVariable ). name () << "!" );
2024-01-16 21:03:46 +00:00
return newVariable ;
}