Qore Programming Language  1.12.0
QoreMapOperatorNode.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreMapOperatorNode.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2022 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_QOREMAPOPERATORNODE_H
33 
34 #define _QORE_QOREMAPOPERATORNODE_H
35 
36 #include "qore/intern/AbstractIteratorHelper.h"
37 #include "qore/intern/FunctionalOperator.h"
38 #include "qore/intern/FunctionalOperatorInterface.h"
39 
40 class QoreMapOperatorNode : public QoreBinaryOperatorNode<>, public FunctionalOperator {
41  friend class QoreFunctionalMapListOperator;
42  friend class QoreFunctionalMapSingleValueOperator;
43  friend class QoreFunctionalMapIteratorOperator;
44  friend class QoreFunctionalMapOperator;
45 
46 public:
47  DLLLOCAL QoreMapOperatorNode(const QoreProgramLocation* loc, QoreValue l, QoreValue r)
48  : QoreBinaryOperatorNode<>(loc, l, r), returnTypeInfo(nullptr), iterator_func(nullptr) {
49  }
50 
51  DLLLOCAL virtual QoreString* getAsString(bool& del, int foff, ExceptionSink* xsink) const;
52  DLLLOCAL virtual int getAsString(QoreString& str, int foff, ExceptionSink* xsink) const;
53 
54  // returns the type name as a c string
55  DLLLOCAL virtual const char* getTypeName() const {
56  return map_str.getBuffer();
57  }
58 
59  DLLLOCAL virtual QoreOperatorNode* copyBackground(ExceptionSink *xsink) const {
60  QoreMapOperatorNode* rv = copyBackgroundExplicit<QoreMapOperatorNode>(xsink);
61  rv->iterator_func = dynamic_cast<FunctionalOperator*>(rv->right.getInternalNode());
62  return rv;
63  }
64 
65  DLLLOCAL static const QoreTypeInfo* setReturnTypeInfo(const QoreTypeInfo*& returnTypeInfo,
66  const QoreTypeInfo* expTypeInfo, const QoreTypeInfo* iteratorTypeInfo);
67 
68  DLLLOCAL virtual bool hasEffectAsRoot() const {
69  return true;
70  }
71 
72 protected:
73  const QoreTypeInfo* returnTypeInfo;
74  const QoreTypeInfo* expTypeInfo = nullptr;
75  FunctionalOperator* iterator_func;
76 
77  DLLLOCAL static QoreString map_str;
78 
79  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
80 
81  DLLLOCAL QoreValue evalFunc(bool& needs_deref, ExceptionSink* xsink) const;
82 
83  DLLLOCAL virtual ~QoreMapOperatorNode() {
84  }
85 
86  DLLLOCAL virtual int parseInitImpl(QoreValue& val, QoreParseContext& parse_context);
87 
88  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
89  return returnTypeInfo;
90  }
91 
92  DLLLOCAL QoreValue mapIterator(AbstractIteratorHelper& h, ExceptionSink* xsink) const;
93 
94  DLLLOCAL virtual FunctionalOperatorInterface* getFunctionalIteratorImpl(FunctionalValueType& value_type,
95  ExceptionSink* xsink) const;
96 };
97 
98 class QoreFunctionalMapListOperator : public FunctionalOperatorInterface, public ConstListIterator {
99 public:
100  DLLLOCAL QoreFunctionalMapListOperator(const QoreMapOperatorNode* m, QoreListNode* l, ExceptionSink* xs)
101  : ConstListIterator(l), map(m), xsink(xs) {
102  }
103 
104  DLLLOCAL virtual ~QoreFunctionalMapListOperator() {
105  const_cast<QoreListNode*>(getList())->deref(xsink);
106  }
107 
108  DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
109  // issue #2651: return the iterator expression type as the functional value type
110  return map->expTypeInfo;
111  }
112 
113  DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
114 
115 protected:
116  const QoreMapOperatorNode* map;
117  ExceptionSink* xsink;
118 };
119 
120 class QoreFunctionalMapSingleValueOperator : public FunctionalOperatorInterface {
121 public:
122  DLLLOCAL QoreFunctionalMapSingleValueOperator(const QoreMapOperatorNode* m, QoreValue n, ExceptionSink* xs)
123  : map(m), v(n), xsink(xs) {
124  }
125 
126  DLLLOCAL virtual ~QoreFunctionalMapSingleValueOperator() {
127  v.discard(xsink);
128  }
129 
130  DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
131 
132  DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
133  return v.getTypeInfo();
134  }
135 
136 protected:
137  const QoreMapOperatorNode* map;
138  QoreValue v;
139  bool done = false;
140  ExceptionSink* xsink;
141 };
142 
143 class QoreFunctionalMapIteratorOperator : public FunctionalOperatorInterface {
144 public:
145  DLLLOCAL QoreFunctionalMapIteratorOperator(const QoreMapOperatorNode* m, bool t, AbstractIteratorHelper n_h,
146  ExceptionSink* xs) : map(m), temp(t), h(n_h), xsink(xs) {
147  }
148 
149  DLLLOCAL ~QoreFunctionalMapIteratorOperator() {
150  if (temp)
151  h.obj->deref(xsink);
152  }
153 
154  DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
155 
156  DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
157  return autoTypeInfo;
158  }
159 
160 protected:
161  const QoreMapOperatorNode* map;
162  bool temp;
163  AbstractIteratorHelper h;
164  size_t index = 0;
165  ExceptionSink* xsink;
166 };
167 
168 class QoreFunctionalMapOperator : public FunctionalOperatorInterface {
169 public:
170  DLLLOCAL QoreFunctionalMapOperator(const QoreMapOperatorNode* m, FunctionalOperatorInterface* n_f) : map(m), f(n_f) {
171  }
172 
173  DLLLOCAL ~QoreFunctionalMapOperator() {
174  delete f;
175  }
176 
177  DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
178  return map->expTypeInfo;
179  }
180 
181  DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
182 
183 protected:
184  const QoreMapOperatorNode* map;
185  FunctionalOperatorInterface* f;
186  size_t index = 0;
187 };
188 
189 #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:48
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
allows storing a value and setting a boolean flag that indicates if the value should be dereference i...
Definition: QoreValue.h:509
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:275