/* -*- mode: c++; indent-tabs-mode: nil -*- */
/** @file QC_DestructorMethod.cpp DestructorMethod 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_DestructorMethod.h"
#include "QC_Class.h"

//! This class provides information about %Qore destructor methods
/**
    @see @ref Qore::Reflection::DestructorMethodVariant "DestructorMethodVariant"

    @since %Qore 0.9.0
 */
qclass DestructorMethod [ns=Qore::Reflection; arg=QoreReflectionMethod* m; vparent=AbstractMethod; dom=REFLECTION];

//! Creates the object from the class if the class implements a destructor method
/** @par Example:
    @code{.py}
DestructorMethod m(cls);
    @endcode

    @param cls the class for the method

    @throw UNKOWN-METHOD cannot find a destructor method for the class
 */
DestructorMethod::constructor(Class[QoreReflectionClass] cls) {
    ReferenceHolder<QoreReflectionClass> holder(cls, xsink);

    // get exclusive access to program object
    QoreExternalProgramContextHelper pch(xsink, cls->pgm);
    if (*xsink) {
        return;
    }

    // the class pointer must be valid here
    ReferenceHolder<QoreReflectionMethod> m(new QoreReflectionMethod(cls->cls, "destructor", xsink), xsink);
    if (*xsink) {
        return;
    }

    assert(m->mtype == MT_Destructor);

    self->setPrivate(CID_DESTRUCTORMETHOD, m.release());
}

//! Creates the object from the class name or namespace path if the class implements a destructor method
/** @par Example:
    @code{.py}
DestructorMethod m("Thread::Mutex");
    @endcode

    @param class_name the class name or namespace path for the class

    @throw UNKNOWN-CLASS cannot find a class with the given name or path
    @throw UNKOWN-METHOD cannot find a destructor method for the class
 */
DestructorMethod::constructor(string class_name) {
    TempEncodingHelper npath(class_name, QCS_DEFAULT, xsink);
    if (*xsink) {
        return;
    }

    ReferenceHolder<QoreReflectionMethod> m(new QoreReflectionMethod(npath->c_str(), "destructor", xsink), xsink);
    if (*xsink) {
        return;
    }

    assert(m->mtype == MT_Destructor);

    self->setPrivate(CID_DESTRUCTORMETHOD, m.release());
}

//! calls the destructor on given object; destroys the object
/** @par Example:
    @code{.py}
m.call(obj);
    @endcode

    @param obj the object to destroy

    @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
*/
auto DestructorMethod::call(object obj) {
    // get read access to program object
    QoreExternalProgramContextHelper tch(xsink, m->pgm);
    if (*xsink) {
        return QoreValue();
    }

    // we have to explictly test object compatibility here
    if (check_call(obj, *m->m, xsink)) {
        return QoreValue();
    }

    obj->doDelete(xsink);
}
