Qore Programming Language  0.9.4.6
QoreMapOperatorNode.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreMapOperatorNode.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2019 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, ExceptionSink* xs) : map(m), temp(t), h(n_h), xsink(xs) {
141  }
142 
143  DLLLOCAL ~QoreFunctionalMapIteratorOperator() {
144  if (temp)
145  h.obj->deref(xsink);
146  }
147 
148  DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
149 
150  DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
151  return autoTypeInfo;
152  }
153 
154 protected:
155  const QoreMapOperatorNode* map;
156  bool temp;
157  AbstractIteratorHelper h;
158  size_t index = 0;
159  ExceptionSink* xsink;
160 };
161 
162 class QoreFunctionalMapOperator : public FunctionalOperatorInterface {
163 public:
164  DLLLOCAL QoreFunctionalMapOperator(const QoreMapOperatorNode* m, FunctionalOperatorInterface* n_f) : map(m), f(n_f) {
165  }
166 
167  DLLLOCAL ~QoreFunctionalMapOperator() {
168  delete f;
169  }
170 
171  DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
172  return f->getValueType();
173  }
174 
175  DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
176 
177 protected:
178  const QoreMapOperatorNode* map;
179  FunctionalOperatorInterface* f;
180  size_t index = 0;
181 };
182 
183 #endif
allows storing a value and setting a boolean flag that indicates if the value should be dereference i...
Definition: QoreValue.h:485
Qore&#39;s string type supported by the QoreEncoding class.
Definition: QoreString.h:81
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:262
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:46
For use on the stack only: iterates through elements of a const QoreListNode.
Definition: QoreListNode.h:566