All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
QoreListNode.h
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
QoreListNode.h
Qore Programming Language
Copyright 2003 - 2013 David Nichols
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License aqore_offset_t with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _QORE_QORELISTNODE_H
#define _QORE_QORELISTNODE_H
#include <qore/AbstractQoreNode.h>
#ifdef min
#undef min
#endif
#ifdef max
#undef max
#endif
class ResolvedCallReferenceNode;
class QoreListNode : public AbstractQoreNode {
friend class StackList;
private:
DLLLOCAL QoreListNode(const QoreListNode&);
DLLLOCAL QoreListNode& operator=(const QoreListNode&);
protected:
struct qore_list_private *priv;
DLLLOCAL void resize(qore_size_t num);
DLLLOCAL QoreListNode* splice_intern(qore_size_t offset, qore_size_t length, ExceptionSink* xsink, bool extract = false);
DLLLOCAL QoreListNode* splice_intern(qore_size_t offset, qore_size_t length, const AbstractQoreNode* l, ExceptionSink* xsink, bool extract = false);
DLLLOCAL qore_size_t check_offset(qore_offset_t offset);
DLLLOCAL void check_offset(qore_offset_t offset, qore_offset_t len, qore_size_t &n_offset, qore_size_t &n_len);
DLLLOCAL int qsort(const ResolvedCallReferenceNode* fr, qore_size_t left, qore_size_t right, bool ascending, ExceptionSink* xsink);
DLLLOCAL int mergesort(const ResolvedCallReferenceNode* fr, bool ascending, ExceptionSink* xsink);
DLLLOCAL QoreListNode* eval_intern(ExceptionSink* xsink) const;
DLLEXPORT virtual ~QoreListNode();
DLLEXPORT virtual bool derefImpl(ExceptionSink* xsink);
DLLEXPORT virtual AbstractQoreNode* evalImpl(ExceptionSink* xsink) const;
DLLLOCAL virtual AbstractQoreNode* evalImpl(bool &needs_deref, ExceptionSink* xsink) const;
DLLLOCAL virtual int64 bigIntEvalImpl(ExceptionSink* xsink) const;
DLLLOCAL virtual int integerEvalImpl(ExceptionSink* xsink) const;
DLLLOCAL virtual bool boolEvalImpl(ExceptionSink* xsink) const;
DLLLOCAL virtual double floatEvalImpl(ExceptionSink* xsink) const;
public:
DLLEXPORT QoreListNode();
DLLEXPORT virtual bool getAsBoolImpl() const;
DLLEXPORT int getAsString(QoreString &str, int foff, ExceptionSink* xsink) const;
DLLEXPORT QoreString *getAsString(bool &del, int foff, ExceptionSink* xsink) const;
//DLLEXPORT virtual bool needs_eval() const;
DLLEXPORT virtual AbstractQoreNode* realCopy() const;
DLLEXPORT virtual bool is_equal_soft(const AbstractQoreNode* v, ExceptionSink* xsink) const;
DLLEXPORT virtual bool is_equal_hard(const AbstractQoreNode* v, ExceptionSink* xsink) const;
DLLEXPORT virtual const char *getTypeName() const;
//DLLEXPORT virtual bool is_value() const;
DLLLOCAL static const char *getStaticTypeName() {
return "list";
}
DLLEXPORT AbstractQoreNode* retrieve_entry(qore_size_t index);
DLLEXPORT const AbstractQoreNode* retrieve_entry(qore_size_t index) const;
DLLEXPORT AbstractQoreNode* get_referenced_entry(qore_size_t index) const;
DLLEXPORT int getEntryAsInt(qore_size_t index) const;
DLLEXPORT AbstractQoreNode** get_entry_ptr(qore_size_t index);
DLLEXPORT AbstractQoreNode** getExistingEntryPtr(qore_size_t index);
DLLEXPORT void set_entry(qore_size_t index, AbstractQoreNode* val, ExceptionSink* xsink);
DLLEXPORT void push(AbstractQoreNode* val);
DLLEXPORT void insert(AbstractQoreNode* val);
DLLEXPORT AbstractQoreNode* pop();
DLLEXPORT AbstractQoreNode* shift();
DLLEXPORT void merge(const QoreListNode* list);
DLLEXPORT int delete_entry(qore_size_t index, ExceptionSink* xsink);
DLLEXPORT void pop_entry(qore_size_t index, ExceptionSink* xsink);
DLLEXPORT QoreListNode* evalList(ExceptionSink* xsink) const;
DLLEXPORT QoreListNode* evalList(bool &needs_deref, ExceptionSink* xsink) const;
DLLEXPORT QoreListNode* copy() const;
DLLEXPORT QoreListNode* copyListFrom(qore_size_t index) const;
DLLEXPORT QoreListNode* sort() const;
DLLEXPORT QoreListNode* sort(const ResolvedCallReferenceNode* fr, ExceptionSink* xsink) const;
DLLEXPORT QoreListNode* sortStable() const;
DLLEXPORT QoreListNode* sortStable(const ResolvedCallReferenceNode* fr, ExceptionSink* xsink) const;
DLLEXPORT QoreListNode* sortDescending() const;
DLLEXPORT QoreListNode* sortDescending(const ResolvedCallReferenceNode* fr, ExceptionSink* xsink) const;
DLLEXPORT QoreListNode* sortDescendingStable() const;
DLLEXPORT QoreListNode* sortDescendingStable(const ResolvedCallReferenceNode* fr, ExceptionSink* xsink) const;
DLLEXPORT AbstractQoreNode* min() const;
DLLEXPORT AbstractQoreNode* max() const;
DLLEXPORT AbstractQoreNode* min(const ResolvedCallReferenceNode* fr, ExceptionSink* xsink) const;
DLLEXPORT AbstractQoreNode* max(const ResolvedCallReferenceNode* fr, ExceptionSink* xsink) const;
DLLEXPORT void splice(qore_offset_t offset, ExceptionSink* xsink);
DLLEXPORT void splice(qore_offset_t offset, qore_offset_t length, ExceptionSink* xsink);
DLLEXPORT void splice(qore_offset_t offset, qore_offset_t length, const AbstractQoreNode* l, ExceptionSink* xsink);
DLLEXPORT QoreListNode* extract(qore_offset_t offset, ExceptionSink* xsink);
DLLEXPORT QoreListNode* extract(qore_offset_t offset, qore_offset_t length, ExceptionSink* xsink);
DLLEXPORT QoreListNode* extract(qore_offset_t offset, qore_offset_t length, const AbstractQoreNode* l, ExceptionSink* xsink);
DLLEXPORT qore_size_t size() const;
DLLEXPORT bool empty() const;
DLLEXPORT QoreListNode* reverse() const;
DLLEXPORT QoreListNode* listRefSelf() const;
DLLEXPORT AbstractQoreNode* swap(qore_offset_t offset, AbstractQoreNode* val);
DLLEXPORT AbstractQoreNode* takeExists(qore_offset_t offset);
// needed only while parsing
DLLLOCAL QoreListNode(bool i);
DLLLOCAL bool isFinalized() const;
DLLLOCAL void setFinalized();
DLLLOCAL bool isVariableList() const;
DLLLOCAL void setVariableList();
DLLLOCAL void clearNeedsEval();
DLLLOCAL void setNeedsEval();
DLLLOCAL void clear();
DLLLOCAL AbstractQoreNode* eval_entry(qore_size_t num, ExceptionSink* xsink) const;
DLLLOCAL virtual AbstractQoreNode* parseInit(LocalVar *oflag, int pflag, int &lvids, const QoreTypeInfo *&typeInfo);
DLLLOCAL QoreListNode* parseInitList(LocalVar *oflag, int pflag, int &lvids, const QoreTypeInfo *&typeInfo);
};
#include <qore/ReferenceHolder.h>
typedef ReferenceHolder<QoreListNode> QoreListNodeHolder;
class ListIterator {
protected:
QoreListNode* l;
qore_offset_t pos;
DLLLOCAL void *operator new(size_t);
public:
DLLEXPORT ListIterator(QoreListNode* lst, qore_size_t n_pos = -1);
DLLEXPORT ListIterator(QoreListNode& lst, qore_size_t n_pos = -1);
DLLEXPORT bool next();
DLLEXPORT bool prev();
DLLEXPORT int set(qore_size_t n_pos);
DLLEXPORT AbstractQoreNode* getValue() const;
DLLEXPORT AbstractQoreNode** getValuePtr() const;
DLLEXPORT AbstractQoreNode* getReferencedValue() const;
DLLEXPORT AbstractQoreNode* takeValue();
DLLEXPORT bool first() const;
DLLEXPORT bool last() const;
//DLLEXPORT void setValue(AbstractQoreNode* val, ExceptionSink* xsink) const;
DLLLOCAL qore_size_t index() const { return pos; }
DLLLOCAL qore_size_t max() const { return l->size(); }
DLLLOCAL QoreListNode* getList() { return l; }
DLLLOCAL bool empty() const {
return l->empty();
}
DLLLOCAL bool valid() const {
return pos == -1 ? false : true;
}
};
class ConstListIterator {
protected:
const QoreListNode* l;
qore_offset_t pos;
public:
DLLEXPORT ConstListIterator(const QoreListNode* lst, qore_size_t n_pos = -1);
DLLEXPORT ConstListIterator(const QoreListNode& lst, qore_size_t n_pos = -1);
DLLEXPORT bool next();
DLLEXPORT bool prev();
DLLEXPORT int set(qore_size_t n_pos);
DLLEXPORT const AbstractQoreNode* getValue() const;
DLLEXPORT AbstractQoreNode* getReferencedValue() const;
DLLEXPORT bool first() const;
DLLEXPORT bool last() const;
DLLLOCAL qore_size_t index() const { return pos; }
DLLLOCAL qore_size_t max() const { return l->size(); }
DLLLOCAL const QoreListNode* getList() const { return l; }
DLLLOCAL bool empty() const {
return l->empty();
}
DLLLOCAL bool valid() const {
return pos == -1 ? false : true;
}
DLLEXPORT void reset();
};
class QoreListNodeEvalOptionalRefHolder {
private:
QoreListNode* val;
ExceptionSink* xsink;
bool needs_deref;
DLLLOCAL void discard_intern() {
if (needs_deref && val)
val->deref(xsink);
}
DLLLOCAL void eval_intern(const QoreListNode* exp) {
if (exp)
val = exp->evalList(needs_deref, xsink);
else {
val = 0;
needs_deref = false;
}
}
DLLLOCAL QoreListNodeEvalOptionalRefHolder(const QoreListNodeEvalOptionalRefHolder&);
DLLLOCAL QoreListNodeEvalOptionalRefHolder& operator=(const QoreListNodeEvalOptionalRefHolder&);
DLLLOCAL void *operator new(size_t);
public:
DLLLOCAL QoreListNodeEvalOptionalRefHolder(ExceptionSink* n_xsink) : xsink(n_xsink) {
needs_deref = false;
val = 0;
}
DLLLOCAL QoreListNodeEvalOptionalRefHolder(const QoreListNode* exp, ExceptionSink* n_xsink) : xsink(n_xsink) {
eval_intern(exp);
}
DLLLOCAL ~QoreListNodeEvalOptionalRefHolder() {
discard_intern();
}
DLLLOCAL void discard() {
discard_intern();
needs_deref = false;
val = 0;
}
DLLLOCAL void assignEval(const QoreListNode* exp) {
discard_intern();
eval_intern(exp);
}
DLLLOCAL void assign(bool n_needs_deref, QoreListNode* n_val) {
discard_intern();
needs_deref = n_needs_deref;
val = n_val;
}
DLLLOCAL bool needsDeref() const {
return needs_deref;
}
DLLLOCAL QoreListNode* getReferencedValue() {
if (needs_deref)
needs_deref = false;
else if (val)
val->ref();
return val;
}
DLLLOCAL void edit() {
if (!val) {
val = new QoreListNode;
needs_deref = true;
}
else if (!needs_deref || !val->is_unique()) {
val = val->copy();
needs_deref = true;
}
}
DLLLOCAL const QoreListNode* operator->() const { return val; }
DLLLOCAL const QoreListNode* operator*() const { return val; }
DLLLOCAL operator bool() const { return val != 0; }
};
#endif