Qore Programming Language  0.9.16
QoreMapSelectOperatorNode.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreMapSelectOperatorNode.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_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 
40 class QoreMapSelectOperatorNode : public QoreNOperatorNodeBase<3>, public FunctionalOperator {
41  friend class QoreFunctionalMapSelectListOperator;
42  friend class QoreFunctionalMapSelectSingleValueOperator;
43  friend class QoreFunctionalMapSelectIteratorOperator;
44  friend class QoreFunctionalMapSelectOperator;
45 
46 public:
47  DLLLOCAL QoreMapSelectOperatorNode(const QoreProgramLocation* loc, QoreValue e0, QoreValue e1, QoreValue e2) : QoreNOperatorNodeBase<3>(loc, e0, QoreSimpleValue().assign(e1), QoreSimpleValue().assign(e2)), 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  ValueHolder n_e0(copy_value_and_resolve_lvar_refs(e[0], xsink), xsink);
60  if (*xsink)
61  return nullptr;
62  ValueHolder n_e1(copy_value_and_resolve_lvar_refs(e[1], xsink), xsink);
63  if (*xsink)
64  return nullptr;
65  ValueHolder n_e2(copy_value_and_resolve_lvar_refs(e[2], xsink), xsink);
66  if (*xsink)
67  return nullptr;
68  QoreMapSelectOperatorNode* rv = new QoreMapSelectOperatorNode(loc, n_e0.release(), n_e1.release(), n_e2.release());
69  rv->iterator_func = dynamic_cast<FunctionalOperator*>(rv->e[1].getInternalNode());
70  return rv;
71  }
72 
73  DLLLOCAL virtual bool hasEffectAsRoot() const {
74  return true;
75  }
76 
77  DLLLOCAL virtual FunctionalOperatorInterface* getFunctionalIteratorImpl(FunctionalValueType& value_type, ExceptionSink* xsink) const;
78 
79 protected:
80  const QoreTypeInfo* returnTypeInfo;
81  const QoreTypeInfo* expTypeInfo = nullptr;
82  FunctionalOperator* iterator_func;
83 
84  DLLLOCAL static QoreString map_str;
85 
86  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
87 
88  DLLLOCAL virtual ~QoreMapSelectOperatorNode() {
89  }
90 
91  DLLLOCAL virtual void parseInitImpl(QoreValue& val, LocalVar* oflag, int pflag, int& lvids, const QoreTypeInfo*& typeInfo);
92 
93  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
94  return returnTypeInfo;
95  }
96 
97  DLLLOCAL QoreValue mapSelectIterator(AbstractIteratorHelper& h, ExceptionSink* xsink) const;
98 };
99 
100 class QoreFunctionalMapSelectListOperator : public FunctionalOperatorInterface, public ConstListIterator {
101 protected:
102  const QoreMapSelectOperatorNode* map;
103  ExceptionSink* xsink;
104 
105 public:
106  DLLLOCAL QoreFunctionalMapSelectListOperator(const QoreMapSelectOperatorNode* m, QoreListNode* l, ExceptionSink* xs) : ConstListIterator(l), map(m), xsink(xs) {
107  }
108 
109  DLLLOCAL virtual ~QoreFunctionalMapSelectListOperator() {
110  const_cast<QoreListNode*>(getList())->deref(xsink);
111  }
112 
113  DLLLOCAL virtual const QoreTypeInfo* getValueType() const {
114  return getList()->getValueTypeInfo();
115  }
116 
117  DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
118 
119  DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
120  return l->getValueTypeInfo();
121  }
122 };
123 
124 class QoreFunctionalMapSelectSingleValueOperator : public FunctionalOperatorInterface {
125 protected:
126  const QoreMapSelectOperatorNode* map;
127  QoreValue v;
128  bool done;
129  ExceptionSink* xsink;
130 
131 public:
132  DLLLOCAL QoreFunctionalMapSelectSingleValueOperator(const QoreMapSelectOperatorNode* m, QoreValue n, ExceptionSink* xs) : map(m), v(n), done(false), xsink(xs) {
133  }
134 
135  DLLLOCAL virtual ~QoreFunctionalMapSelectSingleValueOperator() {
136  v.discard(xsink);
137  }
138 
139  DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
140 
141  DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
142  return v.getTypeInfo();
143  }
144 };
145 
146 class QoreFunctionalMapSelectIteratorOperator : public FunctionalOperatorInterface {
147 protected:
148  const QoreMapSelectOperatorNode* map;
149  bool temp;
150  AbstractIteratorHelper h;
151  size_t index = 0;
152  ExceptionSink* xsink;
153 
154 public:
155  DLLLOCAL QoreFunctionalMapSelectIteratorOperator(const QoreMapSelectOperatorNode* m, bool t, AbstractIteratorHelper n_h, ExceptionSink* xs) : map(m), temp(t), h(n_h), xsink(xs) {
156  }
157 
158  DLLLOCAL ~QoreFunctionalMapSelectIteratorOperator() {
159  if (temp)
160  h.obj->deref(xsink);
161  }
162 
163  DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
164 
165  DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
166  return autoTypeInfo;
167  }
168 };
169 
170 class QoreFunctionalMapSelectOperator : public FunctionalOperatorInterface {
171 protected:
172  const QoreMapSelectOperatorNode* map;
173  FunctionalOperatorInterface* f;
174  size_t index;
175 
176 public:
177  DLLLOCAL QoreFunctionalMapSelectOperator(const QoreMapSelectOperatorNode* m, FunctionalOperatorInterface* n_f) : map(m), f(n_f), index(0) {
178  }
179 
180  DLLLOCAL ~QoreFunctionalMapSelectOperator() {
181  delete f;
182  }
183 
184  DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
185 
186  DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
187  return map->expTypeInfo;
188  }
189 };
190 
191 #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
QoreValue::discard
DLLEXPORT void discard(ExceptionSink *xsink)
dereferences any contained AbstractQoreNode pointer and sets to 0; does not modify other values
QoreListNode::getValueTypeInfo
const DLLEXPORT QoreTypeInfo * getValueTypeInfo() const
returns the value type declaration (if set)
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
QoreValue::getTypeInfo
const DLLEXPORT QoreTypeInfo * getTypeInfo() const
returns the type of the value
ValueOptionalRefHolder
allows storing a value and setting a boolean flag that indicates if the value should be dereference i...
Definition: QoreValue.h:485
ValueHolder
holds an object and dereferences it in the destructor
Definition: QoreValue.h:452