/*
 * Copyright 2015 - 2022 gary@drinkingtea.net
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#pragma once

#include "defines.hpp"
#include "types.hpp"

#if defined(_MSC_VER)
#include <malloc.h>
#elif OX_USE_STDLIB
#include <stdlib.h>
#endif


#if __has_include(<new>)
#include <new>
#else
constexpr void *operator new(std::size_t, void *addr) noexcept {
	return addr;
}

constexpr void *operator new[](std::size_t, void *addr) noexcept {
	return addr;
}
#endif


namespace ox {

constexpr auto MallocaStackLimit = defines::UseStdLib ? 1024 : 0;

/**
 * MallocaPtr will automatically cleanup the pointed to address upon
 * destruction if the size of the allocation is greater than MallocaStackLimit.
 */
template<typename T>
class MallocaPtr {

	private:
		bool m_onHeap = false;
		T *m_val = nullptr;

	public:
		constexpr MallocaPtr() noexcept = default;

		constexpr MallocaPtr(MallocaPtr &other) = delete;

		constexpr MallocaPtr(const MallocaPtr &other) = delete;

		constexpr MallocaPtr(MallocaPtr &&other) noexcept {
			m_onHeap = other.m_onHeap;
			m_val = other.m_val;
			other.m_onHeap = false;
			other.m_val = nullptr;
		}

		constexpr MallocaPtr(bool onHeap, T *val) noexcept {
			m_onHeap = onHeap;
			m_val = val;
		}

		constexpr ~MallocaPtr() noexcept {
			if (m_onHeap && m_val) {
				delete[] reinterpret_cast<uint8_t*>(m_val);
			}
		}

		constexpr const T *get() const noexcept {
			return reinterpret_cast<T*>(m_val);
		}

		constexpr T *get() noexcept {
			return reinterpret_cast<T*>(m_val);
		}

		constexpr const T &operator=(MallocaPtr &other) = delete;

		constexpr const T &operator=(const MallocaPtr &other) = delete;

		constexpr const T &operator=(MallocaPtr &&other) noexcept {
			if (m_onHeap && m_val) {
				delete[] reinterpret_cast<uint8_t*>(m_val);
			}
			m_onHeap = other.m_onHeap;
			m_val = other.m_val;
			other.m_onHeap = false;
			other.m_val = nullptr;
		}

		constexpr const T *operator->() const noexcept {
			return reinterpret_cast<T*>(m_val);
		}

		constexpr T *operator->() noexcept {
			return reinterpret_cast<T*>(m_val);
		}

		constexpr operator const T*() const noexcept {
			return reinterpret_cast<T*>(m_val);
		}

		constexpr operator T*() noexcept {
			return reinterpret_cast<T*>(m_val);
		}

		constexpr const T &operator*() const noexcept {
			return *reinterpret_cast<T*>(m_val);
		}

		constexpr T &operator*() noexcept {
			return *reinterpret_cast<T*>(m_val);
		}

		constexpr bool operator==(const MallocaPtr<T> &other) const noexcept {
			return m_val == other.m_val && m_onHeap == other.m_onHeap;
		}

		constexpr bool operator!=(const MallocaPtr<T> &other) const noexcept {
			return m_val != other.m_val || m_onHeap != other.m_onHeap;
		}

};

}
