Qore Programming Language 2.3.0
Loading...
Searching...
No Matches
common.h
Go to the documentation of this file.
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 common.h
4
5 Qore Programming Language
6
7 Copyright (C) 2003 - 2024 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_COMMON_H
33
34#define _QORE_COMMON_H
35
40#include <algorithm>
41#include <cinttypes>
42#include <cstdarg>
43#include <cstddef>
44#include <cstdlib>
45#include <cstring>
46#include <functional>
47#include <list>
48#include <set>
49#include <set>
50#include <string>
51#include <strings.h>
52#include <vector>
53
54#ifdef __GNUC__
55#define PACK __attribute__ ((packed))
56#else
57#define PACK
58#endif
59
61class QoreTypeInfo;
62class RuntimeConfig;
63
65#define Q_AF_UNSPEC -1
66
68#define Q_AF_INET -2
69
71#define Q_AF_INET6 -3
72
74#define Q_SOCK_STREAM -1
75
77typedef int16_t qore_type_t;
78
80typedef size_t qore_size_t;
81
83typedef intptr_t qore_offset_t;
84
86typedef unsigned qore_classid_t;
87
89typedef std::set<int> int_set_t;
90
93 QL_GPL = 0,
94 QL_LGPL = 1,
95 QL_MIT = 2
96};
97
98// class access values
99enum ClassAccess : unsigned char {
100 Public = 0, // publicly accessible
101 Private = 1, // accessible only in the class hierarchy (like c++'s 'protected')
102 Internal = 2, // accessible only in the class itself
103 Inaccessible = 3, // not accessible from the class
104};
105
106#if defined _MSC_VER || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
107#define _Q_WINDOWS 1
108#ifdef _WIN64
109#define _Q_WINDOWS64 1
110#endif
111#endif
112
113#ifdef _Q_WINDOWS
114 #ifdef BUILDING_DLL
115 #define DLLEXPORT __declspec(dllexport)
116 #else
117 #define DLLEXPORT __declspec(dllimport)
118 #endif
119 #define DLLLOCAL
120
121 #define QLLD "%I64d"
122 #define QLLX "%I64x"
123 #define QLLDx(a) "%" #a "I64d"
124 #define QORE_DIR_SEP '\\'
125 #define QORE_DIR_SEP_STR "\\"
126 #define QORE_PATH_SEP ';'
127 #define QORE_PATH_SEP_STR ";"
128 #include <winsock2.h>
129#else
130 #ifdef HAVE_GCC_VISIBILITY
131 #define DLLEXPORT __attribute__ ((visibility("default")))
132 #define DLLLOCAL __attribute__ ((visibility("hidden")))
133 #else
134 #define DLLEXPORT
135 #define DLLLOCAL
136 #endif
137 #define QLLD "%lld"
138 #define QLLX "%llx"
139 #define QLLDx(a) "%" #a "lld"
140 #define QORE_DIR_SEP '/'
141 #define QORE_DIR_SEP_STR "/"
142 #define QORE_PATH_SEP ':'
143 #define QORE_PATH_SEP_STR ":"
144#endif
145
146#define _Q_MAKE_STRING(x) #x
147#define MAKE_STRING_FROM_SYMBOL(x) _Q_MAKE_STRING(x)
148
149// forward references
150class AbstractQoreNode;
151class QoreListNode;
152class ExceptionSink;
153class QoreObject;
155class QoreMethod;
156class QoreBuiltinMethod;
157class QoreClass;
158class TypedHashDecl;
159class QoreExternalFunction;
160class QoreExternalGlobalVar;
162class QoreNamespace;
163class QoreHashNode;
164
165class QoreValue;
166
167typedef std::vector<const QoreClass*> class_vec_t;
168typedef std::vector<std::pair<const TypedHashDecl*, const QoreNamespace*>> hashdecl_vec_t;
169typedef std::vector<const QoreExternalFunction*> func_vec_t;
170typedef std::vector<const QoreNamespace*> ns_vec_t;
171typedef std::vector<std::pair<const QoreExternalGlobalVar*, const QoreNamespace*>> gvar_vec_t;
172typedef std::vector<std::pair<const QoreExternalConstant*, const QoreNamespace*>> const_vec_t;
173
175template <typename T> hashdecl free_ptr {
176 DLLLOCAL void operator()(T* ptr) {
177 free(ptr);
178 }
179};
180
182template <typename T> hashdecl simple_delete {
183 DLLLOCAL void operator()(T* ptr) {
184 delete ptr;
185 }
186};
187
189template <typename T> hashdecl simple_deref {
190 DLLLOCAL void operator()(T* ptr) {
191 ptr->deref();
192 }
193 DLLLOCAL void operator()(T* ptr, ExceptionSink* xsink) {
194 ptr->deref(xsink);
195 }
196};
197
199class ltstr {
200public:
201 DLLLOCAL bool operator()(const char* s1, const char* s2) const {
202 return strcmp(s1, s2) < 0;
203 }
204};
205
208public:
209 DLLLOCAL bool operator()(const char* s1, const char* s2) const {
210 return strcasecmp(s1, s2) < 0;
211 }
212};
213
216public:
217 DLLLOCAL bool operator()(std::string s1, std::string s2) const {
218 return strcasecmp(s1.c_str(), s2.c_str()) < 0;
219 }
220};
221
222class eqstr {
223public:
224 DLLLOCAL bool operator()(const char* s1, const char* s2) const {
225 return !strcmp(s1, s2);
226 }
227};
228
229class eqstrcase {
230public:
231 DLLLOCAL bool operator()(const char* s1, const char* s2) const {
232 return !strcasecmp(s1, s2);
233 }
234};
235
237class ltchar {
238public:
239 DLLLOCAL bool operator()(const char s1, const char s2) const {
240 return s1 < s2;
241 }
242};
243
245class cstr_vector_t : public std::vector<char*> {
246public:
247 DLLLOCAL ~cstr_vector_t() {
248 clear();
249 }
250
251 DLLLOCAL void clear() {
252 std::for_each(begin(), end(), free_ptr<char>());
253 std::vector<char*>::clear();
254 }
255};
256
258typedef std::vector<const QoreTypeInfo*> type_vec_t;
259
261typedef std::vector<QoreValue> arg_vec_t;
262
264typedef std::vector<std::string> name_vec_t;
265
267typedef long long int64;
268
270typedef uint64_t q_rt_flags_t;
271
273constexpr size_t QSF_INCLUDE_STATIC_CLASS_VARS = (1 << 0);
274
276constexpr size_t QDF_STATIC_CLASS_VARS = (1 << 0);
277
280
282
285public:
287
292 DLLEXPORT int serializeObject(const QoreObject& obj, std::string& index, ExceptionSink* xsink);
293
295
300 DLLEXPORT int serializeHash(const QoreHashNode& h, std::string& index, ExceptionSink* xsink);
301
303
308 DLLEXPORT int serializeList(const QoreListNode& l, std::string& index, ExceptionSink* xsink);
309
311
313 DLLEXPORT QoreValue serializeValue(const QoreValue val, ExceptionSink* xsink);
314
316 DLLEXPORT void addModule(const char* module);
317
319 DLLEXPORT int64 getFlags() const;
320
321private:
323 DLLLOCAL QoreSerializationContext();
324};
325
327
330public:
332
334 DLLEXPORT QoreValue deserializeContainer(const char* index, ExceptionSink* xsink);
335
337
339 DLLEXPORT QoreValue deserializeValue(const QoreValue val, ExceptionSink* xsink);
340
342 DLLEXPORT int64 getFlags() const;
343
344private:
347};
348
350
356typedef QoreValue (*q_func_t)(const QoreListNode* args, RuntimeConfig& rc, ExceptionSink* xsink);
357
359
368typedef QoreValue (*q_external_func_t)(const void* ptr, const QoreListNode* args, RuntimeConfig& rc,
369 ExceptionSink* xsink);
370
372
380typedef QoreValue (*q_method_t)(QoreObject* self, AbstractPrivateData* private_data, const QoreListNode* args,
381 RuntimeConfig& rc, ExceptionSink* xsink);
382
384
396typedef QoreValue (*q_external_method_t)(const QoreMethod& method, const void* ptr, QoreObject* self,
397 AbstractPrivateData* private_data, const QoreListNode* args, RuntimeConfig& rc, ExceptionSink* xsink);
398
400
410typedef QoreValue (*q_external_static_method_t)(const QoreMethod& method, const void* ptr, const QoreListNode* args,
411 RuntimeConfig& rc, ExceptionSink* xsink);
412
414
419typedef void (*q_constructor_t)(QoreObject* self, const QoreListNode* args, RuntimeConfig& rc,
420 ExceptionSink* xsink);
421
423
432typedef void (*q_external_constructor_t)(const QoreMethod& method, const void* ptr, QoreObject* self,
433 const QoreListNode* args, RuntimeConfig& rc, ExceptionSink* xsink);
434
436
445typedef void (*q_system_constructor_t)(QoreObject* self, int code, va_list args);
446
448
454typedef void (*q_destructor_t)(QoreObject* self, AbstractPrivateData* private_data, RuntimeConfig& rc,
455 ExceptionSink* xsink);
456
458
466typedef void (*q_external_destructor_t)(const QoreClass& thisclass, const void* ptr, QoreObject* self,
467 AbstractPrivateData* private_data, RuntimeConfig& rc, ExceptionSink* xsink);
468
470
477typedef void (*q_copy_t)(QoreObject* self, QoreObject* old, AbstractPrivateData* private_data, RuntimeConfig& rc,
478 ExceptionSink* xsink);
479
481
490typedef void (*q_external_copy_t)(const QoreClass& thisclass, const void* ptr, QoreObject* self, QoreObject* old,
491 AbstractPrivateData* private_data, RuntimeConfig& rc, ExceptionSink* xsink);
492
494
502typedef QoreHashNode* (*q_serializer_t)(const QoreObject& self, const AbstractPrivateData& data,
504
506
516typedef void (*q_deserializer_t)(QoreObject& self, const QoreHashNode* sdata, QoreDeserializationContext& context,
517 RuntimeConfig& rc, ExceptionSink* xsink);
518
520
522typedef unsigned q_trid_t;
523
525DLLEXPORT long long q_atoll(const char* str);
526
528
530DLLEXPORT const char* type_get_name(const QoreTypeInfo* t);
531
532#endif // _QORE_COMMON_H
the base class for all data to be used as private data of Qore objects
Definition AbstractPrivateData.h:44
The base class for all value and parse types in Qore expression trees.
Definition AbstractQoreNode.h:60
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition ExceptionSink.h:50
defines a Qore-language class
Definition QoreClass.h:315
deserialization context object used in builtin deserializer methods
Definition common.h:329
DLLEXPORT QoreValue deserializeContainer(const char *index, ExceptionSink *xsink)
returns the container corresponding to the given serialization index
DLLEXPORT QoreValue deserializeValue(const QoreValue val, ExceptionSink *xsink)
deserializes the given data value and returns the deserialized value
DLLEXPORT int64 getFlags() const
Returns the current deserialization flag bitfield.
external wrapper class for constants
Definition QoreReflection.h:200
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition QoreHashNode.h:51
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition QoreListNode.h:52
a method in a QoreClass
Definition QoreClass.h:148
contains constants, classes, and subnamespaces in QoreProgram objects
Definition QoreNamespace.h:66
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition QoreObject.h:61
serialization context object used in builtin serializer methods
Definition common.h:284
DLLEXPORT int serializeHash(const QoreHashNode &h, std::string &index, ExceptionSink *xsink)
Serializes the given hash and returns the serialization index.
DLLEXPORT int serializeObject(const QoreObject &obj, std::string &index, ExceptionSink *xsink)
Serializes the given object and returns the serialization index.
DLLEXPORT int64 getFlags() const
Returns the current serialization flag bitfield.
DLLEXPORT void addModule(const char *module)
Adds a module to be loaded when the data is deserialized.
DLLEXPORT int serializeList(const QoreListNode &l, std::string &index, ExceptionSink *xsink)
Serializes the given list and returns the serialization index.
DLLEXPORT QoreValue serializeValue(const QoreValue val, ExceptionSink *xsink)
Serializes the given value and returns a serialized value representing the value to be serialized.
The main value class in Qore, designed to be passed by value.
Definition QoreValue.h:264
Runtime configuration passed through the evaluation call chain.
Definition RuntimeConfig.h:49
typed hash declaration
Definition TypedHashDecl.h:44
non-thread-safe vector for storing "char*" that you want to delete
Definition common.h:245
for char less-than comparisons
Definition common.h:237
for simple c-string case-insensitive less-than comparisons
Definition common.h:207
for simple c-string less-than comparisons
Definition common.h:199
for std::string case-insensitive less-than comparisons
Definition common.h:215
void(* q_destructor_t)(QoreObject *self, AbstractPrivateData *private_data, RuntimeConfig &rc, ExceptionSink *xsink)
the type used for builtin QoreClass destructor signatures
Definition common.h:454
void(* q_deserializer_t)(QoreObject &self, const QoreHashNode *sdata, QoreDeserializationContext &context, RuntimeConfig &rc, ExceptionSink *xsink)
the type used for builtin QoreClass deserializer method signatures
Definition common.h:516
QoreValue(* q_external_func_t)(const void *ptr, const QoreListNode *args, RuntimeConfig &rc, ExceptionSink *xsink)
the type used for builtin function signatures for external functions
Definition common.h:368
constexpr size_t QSF_INCLUDE_STATIC_CLASS_VARS
Serialization flag; include static class vars.
Definition common.h:273
QoreValue(* q_external_static_method_t)(const QoreMethod &method, const void *ptr, const QoreListNode *args, RuntimeConfig &rc, ExceptionSink *xsink)
the type used for external static methods
Definition common.h:410
QoreValue(* q_method_t)(QoreObject *self, AbstractPrivateData *private_data, const QoreListNode *args, RuntimeConfig &rc, ExceptionSink *xsink)
the type used for builtin QoreClass method signatures
Definition common.h:380
int16_t qore_type_t
used to identify unique Qore data and parse types (descendents of AbstractQoreNode)
Definition common.h:77
DLLEXPORT const char * type_get_name(const QoreTypeInfo *t)
returns the type name for an opaqua QoreTypeInfo ptr
uint64_t q_rt_flags_t
runtime code execution flags
Definition common.h:270
DLLEXPORT long long q_atoll(const char *str)
returns an integer value for a string
constexpr size_t QDF_STATIC_CLASS_VARS
Deserialization flag: deserialize static class vars.
Definition common.h:276
void(* q_copy_t)(QoreObject *self, QoreObject *old, AbstractPrivateData *private_data, RuntimeConfig &rc, ExceptionSink *xsink)
the type used for builtin QoreClass copy signatures
Definition common.h:477
void(* q_system_constructor_t)(QoreObject *self, int code, va_list args)
the type used for builtin QoreClass system constructor method signatures
Definition common.h:445
void(* q_external_destructor_t)(const QoreClass &thisclass, const void *ptr, QoreObject *self, AbstractPrivateData *private_data, RuntimeConfig &rc, ExceptionSink *xsink)
the type used for builtin QoreClass destructor signatures with the new generic calling convention and...
Definition common.h:466
std::set< int > int_set_t
set of integers
Definition common.h:89
std::vector< const QoreTypeInfo * > type_vec_t
vector of type information for parameter lists
Definition common.h:258
QoreValue(* q_func_t)(const QoreListNode *args, RuntimeConfig &rc, ExceptionSink *xsink)
the type used for builtin function signatures
Definition common.h:356
unsigned q_trid_t
type for thread resource IDs (unique within a single running qore library process)
Definition common.h:522
QoreValue(* q_external_method_t)(const QoreMethod &method, const void *ptr, QoreObject *self, AbstractPrivateData *private_data, const QoreListNode *args, RuntimeConfig &rc, ExceptionSink *xsink)
the type used for builtin QoreClass method signatures
Definition common.h:396
qore_license_t
qore library and module license type identifiers
Definition common.h:92
@ QL_GPL
code to be used under the GPL license
Definition common.h:93
@ QL_LGPL
code to be used under the LGPL license
Definition common.h:94
@ QL_MIT
code to be used under the MIT license
Definition common.h:95
unsigned qore_classid_t
used for the unique class ID for QoreClass objects
Definition common.h:86
void(* q_constructor_t)(QoreObject *self, const QoreListNode *args, RuntimeConfig &rc, ExceptionSink *xsink)
the type used for builtin QoreClass constructor method signatures
Definition common.h:419
std::vector< std::string > name_vec_t
vector of parameter names for parameter lists
Definition common.h:264
void(* q_external_copy_t)(const QoreClass &thisclass, const void *ptr, QoreObject *self, QoreObject *old, AbstractPrivateData *private_data, RuntimeConfig &rc, ExceptionSink *xsink)
the type used for builtin QoreClass copy signatures with the new generic calling convention
Definition common.h:490
void(* q_external_constructor_t)(const QoreMethod &method, const void *ptr, QoreObject *self, const QoreListNode *args, RuntimeConfig &rc, ExceptionSink *xsink)
the type used for builtin QoreClass constructor method signatures
Definition common.h:432
intptr_t qore_offset_t
used for offsets that could be negative
Definition common.h:83
size_t qore_size_t
used for sizes (same range as a pointer)
Definition common.h:80
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition common.h:267
std::vector< QoreValue > arg_vec_t
vector of value information for default argument lists
Definition common.h:261
constexpr size_t QDF_MERGE_STATIC_CLASS_VARS
Deserialization flag: deserialize and merge static class vars; implies QDF_STATIC_CLASS_VARS.
Definition common.h:279
functor template for calling free() on pointers
Definition common.h:175
functor template for deleting elements
Definition common.h:182
functor template for dereferencing elements
Definition common.h:189