Qore Programming Language  0.8.11.1
params.h
Go to the documentation of this file.
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  params.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2014 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_PARAMS_H
33 
34 #define _QORE_PARAMS_H
35 
36 #include <qore/AbstractQoreNode.h>
37 
42 
46 static inline unsigned num_args(const QoreListNode *n) {
47  return n ? (unsigned)n->size() : 0;
48 }
49 
51 
54 static inline unsigned num_params(const QoreListNode *n) {
55  return n ? (unsigned)n->size() : 0;
56 }
57 
59 
64 static inline const AbstractQoreNode *get_param(const QoreListNode *n, qore_size_t i) {
65  if (!n) return 0;
66  const AbstractQoreNode *p = n->retrieve_entry(i);
67  return is_nothing(p) ? 0 : p;
68 }
69 
71 
76 static inline qore_type_t get_param_type(const QoreListNode *n, qore_size_t i) {
77  if (!n) return NT_NOTHING;
78  const AbstractQoreNode *p = n->retrieve_entry(i);
79  return p ? p->getType() : NT_NOTHING;
80 }
81 
83 static inline int get_int_param(const QoreListNode *n, qore_size_t i) {
84  if (!n) return 0;
85  const AbstractQoreNode *p = n->retrieve_entry(i);
86  return is_nothing(p) ? 0 : p->getAsInt();
87 }
88 
90 static inline int64 get_bigint_param(const QoreListNode *n, qore_size_t i) {
91  if (!n) return 0;
92  const AbstractQoreNode *p = n->retrieve_entry(i);
93  return is_nothing(p) ? 0 : p->getAsBigInt();
94 }
95 
97 static inline int get_int_param_with_default(const QoreListNode *n, qore_size_t i, int def) {
98  if (!n) return def;
99  const AbstractQoreNode *p = n->retrieve_entry(i);
100  return is_nothing(p) ? def : p->getAsInt();
101 }
102 
104 static inline int64 get_bigint_param_with_default(const QoreListNode *n, qore_size_t i, int64 def) {
105  if (!n) return def;
106  const AbstractQoreNode *p = n->retrieve_entry(i);
107  return is_nothing(p) ? def : p->getAsBigInt();
108 }
109 
111 static inline double get_float_param(const QoreListNode *n, qore_size_t i) {
112  if (!n) return 0;
113  const AbstractQoreNode *p = n->retrieve_entry(i);
114  return is_nothing(p) ? 0 : p->getAsFloat();
115 }
116 
118 static inline bool get_bool_param(const QoreListNode *n, qore_size_t i) {
119  if (!n) return 0;
120  const AbstractQoreNode *p = n->retrieve_entry(i);
121  return is_nothing(p) ? false : p->getAsBool();
122 }
123 
125 
130 static inline const BinaryNode *test_binary_param(const QoreListNode *n, qore_size_t i) {
131  if (!n) return 0;
132  const AbstractQoreNode *p = n->retrieve_entry(i);
133  // the following is faster than a dynamic_cast
134  return p && p->getType() == NT_BINARY ? reinterpret_cast<const BinaryNode *>(p) : 0;
135 }
136 
138 
143 static inline const QoreStringNode *test_string_param(const QoreListNode *n, qore_size_t i) {
144  if (!n) return 0;
145  const AbstractQoreNode *p = n->retrieve_entry(i);
146  // the following is faster than a dynamic_cast
147  return p && p->getType() == NT_STRING ? reinterpret_cast<const QoreStringNode *>(p) : 0;
148 }
149 
151 
157  if (!n) return 0;
158  const AbstractQoreNode *p = n->retrieve_entry(i);
159  // the following is faster than a dynamic_cast
160  return p && p->getType() == NT_OBJECT ? const_cast<QoreObject *>(reinterpret_cast<const QoreObject *>(p)) : 0;
161 }
162 
164 
169 static inline const DateTimeNode *test_date_param(const QoreListNode *n, qore_size_t i) {
170  if (!n) return 0;
171  const AbstractQoreNode *p = n->retrieve_entry(i);
172  // the following is faster than a dynamic_cast
173  return p && p->getType() == NT_DATE ? reinterpret_cast<const DateTimeNode *>(p) : 0;
174 }
175 
177 
182 static inline const QoreHashNode *test_hash_param(const QoreListNode *n, qore_size_t i) {
183  if (!n) return 0;
184  const AbstractQoreNode *p = n->retrieve_entry(i);
185  // the following is faster than a dynamic_cast
186  return p && p->getType() == NT_HASH ? reinterpret_cast<const QoreHashNode *>(p) : 0;
187 }
188 
190 
195 static inline const QoreListNode *test_list_param(const QoreListNode *n, qore_size_t i) {
196  if (!n) return 0;
197  const AbstractQoreNode *p = n->retrieve_entry(i);
198  // the following is faster than a dynamic_cast
199  return p && p->getType() == NT_LIST ? reinterpret_cast<const QoreListNode *>(p) : 0;
200 }
201 
203 
209  if (!n) return 0;
210  const AbstractQoreNode *p = n->retrieve_entry(i);
211  // the following is faster than a dynamic_cast
212  return p && (p->getType() == NT_FUNCREF || p->getType() == NT_RUNTIME_CLOSURE) ? reinterpret_cast<const ResolvedCallReferenceNode *>(p) : 0;
213 }
214 
216 
222  return test_callref_param(n, i);
223 }
224 
226 
232 static inline const ReferenceNode *test_reference_param(const QoreListNode *n, qore_size_t i) {
233  if (!n) return 0;
234  const AbstractQoreNode *p = n->retrieve_entry(i);
235  // the following is faster than a dynamic_cast
236  return p && p->getType() == NT_REFERENCE ? reinterpret_cast<const ReferenceNode *>(p) : 0;
237 }
238 
240 
245 static inline bool test_nothing_param(const QoreListNode *n, qore_size_t i) {
246  if (!n) return true;
247  return is_nothing(n->retrieve_entry(i));
248 }
249 
251 static inline const QoreEncoding *get_encoding_param(const QoreListNode *n, qore_size_t i, const QoreEncoding *def = QCS_DEFAULT) {
252  const QoreStringNode *str = test_string_param(n, i);
253  return str ? QEM.findCreate(str) : def;
254 }
255 
257 template <typename T>
258 static inline T *get_hard_or_nothing_param(const QoreListNode *n, qore_size_t i) {
259  assert(n);
260  return reinterpret_cast<T*>(n->retrieve_entry(i));
261 }
262 
264 template <typename T>
265 static inline T *get_hard_param(const QoreListNode *n, qore_size_t i) {
266  assert(n);
267  assert(dynamic_cast<T*>(n->retrieve_entry(i)));
268  return reinterpret_cast<T*>(n->retrieve_entry(i));
269 }
270 
271 static inline void HARD_QORE_DATA(const QoreListNode *n, qore_size_t i, const void *&ptr, qore_size_t &len) {
272  const AbstractQoreNode *p = get_hard_param<const AbstractQoreNode>(n, i);
273  if (p->getType() == NT_STRING) {
274  const QoreStringNode *str = reinterpret_cast<const QoreStringNode *>(p);
275  ptr = (const void *)str->getBuffer();
276  len = str->size();
277  return;
278  }
279  const BinaryNode *b = reinterpret_cast<const BinaryNode *>(p);
280  ptr = b->getPtr();
281  len = b->size();
282 }
283 
285 #define HARD_QORE_OR_NOTHING_PARAM(name, Type, list, i) Type *name = get_hard_or_nothing_param<Type>(list, i)
286 
288 #define HARD_QORE_PARAM(name, Type, list, i) Type *name = get_hard_param<Type>(list, i)
289 
291 #define HARD_QORE_INT(list, i) get_hard_param<const QoreBigIntNode>(list, i)->val
292 
294 #define HARD_QORE_FLOAT(list, i) get_hard_param<const QoreFloatNode>(list, i)->f
295 
297 #define HARD_QORE_NUMBER(list, i) get_hard_param<const QoreNumberNode>(list, i)
298 
300 #define HARD_QORE_BOOL(list, i) get_hard_param<const QoreBoolNode>(list, i)->getValue()
301 
303 #define HARD_QORE_STRING(list, i) get_hard_param<const QoreStringNode>(list, i)
304 
306 #define HARD_QORE_DATE(list, i) get_hard_param<const DateTimeNode>(list, i)
307 
309 #define HARD_QORE_BINARY(list, i) get_hard_param<const BinaryNode>(list, i)
310 
312 #define HARD_QORE_LIST(list, i) get_hard_param<const QoreListNode>(list, i)
313 
315 #define HARD_QORE_HASH(list, i) get_hard_param<const QoreHashNode>(list, i)
316 
318 #define HARD_QORE_REF(list, i) get_hard_param<const ReferenceNode>(list, i)
319 
321 #define HARD_QORE_OBJECT(list, i) const_cast<QoreObject *>(get_hard_param<const QoreObject>(list, i))
322 
323 // sets up an object pointer
324 #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)
325 
326 // sets up an object pointer
327 #define HARD_QORE_OBJ_OR_NOTHING_DATA(vname, Type, list, i, cid, xsink) HARD_QORE_OR_NOTHING_PARAM(obj_##vname, const QoreObject, list, i); Type* vname = obj_##vname ? reinterpret_cast<Type*>(obj_##vname->getReferencedPrivateData(cid, xsink)) : 0;
328 
331  HARD_QORE_PARAM(str, const QoreStringNode, n, i);
332  return QEM.findCreate(str);
333 }
334 
335 #endif
const qore_type_t NT_BINARY
type value for BinaryNode
Definition: node_types.h:49
defines string encoding functions in Qore
Definition: QoreEncoding.h:80
#define HARD_QORE_PARAM(name, Type, list, i)
returns a hard typed parameter
Definition: params.h:288
This is the hash or associative list container type in Qore, dynamically allocated only...
Definition: QoreHashNode.h:49
DLLEXPORT bool getAsBool() const
returns the boolean value of the object
static const ResolvedCallReferenceNode * test_callref_param(const QoreListNode *n, qore_size_t i)
returns a ResolvedCallReferenceNode pointer for the argument position given or 0 if there is no argum...
Definition: params.h:208
DLLEXPORT const QoreEncoding * QCS_DEFAULT
the default encoding for the Qore library
static int get_int_param_with_default(const QoreListNode *n, qore_size_t i, int def)
returns an integer corresponding to the argument given or a default value if there is none ...
Definition: params.h:97
static const ResolvedCallReferenceNode * test_funcref_param(const QoreListNode *n, qore_size_t i)
returns a ResolvedCallReferenceNode pointer for the argument position given or 0 if there is no argum...
Definition: params.h:221
DLLEXPORT double getAsFloat() const
returns the float value of the object
static const QoreHashNode * test_hash_param(const QoreListNode *n, qore_size_t i)
returns a QoreHashNode pointer for the argument position given or 0 if there is no argument there or ...
Definition: params.h:182
const qore_type_t NT_LIST
type value for QoreListNode
Definition: node_types.h:50
DLLEXPORT qore_size_t size() const
returns the number of bytes in the object
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:55
static T * get_hard_or_nothing_param(const QoreListNode *n, qore_size_t i)
returns the given type for hard typed parameters
Definition: params.h:258
const qore_type_t NT_NOTHING
type value for QoreNothingNode
Definition: node_types.h:42
const qore_type_t NT_OBJECT
type value for QoreObject
Definition: node_types.h:52
size_t qore_size_t
used for sizes (same range as a pointer)
Definition: common.h:70
static const DateTimeNode * test_date_param(const QoreListNode *n, qore_size_t i)
returns a DateTimeNode pointer for the argument position given or 0 if there is no argument there or ...
Definition: params.h:169
static const ReferenceNode * test_reference_param(const QoreListNode *n, qore_size_t i)
returns a ReferenceNode pointer for the argument position given or 0 if there is no argument there or...
Definition: params.h:232
const qore_type_t NT_DATE
type value for DateTimeNode
Definition: node_types.h:46
static const QoreEncoding * get_hard_qore_encoding_param(const QoreListNode *n, qore_size_t i)
returns the QoreEncoding corresponding to the string passed or a default encoding ...
Definition: params.h:330
const qore_type_t NT_FUNCREF
type value for AbstractCallReferenceNode
Definition: node_types.h:71
static QoreObject * test_object_param(const QoreListNode *n, qore_size_t i)
returns a QoreObject pointer for the argument position given or 0 if there is no argument there or if...
Definition: params.h:156
static const BinaryNode * test_binary_param(const QoreListNode *n, qore_size_t i)
returns a const BinaryNode pointer for the argument position given or 0 if there is no argument there...
Definition: params.h:130
DLLEXPORT int getAsInt() const
returns the integer value of the object
DLLEXPORT AbstractQoreNode * retrieve_entry(qore_size_t index)
returns the element at "index" (first element is index 0)
DLLEXPORT int64 getAsBigInt() const
returns the 64-bit integer value of the object
signed short qore_type_t
used to identify unique Qore data and parse types (descendents of AbstractQoreNode) ...
Definition: common.h:67
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:48
static unsigned num_args(const QoreListNode *n)
returns the number of arguments passed to the function
Definition: params.h:46
static bool is_nothing(const AbstractQoreNode *n)
to check if an AbstractQoreNode object is NOTHING
Definition: QoreLib.h:301
static T * get_hard_param(const QoreListNode *n, qore_size_t i)
returns the given type for hard typed parameters
Definition: params.h:265
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
DLLEXPORT QoreEncodingManager QEM
the QoreEncodingManager object
Qore's parse tree/value type for date-time values, reference-counted, dynamically-allocated only...
Definition: DateTimeNode.h:44
static bool get_bool_param(const QoreListNode *n, qore_size_t i)
returns a boolean value corresponding to the argument given or false if there is none ...
Definition: params.h:118
parse type: reference to a lvalue expression
Definition: ReferenceNode.h:45
const qore_type_t NT_RUNTIME_CLOSURE
type value for ResolvedCallReferenceNode (QoreClosureNode, QoreObjectClosureNode) ...
Definition: node_types.h:74
const qore_type_t NT_HASH
type value for QoreHashNode
Definition: node_types.h:51
static bool test_nothing_param(const QoreListNode *n, qore_size_t i)
returns true if the arugment position given is NOTHING
Definition: params.h:245
const qore_type_t NT_REFERENCE
type value for ReferenceNode
Definition: node_types.h:64
static DLLEXPORT const QoreEncoding * findCreate(const char *name)
finds an encoding if it exists (also looks up against alias names) and creates a new one if it doesn'...
static const QoreListNode * test_list_param(const QoreListNode *n, qore_size_t i)
returns a QoreListNode pointer for the argument position given or 0 if there is no argument there or ...
Definition: params.h:195
const qore_type_t NT_STRING
type value for QoreStringNode
Definition: node_types.h:45
DLLEXPORT qore_size_t size() const
returns the number of elements in the list
static const QoreStringNode * test_string_param(const QoreListNode *n, qore_size_t i)
returns a const QoreStringNode pointer for the argument position given or 0 if there is no argument t...
Definition: params.h:143
the implementation of Qore's object data type, reference counted, dynamically-allocated only ...
Definition: QoreObject.h:64
static unsigned num_params(const QoreListNode *n)
returns the number of arguments passed to the function
Definition: params.h:54
static int64 get_bigint_param(const QoreListNode *n, qore_size_t i)
returns a 64-bit integer corresponding to the argument given or 0 if there is none ...
Definition: params.h:90
DLLLOCAL qore_type_t getType() const
returns the data type
Definition: AbstractQoreNode.h:294
base class for resolved call references
Definition: CallReferenceNode.h:128
static int64 get_bigint_param_with_default(const QoreListNode *n, qore_size_t i, int64 def)
returns a 64-bit integer corresponding to the argument given or a default value if there is none ...
Definition: params.h:104
static const QoreEncoding * get_encoding_param(const QoreListNode *n, qore_size_t i, const QoreEncoding *def=QCS_DEFAULT)
returns the QoreEncoding corresponding to the string passed or a default encoding ...
Definition: params.h:251
static double get_float_param(const QoreListNode *n, qore_size_t i)
returns a float corresponding to the argument given or 0 if there is none
Definition: params.h:111
DLLEXPORT const void * getPtr() const
returns the pointer to the data
static int get_int_param(const QoreListNode *n, qore_size_t i)
returns an integer corresponding to the argument given or 0 if there is none
Definition: params.h:83
static const AbstractQoreNode * get_param(const QoreListNode *n, qore_size_t i)
returns the argument in the position given or 0 if there is none
Definition: params.h:64
DLLEXPORT qore_size_t size() const
returns number of bytes in the string (not including the null pointer)
static qore_type_t get_param_type(const QoreListNode *n, qore_size_t i)
returns the argument type in the position given or 0 if there is none
Definition: params.h:76
holds arbitrary binary data
Definition: BinaryNode.h:41
DLLEXPORT const char * getBuffer() const
returns the string's buffer; this data should not be changed