Qore Programming Language 1.19.1
Loading...
Searching...
No Matches
QC_FilePollOperation.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 QC_FilePollOperation.h
4
5 Qore Programming Language
6
7 Copyright (C) 2003 - 2023 Qore Technologies, s.r.o.
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the "Software"),
11 to deal in the Software without restriction, including without limitation
12 the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26
27 Note that the Qore library is released under a choice of three open-source
28 licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29 information.
30*/
31
32#ifndef _QORE_CLASS_FILEPOLLOPERATION_H
33
34#define _QORE_CLASS_FILEPOLLOPERATION_H
35
36#include "qore/intern/QC_SocketPollOperationBase.h"
37#include "qore/intern/qore_qf_private.h"
38#include "qore/QoreFile.h"
39
40#include <memory>
41
42// states: none -> opened -> reading -> read-done
43constexpr int FPS_NONE = 0;
44constexpr int FPS_READING = 1;
45constexpr int FPS_READ_DONE = 2;
46
47class FileReadPollOperationBase : public SocketPollOperationBase {
48public:
49 DLLLOCAL FileReadPollOperationBase(File* file, bool to_string) : file(file), to_string(to_string) {
50 }
51
52 DLLLOCAL virtual void deref(ExceptionSink* xsink) {
53 if (ROdereference()) {
54 if (set_non_block) {
55 AutoLocker al(file->priv->m);
56 file->priv->clearNonBlock(xsink);
57 }
58 file->deref(xsink);
59 delete this;
60 }
61 }
62
63 DLLLOCAL virtual bool goalReached() const {
64 return state == FPS_READ_DONE;
65 }
66
67 DLLLOCAL virtual const char* getStateImpl() const {
68 switch (state) {
69 case FPS_NONE: return "none";
70 case FPS_READING: return "reading";
71 case FPS_READ_DONE: return "read-done";
72 }
73 assert(false);
74 return "error";
75 }
76
77 DLLLOCAL virtual QoreValue getOutput() const {
78 return data ? data->refSelf() : QoreValue();
79 }
80
87 DLLLOCAL virtual QoreHashNode* continuePoll(ExceptionSink* xsink);
88
89protected:
90 std::unique_ptr<AbstractPollState> poll_state;
92 File* file;
93 bool to_string;
94 bool set_non_block = false;
95 int state = FPS_NONE;
96
97 DLLLOCAL int initIntern(ExceptionSink* xsink);
98};
99
100class FileReadPollOperation : public FileReadPollOperationBase {
101public:
102 DLLLOCAL FileReadPollOperation(ExceptionSink* xsink, File* file, const char* path, ssize_t size,
103 bool to_string);
104
105private:
106 std::string path;
107 ssize_t size;
108};
109
110DLLLOCAL QoreClass* initFilePollOperationClass(QoreNamespace& qorens);
111
112#endif // _QORE_CLASS_FILEPOLLOPERATION_H
virtual DLLLOCAL void deref()
decrements the reference count of the object without the possibility of throwing a Qore-language exce...
Definition: AbstractPrivateData.h:65
provides a safe and exception-safe way to hold locks in Qore, only to be used on the stack,...
Definition: QoreThreadLock.h:136
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
defines a Qore-language class
Definition: QoreClass.h:253
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition: QoreHashNode.h:50
contains constants, classes, and subnamespaces in QoreProgram objects
Definition: QoreNamespace.h:65
DLLEXPORT bool ROdereference() const
atomically decrements the reference count
manages a reference count of a pointer to a class that takes a simple "deref()" call with no argument...
Definition: ReferenceHolder.h:127
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:276
DLLEXPORT QoreValue refSelf() const
references the contained value if type == QV_Node, returns itself