Qore Programming Language  0.8.12
QoreValueList.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreValueList.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2015 David Nichols
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_QOREVALUELIST_H
33 
34 #define _QORE_QOREVALUELIST_H
35 
36 #include <qore/AbstractQoreNode.h>
37 
39 
41 
46  friend struct qore_value_list_private;
47 
48 private:
50  DLLLOCAL QoreValueList(const QoreValueList&);
51 
53  DLLLOCAL QoreValueList& operator=(const QoreValueList&);
54 
55 protected:
57 
59  struct qore_value_list_private *priv;
60 
62 
66  DLLEXPORT virtual ~QoreValueList();
67 
69 
75  DLLEXPORT virtual bool derefImpl(ExceptionSink* xsink);
76 
78 
82  DLLEXPORT virtual AbstractQoreNode* evalImpl(ExceptionSink* xsink) const;
83 
85 
88  DLLLOCAL virtual AbstractQoreNode* evalImpl(bool &needs_deref, ExceptionSink* xsink) const;
89 
91  DLLLOCAL virtual int64 bigIntEvalImpl(ExceptionSink* xsink) const;
92 
94  DLLLOCAL virtual int integerEvalImpl(ExceptionSink* xsink) const;
95 
97  DLLLOCAL virtual bool boolEvalImpl(ExceptionSink* xsink) const;
98 
100  DLLLOCAL virtual double floatEvalImpl(ExceptionSink* xsink) const;
101 
102 public:
103  DLLEXPORT QoreValueList();
104 
106 
108  DLLEXPORT virtual bool getAsBoolImpl() const;
109 
111 
117  DLLEXPORT int getAsString(QoreString &str, int foff, ExceptionSink* xsink) const;
118 
120 
127  DLLEXPORT QoreString* getAsString(bool &del, int foff, ExceptionSink* xsink) const;
128 
130  //DLLEXPORT virtual bool needs_eval() const;
131 
133  DLLEXPORT virtual AbstractQoreNode* realCopy() const;
134 
136 
140  DLLEXPORT virtual bool is_equal_soft(const AbstractQoreNode* v, ExceptionSink* xsink) const;
141 
143 
147  DLLEXPORT virtual bool is_equal_hard(const AbstractQoreNode* v, ExceptionSink* xsink) const;
148 
150  DLLEXPORT virtual const char* getTypeName() const;
151 
153  //DLLEXPORT virtual bool is_value() const;
154 
155  DLLLOCAL static const char* getStaticTypeName() {
156  return "list";
157  }
158 
160 
164  DLLEXPORT QoreValue retrieveEntry(size_t index);
165 
167 
171  DLLEXPORT const QoreValue retrieveEntry(size_t index) const;
172 
174 
178  DLLEXPORT QoreValue getReferencedEntry(size_t index) const;
179 
183  DLLEXPORT QoreValue& getEntryReference(size_t index);
184 
188  DLLEXPORT QoreValue* getExistingEntryPtr(size_t index);
189 
191 
198  DLLEXPORT void setEntry(size_t index, QoreValue val, ExceptionSink* xsink);
199 
200  DLLEXPORT void push(QoreValue val);
201  DLLEXPORT void insert(QoreValue val);
202 
204 
206  DLLEXPORT QoreValue pop();
207 
209 
212  DLLEXPORT QoreValue shift();
213 
215  DLLEXPORT void merge(const QoreValueList* list);
216 
223  //DLLEXPORT void popEntry(size_t index, ExceptionSink* xsink);
224 
226 
232  DLLEXPORT QoreValueList* evalList(ExceptionSink* xsink) const;
233 
235 
242  DLLEXPORT QoreValueList* evalList(bool &needs_deref, ExceptionSink* xsink) const;
243 
245  DLLEXPORT QoreValueList* copy() const;
246 
248 
251  DLLEXPORT QoreValueList* copyListFrom(size_t index) const;
252 
254 
257  DLLEXPORT QoreValueList* sort(ExceptionSink* xsink) const;
258 
260 
264  DLLEXPORT QoreValueList* sort(const ResolvedCallReferenceNode* fr, ExceptionSink* xsink) const;
265 
267 
270  DLLEXPORT QoreValueList* sortStable(ExceptionSink* xsink) const;
271 
273 
277  DLLEXPORT QoreValueList* sortStable(const ResolvedCallReferenceNode* fr, ExceptionSink* xsink) const;
278 
280 
283  DLLEXPORT QoreValueList* sortDescending(ExceptionSink* xsink) const;
284 
286 
290  DLLEXPORT QoreValueList* sortDescending(const ResolvedCallReferenceNode* fr, ExceptionSink* xsink) const;
291 
293 
296  DLLEXPORT QoreValueList* sortDescendingStable(ExceptionSink* xsink) const;
297 
299 
303  DLLEXPORT QoreValueList* sortDescendingStable(const ResolvedCallReferenceNode* fr, ExceptionSink* xsink) const;
304 
306 
309  DLLEXPORT QoreValue minValue(ExceptionSink* xsink) const;
310 
312 
315  DLLEXPORT QoreValue maxValue(ExceptionSink* xsink) const;
316 
318 
322  DLLEXPORT QoreValue minValue(const ResolvedCallReferenceNode* fr, ExceptionSink* xsink) const;
323 
325 
329  DLLEXPORT QoreValue maxValue(const ResolvedCallReferenceNode* fr, ExceptionSink* xsink) const;
330 
332 
338  DLLEXPORT QoreValueList* splice(ptrdiff_t offset, ExceptionSink* xsink);
339 
341 
348  DLLEXPORT QoreValueList* splice(ptrdiff_t offset, ptrdiff_t length, ExceptionSink* xsink);
349 
351 
361  DLLEXPORT QoreValueList* splice(ptrdiff_t offset, ptrdiff_t length, const QoreValue l, ExceptionSink* xsink);
362 
364 
369  DLLEXPORT QoreValueList* extract(ptrdiff_t offset, ExceptionSink* xsink);
370 
372 
378  DLLEXPORT QoreValueList* extract(ptrdiff_t offset, ptrdiff_t length, ExceptionSink* xsink);
379 
381 
390  DLLEXPORT QoreValueList* extract(ptrdiff_t offset, ptrdiff_t length, const QoreValue l, ExceptionSink* xsink);
391 
393 
395  DLLEXPORT size_t size() const;
396 
398 
400  DLLEXPORT bool empty() const;
401 
403  DLLEXPORT QoreValueList* reverse() const;
404 
406  DLLEXPORT QoreValueList* listRefSelf() const;
407 
409  DLLEXPORT QoreValue swap(ptrdiff_t offset, QoreValue val);
410 
412  DLLEXPORT QoreValue takeExists(ptrdiff_t offset);
413 };
414 
415 #include <qore/ReferenceHolder.h>
416 
418 
422 
424 
444 protected:
445  QoreValueList* l;
446  ptrdiff_t pos;
447 
449  DLLLOCAL void *operator new(size_t);
450 
451 public:
453 
456  DLLEXPORT ValueListIterator(QoreValueList* lst, size_t n_pos = -1);
457 
459 
462  DLLEXPORT ValueListIterator(QoreValueList& lst, size_t n_pos = -1);
463 
465 
470  DLLEXPORT bool next();
471 
473 
479  DLLEXPORT bool prev();
480 
482 
487  DLLEXPORT int set(size_t n_pos);
488 
490  DLLEXPORT QoreValue getValue() const;
491 
493  DLLEXPORT QoreValue* getValueReference() const;
494 
496  DLLEXPORT QoreValue getReferencedValue() const;
497 
499  DLLEXPORT bool first() const;
500 
502  DLLEXPORT bool last() const;
503 
504  //DLLEXPORT void setValue(AbstractQoreNode* val, ExceptionSink* xsink) const;
505 
507  DLLLOCAL size_t index() const { return pos; }
508 
510  DLLLOCAL size_t max() const { return l->size(); }
511 
513  DLLLOCAL QoreValueList* getList() { return l; }
514 
516  DLLLOCAL bool empty() const {
517  return l->empty();
518  }
519 
521  DLLLOCAL bool valid() const {
522  return pos == -1 ? false : true;
523  }
524 };
525 
527 
547 protected:
548  const QoreValueList* l;
549  ptrdiff_t pos;
550 
551 public:
553 
556  DLLEXPORT ConstValueListIterator(const QoreValueList* lst, size_t n_pos = -1);
557 
559 
562  DLLEXPORT ConstValueListIterator(const QoreValueList& lst, size_t n_pos = -1);
563 
565 
570  DLLEXPORT bool next();
571 
573 
579  DLLEXPORT bool prev();
580 
582 
587  DLLEXPORT int set(size_t n_pos);
588 
590  DLLEXPORT const QoreValue getValue() const;
591 
593  DLLEXPORT QoreValue getReferencedValue() const;
594 
596  DLLEXPORT bool first() const;
597 
599  DLLEXPORT bool last() const;
600 
602  DLLLOCAL size_t index() const { return pos; }
603 
605  DLLLOCAL size_t max() const { return l->size(); }
606 
608  DLLLOCAL const QoreValueList* getList() const { return l; }
609 
611  DLLLOCAL bool empty() const {
612  return l->empty();
613  }
614 
616  DLLLOCAL bool valid() const {
617  return pos == -1 ? false : true;
618  }
619 
621  DLLEXPORT void reset();
622 };
623 
626 private:
627  QoreValueList* val;
628  ExceptionSink* xsink;
629  bool needs_deref;
630 
631  DLLLOCAL void discard_intern() {
632  if (needs_deref && val)
633  val->deref(xsink);
634  }
635 
636  DLLLOCAL void eval_intern(const QoreValueList* exp) {
637  if (exp)
638  val = exp->evalList(needs_deref, xsink);
639  else {
640  val = 0;
641  needs_deref = false;
642  }
643  }
644 
648  DLLLOCAL QoreValueListEvalOptionalRefHolder& operator=(const QoreValueListEvalOptionalRefHolder&);
650  DLLLOCAL void *operator new(size_t);
651 
652 public:
654  DLLLOCAL QoreValueListEvalOptionalRefHolder(ExceptionSink* n_xsink) : xsink(n_xsink) {
655  needs_deref = false;
656  val = 0;
657  }
658 
660  DLLLOCAL QoreValueListEvalOptionalRefHolder(const QoreValueList* exp, ExceptionSink* n_xsink) : xsink(n_xsink) {
661  eval_intern(exp);
662  }
663 
666  discard_intern();
667  }
668 
670  DLLLOCAL void discard() {
671  discard_intern();
672  needs_deref = false;
673  val = 0;
674  }
675 
677  DLLLOCAL void assignEval(const QoreValueList* exp) {
678  discard_intern();
679  eval_intern(exp);
680  }
681 
683  DLLLOCAL void assign(bool n_needs_deref, QoreValueList* n_val) {
684  discard_intern();
685  needs_deref = n_needs_deref;
686  val = n_val;
687  }
688 
690  DLLLOCAL bool needsDeref() const {
691  return needs_deref;
692  }
693 
695 
700  if (needs_deref)
701  needs_deref = false;
702  else if (val)
703  val->ref();
704  return val;
705  }
706 
708  DLLLOCAL void edit() {
709  if (!val) {
710  val = new QoreValueList;
711  needs_deref = true;
712  }
713  else if (!needs_deref || !val->is_unique()) {
714  val = val->copy();
715  needs_deref = true;
716  }
717  }
718 
720 
724  DLLLOCAL const QoreValueList* operator->() const { return val; }
725 
727  DLLLOCAL const QoreValueList* operator*() const { return val; }
728 
730  DLLLOCAL operator bool() const { return val != 0; }
731 };
732 
733 #endif
DLLEXPORT bool first() const
returns true when the iterator is pointing to the first element in the list
DLLEXPORT bool prev()
moves the iterator to the previous element, returns true if the iterator is pointing to an element of...
DLLEXPORT QoreValue swap(ptrdiff_t offset, QoreValue val)
swaps the value at the given position with the value given, caller owns any reference returned ...
DLLLOCAL void edit()
will create a unique list so the list can be edited
Definition: QoreValueList.h:708
DLLEXPORT ValueListIterator(QoreValueList *lst, size_t n_pos=-1)
initializes the iterator to the position given or, if omitted, just before the first element ...
For use on the stack only: iterates through a the elements of a QoreValueList.
Definition: QoreValueList.h:443
DLLEXPORT bool first() const
returns true when the iterator is pointing to the first element in the list
DLLEXPORT QoreValueList * evalList(ExceptionSink *xsink) const
evaluates the list and returns a value (or 0)
DLLLOCAL QoreValueList * getList()
returns the list
Definition: QoreValueList.h:513
DLLEXPORT const QoreValue getValue() const
returns a pointer to the value of the list element
DLLEXPORT int set(size_t n_pos)
sets the iterator to a specific position in the list
DLLEXPORT int set(size_t n_pos)
sets the iterator to a specific position in the list
DLLLOCAL bool empty() const
returns true if the list is empty
Definition: QoreValueList.h:611
DLLEXPORT QoreValue takeExists(ptrdiff_t offset)
takes the value at the given position and replaces with NULL, only if the list position exists ...
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreValueList.h:45
DLLLOCAL size_t index() const
returns the current iterator position in the list or -1 if not pointing at a valid element ...
Definition: QoreValueList.h:507
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:55
DLLLOCAL bool valid() const
returns true if the iterator is pointing to a valid element
Definition: QoreValueList.h:521
DLLEXPORT QoreValue getValue() const
returns a pointer to the value of the list element
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...
For use on the stack only: manages result of the optional evaluation of a QoreValueList.
Definition: QoreValueList.h:625
DLLEXPORT QoreValueList * splice(ptrdiff_t offset, ExceptionSink *xsink)
truncates the list at position "offset" (first element is offset 0)
DLLLOCAL ~QoreValueListEvalOptionalRefHolder()
clears the object (dereferences the old object if necessary)
Definition: QoreValueList.h:665
virtual DLLEXPORT AbstractQoreNode * realCopy() const
returns true if the list contains parse expressions and therefore needs evaluation to return a value...
DLLEXPORT ConstValueListIterator(const QoreValueList *lst, size_t n_pos=-1)
initializes the iterator to the position given or, if omitted, just before the first element ...
DLLEXPORT bool last() const
returns true when the iterator is pointing to the last element in the list
DLLLOCAL const QoreValueList * getList() const
returns the list
Definition: QoreValueList.h:608
DLLLOCAL QoreValueListEvalOptionalRefHolder(const QoreValueList *exp, ExceptionSink *n_xsink)
performs an optional evaluation of the list (sets the dereference flag)
Definition: QoreValueList.h:660
virtual DLLLOCAL bool boolEvalImpl(ExceptionSink *xsink) const
always returns false
virtual DLLLOCAL int64 bigIntEvalImpl(ExceptionSink *xsink) const
always returns 0
DLLEXPORT QoreValue * getExistingEntryPtr(size_t index)
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:50
DLLEXPORT QoreValue shift()
returns the first element of the list, all other entries are moved down to fill up the first position...
virtual DLLEXPORT bool getAsBoolImpl() const
returns false unless perl-boolean-evaluation is enabled, in which case it returns false only when emp...
DLLLOCAL bool needsDeref() const
returns true if the object contains a temporary (evaluated) value that needs a dereference ...
Definition: QoreValueList.h:690
struct qore_value_list_private * priv
this structure holds the private implementation for the type
Definition: QoreValueList.h:59
DLLEXPORT void setEntry(size_t index, QoreValue val, ExceptionSink *xsink)
sets the value of a list element
DLLEXPORT bool prev()
moves the iterator to the previous element, returns true if the iterator is pointing to an element of...
DLLEXPORT QoreValueList * sortDescending(ExceptionSink *xsink) const
returns a new list based on quicksorting the source list ("this") in descending order ...
DLLEXPORT QoreValueList * copyListFrom(size_t index) const
performs a deep copy of the list starting from element "offset" and returns the new list ...
DLLEXPORT QoreValue getReferencedEntry(size_t index) const
returns the element at "index" (first element is index 0), the caller owns the reference ...
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...
DLLEXPORT QoreValueList * sort(ExceptionSink *xsink) const
returns a new list based on quicksorting the source list ("this")
DLLLOCAL bool empty() const
returns true if the list is empty
Definition: QoreValueList.h:516
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...
DLLEXPORT bool empty() const
returns true if the list is empty
static DLLLOCAL const char * getStaticTypeName()
returns true if the list does not contain any parse expressions, otherwise returns false ...
Definition: QoreValueList.h:155
DLLLOCAL size_t index() const
returns the current iterator position in the list or -1 if not pointing at a valid element ...
Definition: QoreValueList.h:602
DLLEXPORT QoreValue minValue(ExceptionSink *xsink) const
returns the element having the lowest value (determined by calling OP_LOG_LT - the less-than "<" oper...
DLLEXPORT QoreValue maxValue(ExceptionSink *xsink) const
returns the element having the highest value (determined by calling OP_LOG_GT - the greater-than ">" ...
DLLEXPORT QoreValueList * listRefSelf() const
returns "this" with an incremented reference count
DLLLOCAL const QoreValueList * operator*() const
returns a pointer to the QoreValueList object being managed
Definition: QoreValueList.h:727
virtual DLLLOCAL double floatEvalImpl(ExceptionSink *xsink) const
always returns 0.0
DLLEXPORT void ref() const
increments the reference count
For use on the stack only: iterates through elements of a const QoreValueList.
Definition: QoreValueList.h:546
DLLLOCAL QoreValueList * getReferencedValue()
returns a referenced value - the caller will own the reference
Definition: QoreValueList.h:699
DLLEXPORT QoreValueList * reverse() const
returns a list with the order of the elements reversed
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:43
DLLEXPORT QoreValueList * copy() const
performs a deep copy of the list and returns the new list
DLLLOCAL bool valid() const
returns true if the iterator is pointing to a valid element
Definition: QoreValueList.h:616
virtual DLLEXPORT const char * getTypeName() const
returns the type name as a c string
DLLEXPORT void merge(const QoreValueList *list)
appends the elements of "list" to this list
DLLEXPORT void deref(ExceptionSink *xsink)
decrements the reference count and calls derefImpl() if there_can_be_only_one is false, otherwise does nothing
virtual DLLEXPORT ~QoreValueList()
the destructor is protected so it cannot be called directly
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:225
DLLLOCAL void assign(bool n_needs_deref, QoreValueList *n_val)
assigns a new value and dereference flag to this object, dereferences the old object if necessary ...
Definition: QoreValueList.h:683
DLLEXPORT QoreValueList * extract(ptrdiff_t offset, ExceptionSink *xsink)
truncates the list at position "offset" (first element is offset 0) and returns any elements removed ...
DLLEXPORT QoreValue getReferencedValue() const
returns the current value with an incremented reference count
DLLEXPORT size_t size() const
returns the number of elements in the list
DLLLOCAL bool is_unique() const
returns true if the reference count is 1
Definition: QoreReferenceCounter.h:69
virtual DLLEXPORT AbstractQoreNode * evalImpl(ExceptionSink *xsink) const
evaluates the list and returns a value (or 0)
base class for resolved call references
Definition: CallReferenceNode.h:130
DLLEXPORT QoreValue * getValueReference() const
returns a pointer to a pointer of the value of the list element, so it can be changed externally ...
DLLEXPORT QoreValue retrieveEntry(size_t index)
returns the element at "index" (first element is index 0)
DLLLOCAL QoreValueListEvalOptionalRefHolder(ExceptionSink *n_xsink)
initializes an empty object and saves the ExceptionSink object
Definition: QoreValueList.h:654
DLLLOCAL void discard()
clears the object (dereferences the old object if necessary)
Definition: QoreValueList.h:670
DLLEXPORT void reset()
resets the iterator to its initial state
virtual DLLEXPORT bool derefImpl(ExceptionSink *xsink)
dereferences all elements of the list
DLLEXPORT bool last() const
returns true when the iterator is pointing to the last element in the list
DLLEXPORT QoreValueList * sortDescendingStable(ExceptionSink *xsink) const
returns a new list based on executing mergesort on the source list ("this") in descending order ...
DLLEXPORT QoreValue & getEntryReference(size_t index)
DLLLOCAL size_t max() const
returns the number of elements in the list
Definition: QoreValueList.h:510
DLLLOCAL const QoreValueList * operator->() const
returns a pointer to the QoreValueList object being managed
Definition: QoreValueList.h:724
DLLEXPORT QoreValue getReferencedValue() const
returns the current value with an incremented reference count
DLLEXPORT bool next()
moves the iterator to the next element, returns true if the iterator is pointing to an element of the...
DLLLOCAL size_t max() const
returns the number of elements in the list
Definition: QoreValueList.h:605
DLLEXPORT bool next()
moves the iterator to the next element, returns true if the iterator is pointing to an element of the...
virtual DLLLOCAL int integerEvalImpl(ExceptionSink *xsink) const
always returns 0
DLLLOCAL void assignEval(const QoreValueList *exp)
assigns a new value by executing the given list and dereference flag to this object, dereferences the old object if necessary
Definition: QoreValueList.h:677
a templated class to manage a reference count of an object that can throw a Qore-language exception w...
Definition: ReferenceHolder.h:51
DLLEXPORT QoreValue pop()
returns the last element of the list, the length is decremented by one, caller owns the reference ...
DLLEXPORT QoreValueList * sortStable(ExceptionSink *xsink) const
returns a new list based on executing mergesort on the source list ("this")