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