/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
    QC_FilePollOperation.qpp

    FilePollOperation 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/Qore.h"
#include "qore/intern/QC_FilePollOperation.h"

//! File poll operation object returned by File polling methods
/** This class implements the polling logic for Files and the state machine behind it

    private members:
    - @ref Qore::File "File" sock: the File being polled
    - @ref string_type "string" goal: the goal of the poll operation

    @since %Qore 1.19
*/
qclass FilePollOperation [arg=SocketPollOperationBase* op; vparent=SocketPollOperationBase];

//! Creates the object with the given goal
/** @param file the File object to poll
    @param path the path to read
    @param to_string if the output should be a string or not; if so, the string's encoding will be the same as the
    %File's encoding
    @param num_bytes the number of bytes to read; < 0 means read the entire file
*/
FilePollOperation::constructor(ReadOnlyFile[File] file, string path, bool to_string, int num_bytes = -1) {
    ReferenceHolder<File> holder(file, xsink);
    TempEncodingHelper npath(path, QCS_DEFAULT, xsink);
    if (*xsink) {
        return;
    }
    ReferenceHolder<SocketPollOperationBase> poller(new FileReadPollOperation(xsink, holder.release(), npath->c_str(),
        num_bytes, to_string), xsink);
    if (*xsink) {
        return;
    }

    poller->setSelf(self);
    self->setPrivate(CID_FILEPOLLOPERATION, poller.release());
    if (!*xsink) {
        self->setValue("sock", obj_file->objectRefSelf(), xsink);
        self->setValue("goal", new QoreStringNode("read-done"), xsink);
    }
}

//! Destroys the object
/** Destroys the object
 */
FilePollOperation::destructor() {
    op->deref(xsink);
}

//! Throws an exception; objects of this class cannot be copied
/**
    @throw FILEPOLLOPERATION-COPY-ERROR objects of this class cannot be copied
 */
FilePollOperation::copy() {
    xsink->raiseException("FILEPOLLOPERATION-COPY-ERROR", "objects of this class cannot be copied");
}

//! Returns a socket poll hash if polling should continue or @ref nothing if complete
/** @return @ref NOTHING if there is no more to poll and the poll operation was successful (i.e. the goal was
    reached), otherwise this method must return a new socket poll hash to continue the polling operation with another
    operation, after which this method will be called again if a matching event is received for the socket.  If a
    socket poll hash is returned, then the internal state is also updated.
*/
*hash<SocketPollInfo> FilePollOperation::continuePoll() {
    return op->continuePoll(xsink);
}
