/* -*- mode: c++; indent-tabs-mode: nil -*- */
/** @file QC_TypedHashMember.cpp TypedHashMember 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_TypedHashMember.h>

static int check_member(const QoreHashNode* h, const char* mname, const TypedHashDecl* th, ExceptionSink* xsink) {
    if (h->getHashDecl() != th) {
        xsink->raiseException("MEMBER-ERROR", "hash was not declared as hashdecl '%s' (referencing member '%s')", th->getName(), mname);
        return -1;
    }
    return 0;
}

//! This class provides information about %Qore @ref hashdecl "typed hash (hashdecl)" members
/** There is no setter method in this class due to design issues related to lvalue restrictions
    with non-object data.

    @since %Qore 0.9.0
 */
qclass TypedHashMember [ns=Qore::Reflection; arg=QoreReflectionHashDeclMember* m; vparent=AbstractMember; dom=REFLECTION];

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

//! returns @ref True if the @ref hashdecl "typed hash (hashdecl)" defining the member is a builtin @ref hashdecl "typed hash (hashdecl)"
/** @par Example:
    @code{.py}
bool b = m.isBuiltin();
    @endcode

    @throw PROGRAM-ERROR thrown if the @ref Qore::Program "Program" object holding the @ref hashdecl "typed hash (hashdecl)" has been destroyed
*/
bool TypedHashMember::isBuiltin() [flags=RET_VALUE_ONLY] {
    // get read access to program object
    QoreExternalProgramContextHelper tch(xsink, m->pgm);
    if (*xsink) {
        return QoreValue();
    }

    // the typed hash ptr must be valid
    return m->th->isSystem();
}

//! returns @ref True if the @ref hashdecl "typed hash (hashdecl)" defining the member is a user @ref hashdecl "typed hash (hashdecl)"
/** @par Example:
    @code{.py}
bool b = m.isUser();
    @endcode

    @throw PROGRAM-ERROR thrown if the @ref Qore::Program "Program" object holding the cl@ref hashdecl "typed hash (hashdecl)"ass has been destroyed
*/
bool TypedHashMember::isUser() [flags=RET_VALUE_ONLY] {
    // get read access to program object
    QoreExternalProgramContextHelper tch(xsink, m->pgm);
    if (*xsink) {
        return QoreValue();
    }

    // the typed hash ptr must be valid
    return !m->th->isSystem();
}

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

    @param h a hash to get the member value from; must have been declared with the @ref hashdecl "typed hash (hashdecl)"'s type declaring the member, or a \c MEMBER-ERROR exception is thrown

    @return the value of the member for the given object; must have been declared with the @ref hashdecl "typed hash (hashdecl)"'s type declaring the member, or 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 hash was not declared with the @ref hashdecl "typed hash (hashdecl)"'s type implementing this member
*/
auto TypedHashMember::getValue(hash<auto> h) [flags=RET_VALUE_ONLY] {
    // get read access to program object
    QoreExternalProgramContextHelper tch(xsink, m->pgm);
    if (*xsink) {
        return QoreValue();
    }

    // the typed hash ptr must be valid
    if (check_member(h, m->name.c_str(), m->th, xsink)) {
        return QoreValue();
    }

    return h->getKeyValue(m->name.c_str());
}
