|
Qore Programming Language
0.8.3
|
00001 /* -*- mode: c++; indent-tabs-mode: nil -*- */ 00002 /* 00003 params.h 00004 00005 Qore Programming Language 00006 00007 Copyright 2003 - 2011 David Nichols 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00022 */ 00023 00024 #ifndef _QORE_PARAMS_H 00025 00026 #define _QORE_PARAMS_H 00027 00028 #include <qore/AbstractQoreNode.h> 00029 00034 00035 00038 static inline unsigned num_args(const QoreListNode *n) { 00039 return n ? (unsigned)n->size() : 0; 00040 } 00041 00043 00046 static inline unsigned num_params(const QoreListNode *n) { 00047 return n ? (unsigned)n->size() : 0; 00048 } 00049 00051 00056 static inline const AbstractQoreNode *get_param(const QoreListNode *n, qore_size_t i) { 00057 if (!n) return 0; 00058 const AbstractQoreNode *p = n->retrieve_entry(i); 00059 return is_nothing(p) ? 0 : p; 00060 } 00061 00063 00068 static inline qore_type_t get_param_type(const QoreListNode *n, qore_size_t i) { 00069 if (!n) return NT_NOTHING; 00070 const AbstractQoreNode *p = n->retrieve_entry(i); 00071 return p ? p->getType() : NT_NOTHING; 00072 } 00073 00075 static inline int get_int_param(const QoreListNode *n, qore_size_t i) { 00076 if (!n) return 0; 00077 const AbstractQoreNode *p = n->retrieve_entry(i); 00078 return is_nothing(p) ? 0 : p->getAsInt(); 00079 } 00080 00082 static inline int64 get_bigint_param(const QoreListNode *n, qore_size_t i) { 00083 if (!n) return 0; 00084 const AbstractQoreNode *p = n->retrieve_entry(i); 00085 return is_nothing(p) ? 0 : p->getAsBigInt(); 00086 } 00087 00089 static inline int get_int_param_with_default(const QoreListNode *n, qore_size_t i, int def) { 00090 if (!n) return def; 00091 const AbstractQoreNode *p = n->retrieve_entry(i); 00092 return is_nothing(p) ? def : p->getAsInt(); 00093 } 00094 00096 static inline int64 get_bigint_param_with_default(const QoreListNode *n, qore_size_t i, int64 def) { 00097 if (!n) return def; 00098 const AbstractQoreNode *p = n->retrieve_entry(i); 00099 return is_nothing(p) ? def : p->getAsBigInt(); 00100 } 00101 00103 static inline double get_float_param(const QoreListNode *n, qore_size_t i) { 00104 if (!n) return 0; 00105 const AbstractQoreNode *p = n->retrieve_entry(i); 00106 return is_nothing(p) ? 0 : p->getAsFloat(); 00107 } 00108 00110 static inline bool get_bool_param(const QoreListNode *n, qore_size_t i) { 00111 if (!n) return 0; 00112 const AbstractQoreNode *p = n->retrieve_entry(i); 00113 return is_nothing(p) ? false : p->getAsBool(); 00114 } 00115 00117 00122 static inline const BinaryNode *test_binary_param(const QoreListNode *n, qore_size_t i) { 00123 if (!n) return 0; 00124 const AbstractQoreNode *p = n->retrieve_entry(i); 00125 // the following is faster than a dynamic_cast 00126 return p && p->getType() == NT_BINARY ? reinterpret_cast<const BinaryNode *>(p) : 0; 00127 } 00128 00130 00135 static inline const QoreStringNode *test_string_param(const QoreListNode *n, qore_size_t i) { 00136 if (!n) return 0; 00137 const AbstractQoreNode *p = n->retrieve_entry(i); 00138 // the following is faster than a dynamic_cast 00139 return p && p->getType() == NT_STRING ? reinterpret_cast<const QoreStringNode *>(p) : 0; 00140 } 00141 00143 00148 static inline QoreObject *test_object_param(const QoreListNode *n, qore_size_t i) { 00149 if (!n) return 0; 00150 const AbstractQoreNode *p = n->retrieve_entry(i); 00151 // the following is faster than a dynamic_cast 00152 return p && p->getType() == NT_OBJECT ? const_cast<QoreObject *>(reinterpret_cast<const QoreObject *>(p)) : 0; 00153 } 00154 00156 00161 static inline const DateTimeNode *test_date_param(const QoreListNode *n, qore_size_t i) { 00162 if (!n) return 0; 00163 const AbstractQoreNode *p = n->retrieve_entry(i); 00164 // the following is faster than a dynamic_cast 00165 return p && p->getType() == NT_DATE ? reinterpret_cast<const DateTimeNode *>(p) : 0; 00166 } 00167 00169 00174 static inline const QoreHashNode *test_hash_param(const QoreListNode *n, qore_size_t i) { 00175 if (!n) return 0; 00176 const AbstractQoreNode *p = n->retrieve_entry(i); 00177 // the following is faster than a dynamic_cast 00178 return p && p->getType() == NT_HASH ? reinterpret_cast<const QoreHashNode *>(p) : 0; 00179 } 00180 00182 00187 static inline const QoreListNode *test_list_param(const QoreListNode *n, qore_size_t i) { 00188 if (!n) return 0; 00189 const AbstractQoreNode *p = n->retrieve_entry(i); 00190 // the following is faster than a dynamic_cast 00191 return p && p->getType() == NT_LIST ? reinterpret_cast<const QoreListNode *>(p) : 0; 00192 } 00193 00195 00200 static inline const ResolvedCallReferenceNode *test_callref_param(const QoreListNode *n, qore_size_t i) { 00201 if (!n) return 0; 00202 const AbstractQoreNode *p = n->retrieve_entry(i); 00203 // the following is faster than a dynamic_cast 00204 return p && (p->getType() == NT_FUNCREF || p->getType() == NT_RUNTIME_CLOSURE) ? reinterpret_cast<const ResolvedCallReferenceNode *>(p) : 0; 00205 } 00206 00208 00213 static inline const ResolvedCallReferenceNode *test_funcref_param(const QoreListNode *n, qore_size_t i) { 00214 return test_callref_param(n, i); 00215 } 00216 00218 00224 static inline const ReferenceNode *test_reference_param(const QoreListNode *n, qore_size_t i) { 00225 if (!n) return 0; 00226 const AbstractQoreNode *p = n->retrieve_entry(i); 00227 // the following is faster than a dynamic_cast 00228 return p && p->getType() == NT_REFERENCE ? reinterpret_cast<const ReferenceNode *>(p) : 0; 00229 } 00230 00232 00237 static inline bool test_nothing_param(const QoreListNode *n, qore_size_t i) { 00238 if (!n) return true; 00239 return is_nothing(n->retrieve_entry(i)); 00240 } 00241 00243 static inline const QoreEncoding *get_encoding_param(const QoreListNode *n, qore_size_t i, const QoreEncoding *def = QCS_DEFAULT) { 00244 const QoreStringNode *str = test_string_param(n, i); 00245 return str ? QEM.findCreate(str) : def; 00246 } 00247 00249 template <typename T> 00250 static inline T *get_hard_param(const QoreListNode *n, qore_size_t i) { 00251 assert(n); 00252 assert(dynamic_cast<T *>(n->retrieve_entry(i))); 00253 return reinterpret_cast<T *>(n->retrieve_entry(i)); 00254 } 00255 00256 static inline void HARD_QORE_DATA(const QoreListNode *n, qore_size_t i, const void *&ptr, qore_size_t &len) { 00257 const AbstractQoreNode *p = get_hard_param<const AbstractQoreNode>(n, i); 00258 if (p->getType() == NT_STRING) { 00259 const QoreStringNode *str = reinterpret_cast<const QoreStringNode *>(p); 00260 ptr = (const void *)str->getBuffer(); 00261 len = str->size(); 00262 return; 00263 } 00264 const BinaryNode *b = reinterpret_cast<const BinaryNode *>(p); 00265 ptr = b->getPtr(); 00266 len = b->size(); 00267 } 00268 00270 #define HARD_QORE_PARAM(name, Type, list, i) Type *name = get_hard_param<Type>(list, i) 00271 00273 #define HARD_QORE_INT(list, i) get_hard_param<const QoreBigIntNode>(list, i)->val 00274 00276 #define HARD_QORE_FLOAT(list, i) get_hard_param<const QoreFloatNode>(list, i)->f 00277 00279 #define HARD_QORE_BOOL(list, i) get_hard_param<const QoreBoolNode>(list, i)->getValue() 00280 00282 #define HARD_QORE_STRING(list, i) get_hard_param<const QoreStringNode>(list, i) 00283 00285 #define HARD_QORE_DATE(list, i) get_hard_param<const DateTimeNode>(list, i) 00286 00288 #define HARD_QORE_BINARY(list, i) get_hard_param<const BinaryNode>(list, i) 00289 00291 #define HARD_QORE_LIST(list, i) get_hard_param<const QoreListNode>(list, i) 00292 00294 #define HARD_QORE_HASH(list, i) get_hard_param<const QoreHashNode>(list, i) 00295 00297 #define HARD_QORE_REF(list, i) get_hard_param<const ReferenceNode>(list, i) 00298 00300 #define HARD_QORE_OBJECT(list, i) const_cast<QoreObject *>(get_hard_param<const QoreObject>(list, i)) 00301 00302 // sets up an object pointer 00303 #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) 00304 00306 static inline const QoreEncoding *get_hard_qore_encoding_param(const QoreListNode *n, qore_size_t i) { 00307 HARD_QORE_PARAM(str, const QoreStringNode, n, i); 00308 return QEM.findCreate(str); 00309 } 00310 00311 #endif