/*
	This file is part of Fennix Kernel.

	Fennix Kernel 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.

	Fennix Kernel 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 Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/

#pragma once

#include <convert.h>
#include <cassert>
#include <new>

namespace std
{
	class __simple_string
	{
	private:
		char *data;
		size_t size;

	public:
		__simple_string()
			: data(nullptr),
			  size(0)
		{
		}

		__simple_string(const char *str)
			: data(nullptr),
			  size(0)
		{
			size = strlen(str);
			data = new char[size + 1];
			assert(data != nullptr);
			memcpy(data, str, size);
			data[size] = '\0';
		}

		__simple_string(const __simple_string &other)
			: data(nullptr),
			  size(0)
		{
			size = other.size;
			data = new char[size + 1];
			assert(data != nullptr);
			memcpy(data, other.data, size);
			data[size] = '\0';
		}

		~__simple_string()
		{
			if (data)
				delete[] data;
		}

		const char *c_str() const { return data; }

		__simple_string &operator=(const __simple_string &other)
		{
			if (data)
				delete[] data;

			size = other.size;
			data = new char[size + 1];
			assert(data != nullptr);
			memcpy(data, other.data, size);
			data[size] = '\0';

			return *this;
		}
	};

	class runtime_error : public exception
	{
	};

	class logic_error : public exception
	{
	private:
		__simple_string msg;

	public:
		logic_error(const char *what_arg) : msg(what_arg) {}
		logic_error(const logic_error &other) : msg(other.msg) {}

		logic_error &operator=(const logic_error &other) noexcept
		{
			msg = other.msg;
			return *this;
		}

		virtual const char *what() const noexcept
		{
			return msg.c_str();
		}
	};

	class out_of_range : public logic_error
	{
	public:
		out_of_range(const char *what_arg) : logic_error(what_arg) {}
		out_of_range(const out_of_range &other) = default;
		out_of_range &operator=(const out_of_range &) = default;
		out_of_range(out_of_range &&) = default;
		out_of_range &operator=(out_of_range &&) = default;
		virtual ~out_of_range() = default;
	};
}
