Qore Programming Language  1.12.0
AbstractIteratorHelper.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  AbstractIteratorHelper.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_ABSTRACTITERATORHELPER_H
33 
34 #define _QORE_ABSTRACTITERATORHELPER_H
35 
36 #include "qore/intern/QoreClassIntern.h"
37 
38 class AbstractIteratorHelper {
39 public:
40  QoreObject* obj = nullptr;
41  const QoreMethod* nextMethod = nullptr;
42  const QoreExternalMethodVariant* nextVariant = nullptr;
43  const QoreMethod* getValueMethod = nullptr;
44  const QoreExternalMethodVariant* getValueVariant = nullptr;
45  bool valid = false;
46 
47  DLLLOCAL AbstractIteratorHelper(ExceptionSink* xsink, const char* op, QoreObject* o, bool fwd = true,
48  bool get_value = true) {
49  bool priv;
50  const QoreClass* qc = o->getClass()->getClass(fwd ? *QC_ABSTRACTITERATOR : *QC_ABSTRACTBIDIRECTIONALITERATOR,
51  priv);
52  if (!qc)
53  return;
54 
55  const qore_class_private* class_ctx = runtime_get_class();
56  if (class_ctx && !qore_class_private::runtimeCheckPrivateClassAccess(*o->getClass(), class_ctx))
57  class_ctx = nullptr;
58  ClassAccess access;
59  obj = o;
60  // get "next" method if accessible
61  nextMethod = qore_class_private::get(*o->getClass())->runtimeFindCommittedMethod(fwd ? "next" : "prev",
62  access, class_ctx);
63  // method must be found because we have an instance of AbstractIterator/AbstractBidirectionalIterator
64  assert(nextMethod);
65  nextVariant = getCheckVariant(xsink, op, nextMethod);
66  if (!nextVariant)
67  return;
68  if (get_value) {
69  getValueMethod = qore_class_private::get(*o->getClass())->runtimeFindCommittedMethod("getValue", access,
70  class_ctx);
71  // method must be found because we have an instance of AbstractIterator
72  assert(getValueMethod);
73  getValueVariant = getCheckVariant(xsink, op, getValueMethod);
74  if (!getValueVariant)
75  return;
76  }
77  valid = true;
78  }
79 
80  DLLLOCAL operator bool() const {
81  return valid;
82  }
83 
84  DLLLOCAL bool next(ExceptionSink* xsink) {
85  assert(nextMethod);
86  assert(nextVariant);
87  ValueHolder rv(qore_method_private::evalNormalVariant(*nextMethod, xsink, obj, nextVariant, 0), xsink);
88  return rv->getAsBool();
89  }
90 
91  DLLLOCAL QoreValue getValue(ExceptionSink* xsink) {
92  assert(getValueMethod);
93  assert(getValueVariant);
94  return qore_method_private::evalNormalVariant(*getValueMethod, xsink, obj, getValueVariant, 0);
95  }
96 
97  // finds a method with no arguments
98  DLLLOCAL static const QoreExternalMethodVariant* getCheckVariant(ExceptionSink* xsink, const char* op,
99  const QoreMethod* m) {
100  const qore_class_private* class_ctx = runtime_get_class();
101  const MethodVariantBase* variant = reinterpret_cast<const MethodVariantBase*>(
102  qore_method_private::get(*m)->getFunction()->runtimeFindVariant(xsink, (QoreListNode*)nullptr, false,
103  class_ctx)
104  );
105  // this could throw an exception if the variant is builtin and has functional flags not allowed in the current
106  // pgm, for example
107  assert(xsink);
108  if (*xsink)
109  return nullptr;
110  // we must have a variant here because we have an instance of AbstractIterator
111  assert(variant);
112  if (variant->isPrivate()) {
113  // check for access to the class holding the private method
114  if (!qore_class_private::runtimeCheckPrivateClassAccess(*(variant->method()->getClass()), class_ctx)) {
115  QoreString opstr(op);
116  opstr.toupr();
117  opstr.concat("-ITERATOR-ERROR");
118  xsink->raiseException(opstr.getBuffer(), "cannot access private %s::%s() iterator method with the %s",
119  variant->method()->getClass()->getName(), m->getName(), op);
120  return nullptr;
121  }
122  }
123  return reinterpret_cast<const QoreExternalMethodVariant*>(variant);
124  }
125 
126  /*
127  DLLLOCAL QoreObject* getReferencedObject() const {
128  obj->ref();
129  return obj;
130  }
131  */
132 };
133 
134 #endif
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
DLLEXPORT AbstractQoreNode * raiseException(const char *err, const char *fmt,...)
appends a Qore-language exception to the list
defines a Qore-language class
Definition: QoreClass.h:249
DLLEXPORT const QoreClass * getClass(qore_classid_t cid) const
returns a pointer to the QoreClass object representing the class ID passed if it exists in the class ...
external wrapper class for method variants
Definition: QoreReflection.h:90
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
a method in a QoreClass
Definition: QoreClass.h:135
DLLEXPORT const char * getName() const
returns the method's name
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition: QoreObject.h:60
DLLEXPORT const QoreClass * getClass(qore_classid_t cid) const
returns a pointer to a QoreClass object if the class ID passed is a valid class in the hierarchy
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
holds an object and dereferences it in the destructor
Definition: QoreValue.h:476
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:275