/* -*- mode: c++; indent-tabs-mode: nil -*- */
/** @file QC_PseudoMethodVariant.cpp PseudoMethodVariant class definition */
/*
    Qore Programming Language

    Copyright (C) 2003 - 2023 Qore Technologies, s.r.o.

    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.

    Note that the Qore library is released under a choice of three open-source
    licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
    information.
*/

#include "qore_reflection.h"
#include "QC_PseudoMethodVariant.h"
#include "QC_PseudoMethod.h"

//! The class for %Qore class normal method variants
/**
    This class cannot be instantiated directly and also cannot be directly inherited by user-defined classes.

    @see @ref Qore::Reflection::PseudoMethod "PseudoMethod"

    @since %Qore 0.9.0
 */
qclass PseudoMethodVariant [ns=Qore::Reflection; arg=QoreReflectionVariant* v; vparent=AbstractMethodVariant; dom=REFLECTION];

//! Throws an exception if called directly; this class can only be instantiated by builtin subclasses
/** @throw PSEUDOMETHODVARIANT-CONSTRUCTOR-ERROR this exception is thrown if this class is constructed directly (also if directly inherited by user classes)
 */
PseudoMethodVariant::constructor() {
    xsink->raiseException("PSEUDOMETHODVARIANT-CONSTRUCTOR-ERROR", "this class is an abstract class and cannot be constructed directly or inherited directly by a user-defined class");
}

//! calls the given pseudo-method variant with the given value; the value must be compatible with the pseudo-class's type
/** @par Example:
    @code{.py}
auto v = m.call(val, arg1, arg2);
    @endcode

    @param val the value to make the call on; must be compatible with the pseudo-class's type or a \c METHOD-ERROR exception is thrown
    @param ... optional arguments to the method

    @return the return value of the method call

    @throw METHOD-ERROR the value is not compatible with the class implementing this method
    @throw RUNTIME-TYPE-ERROR invalid argument types passed to the variant

    @see callArgs()
*/
auto PseudoMethodVariant::call(auto val, ...) {
    // we have to explictly test object compatibility here, otherwise QoreObject::evalMethod() will throw
    // a confusing exception
    const QoreExternalMethodVariant* mv = reinterpret_cast<const QoreExternalMethodVariant*>(v->variant);
    const QoreMethod* m = mv->getMethod();
    if (check_pseudo_call(val, *m, xsink)) {
        return QoreValue();
    }

    ReferenceHolder<QoreListNode> call_args(xsink);

    // if there are arguments to pass
    if (num_args(args) > 1) {
        // create argument list by copying current list
        call_args = args->copyListFrom(1);
    }

    // pseudo-classes and pseudo-methods are always valid
    return m->getClass()->evalPseudoMethod(m, mv, val, *call_args, xsink);
}

//! calls the given pseudo-method variant with the given value; the value must be compatible with the pseudo-class's type
/** @par Example:
    @code{.py}
auto v = m.callArgs(val, (arg1, arg2));
    @endcode

    @param val the value to make the call on; must be compatible with the pseudo-class's type or a \c METHOD-ERROR exception is thrown
    @param argv optional arguments to the method

    @return the return value of the method call

    @throw METHOD-ERROR the value is not compatible with the class implementing this method
    @throw RUNTIME-TYPE-ERROR invalid argument types passed to the variant

    @see call()
*/
auto PseudoMethodVariant::callArgs(auto val, *softlist<auto> argv) {
    // we have to explictly test object compatibility here, otherwise QoreObject::evalMethod() will throw
    // a confusing exception
    const QoreExternalMethodVariant* mv = reinterpret_cast<const QoreExternalMethodVariant*>(v->variant);
    const QoreMethod* m = mv->getMethod();
    if (check_pseudo_call(val, *m, xsink)) {
        return QoreValue();
    }

    // pseudo-classes and pseudo-methods are always valid
    return m->getClass()->evalPseudoMethod(m, mv, val, argv, xsink);
}
