/* -*- mode: c++; indent-tabs-mode: nil -*- */
/** @file QC_NormalMember.cpp NormalMember 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_Class.h>
#include <QC_NormalMember.h>

static int check_member(QoreObject* obj, const char* mname, const QoreClass* cls, ExceptionSink* xsink) {
    if (!obj->validInstanceOf(*cls)) {
        xsink->raiseException("MEMBER-ERROR", "object is of class '%s' which is not compatible with member '%s' of class '%s'", obj->getClassName(), mname, cls->getName());
        return -1;
    }
    return 0;
}

//! This class provides information about %Qore class normal (non-static) members
/** @since %Qore 0.9.0
 */
qclass NormalMember [ns=Qore::Reflection; arg=QoreReflectionClassMember* m; vparent=AbstractClassMember; dom=REFLECTION];

//! Throws an exception; this class cannot be created directly or used in a user class hierarchy
/** @throw NORMALMEMBER-CONSTRUCTOR-ERROR the NormalMember class cannot be constructed directly or inherited directly by a user-defined class
 */
NormalMember::constructor() {
    xsink->raiseException("NORMALMEMBER-CONSTRUCTOR-ERROR", "this class cannot be constructed directly or inherited directly by a user-defined class");
}

//! Returns the value of the member for the given object
/** @par Example:
    @code{.py}
auto val = m.getValue(obj);
    @endcode

    @param obj an object to get the member value from; must be an object of the class implementing the member or a \c MEMBER-ERROR exception is thrown

    @return the value of the member for the given object; if the object is not a member of the class, then a \c MEMBER-ERROR exception is thrown

    @throw PROGRAM-ERROR thrown if the @ref Qore::Program "Program" object holding the class has been destroyed
    @throw MEMBER-ERROR the object was not instantiated from the class implementing this member
*/
auto NormalMember::getValue(object obj) [flags=RET_VALUE_ONLY] {
    // get read access to program object
    QoreExternalProgramContextHelper tch(xsink, m->pgm);
    if (*xsink) {
        return QoreValue();
    }

    // the class ptr must be valid
    if (check_member(obj, m->name.c_str(), m->cls, xsink)) {
        return QoreValue();
    }

    return obj->getReferencedMemberNoMethod(m->name.c_str(), m->cls, xsink);
}

//! Sets the value of the member
/** @par Example:
    @code{.py}
m.setValue(obj, val);
    @endcode

    @param obj an object to set the member value on; must be an object of the class implementing the member or a \c MEMBER-ERROR exception is thrown
    @param val the new value of the member

    @throw PROGRAM-ERROR thrown if the @ref Qore::Program "Program" object holding the class has been destroyed
    @throw MEMBER-ERROR the object was not instantiated from the class implementing this member
    @throw RUNTIME-TYPE-ERROR value type is not compatible with the assigned member's type
*/
nothing NormalMember::setValue(object obj, auto val) {
    // get read access to program object
    QoreExternalProgramContextHelper tch(xsink, m->pgm);
    if (*xsink) {
        return QoreValue();
    }

    if (check_member(obj, m->name.c_str(), m->cls, xsink)) {
        return QoreValue();
    }

    obj->setMemberValue(m->name.c_str(), m->cls, val, xsink);
}
