Qore Programming Language  1.12.0
RangeIterator.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  RangeIterator.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_RANGEITERATOR_H
33 
34 #define _QORE_RANGEITERATOR_H
35 
36 extern QoreClass* QC_RANGEITERATOR;
37 
38 // the c++ object. See QC_RangeIterator.qpp for docs.
39 class RangeIterator : public QoreIteratorBase {
40 private:
41  int64 m_start;
42  int64 m_stop;
43  int64 m_step;
44 
45  int64 m_position;
46 
47  bool m_increasing;
48  bool m_valid;
49 
50  QoreValue val;
51 
52 public:
53  DLLLOCAL RangeIterator(int64 start, int64 stop, int64 step, const QoreValue v, ExceptionSink* xsink)
54  : QoreIteratorBase(),
55  m_start(start),
56  m_stop(stop),
57  m_step(step),
58  m_position(-1),
59  m_increasing(start < stop),
60  m_valid(false),
61  val((!v.isNothing() && step >= 0) ? v.refSelf() : QoreValue()) {
62  if (step < 1) {
63  xsink->raiseException("RANGEITERATOR-ERROR", "Value of the 'step' argument has to be greater than 0 " \
64  "(value passed: " QLLD ")", step);
65  }
66  // issue #4031
67  if (runtime_get_parse_options() & PO_BROKEN_RANGE) {
68  if (m_increasing) {
69  ++m_stop;
70  } else {
71  --m_stop;
72  }
73  }
74  }
75 
76  DLLLOCAL RangeIterator(const RangeIterator& old)
77  : m_start(old.m_start), m_stop(old.m_stop), m_step(old.m_step),
78  m_position(old.m_position), m_increasing(old.m_increasing),
79  m_valid(old.m_valid), val(old.val.refSelf()) {
80  }
81 
82  DLLLOCAL virtual ~RangeIterator() {
83  assert(!val.hasNode());
84  }
85 
86  DLLLOCAL void destructor(ExceptionSink* xsink) {
87  val.discard(xsink);
88  }
89 
90  DLLLOCAL bool next() {
91  ++m_position;
92  m_valid = m_increasing ? (calculateCurrent() < m_stop) : (calculateCurrent() > m_stop);
93  if (!m_valid)
94  m_position = -1;
95  return m_valid;
96  }
97 
98  DLLLOCAL bool valid() const {
99  return m_valid;
100  }
101 
102  DLLLOCAL QoreValue getValue(ExceptionSink *xsink) {
103  if (!m_valid) {
104  xsink->raiseException("INVALID-ITERATOR", "the %s is not pointing at a valid element; make sure " \
105  "%s::next() returns True before calling this method", getName(), getName());
106  return QoreValue();
107  }
108 
109  int64 rv = calculateCurrent();
110  return !val.isNothing() ? val.refSelf() : QoreValue(rv);
111  }
112 
113  DLLLOCAL void reset() {
114  m_position = -1;
115  m_valid = false;
116  }
117 
118  DLLLOCAL virtual const char* getName() const { return "RangeIterator"; }
119 
120  DLLLOCAL virtual const QoreTypeInfo* getElementType() const {
121  return val.isNothing() ? bigIntTypeInfo : val.getTypeInfo();
122  }
123 
124 private:
125  DLLLOCAL int64 calculateCurrent() {
126  if (m_increasing) {
127  return m_start + (m_position * m_step);
128  } else {
129  return m_start - (m_position * m_step);
130  }
131  }
132 };
133 
134 #endif // _QORE_RANGEITERATOR_H
#define PO_BROKEN_RANGE
allow for old pre-Qore 0.9.5 "range()" and "xrange()" behavior where the upper limit was included in ...
Definition: Restrictions.h:99
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
abstract base class for iterator private data
Definition: QoreIteratorBase.h:68
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260
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
DLLEXPORT bool hasNode() const
returns true if the object contains a non-null AbstractQoreNode pointer (ie type == QV_Node && v....
DLLEXPORT QoreValue refSelf() const
references the contained value if type == QV_Node, returns itself