Qore Programming Language - C/C++ Library  0.8.13.4
QoreListNode.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreListNode.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 207 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_QORELISTNODE_H
33 
34 #define _QORE_QORELISTNODE_H
35 
36 #include <qore/AbstractQoreNode.h>
37 
38 #ifdef min
39 #undef min
40 #endif
41 #ifdef max
42 #undef max
43 #endif
44 
46 
48 
53  friend class StackList;
54  friend struct qore_list_private;
55 
56 private:
58  DLLLOCAL QoreListNode(const QoreListNode&);
59 
61  DLLLOCAL QoreListNode& operator=(const QoreListNode&);
62 
63 protected:
65 
67  struct qore_list_private *priv;
68 
69  DLLLOCAL void resize(qore_size_t num);
70  DLLLOCAL QoreListNode* splice_intern(qore_size_t offset, qore_size_t length, ExceptionSink* xsink, bool extract = false);
71  DLLLOCAL QoreListNode* splice_intern(qore_size_t offset, qore_size_t length, const AbstractQoreNode* l, ExceptionSink* xsink, bool extract = false);
72  DLLLOCAL qore_size_t check_offset(qore_offset_t offset);
73  DLLLOCAL void check_offset(qore_offset_t offset, qore_offset_t len, qore_size_t &n_offset, qore_size_t &n_len);
74 
76 
78  DLLLOCAL int qsort(const ResolvedCallReferenceNode* fr, qore_size_t left, qore_size_t right, bool ascending, ExceptionSink* xsink);
79 
81 
83  DLLLOCAL int mergesort(const ResolvedCallReferenceNode* fr, bool ascending, ExceptionSink* xsink);
84 
86  DLLLOCAL QoreListNode* eval_intern(ExceptionSink* xsink) const;
87 
89 
93  DLLEXPORT virtual ~QoreListNode();
94 
96 
102  DLLEXPORT virtual bool derefImpl(ExceptionSink* xsink);
103 
105 
109  DLLEXPORT virtual AbstractQoreNode* evalImpl(ExceptionSink* xsink) const;
110 
112 
115  DLLLOCAL virtual AbstractQoreNode* evalImpl(bool &needs_deref, ExceptionSink* xsink) const;
116 
118  DLLLOCAL virtual int64 bigIntEvalImpl(ExceptionSink* xsink) const;
119 
121  DLLLOCAL virtual int integerEvalImpl(ExceptionSink* xsink) const;
122 
124  DLLLOCAL virtual bool boolEvalImpl(ExceptionSink* xsink) const;
125 
127  DLLLOCAL virtual double floatEvalImpl(ExceptionSink* xsink) const;
128 
129 public:
131  DLLEXPORT QoreListNode();
132 
134  DLLEXPORT QoreListNode(const QoreTypeInfo* valueTypeInfo);
135 
137 
139  DLLEXPORT virtual bool getAsBoolImpl() const;
140 
142 
148  DLLEXPORT int getAsString(QoreString &str, int foff, ExceptionSink* xsink) const;
149 
151 
158  DLLEXPORT QoreString *getAsString(bool &del, int foff, ExceptionSink* xsink) const;
159 
161  //DLLEXPORT virtual bool needs_eval() const;
162 
164  DLLEXPORT virtual AbstractQoreNode* realCopy() const;
165 
167 
171  DLLEXPORT virtual bool is_equal_soft(const AbstractQoreNode* v, ExceptionSink* xsink) const;
172 
174 
178  DLLEXPORT virtual bool is_equal_hard(const AbstractQoreNode* v, ExceptionSink* xsink) const;
179 
181  DLLEXPORT virtual const char *getTypeName() const;
182 
184  //DLLEXPORT virtual bool is_value() const;
185 
186  DLLLOCAL static const char *getStaticTypeName() {
187  return "list";
188  }
189 
191 
195  DLLEXPORT AbstractQoreNode* retrieve_entry(qore_size_t index);
196 
198 
202  DLLEXPORT const AbstractQoreNode* retrieve_entry(qore_size_t index) const;
203 
205 
209  DLLEXPORT AbstractQoreNode* get_referenced_entry(qore_size_t index) const;
210 
212 
215  DLLEXPORT int getEntryAsInt(qore_size_t index) const;
216 
220  DLLEXPORT AbstractQoreNode** get_entry_ptr(qore_size_t index);
221 
226 
228 
235  DLLEXPORT void set_entry(qore_size_t index, AbstractQoreNode* val, ExceptionSink* xsink);
236 
238 
240  DLLEXPORT void push(AbstractQoreNode* val);
241 
243 
245  DLLEXPORT int push(AbstractQoreNode* val, ExceptionSink* xsink);
246 
247  DLLEXPORT void insert(AbstractQoreNode* val);
248 
250 
252  DLLEXPORT AbstractQoreNode* pop();
253 
255 
258  DLLEXPORT AbstractQoreNode* shift();
259 
261 
263  DLLEXPORT void merge(const QoreListNode* list);
264 
266 
268  DLLEXPORT int merge(const QoreListNode* list, ExceptionSink* xsink);
269 
277  DLLEXPORT int delete_entry(qore_size_t index, ExceptionSink* xsink);
278 
285  DLLEXPORT void pop_entry(qore_size_t index, ExceptionSink* xsink);
286 
288 
294  DLLEXPORT QoreListNode* evalList(ExceptionSink* xsink) const;
295 
297 
304  DLLEXPORT QoreListNode* evalList(bool &needs_deref, ExceptionSink* xsink) const;
305 
307  DLLEXPORT QoreListNode* copy() const;
308 
310 
313  DLLEXPORT QoreListNode* copyListFrom(qore_size_t index) const;
314 
316 
319  DLLEXPORT QoreListNode* sort() const;
320 
322 
326  DLLEXPORT QoreListNode* sort(const ResolvedCallReferenceNode* fr, ExceptionSink* xsink) const;
327 
329 
332  DLLEXPORT QoreListNode* sortStable() const;
333 
335 
339  DLLEXPORT QoreListNode* sortStable(const ResolvedCallReferenceNode* fr, ExceptionSink* xsink) const;
340 
342 
345  DLLEXPORT QoreListNode* sortDescending() const;
346 
348 
352  DLLEXPORT QoreListNode* sortDescending(const ResolvedCallReferenceNode* fr, ExceptionSink* xsink) const;
353 
355 
358  DLLEXPORT QoreListNode* sortDescendingStable() const;
359 
361 
365  DLLEXPORT QoreListNode* sortDescendingStable(const ResolvedCallReferenceNode* fr, ExceptionSink* xsink) const;
366 
368 
371  DLLEXPORT AbstractQoreNode* min() const;
372 
374 
377  DLLEXPORT AbstractQoreNode* max() const;
378 
380 
384  DLLEXPORT AbstractQoreNode* min(const ResolvedCallReferenceNode* fr, ExceptionSink* xsink) const;
385 
387 
391  DLLEXPORT AbstractQoreNode* max(const ResolvedCallReferenceNode* fr, ExceptionSink* xsink) const;
392 
394 
398  DLLEXPORT void splice(qore_offset_t offset, ExceptionSink* xsink);
399 
401 
406  DLLEXPORT void splice(qore_offset_t offset, qore_offset_t length, ExceptionSink* xsink);
407 
409 
417  DLLEXPORT void splice(qore_offset_t offset, qore_offset_t length, const AbstractQoreNode* l, ExceptionSink* xsink);
418 
420 
425  DLLEXPORT QoreListNode* extract(qore_offset_t offset, ExceptionSink* xsink);
426 
428 
434  DLLEXPORT QoreListNode* extract(qore_offset_t offset, qore_offset_t length, ExceptionSink* xsink);
435 
437 
446  DLLEXPORT QoreListNode* extract(qore_offset_t offset, qore_offset_t length, const AbstractQoreNode* l, ExceptionSink* xsink);
447 
449 
451  DLLEXPORT qore_size_t size() const;
452 
454 
456  DLLEXPORT bool empty() const;
457 
459  DLLEXPORT QoreListNode* reverse() const;
460 
462  DLLEXPORT QoreListNode* listRefSelf() const;
463 
465 
467  DLLEXPORT const QoreTypeInfo* getValueTypeInfo() const;
468 
470 
472  DLLEXPORT const QoreTypeInfo* getTypeInfo() const;
473 
474  // needed only while parsing
476  DLLLOCAL QoreListNode(bool i);
477 
479  DLLLOCAL bool isFinalized() const;
480 
482  DLLLOCAL void setFinalized();
483 
485  DLLLOCAL bool isVariableList() const;
486 
488  DLLLOCAL void setVariableList();
489 
491  DLLLOCAL void clearNeedsEval();
492 
494  DLLLOCAL void setNeedsEval();
495 
497 
501  DLLLOCAL AbstractQoreNode* eval_entry(qore_size_t num, ExceptionSink* xsink) const;
502 
504  DLLLOCAL virtual AbstractQoreNode* parseInit(LocalVar *oflag, int pflag, int &lvids, const QoreTypeInfo *&typeInfo);
505 
507  DLLLOCAL QoreListNode* parseInitList(LocalVar *oflag, int pflag, int &lvids, const QoreTypeInfo *&typeInfo);
508 };
509 
510 #include <qore/ReferenceHolder.h>
511 
513 
517 
519 
539 protected:
540  QoreListNode* l;
541  qore_offset_t pos;
542 
544  DLLLOCAL void *operator new(size_t);
545 
546 public:
548 
551  DLLEXPORT ListIterator(QoreListNode* lst, qore_size_t n_pos = -1);
552 
554 
557  DLLEXPORT ListIterator(QoreListNode& lst, qore_size_t n_pos = -1);
558 
560 
565  DLLEXPORT bool next();
566 
568 
574  DLLEXPORT bool prev();
575 
577 
582  DLLEXPORT int set(qore_size_t n_pos);
583 
585  DLLEXPORT AbstractQoreNode* getValue() const;
586 
588  DLLEXPORT AbstractQoreNode** getValuePtr() const;
589 
591  DLLEXPORT AbstractQoreNode* getReferencedValue() const;
592 
594  DLLEXPORT AbstractQoreNode* takeValue();
595 
597  DLLEXPORT bool first() const;
598 
600  DLLEXPORT bool last() const;
601 
603  DLLEXPORT AbstractQoreNode* swapValue(AbstractQoreNode* val) const;
604 
606  DLLLOCAL qore_size_t index() const { return pos; }
607 
609  DLLLOCAL qore_size_t max() const { return l->size(); }
610 
612  DLLLOCAL QoreListNode* getList() { return l; }
613 
615  DLLLOCAL bool empty() const {
616  return l->empty();
617  }
618 
620  DLLLOCAL bool valid() const {
621  return pos == -1 ? false : true;
622  }
623 };
624 
626 
646 protected:
647  const QoreListNode* l;
648  qore_offset_t pos;
649 
650 public:
652 
655  DLLEXPORT ConstListIterator(const QoreListNode* lst, qore_size_t n_pos = -1);
656 
658 
661  DLLEXPORT ConstListIterator(const QoreListNode& lst, qore_size_t n_pos = -1);
662 
664 
669  DLLEXPORT bool next();
670 
672 
678  DLLEXPORT bool prev();
679 
681 
686  DLLEXPORT int set(qore_size_t n_pos);
687 
689  DLLEXPORT const AbstractQoreNode* getValue() const;
690 
692  DLLEXPORT AbstractQoreNode* getReferencedValue() const;
693 
695  DLLEXPORT bool first() const;
696 
698  DLLEXPORT bool last() const;
699 
701  DLLLOCAL qore_size_t index() const { return pos; }
702 
704  DLLLOCAL qore_size_t max() const { return l->size(); }
705 
707  DLLLOCAL const QoreListNode* getList() const { return l; }
708 
710  DLLLOCAL bool empty() const {
711  return l->empty();
712  }
713 
715  DLLLOCAL bool valid() const {
716  return pos == -1 ? false : true;
717  }
718 
720  DLLEXPORT void reset();
721 };
722 
725 private:
726  QoreListNode* val;
727  ExceptionSink* xsink;
728  bool needs_deref;
729 
730  DLLLOCAL void discard_intern() {
731  if (needs_deref && val)
732  val->deref(xsink);
733  }
734 
735  DLLLOCAL void eval_intern(const QoreListNode* exp) {
736  if (exp)
737  val = exp->evalList(needs_deref, xsink);
738  else {
739  val = 0;
740  needs_deref = false;
741  }
742  }
743 
747  DLLLOCAL QoreListNodeEvalOptionalRefHolder& operator=(const QoreListNodeEvalOptionalRefHolder&);
749  DLLLOCAL void *operator new(size_t);
750 
751 public:
753  DLLLOCAL QoreListNodeEvalOptionalRefHolder(ExceptionSink* n_xsink) : xsink(n_xsink) {
754  needs_deref = false;
755  val = 0;
756  }
757 
759  DLLLOCAL QoreListNodeEvalOptionalRefHolder(const QoreListNode* exp, ExceptionSink* n_xsink) : xsink(n_xsink) {
760  eval_intern(exp);
761  }
762 
764  DLLLOCAL ~QoreListNodeEvalOptionalRefHolder() {
765  discard_intern();
766  }
767 
769  DLLLOCAL void discard() {
770  discard_intern();
771  needs_deref = false;
772  val = 0;
773  }
774 
776  DLLLOCAL void assignEval(const QoreListNode* exp) {
777  discard_intern();
778  eval_intern(exp);
779  }
780 
782  DLLLOCAL void assign(bool n_needs_deref, QoreListNode* n_val) {
783  discard_intern();
784  needs_deref = n_needs_deref;
785  val = n_val;
786  }
787 
789  DLLLOCAL bool needsDeref() const {
790  return needs_deref;
791  }
792 
794 
798  DLLLOCAL QoreListNode* getReferencedValue() {
799  if (needs_deref)
800  needs_deref = false;
801  else if (val)
802  val->ref();
803  return val;
804  }
805 
807  DLLLOCAL void edit() {
808  if (!val) {
809  val = new QoreListNode;
810  needs_deref = true;
811  }
812  else if (!needs_deref || !val->is_unique()) {
813  val = val->copy();
814  needs_deref = true;
815  }
816  }
817 
819 
823  DLLLOCAL const QoreListNode* operator->() const { return val; }
824 
826  DLLLOCAL const QoreListNode* operator*() const { return val; }
827 
829  DLLLOCAL operator bool() const { return val != 0; }
830 };
831 
832 #endif
virtual DLLEXPORT const char * getTypeName() const
returns the type name as a c string
DLLLOCAL void setNeedsEval()
this function is not exported in the qore library
DLLEXPORT QoreListNode * sortStable() const
returns a new list based on executing mergesort on the source list ("this")
DLLEXPORT void pop_entry(qore_size_t index, ExceptionSink *xsink)
DLLEXPORT void merge(const QoreListNode *list)
appends the elements of "list" to this list
DLLLOCAL void clearNeedsEval()
this function is not exported in the qore library
DLLEXPORT AbstractQoreNode * max() const
returns the element having the highest value (determined by calling OP_LOG_GT - the greater-than ">" ...
DLLLOCAL bool isVariableList() const
this function is not exported in the qore library
DLLEXPORT const QoreTypeInfo * getValueTypeInfo() const
returns the value type declaration (if set)
virtual DLLLOCAL int64 bigIntEvalImpl(ExceptionSink *xsink) const
always returns 0
virtual DLLEXPORT bool is_equal_hard(const AbstractQoreNode *v, ExceptionSink *xsink) const
tests for equality ("deep compare" including all contained values) without type conversions (hard com...
DLLLOCAL int mergesort(const ResolvedCallReferenceNode *fr, bool ascending, ExceptionSink *xsink)
mergesort sorts the list in-place (stable)
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:54
virtual DLLLOCAL AbstractQoreNode * parseInit(LocalVar *oflag, int pflag, int &lvids, const QoreTypeInfo *&typeInfo)
for initialization of lists in the parse tree at parse time (always returns "this") ...
size_t qore_size_t
used for sizes (same range as a pointer)
Definition: common.h:74
static void discard(AbstractQoreNode *n, ExceptionSink *xsink)
to deref an AbstractQoreNode (when the pointer may be 0)
Definition: QoreLib.h:327
For use on the stack only: iterates through a the elements of a QoreListNode.
Definition: QoreListNode.h:538
DLLEXPORT QoreListNode * sort() const
returns a new list based on quicksorting the source list ("this")
DLLEXPORT AbstractQoreNode ** getExistingEntryPtr(qore_size_t index)
DLLEXPORT bool empty() const
returns true if the list is empty
DLLEXPORT AbstractQoreNode * retrieve_entry(qore_size_t index)
returns the element at "index" (first element is index 0)
Qore&#39;s string type supported by the QoreEncoding class.
Definition: QoreString.h:82
DLLEXPORT AbstractQoreNode * get_referenced_entry(qore_size_t index) const
returns the element at "index" (first element is index 0), the caller owns the reference ...
virtual DLLEXPORT bool getAsBoolImpl() const
returns false unless perl-boolean-evaluation is enabled, in which case it returns false only when emp...
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
DLLLOCAL void setFinalized()
this function is not exported in the qore library
DLLEXPORT int getEntryAsInt(qore_size_t index) const
returns the value of element at "index" as an integer (first element is index 0)
DLLEXPORT int getAsString(QoreString &str, int foff, ExceptionSink *xsink) const
concatenate the verbose string representation of the list (including all contained values) to an exis...
DLLLOCAL QoreListNode * eval_intern(ExceptionSink *xsink) const
does an unconditional evaluation of the list and returns the new list, 0 if there is a qore-language ...
virtual DLLEXPORT ~QoreListNode()
the destructor is protected so it cannot be called directly
virtual DLLEXPORT AbstractQoreNode * realCopy() const
returns true if the list contains parse expressions and therefore needs evaluation to return a value...
DLLEXPORT QoreListNode * copyListFrom(qore_size_t index) const
performs a deep copy of the list starting from element "offset" and returns the new list ...
DLLEXPORT QoreListNode * listRefSelf() const
returns "this" with an incremented reference count
DLLEXPORT void push(AbstractQoreNode *val)
adds a value to the list
DLLEXPORT AbstractQoreNode * min() const
returns the element having the lowest value (determined by calling OP_LOG_LT - the less-than "<" oper...
virtual DLLEXPORT bool is_equal_soft(const AbstractQoreNode *v, ExceptionSink *xsink) const
tests for equality ("deep compare" including all contained values) with possible type conversion (sof...
DLLEXPORT QoreListNode * reverse() const
returns a list with the order of the elements reversed
DLLEXPORT QoreListNode * sortDescending() const
returns a new list based on quicksorting the source list ("this") in descending order ...
DLLLOCAL QoreListNode * parseInitList(LocalVar *oflag, int pflag, int &lvids, const QoreTypeInfo *&typeInfo)
for initialization of lists in the parse tree at parse time (always returns "this") ...
DLLEXPORT AbstractQoreNode * pop()
returns the last element of the list, the length is decremented by one, caller owns the reference ...
DLLLOCAL void setVariableList()
this function is not exported in the qore library
DLLEXPORT QoreListNode * sortDescendingStable() const
returns a new list based on executing mergesort on the source list ("this") in descending order ...
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:47
virtual DLLEXPORT AbstractQoreNode * evalImpl(ExceptionSink *xsink) const
evaluates the list and returns a value (or 0)
DLLEXPORT QoreListNode * evalList(ExceptionSink *xsink) const
evaluates the list and returns a value (or 0)
DLLEXPORT void deref(ExceptionSink *xsink)
decrements the reference count and calls derefImpl() if there_can_be_only_one is false, otherwise does nothing
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:241
DLLEXPORT int delete_entry(qore_size_t index, ExceptionSink *xsink)
intptr_t qore_offset_t
used for offsets that could be negative
Definition: common.h:77
DLLEXPORT void ref() const
increments the reference count
virtual DLLLOCAL bool boolEvalImpl(ExceptionSink *xsink) const
always returns false
DLLEXPORT void splice(qore_offset_t offset, ExceptionSink *xsink)
truncates the list at position "offset" (first element is offset 0)
DLLEXPORT bool is_unique() const
returns true if the reference count is 1
base class for resolved call references
Definition: CallReferenceNode.h:130
virtual DLLLOCAL int integerEvalImpl(ExceptionSink *xsink) const
always returns 0
DLLEXPORT void set_entry(qore_size_t index, AbstractQoreNode *val, ExceptionSink *xsink)
sets the value of a list element
DLLEXPORT AbstractQoreNode ** get_entry_ptr(qore_size_t index)
DLLLOCAL bool isFinalized() const
this function is not exported in the qore library
static DLLLOCAL const char * getStaticTypeName()
returns true if the list does not contain any parse expressions, otherwise returns false ...
Definition: QoreListNode.h:186
virtual DLLLOCAL double floatEvalImpl(ExceptionSink *xsink) const
always returns 0.0
DLLLOCAL AbstractQoreNode * eval_entry(qore_size_t num, ExceptionSink *xsink) const
this function is not exported in the qore library
DLLEXPORT const QoreTypeInfo * getTypeInfo() const
returns the type info structure for the current value; also works for lists with a specific value typ...
DLLEXPORT qore_size_t size() const
returns the number of elements in the list
For use on the stack only: manages result of the optional evaluation of a QoreListNode.
Definition: QoreListNode.h:724
DLLEXPORT AbstractQoreNode * shift()
returns the first element of the list, all other entries are moved down to fill up the first position...
DLLEXPORT QoreListNode()
create an empty list
a templated class to manage a reference count of an object that can throw a Qore-language exception w...
Definition: ReferenceHolder.h:51
DLLEXPORT QoreListNode * extract(qore_offset_t offset, ExceptionSink *xsink)
truncates the list at position "offset" (first element is offset 0) and returns any elements removed ...
For use on the stack only: iterates through elements of a const QoreListNode.
Definition: QoreListNode.h:645
struct qore_list_private * priv
this structure holds the private implementation for the type
Definition: QoreListNode.h:67
virtual DLLEXPORT bool derefImpl(ExceptionSink *xsink)
dereferences all elements of the list
DLLLOCAL int qsort(const ResolvedCallReferenceNode *fr, qore_size_t left, qore_size_t right, bool ascending, ExceptionSink *xsink)
qsort sorts the list in-place (unstable)
DLLEXPORT QoreListNode * copy() const
performs a deep copy of the list and returns the new list