/*
 * Copyright (c) 2018-2020 Atmosphère-NX
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope 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 this program.  If not, see .
 */
#pragma once
#include "sf_common.hpp"
#include "sf_out.hpp"
namespace ams::sf {
    class IServiceObject {
        public:
            virtual ~IServiceObject() { /* ... */ }
    };
    template
    concept IsServiceObject = std::derived_from;
    class IMitmServiceObject : public IServiceObject {
        public:
            virtual ~IMitmServiceObject() { /* ... */ }
    };
    class MitmServiceImplBase {
        protected:
            std::shared_ptr<::Service> forward_service;
            sm::MitmProcessInfo client_info;
        public:
            MitmServiceImplBase(std::shared_ptr<::Service> &&s, const sm::MitmProcessInfo &c) : forward_service(std::move(s)), client_info(c) { /* ... */ }
    };
    template
    concept IsMitmServiceObject = IsServiceObject && std::derived_from;
    template
    concept IsMitmServiceImpl = requires (std::shared_ptr<::Service> &&s, const sm::MitmProcessInfo &c) {
        { T(std::forward>(s), c) };
        { T::ShouldMitm(c) } -> std::same_as;
    };
    template
        requires std::constructible_from
    constexpr ALWAYS_INLINE std::shared_ptr> MakeShared(Arguments &&... args) {
        return std::make_shared>(std::forward(args)...);
    }
    template
        requires (std::constructible_from && std::derived_from>)
    constexpr ALWAYS_INLINE std::shared_ptr> MakeShared(Arguments &&... args) {
        return std::make_shared>(std::make_shared(std::forward(args)...));
    }
    template
    class ServiceObjectAllocatorImpl {
        private:
            template
            friend class ServiceObjectAllocatorImpl;
        public:
            using value_type = T;
        private:
            MemoryResource * const memory_resource;
        public:
            constexpr ServiceObjectAllocatorImpl(MemoryResource *mr) : memory_resource(mr) { /* ... */ }
            template
            constexpr ServiceObjectAllocatorImpl(const ServiceObjectAllocatorImpl &rhs) : memory_resource(rhs.memory_resource) { /* ... */ }
            value_type *allocate(size_t n) const {
                void *mem = this->memory_resource->Allocate(n * sizeof(value_type), alignof(value_type));
                AMS_ABORT_UNLESS(mem != nullptr);
                return static_cast(mem);
            }
            void deallocate(void *p, size_t n) const {
                this->memory_resource->Deallocate(p, n * sizeof(value_type), alignof(value_type));
            }
            template
            inline bool operator==(const ServiceObjectAllocatorImpl &rhs) const {
                return this->memory_resource->is_equal(*rhs->memory_resource);
            }
            template
            inline bool operator!=(const ServiceObjectAllocatorImpl &rhs) const {
                return !(*this == rhs);
            }
    };
    template 
    using ServiceObjectAllocator = ServiceObjectAllocatorImpl>;
    template
        requires std::constructible_from
    constexpr ALWAYS_INLINE std::shared_ptr> AllocateShared(const Allocator &allocator, Arguments &&... args) {
        return std::allocate_shared>(allocator, std::forward(args)...);
    }
    template
    constexpr ALWAYS_INLINE std::shared_ptr> GetSharedPointerTo(Impl *impl) {
        return std::make_shared>(impl);
    }
    template
    constexpr ALWAYS_INLINE std::shared_ptr> GetSharedPointerTo(Impl &impl) {
        return GetSharedPointerTo(std::addressof(impl));
    }
}