Qore Programming Language 1.19.2
Loading...
Searching...
No Matches
QoreMapSelectOperatorNode.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 QoreMapSelectOperatorNode.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_QOREMAPSELECTOPERATORNODE_H
33
34#define _QORE_QOREMAPSELECTOPERATORNODE_H
35
36#include "qore/intern/AbstractIteratorHelper.h"
37#include "qore/intern/FunctionalOperator.h"
38#include "qore/intern/FunctionalOperatorInterface.h"
39
40class QoreMapSelectOperatorNode : public QoreNOperatorNodeBase<3>, public FunctionalOperator {
41 friend class QoreFunctionalMapSelectListOperator;
42 friend class QoreFunctionalMapSelectSingleValueOperator;
43 friend class QoreFunctionalMapSelectIteratorOperator;
44 friend class QoreFunctionalMapSelectOperator;
45
46public:
47 DLLLOCAL QoreMapSelectOperatorNode(const QoreProgramLocation* loc, QoreValue e0, QoreValue e1, QoreValue e2)
48 : QoreNOperatorNodeBase<3>(loc, e0, QoreSimpleValue().assign(e1), QoreSimpleValue().assign(e2)),
49 returnTypeInfo(nullptr), iterator_func(nullptr) {
50 }
51
52 DLLLOCAL virtual QoreString* getAsString(bool& del, int foff, ExceptionSink* xsink) const;
53 DLLLOCAL virtual int getAsString(QoreString& str, int foff, ExceptionSink* xsink) const;
54
55 // returns the type name as a c string
56 DLLLOCAL virtual const char* getTypeName() const {
57 return map_str.getBuffer();
58 }
59
60 DLLLOCAL virtual QoreOperatorNode* copyBackground(ExceptionSink* xsink) const {
61 ValueHolder n_e0(copy_value_and_resolve_lvar_refs(e[0], xsink), xsink);
62 if (*xsink)
63 return nullptr;
64 ValueHolder n_e1(copy_value_and_resolve_lvar_refs(e[1], xsink), xsink);
65 if (*xsink)
66 return nullptr;
67 ValueHolder n_e2(copy_value_and_resolve_lvar_refs(e[2], xsink), xsink);
68 if (*xsink)
69 return nullptr;
70 QoreMapSelectOperatorNode* rv = new QoreMapSelectOperatorNode(loc, n_e0.release(), n_e1.release(),
71 n_e2.release());
72 rv->iterator_func = dynamic_cast<FunctionalOperator*>(rv->e[1].getInternalNode());
73 return rv;
74 }
75
76 DLLLOCAL virtual bool hasEffectAsRoot() const {
77 return true;
78 }
79
80 DLLLOCAL virtual FunctionalOperatorInterface* getFunctionalIteratorImpl(FunctionalValueType& value_type,
81 ExceptionSink* xsink) const;
82
83protected:
84 const QoreTypeInfo* returnTypeInfo;
85 const QoreTypeInfo* expTypeInfo = nullptr;
86 FunctionalOperator* iterator_func;
87
88 DLLLOCAL static QoreString map_str;
89
90 DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
91
92 DLLLOCAL virtual ~QoreMapSelectOperatorNode() {
93 }
94
95 DLLLOCAL virtual int parseInitImpl(QoreValue& val, QoreParseContext& parse_context);
96
97 DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
98 return returnTypeInfo;
99 }
100
101 DLLLOCAL QoreValue mapSelectIterator(AbstractIteratorHelper& h, ExceptionSink* xsink) const;
102};
103
104class QoreFunctionalMapSelectListOperator : public FunctionalOperatorInterface, public ConstListIterator {
105protected:
106 const QoreMapSelectOperatorNode* map;
107 ExceptionSink* xsink;
108
109public:
110 DLLLOCAL QoreFunctionalMapSelectListOperator(const QoreMapSelectOperatorNode* m, QoreListNode* l, ExceptionSink* xs) : ConstListIterator(l), map(m), xsink(xs) {
111 }
112
113 DLLLOCAL virtual ~QoreFunctionalMapSelectListOperator() {
114 const_cast<QoreListNode*>(getList())->deref(xsink);
115 }
116
117 DLLLOCAL virtual const QoreTypeInfo* getValueType() const {
118 return getList()->getValueTypeInfo();
119 }
120
121 DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
122
123 DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
124 return l->getValueTypeInfo();
125 }
126};
127
128class QoreFunctionalMapSelectSingleValueOperator : public FunctionalOperatorInterface {
129protected:
130 const QoreMapSelectOperatorNode* map;
131 QoreValue v;
132 bool done;
133 ExceptionSink* xsink;
134
135public:
136 DLLLOCAL QoreFunctionalMapSelectSingleValueOperator(const QoreMapSelectOperatorNode* m, QoreValue n, ExceptionSink* xs) : map(m), v(n), done(false), xsink(xs) {
137 }
138
139 DLLLOCAL virtual ~QoreFunctionalMapSelectSingleValueOperator() {
140 v.discard(xsink);
141 }
142
143 DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
144
145 DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
146 return v.getTypeInfo();
147 }
148};
149
150class QoreFunctionalMapSelectIteratorOperator : public FunctionalOperatorInterface {
151protected:
152 const QoreMapSelectOperatorNode* map;
153 bool temp;
154 AbstractIteratorHelper h;
155 size_t index = 0;
156 ExceptionSink* xsink;
157
158public:
159 DLLLOCAL QoreFunctionalMapSelectIteratorOperator(const QoreMapSelectOperatorNode* m, bool t, AbstractIteratorHelper n_h, ExceptionSink* xs) : map(m), temp(t), h(n_h), xsink(xs) {
160 }
161
162 DLLLOCAL ~QoreFunctionalMapSelectIteratorOperator() {
163 if (temp)
164 h.obj->deref(xsink);
165 }
166
167 DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
168
169 DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
170 return autoTypeInfo;
171 }
172};
173
174class QoreFunctionalMapSelectOperator : public FunctionalOperatorInterface {
175protected:
176 const QoreMapSelectOperatorNode* map;
177 FunctionalOperatorInterface* f;
178 size_t index;
179
180public:
181 DLLLOCAL QoreFunctionalMapSelectOperator(const QoreMapSelectOperatorNode* m, FunctionalOperatorInterface* n_f) : map(m), f(n_f), index(0) {
182 }
183
184 DLLLOCAL ~QoreFunctionalMapSelectOperator() {
185 delete f;
186 }
187
188 DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
189
190 DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
191 return map->expTypeInfo;
192 }
193};
194
195#endif
For use on the stack only: iterates through elements of a const QoreListNode.
Definition: QoreListNode.h:563
DLLLOCAL const QoreListNode * getList() const
returns the list
Definition: QoreListNode.h:627
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
DLLEXPORT const QoreTypeInfo * getValueTypeInfo() const
returns the value type declaration (if set)
Base value class; parent of QoreValue; designed to be passed by value.
Definition: QoreValue.h:113
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
holds an object and dereferences it in the destructor
Definition: QoreValue.h:487
allows storing a value and setting a boolean flag that indicates if the value should be dereference i...
Definition: QoreValue.h:520
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:276
DLLEXPORT const QoreTypeInfo * getTypeInfo() const
returns the type of the value
DLLEXPORT void discard(ExceptionSink *xsink)
dereferences any contained AbstractQoreNode pointer and sets to 0; does not modify other values