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