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