/*
 * Copyright (c) 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 
#include 
namespace ams::tipc {
    namespace impl {
        struct OutBaseTag{};
    }
    template
    struct IsOutForceEnabled : public std::false_type{};
    template<>
    struct IsOutForceEnabled<::ams::Result> : public std::true_type{};
    template
    concept OutEnabled = (std::is_trivial::value || IsOutForceEnabled::value) && !std::is_pointer::value;
    template
    class Out : public impl::OutBaseTag {
        static_assert(OutEnabled);
        public:
            static constexpr size_t TypeSize = sizeof(T);
        private:
            T *m_ptr;
        public:
            constexpr Out(uintptr_t p) : m_ptr(reinterpret_cast(p)) { /* ... */ }
            constexpr Out(T *p) : m_ptr(p) { /* ... */ }
            constexpr Out(const tipc::PointerAndSize &pas) : m_ptr(reinterpret_cast(pas.GetAddress())) { /* TODO: Is AMS_ABORT_UNLESS(pas.GetSize() >= sizeof(T)); necessary? */ }
            ALWAYS_INLINE void SetValue(const T& value) const {
                *m_ptr = value;
            }
            ALWAYS_INLINE const T &GetValue() const {
                return *m_ptr;
            }
            ALWAYS_INLINE T *GetPointer() const {
                return m_ptr;
            }
            /* Convenience operators. */
            ALWAYS_INLINE T &operator*() const {
                return *m_ptr;
            }
            ALWAYS_INLINE T *operator->() const {
                return m_ptr;
            }
    };
    template
    class Out {
        static_assert(!std::is_same::value, "Invalid tipc::Out (Raw Pointer)");
    };
}