Qore Programming Language  1.12.2
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 - 2022 Qore Technologies, s.r.o.
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 
43 
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 
65 static inline const ReferenceNode* test_reference_param(const QoreListNode* n, size_t i) {
66  if (!n) return nullptr;
67  QoreValue p = n->retrieveEntry(i);
68  // the following is faster than a dynamic_cast
69  return p.getType() == NT_REFERENCE ? p.get<const ReferenceNode>() : nullptr;
70 }
71 
73 
78 static inline QoreValue get_param_value(const QoreListNode* n, size_t i) {
79  if (!n)
80  return QoreValue();
81  return n->retrieveEntry(i);
82 }
83 
85 template <typename T>
86 static inline T* get_hard_value_or_nothing_param(const QoreListNode* n, size_t i) {
87  assert(n);
88  return n->retrieveEntry(i).get<T>();
89 }
90 
92 static QoreValue get_hard_value_param(const QoreListNode* n, size_t i) {
93  assert(n);
94  return n->retrieveEntry(i);
95 }
96 
98 #define HARD_QORE_VALUE_OR_NOTHING_PARAM(name, Type, list, i) Type* name = get_hard_value_or_nothing_param<Type>(list, i)
99 
101 #define HARD_QORE_VALUE_PARAM(name, Type, list, i) Type* name = get_hard_value_param(list, i).get<Type>()
102 
104 #define HARD_QORE_VALUE_INT(list, i) get_hard_value_param(list, i).getAsBigInt()
105 
107 #define HARD_QORE_VALUE_FLOAT(list, i) get_hard_value_param(list, i).getAsFloat()
108 
110 #define HARD_QORE_VALUE_NUMBER(list, i) get_hard_value_param(list, i).get<const QoreNumberNode>()
111 
113 #define HARD_QORE_VALUE_BOOL(list, i) get_hard_value_param(list, i).getAsBool()
114 
116 #define HARD_QORE_VALUE_STRING(list, i) get_hard_value_param(list, i).get<const QoreStringNode>()
117 
119 #define HARD_QORE_VALUE_DATE(list, i) get_hard_value_param(list, i).get<const DateTimeNode>()
120 
122 #define HARD_QORE_VALUE_BINARY(list, i) get_hard_value_param(list, i).get<const BinaryNode>()
123 
125 #define HARD_QORE_VALUE_LIST(list, i) get_hard_value_param(list, i).get<const QoreListNode>()
126 
128 #define HARD_QORE_VALUE_HASH(list, i) get_hard_value_param(list, i).get<const QoreHashNode>()
129 
131 #define HARD_QORE_VALUE_REF(list, i) get_hard_value_param(list, i).get<const ReferenceNode>()
132 
134 #define HARD_QORE_VALUE_OBJECT(list, i) const_cast<QoreObject*>(get_hard_value_param(list, i).get<const QoreObject>())
135 
137 #define HARD_QORE_VALUE_OBJ_DATA(vname, Type, list, i, cid, dname, cname, xsink) HARD_QORE_VALUE_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)
138 
140 #define TAKE_HARD_QORE_VALUE_OBJ_DATA(vname, Type, list, i, cid, dname, cname, xsink) HARD_QORE_VALUE_PARAM(obj_##vname, const QoreObject, list, i); Type* vname = reinterpret_cast<Type*>(const_cast<QoreObject*>(obj_##vname)->getAndClearPrivateData(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); else if (vname) const_cast<QoreObject*>(obj_##vname)->doDelete(xsink)
141 
143 #define HARD_QORE_VALUE_OBJ_OR_NOTHING_DATA(vname, Type, list, i, cid, xsink) HARD_QORE_VALUE_OR_NOTHING_PARAM(obj_##vname, const QoreObject, list, i); Type* vname = obj_##vname ? reinterpret_cast<Type*>(obj_##vname->getReferencedPrivateData(cid, xsink)) : 0;
144 
146 static inline const QoreEncoding* get_value_encoding_param(const QoreListNode* n, size_t i, const QoreEncoding* def = QCS_DEFAULT) {
147  const QoreStringNode* str = HARD_QORE_VALUE_STRING(n, i);
148  return str ? QEM.findCreate(str) : def;
149 }
150 
151 static inline const QoreEncoding* get_hard_qore_value_encoding_param(const QoreListNode* n, size_t i) {
152  HARD_QORE_VALUE_PARAM(str, const QoreStringNode, n, i);
153  return QEM.findCreate(str);
154 }
155 
156 #endif
DLLEXPORT const QoreEncoding * QCS_DEFAULT
the default encoding for the Qore library
DLLEXPORT QoreEncodingManager QEM
the QoreEncodingManager object
defines string encoding functions in Qore
Definition: QoreEncoding.h:83
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'...
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
DLLEXPORT size_t size() const
returns the number of elements in the list
DLLEXPORT QoreValue retrieveEntry(size_t index)
returns the element at "index" (first element is index 0)
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
parse type: reference to a lvalue expression
Definition: ReferenceNode.h:45
const qore_type_t NT_REFERENCE
type value for ReferenceNode
Definition: node_types.h:64
static const QoreEncoding * get_value_encoding_param(const QoreListNode *n, size_t i, const QoreEncoding *def=QCS_DEFAULT)
returns the QoreEncoding corresponding to the string passed or a default encoding
Definition: params.h:146
static QoreValue get_hard_value_param(const QoreListNode *n, size_t i)
returns the given type for hard typed parameters
Definition: params.h:92
static unsigned num_args(const QoreListNode *n)
returns the number of arguments passed to the function
Definition: params.h:46
static unsigned num_params(const QoreListNode *n)
returns the number of arguments passed to the function
Definition: params.h:54
static T * get_hard_value_or_nothing_param(const QoreListNode *n, size_t i)
returns the given type for hard typed parameters
Definition: params.h:86
#define HARD_QORE_VALUE_STRING(list, i)
returns a const QoreStringNode* from a hard typed string param
Definition: params.h:116
static QoreValue get_param_value(const QoreListNode *n, size_t i)
returns the argument in the position given or 0 if there is none
Definition: params.h:78
#define HARD_QORE_VALUE_PARAM(name, Type, list, i)
returns a hard typed parameter
Definition: params.h:101
static const ReferenceNode * test_reference_param(const QoreListNode *n, size_t i)
returns a ReferenceNode pointer for the argument position given or 0 if there is no argument there or...
Definition: params.h:65
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:275