Qore Programming Language  0.9.16
QoreSelectOperatorNode.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreSelectOperatorNode.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_QORESELECTOPERATORNODE_H
33 
34 #define _QORE_QORESELECTOPERATORNODE_H
35 
36 #include "qore/intern/AbstractIteratorHelper.h"
37 #include "qore/intern/FunctionalOperator.h"
38 #include "qore/intern/FunctionalOperatorInterface.h"
39 
40 class QoreSelectOperatorNode : public QoreBinaryOperatorNode<>, public FunctionalOperator {
41  friend class QoreFunctionalSelectListOperator;
42  friend class QoreFunctionalSelectSingleValueOperator;
43  friend class QoreFunctionalSelectIteratorOperator;
44  friend class QoreFunctionalSelectOperator;
45 
46 protected:
47  const QoreTypeInfo* returnTypeInfo;
48  FunctionalOperator* iterator_func;
49 
50  DLLLOCAL static QoreString select_str;
51 
52  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
53 
54  DLLLOCAL QoreValue evalFunc(bool& needs_deref, ExceptionSink* xsink) const;
55 
56  DLLLOCAL virtual ~QoreSelectOperatorNode() {
57  }
58 
59  DLLLOCAL virtual void parseInitImpl(QoreValue& val, LocalVar* oflag, int pflag, int& lvids, const QoreTypeInfo*& typeInfo);
60 
61  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
62  return returnTypeInfo;
63  }
64 
65  DLLLOCAL QoreValue selectIterator(AbstractIteratorHelper& h, ExceptionSink* xsink) const;
66 
67 public:
68  DLLLOCAL QoreSelectOperatorNode(const QoreProgramLocation* loc, QoreValue l, QoreValue r) : QoreBinaryOperatorNode<>(loc, l, r), returnTypeInfo(nullptr), iterator_func(nullptr) {
69  }
70 
71  DLLLOCAL virtual QoreString* getAsString(bool& del, int foff, ExceptionSink* xsink) const;
72  DLLLOCAL virtual int getAsString(QoreString& str, int foff, ExceptionSink* xsink) const;
73 
74  // returns the type name as a c string
75  DLLLOCAL virtual const char* getTypeName() const {
76  return select_str.getBuffer();
77  }
78 
79  DLLLOCAL virtual QoreOperatorNode* copyBackground(ExceptionSink *xsink) const {
80  QoreSelectOperatorNode* rv = copyBackgroundExplicit<QoreSelectOperatorNode>(xsink);
81  rv->iterator_func = dynamic_cast<FunctionalOperator*>(rv->left.getInternalNode());
82  return rv;
83  }
84 
85  DLLLOCAL virtual FunctionalOperatorInterface* getFunctionalIteratorImpl(FunctionalValueType& value_type, ExceptionSink* xsink) const;
86 };
87 
88 class QoreFunctionalSelectListOperator : public FunctionalOperatorInterface, public ConstListIterator {
89 protected:
90  const QoreSelectOperatorNode* select;
91  ExceptionSink* xsink;
92 
93 public:
94  DLLLOCAL QoreFunctionalSelectListOperator(const QoreSelectOperatorNode* s, QoreListNode* l, ExceptionSink* xs) : ConstListIterator(l), select(s), xsink(xs) {
95  }
96 
97  DLLLOCAL virtual ~QoreFunctionalSelectListOperator() {
98  const_cast<QoreListNode*>(getList())->deref(xsink);
99  }
100 
101  DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
102 
103  DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
104  return l->getValueTypeInfo();
105  }
106 };
107 
108 class QoreFunctionalSelectSingleValueOperator : public FunctionalOperatorInterface {
109 protected:
110  const QoreSelectOperatorNode* select;
111  QoreValue v;
112  bool done;
113  ExceptionSink* xsink;
114 
115 public:
116  DLLLOCAL QoreFunctionalSelectSingleValueOperator(const QoreSelectOperatorNode* s, QoreValue n, ExceptionSink* xs) : select(s), v(n), done(false), xsink(xs) {
117  }
118 
119  DLLLOCAL virtual ~QoreFunctionalSelectSingleValueOperator() {
120  v.discard(xsink);
121  }
122 
123  DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
124 
125  DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
126  return v.getTypeInfo();
127  }
128 };
129 
130 class QoreFunctionalSelectIteratorOperator : public FunctionalOperatorInterface {
131 protected:
132  const QoreSelectOperatorNode* select;
133  bool temp;
134  AbstractIteratorHelper h;
135  size_t index;
136  ExceptionSink* xsink;
137 
138 public:
139  DLLLOCAL QoreFunctionalSelectIteratorOperator(const QoreSelectOperatorNode* s, bool t, AbstractIteratorHelper n_h, ExceptionSink* xs) : select(s), temp(t), h(n_h), index(0), xsink(xs) {
140  }
141 
142  DLLLOCAL ~QoreFunctionalSelectIteratorOperator() {
143  if (temp)
144  h.obj->deref(xsink);
145  }
146 
147  DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
148 
149  DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
150  return autoTypeInfo;
151  }
152 };
153 
154 class QoreFunctionalSelectOperator : public FunctionalOperatorInterface {
155 protected:
156  const QoreSelectOperatorNode* select;
157  FunctionalOperatorInterface* f;
158  size_t index;
159 
160 public:
161  DLLLOCAL QoreFunctionalSelectOperator(const QoreSelectOperatorNode* s, FunctionalOperatorInterface* n_f) : select(s), f(n_f), index(0) {
162  }
163 
164  DLLLOCAL ~QoreFunctionalSelectOperator() {
165  delete f;
166  }
167 
168  DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
169 
170  DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
171  return f->getValueType();
172  }
173 };
174 
175 #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