Qore Programming Language  0.9.1
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 - 2018 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 
55 class QoreTypeInfo;
56 
58 #define Q_AF_UNSPEC -1
59 
61 #define Q_AF_INET -2
62 
64 #define Q_AF_INET6 -3
65 
67 #define Q_SOCK_STREAM -1
68 
70 typedef int16_t qore_type_t;
71 
73 typedef size_t qore_size_t;
74 
76 typedef intptr_t qore_offset_t;
77 
79 typedef unsigned qore_classid_t;
80 
82 typedef std::set<int> int_set_t;
83 
86  QL_GPL = 0,
87  QL_LGPL = 1,
88  QL_MIT = 2
89 };
90 
91 // class access values
92 enum ClassAccess : unsigned char {
93  Public = 0, // publicly accessible
94  Private = 1, // accessible only in the class hierarchy (like c++'s 'protected')
95  Internal = 2, // accessible only in the class itself
96  Inaccessible = 3, // not accessible from the class
97 };
98 
99 #if defined _MSC_VER || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
100 #define _Q_WINDOWS 1
101 #ifdef _WIN64
102 #define _Q_WINDOWS64 1
103 #endif
104 #endif
105 
106 #ifdef _Q_WINDOWS
107  #ifdef BUILDING_DLL
108  #define DLLEXPORT __declspec(dllexport)
109  #else
110  #define DLLEXPORT __declspec(dllimport)
111  #endif
112  #define DLLLOCAL
113 
114  #define QLLD "%I64d"
115  #define QLLX "%I64x"
116  #define QLLDx(a) "%" #a "I64d"
117  #define QORE_DIR_SEP '\\'
118  #define QORE_DIR_SEP_STR "\\"
119  #define QORE_PATH_SEP ';'
120  #define QORE_PATH_SEP_STR ";"
121  #include <winsock2.h>
122 #else
123  #ifdef HAVE_GCC_VISIBILITY
124  #define DLLEXPORT __attribute__ ((visibility("default")))
125  #define DLLLOCAL __attribute__ ((visibility("hidden")))
126  #else
127  #define DLLEXPORT
128  #define DLLLOCAL
129  #endif
130  #define QLLD "%lld"
131  #define QLLX "%llx"
132  #define QLLDx(a) "%" #a "lld"
133  #define QORE_DIR_SEP '/'
134  #define QORE_DIR_SEP_STR "/"
135  #define QORE_PATH_SEP ':'
136  #define QORE_PATH_SEP_STR ":"
137 #endif
138 
139 #define _Q_MAKE_STRING(x) #x
140 #define MAKE_STRING_FROM_SYMBOL(x) _Q_MAKE_STRING(x)
141 
142 // forward references
143 class AbstractQoreNode;
144 class QoreListNode;
145 class ExceptionSink;
146 class QoreObject;
147 class AbstractPrivateData;
148 class QoreMethod;
149 class QoreBuiltinMethod;
150 class QoreClass;
151 class TypedHashDecl;
152 class QoreExternalFunction;
153 class QoreExternalGlobalVar;
155 class QoreNamespace;
156 class QoreHashNode;
157 
158 hashdecl QoreValue;
159 
160 typedef std::vector<const QoreClass*> class_vec_t;
161 typedef std::vector<std::pair<const TypedHashDecl*, const QoreNamespace*>> hashdecl_vec_t;
162 typedef std::vector<const QoreExternalFunction*> func_vec_t;
163 typedef std::vector<const QoreNamespace*> ns_vec_t;
164 typedef std::vector<std::pair<const QoreExternalGlobalVar*, const QoreNamespace*>> gvar_vec_t;
165 typedef std::vector<std::pair<const QoreExternalConstant*, const QoreNamespace*>> const_vec_t;
166 
168 template <typename T> hashdecl free_ptr : std::unary_function <T*, void> {
169  DLLLOCAL void operator()(T* ptr) {
170  free(ptr);
171  }
172 };
173 
175 template <typename T> hashdecl simple_delete {
176  DLLLOCAL void operator()(T* ptr) {
177  delete ptr;
178  }
179 };
180 
182 template <typename T> hashdecl simple_deref {
183  DLLLOCAL void operator()(T* ptr) {
184  ptr->deref();
185  }
186  DLLLOCAL void operator()(T* ptr, ExceptionSink* xsink) {
187  ptr->deref(xsink);
188  }
189 };
190 
192 class ltstr {
193 public:
194  DLLLOCAL bool operator()(const char* s1, const char* s2) const {
195  return strcmp(s1, s2) < 0;
196  }
197 };
198 
200 class ltcstrcase {
201 public:
202  DLLLOCAL bool operator()(const char* s1, const char* s2) const {
203  return strcasecmp(s1, s2) < 0;
204  }
205 };
206 
208 class ltstrcase {
209 public:
210  DLLLOCAL bool operator()(std::string s1, std::string s2) const {
211  return strcasecmp(s1.c_str(), s2.c_str()) < 0;
212  }
213 };
214 
215 class eqstr {
216 public:
217  DLLLOCAL bool operator()(const char* s1, const char* s2) const {
218  return !strcmp(s1, s2);
219  }
220 };
221 
222 class eqstrcase {
223 public:
224  DLLLOCAL bool operator()(const char* s1, const char* s2) const {
225  return !strcasecmp(s1, s2);
226  }
227 };
228 
230 class ltchar {
231 public:
232  DLLLOCAL bool operator()(const char s1, const char s2) const {
233  return s1 < s2;
234  }
235 };
236 
238 class cstr_vector_t : public std::vector<char*> {
239 public:
240  DLLLOCAL ~cstr_vector_t() {
241  clear();
242  }
243 
244  DLLLOCAL void clear() {
245  std::for_each(begin(), end(), free_ptr<char>());
246  std::vector<char*>::clear();
247  }
248 };
249 
251 typedef std::vector<const QoreTypeInfo*> type_vec_t;
252 
254 typedef std::vector<QoreValue> arg_vec_t;
255 
257 typedef std::vector<std::string> name_vec_t;
258 
260 typedef long long int64;
261 
263 typedef uint64_t q_rt_flags_t;
264 
266 
269 public:
271  DLLLOCAL int serializeObject(const QoreObject& obj, std::string& index, ExceptionSink* xsink);
272 
274  DLLLOCAL QoreValue serializeValue(const QoreValue val, ExceptionSink* xsink);
275 
277  DLLLOCAL void addModule(const char* module);
278 
279 private:
281  DLLLOCAL QoreSerializationContext();
282 };
283 
285 
288 public:
290  DLLLOCAL QoreObject* deserializeObject(const char* index, ExceptionSink* xsink);
291 
293  DLLLOCAL QoreValue deserializeValue(const QoreValue val, ExceptionSink* xsink);
294 
295 private:
297  DLLLOCAL QoreDeserializationContext();
298 };
299 
301 
307 typedef QoreValue (*q_func_n_t)(const QoreListNode* args, q_rt_flags_t flags, ExceptionSink* xsink);
308 
310 
318 typedef QoreValue (*q_method_n_t)(QoreObject* self, AbstractPrivateData* private_data, const QoreListNode* args, q_rt_flags_t flags, ExceptionSink* xsink);
319 
321 
333 typedef QoreValue (*q_external_method_t)(const QoreMethod& method, const void* ptr, QoreObject* self, AbstractPrivateData* private_data, const QoreListNode* args, q_rt_flags_t flags, ExceptionSink* xsink);
334 
336 
346 typedef QoreValue (*q_external_static_method_t)(const QoreMethod& method, const void* ptr, const QoreListNode* args, q_rt_flags_t flags, ExceptionSink* xsink);
347 
349 
354 typedef void (*q_constructor_n_t)(QoreObject* self, const QoreListNode* args, q_rt_flags_t rtflags, ExceptionSink* xsink);
355 
357 
366 typedef void (*q_external_constructor_t)(const QoreMethod& method, const void* ptr, QoreObject* self, const QoreListNode* args, q_rt_flags_t rtflags, ExceptionSink* xsink);
367 
369 
378 typedef void (*q_system_constructor_t)(QoreObject* self, int code, va_list args);
379 
381 
386 typedef void (*q_destructor_t)(QoreObject* self, AbstractPrivateData* private_data, ExceptionSink* xsink);
387 
389 
396 typedef void (*q_external_destructor_t)(const QoreClass& thisclass, const void* ptr, QoreObject* self, AbstractPrivateData* private_data, ExceptionSink* xsink);
397 
399 
405 typedef void (*q_copy_t)(QoreObject* self, QoreObject* old, AbstractPrivateData* private_data, ExceptionSink* xsink);
406 
408 
416 typedef void (*q_external_copy_t)(const QoreClass& thisclass, const void* ptr, QoreObject* self, QoreObject* old, AbstractPrivateData* private_data, ExceptionSink* xsink);
417 
419 
424 typedef bool (*q_delete_blocker_t)(QoreObject* self, AbstractPrivateData* private_data);
425 
427 
434 typedef QoreHashNode* (*q_serializer_t)(const QoreObject& self, const AbstractPrivateData& data, QoreSerializationContext& context, ExceptionSink* xsink);
435 
437 
446 typedef void (*q_deserializer_t)(QoreObject& self, const QoreHashNode* sdata, QoreDeserializationContext& context, ExceptionSink* xsink);
447 
449 
451 typedef unsigned q_trid_t;
452 
454 DLLEXPORT long long q_atoll(const char* str);
455 
457 
459 DLLEXPORT const char* type_get_name(const QoreTypeInfo* t);
460 
461 #endif // _QORE_COMMON_H
void(* q_external_constructor_t)(const QoreMethod &method, const void *ptr, QoreObject *self, const QoreListNode *args, q_rt_flags_t rtflags, ExceptionSink *xsink)
the type used for builtin QoreClass constructor method signatures
Definition: common.h:366
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:50
the base class for all data to be used as private data of Qore objects
Definition: AbstractPrivateData.h:44
void(* q_destructor_t)(QoreObject *self, AbstractPrivateData *private_data, ExceptionSink *xsink)
the type used for builtin QoreClass destructor signatures
Definition: common.h:386
DLLLOCAL void addModule(const char *module)
adds a module to be loaded when the data is deserialized
QoreValue(* q_external_static_method_t)(const QoreMethod &method, const void *ptr, const QoreListNode *args, q_rt_flags_t flags, ExceptionSink *xsink)
the type used for external static methods
Definition: common.h:346
DLLEXPORT const char * type_get_name(const QoreTypeInfo *t)
returns the type name for an opaqua QoreTypeInfo ptr
DLLEXPORT long long q_atoll(const char *str)
returns an integer value for a string
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:378
non-thread-safe vector for storing "char*" that you want to delete
Definition: common.h:238
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:54
DLLLOCAL QoreValue serializeValue(const QoreValue val, ExceptionSink *xsink)
serializes the given value and returns a serialized value representing the value to be serialized...
code to be used under the LGPL license
Definition: common.h:87
contains constants, classes, and subnamespaces in QoreProgram objects
Definition: QoreNamespace.h:64
size_t qore_size_t
used for sizes (same range as a pointer)
Definition: common.h:73
for char less-than comparisons
Definition: common.h:230
for simple c-string less-than comparisons
Definition: common.h:192
uint64_t q_rt_flags_t
runtime code execution flags
Definition: common.h:263
for simple c-string case-insensitive less-than comparisons
Definition: common.h:200
serialization context object used in builtin serializer methods
Definition: common.h:268
code to be used under the MIT license
Definition: common.h:88
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
DLLLOCAL QoreValue deserializeValue(const QoreValue val, ExceptionSink *xsink)
deserializes the given data value and returns the deserialized value, throws a Qore-language exceptio...
void(* q_external_copy_t)(const QoreClass &thisclass, const void *ptr, QoreObject *self, QoreObject *old, AbstractPrivateData *private_data, ExceptionSink *xsink)
the type used for builtin QoreClass copy signatures with the new generic calling convention ...
Definition: common.h:416
defines a Qore-language class
Definition: QoreClass.h:237
void(* q_external_destructor_t)(const QoreClass &thisclass, const void *ptr, QoreObject *self, AbstractPrivateData *private_data, ExceptionSink *xsink)
the type used for builtin QoreClass destructor signatures with the new generic calling convention and...
Definition: common.h:396
bool(* q_delete_blocker_t)(QoreObject *self, AbstractPrivateData *private_data)
the typed used for QoreClass deleteBlocker signatures
Definition: common.h:424
void(* q_deserializer_t)(QoreObject &self, const QoreHashNode *sdata, QoreDeserializationContext &context, ExceptionSink *xsink)
the type used for builtin QoreClass deserializer method signatures
Definition: common.h:446
functor template for deleting elements
Definition: common.h:175
void(* q_constructor_n_t)(QoreObject *self, const QoreListNode *args, q_rt_flags_t rtflags, ExceptionSink *xsink)
the type used for builtin QoreClass constructor method signatures
Definition: common.h:354
QoreValue(* q_method_n_t)(QoreObject *self, AbstractPrivateData *private_data, const QoreListNode *args, q_rt_flags_t flags, ExceptionSink *xsink)
the type used for builtin QoreClass method signatures
Definition: common.h:318
unsigned q_trid_t
type for thread resource IDs (unique within a single running qore library process) ...
Definition: common.h:451
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:262
qore_license_t
qore library and module license type identifiers
Definition: common.h:85
the implementation of Qore&#39;s object data type, reference counted, dynamically-allocated only ...
Definition: QoreObject.h:61
unsigned qore_classid_t
used for the unique class ID for QoreClass objects
Definition: common.h:79
for std::string case-insensitive less-than comparisons
Definition: common.h:208
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:46
std::vector< std::string > name_vec_t
vector of parameter names for parameter lists
Definition: common.h:257
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:260
std::vector< QoreValue > arg_vec_t
vector of value information for default argument lists
Definition: common.h:254
intptr_t qore_offset_t
used for offsets that could be negative
Definition: common.h:76
typed hash declaration
Definition: TypedHashDecl.h:44
code to be used under the GPL license
Definition: common.h:86
int16_t qore_type_t
used to identify unique Qore data and parse types (descendents of AbstractQoreNode) ...
Definition: common.h:70
std::set< int > int_set_t
set of integers
Definition: common.h:82
void(* q_copy_t)(QoreObject *self, QoreObject *old, AbstractPrivateData *private_data, ExceptionSink *xsink)
the type used for builtin QoreClass copy signatures
Definition: common.h:405
QoreValue(* q_external_method_t)(const QoreMethod &method, const void *ptr, QoreObject *self, AbstractPrivateData *private_data, const QoreListNode *args, q_rt_flags_t flags, ExceptionSink *xsink)
the type used for builtin QoreClass method signatures
Definition: common.h:333
std::vector< const QoreTypeInfo * > type_vec_t
vector of type information for parameter lists
Definition: common.h:251
a method in a QoreClass
Definition: QoreClass.h:123
DLLLOCAL int serializeObject(const QoreObject &obj, std::string &index, ExceptionSink *xsink)
serializes the given object and return the serialization index, throws a Qore-language exception if t...
deserialization context object used in builtin deserializer methods
Definition: common.h:287
DLLLOCAL QoreObject * deserializeObject(const char *index, ExceptionSink *xsink)
returns the object corresponding to the given object serialization index, throws a Qore-language exce...
QoreValue(* q_func_n_t)(const QoreListNode *args, q_rt_flags_t flags, ExceptionSink *xsink)
the type used for builtin function signatures
Definition: common.h:307
functor template for dereferencing elements
Definition: common.h:182
functor template for calling free() on pointers
Definition: common.h:168