/* -*- mode: c++; indent-tabs-mode: nil -*- */
/** @file QC_AbstractMethodVariant.cpp AbstractMethodVariant 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_AbstractMethodVariant.h"
#include "QC_AbstractClass.h"
#include "QC_AbstractClass.h"
#include "QC_AbstractMethod.h"

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

    @see @ref Qore::Reflection::AbstractMethod "AbstractMethod"

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

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

//! returns @ref True if the variant is abstract
/** @par Example:
    @code{.py}
bool b = v.isAbstract();
    @endcode

    @return @ref True if the variant is abstract

    @throw PROGRAM-ERROR thrown if the @ref Qore::Program "Program" object holding the class and method has been destroyed
*/
bool AbstractMethodVariant::isAbstract() [flags=RET_VALUE_ONLY] {
    // get read access to program object
    QoreExternalProgramContextHelper tch(xsink, v->pgm);
    if (*xsink) {
        return QoreValue();
    }

    // the variant ptr must be valid
    const QoreExternalMethodVariant* mv = reinterpret_cast<const QoreExternalMethodVariant*>(v->variant);
    return mv->isAbstract();
}

//! returns @ref True if the variant is final
/** @par Example:
    @code{.py}
bool b = v.isFinal();
    @endcode

    @return @ref True if the variant is final

    @throw PROGRAM-ERROR thrown if the @ref Qore::Program "Program" object holding the class and method has been destroyed
*/
bool AbstractMethodVariant::isFinal() [flags=RET_VALUE_ONLY] {
    // get read access to program object
    QoreExternalProgramContextHelper tch(xsink, v->pgm);
    if (*xsink) {
        return QoreValue();
    }

    // the variant ptr must be valid
    const QoreExternalMethodVariant* mv = reinterpret_cast<const QoreExternalMethodVariant*>(v->variant);
    return mv->isFinal();
}

//! Returns the method for the method variant
/**
    @return the method for the method variant

    @par Example:
    @code{.py}
AbbstractMethod m = v.getMethod();
    @endcode
 */
AbstractMethod AbstractMethodVariant::getMethod() [flags=CONSTANT] {
    const QoreExternalMethodVariant* mv = reinterpret_cast<const QoreExternalMethodVariant*>(v->variant);
    ReferenceHolder<QoreReflectionMethod> m(new QoreReflectionMethod(v->pgm,
        mv->getMethod()), xsink);
    return get_method_object(m, xsink);
}

//! Returns the class for the variant
/** @par Example:
    @code{.py}
AbstractClass cls = v.getClass();
    @endcode

    @return the class for the variant
 */
AbstractClass AbstractMethodVariant::getClass() [flags=CONSTANT] {
    const QoreExternalMethodVariant* mv = reinterpret_cast<const QoreExternalMethodVariant*>(v->variant);
    return get_class_object(v->pgm, mv->getClass());
}

//! returns a string for the access modifier for the method variant
/** @par Example:
    @code{.py}
string str = v.getAccessModifierString();
    @endcode

    @return a string for the access modifier for the method variant; possible values are:
    - \c "private": gives the accessibility of the variant
    - \c "private:internal": gives the accessibility of the variant
    - \c "public": gives the accessibility of the variant

    @throw PROGRAM-ERROR thrown if the @ref Qore::Program "Program" object holding the class has been destroyed
*/
string AbstractMethodVariant::getAccessModifierString() [flags=RET_VALUE_ONLY] {
    // get read access to program object
    QoreExternalProgramContextHelper tch(xsink, v->pgm);
    if (*xsink) {
        return QoreValue();
    }

    // the variant ptr must be valid
    const QoreExternalMethodVariant* mv = reinterpret_cast<const QoreExternalMethodVariant*>(v->variant);
    return new QoreStringNode(mv->getAccessString());
}

