All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
params.h
Go to the documentation of this file.
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
params.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 along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _QORE_PARAMS_H
#define _QORE_PARAMS_H
#include <qore/AbstractQoreNode.h>
static inline unsigned num_args(const QoreListNode *n) {
return n ? (unsigned)n->size() : 0;
}
static inline unsigned num_params(const QoreListNode *n) {
return n ? (unsigned)n->size() : 0;
}
static inline const AbstractQoreNode *get_param(const QoreListNode *n, qore_size_t i) {
if (!n) return 0;
const AbstractQoreNode *p = n->retrieve_entry(i);
return is_nothing(p) ? 0 : p;
}
static inline qore_type_t get_param_type(const QoreListNode *n, qore_size_t i) {
if (!n) return NT_NOTHING;
const AbstractQoreNode *p = n->retrieve_entry(i);
return p ? p->getType() : NT_NOTHING;
}
static inline int get_int_param(const QoreListNode *n, qore_size_t i) {
if (!n) return 0;
const AbstractQoreNode *p = n->retrieve_entry(i);
return is_nothing(p) ? 0 : p->getAsInt();
}
static inline int64 get_bigint_param(const QoreListNode *n, qore_size_t i) {
if (!n) return 0;
const AbstractQoreNode *p = n->retrieve_entry(i);
return is_nothing(p) ? 0 : p->getAsBigInt();
}
static inline int get_int_param_with_default(const QoreListNode *n, qore_size_t i, int def) {
if (!n) return def;
const AbstractQoreNode *p = n->retrieve_entry(i);
return is_nothing(p) ? def : p->getAsInt();
}
static inline int64 get_bigint_param_with_default(const QoreListNode *n, qore_size_t i, int64 def) {
if (!n) return def;
const AbstractQoreNode *p = n->retrieve_entry(i);
return is_nothing(p) ? def : p->getAsBigInt();
}
static inline double get_float_param(const QoreListNode *n, qore_size_t i) {
if (!n) return 0;
const AbstractQoreNode *p = n->retrieve_entry(i);
return is_nothing(p) ? 0 : p->getAsFloat();
}
static inline bool get_bool_param(const QoreListNode *n, qore_size_t i) {
if (!n) return 0;
const AbstractQoreNode *p = n->retrieve_entry(i);
return is_nothing(p) ? false : p->getAsBool();
}
static inline const BinaryNode *test_binary_param(const QoreListNode *n, qore_size_t i) {
if (!n) return 0;
const AbstractQoreNode *p = n->retrieve_entry(i);
// the following is faster than a dynamic_cast
return p && p->getType() == NT_BINARY ? reinterpret_cast<const BinaryNode *>(p) : 0;
}
static inline const QoreStringNode *test_string_param(const QoreListNode *n, qore_size_t i) {
if (!n) return 0;
const AbstractQoreNode *p = n->retrieve_entry(i);
// the following is faster than a dynamic_cast
return p && p->getType() == NT_STRING ? reinterpret_cast<const QoreStringNode *>(p) : 0;
}
static inline QoreObject *test_object_param(const QoreListNode *n, qore_size_t i) {
if (!n) return 0;
const AbstractQoreNode *p = n->retrieve_entry(i);
// the following is faster than a dynamic_cast
return p && p->getType() == NT_OBJECT ? const_cast< QoreObject *>(reinterpret_cast<const QoreObject *>(p)) : 0;
}
static inline const DateTimeNode *test_date_param(const QoreListNode *n, qore_size_t i) {
if (!n) return 0;
const AbstractQoreNode *p = n->retrieve_entry(i);
// the following is faster than a dynamic_cast
return p && p->getType() == NT_DATE ? reinterpret_cast<const DateTimeNode *>(p) : 0;
}
static inline const QoreHashNode *test_hash_param(const QoreListNode *n, qore_size_t i) {
if (!n) return 0;
const AbstractQoreNode *p = n->retrieve_entry(i);
// the following is faster than a dynamic_cast
return p && p->getType() == NT_HASH ? reinterpret_cast<const QoreHashNode *>(p) : 0;
}
static inline const QoreListNode *test_list_param(const QoreListNode *n, qore_size_t i) {
if (!n) return 0;
const AbstractQoreNode *p = n->retrieve_entry(i);
// the following is faster than a dynamic_cast
return p && p->getType() == NT_LIST ? reinterpret_cast<const QoreListNode *>(p) : 0;
}
static inline const ResolvedCallReferenceNode *test_callref_param(const QoreListNode *n, qore_size_t i) {
if (!n) return 0;
const AbstractQoreNode *p = n->retrieve_entry(i);
// the following is faster than a dynamic_cast
return p && (p->getType() == NT_FUNCREF || p->getType() == NT_RUNTIME_CLOSURE) ? reinterpret_cast<const ResolvedCallReferenceNode *>(p) : 0;
}
static inline const ResolvedCallReferenceNode *test_funcref_param(const QoreListNode *n, qore_size_t i) {
return test_callref_param(n, i);
}
static inline const ReferenceNode *test_reference_param(const QoreListNode *n, qore_size_t i) {
if (!n) return 0;
const AbstractQoreNode *p = n->retrieve_entry(i);
// the following is faster than a dynamic_cast
return p && p->getType() == NT_REFERENCE ? reinterpret_cast<const ReferenceNode *>(p) : 0;
}
static inline bool test_nothing_param(const QoreListNode *n, qore_size_t i) {
if (!n) return true;
return is_nothing(n->retrieve_entry(i));
}
static inline const QoreEncoding *get_encoding_param(const QoreListNode *n, qore_size_t i, const QoreEncoding *def = QCS_DEFAULT) {
const QoreStringNode *str = test_string_param(n, i);
return str ? QEM.findCreate(str) : def;
}
template <typename T>
static inline T *get_hard_param(const QoreListNode *n, qore_size_t i) {
assert(n);
assert(dynamic_cast<T *>(n->retrieve_entry(i)));
return reinterpret_cast<T *>(n->retrieve_entry(i));
}
static inline void HARD_QORE_DATA(const QoreListNode *n, qore_size_t i, const void *&ptr, qore_size_t &len) {
const AbstractQoreNode *p = get_hard_param<const AbstractQoreNode>(n, i);
if (p->getType() == NT_STRING) {
const QoreStringNode *str = reinterpret_cast<const QoreStringNode *>(p);
ptr = (const void *)str->getBuffer();
len = str->size();
return;
}
const BinaryNode *b = reinterpret_cast<const BinaryNode *>(p);
ptr = b->getPtr();
len = b->size();
}
#define HARD_QORE_PARAM(name, Type, list, i) Type *name = get_hard_param<Type>(list, i)
#define HARD_QORE_INT(list, i) get_hard_param<const QoreBigIntNode>(list, i)->val
#define HARD_QORE_FLOAT(list, i) get_hard_param<const QoreFloatNode>(list, i)->f
#define HARD_QORE_NUMBER(list, i) get_hard_param<const QoreNumberNode>(list, i)
#define HARD_QORE_BOOL(list, i) get_hard_param<const QoreBoolNode>(list, i)->getValue()
#define HARD_QORE_STRING(list, i) get_hard_param<const QoreStringNode>(list, i)
#define HARD_QORE_DATE(list, i) get_hard_param<const DateTimeNode>(list, i)
#define HARD_QORE_BINARY(list, i) get_hard_param<const BinaryNode>(list, i)
#define HARD_QORE_LIST(list, i) get_hard_param<const QoreListNode>(list, i)
#define HARD_QORE_HASH(list, i) get_hard_param<const QoreHashNode>(list, i)
#define HARD_QORE_REF(list, i) get_hard_param<const ReferenceNode>(list, i)
#define HARD_QORE_OBJECT(list, i) const_cast<QoreObject *>(get_hard_param<const QoreObject>(list, i))
// sets up an object pointer
#define HARD_QORE_OBJ_DATA(vname, Type, list, i, cid, dname, cname, xsink) HARD_QORE_PARAM(obj_##vname, const QoreObject, list, i); Type *vname = reinterpret_cast<Type *>(obj_##vname->getReferencedPrivateData(cid, xsink)); if (!vname && !*xsink) xsink->raiseException("OBJECT-ALREADY-DELETED", "cannot complete call setup to %s() because parameter %d (<class %s>) has already been deleted", cname, i + 1, dname)
static inline const QoreEncoding *get_hard_qore_encoding_param(const QoreListNode *n, qore_size_t i) {
HARD_QORE_PARAM(str, const QoreStringNode, n, i);
return QEM.findCreate(str);
}
#endif