/* -*- mode: c++; indent-tabs-mode: nil -*- */
/** @file QC_DestructorMethodVariant.cpp DestructorMethodVariant 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_DestructorMethodVariant.h"
#include "QC_AbstractMethod.h"
#include "QC_NormalMethodVariant.h"

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

    @see @ref Qore::Reflection::DestructorMethod "DestructorMethod"

    @since %Qore 0.9.0
 */
qclass DestructorMethodVariant [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 DESTRUCTORMETHODVARIANT-CONSTRUCTOR-ERROR this exception is thrown if this class is constructed directly (also if directly inherited by user classes)
 */
DestructorMethodVariant::constructor() {
    xsink->raiseException("DESTRUCTORMETHODVARIANT-CONSTRUCTOR-ERROR", "this class is an abstract class and cannot be constructed directly or inherited directly by a user-defined class");
}

//! calls the destructor method variant with the given object; the object must be a member of a compatible class; the object is destroyed when this call is made successfully
/** @par Example:
    @code{.py}
auto v = m.call(obj, arg1, arg2);
    @endcode

    @param obj the object to make the call on; must be an object of the class implementing the method or a \c METHOD-ERROR exception is thrown

    @throw PROGRAM-ERROR thrown if the @ref Qore::Program "Program" object holding the class and method has been destroyed
    @throw METHOD-ERROR the object was not instantiated from the class implementing this method
*/
nothing DestructorMethodVariant::call(object obj) {
    // get read access to program object
    QoreExternalProgramContextHelper tch(xsink, v->pgm);
    if (*xsink) {
        return QoreValue();
    }

    // we have to explictly test object compatibility here
    const QoreExternalMethodVariant* mv = static_cast<const QoreExternalMethodVariant*>(v->variant);
    const QoreMethod* m = mv->getMethod();
    if (check_variant_call(mv, obj, *m, xsink)) {
        return QoreValue();
    }

    obj->doDelete(xsink);
}
