Qore Programming Language  1.12.0
FunctionalOperatorInterface.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  FunctionalOperatorInterface.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_INTERN_FUNCTIONALOPERATORINTERFACE_H
33 
34 #define _QORE_INTERN_FUNCTIONALOPERATORINTERFACE_H
35 
36 #include "qore/intern/AbstractIteratorHelper.h"
37 #include "qore/intern/FunctionalOperator.h"
38 
39 class FunctionalOperatorInterface {
40 public:
41  DLLLOCAL virtual ~FunctionalOperatorInterface() {
42  }
43 
44  // returns the next value, if done iterating, then done is returned as true and the return value is NOTHING
45  /* @return if true then done iterating, in which case the return value must be NOTHING
46  */
47  DLLLOCAL bool getNext(ValueOptionalRefHolder& val, ExceptionSink* xsink);
48 
49  virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink) = 0;
50 
51  DLLLOCAL virtual const QoreTypeInfo* getValueType() const {
52  return getValueTypeImpl();
53  }
54 
55  DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const = 0;
56 
57  DLLLOCAL static FunctionalOperatorInterface* getFunctionalIterator(
58  FunctionalOperator::FunctionalValueType& value_type, QoreValue exp, bool fwd, const char* who,
59  ExceptionSink* xsink);
60 };
61 
62 class QoreFunctionalListOperator : public FunctionalOperatorInterface, public ConstListIterator {
63 protected:
64  bool fwd;
65  ExceptionSink* xsink;
66 
67 public:
68  DLLLOCAL QoreFunctionalListOperator(bool f, QoreListNode* l, ExceptionSink* xs) : ConstListIterator(l), fwd(f),
69  xsink(xs) {
70  }
71 
72  DLLLOCAL virtual ~QoreFunctionalListOperator() {
73  const_cast<QoreListNode*>(getList())->deref(xsink);
74  }
75 
76  DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
77  return l->getValueTypeInfo();
78  }
79 
80  DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
81 };
82 
83 class QoreFunctionalSingleValueOperator : public FunctionalOperatorInterface {
84 protected:
85  QoreValue v;
86  bool done = false;
87  ExceptionSink* xsink;
88 
89 public:
90  DLLLOCAL QoreFunctionalSingleValueOperator(QoreValue n, ExceptionSink* xs) : v(n), xsink(xs) {
91  }
92 
93  DLLLOCAL virtual ~QoreFunctionalSingleValueOperator() {
94  v.discard(xsink);
95  }
96 
97  DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
98  return v.getTypeInfo();
99  }
100 
101  DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
102 };
103 
104 class QoreFunctionalIteratorOperator : public FunctionalOperatorInterface {
105 protected:
106  bool temp;
107  AbstractIteratorHelper h;
108  size_t index = 0;
109  ExceptionSink* xsink;
110 
111 public:
112  DLLLOCAL QoreFunctionalIteratorOperator(bool t, AbstractIteratorHelper n_h, ExceptionSink* xs) : temp(t), h(n_h),
113  xsink(xs) {
114  }
115 
116  DLLLOCAL ~QoreFunctionalIteratorOperator() {
117  if (temp)
118  h.obj->deref(xsink);
119  }
120 
121  DLLLOCAL virtual const QoreTypeInfo* getValueTypeImpl() const {
122  return autoTypeInfo;
123  }
124 
125  DLLLOCAL virtual bool getNextImpl(ValueOptionalRefHolder& val, ExceptionSink* xsink);
126 };
127 
128 #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
DLLEXPORT const QoreTypeInfo * getValueTypeInfo() const
returns the value type declaration (if set)
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
DLLEXPORT void discard(ExceptionSink *xsink)
dereferences any contained AbstractQoreNode pointer and sets to 0; does not modify other values
DLLEXPORT const QoreTypeInfo * getTypeInfo() const
returns the type of the value