Qore Programming Language  0.9.4.6
QoreNamespace.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreNamespace.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 /*
33  namespaces are children of a program object. there is a parse
34  lock per program object to ensure that objects are added (or backed out)
35  atomically per program object. All the objects referenced here should
36  be safe to read & copied at all times. They will only be deleted when the
37  program object is deleted (except the pending structures, which will be
38  deleted any time there is a parse error, together with all other
39  pending structures)
40 */
41 
42 #ifndef _QORE_QORENAMESPACE_H
43 
44 #define _QORE_QORENAMESPACE_H
45 
46 #include <cstdlib>
47 #include <cstring>
48 #include <string>
49 
50 // forward declarations
51 class QoreExternalFunction;
53 class QoreExternalGlobalVar;
54 
56 
61 typedef QoreClass* (*q_ns_class_handler_t)(QoreNamespace* ns, const char* cname);
62 
65  friend class QoreNamespaceList;
66  friend class RootQoreNamespace;
67  friend class qore_ns_private;
68  friend class qore_root_ns_private;
69  friend hashdecl NSOInfoBase;
70 
71 public:
73 
76  DLLEXPORT QoreNamespace(const char* n);
77 
79  DLLEXPORT virtual ~QoreNamespace();
80 
82 
86  DLLEXPORT void clear(ExceptionSink* xsink);
87 
89 
95  DLLEXPORT void addConstant(const char* name, QoreValue value);
96 
98 
104  DLLEXPORT void addConstant(const char* name, QoreValue value, const QoreTypeInfo* typeInfo);
105 
107 
110  DLLEXPORT void addSystemClass(QoreClass* oc);
111 
113 
118  DLLEXPORT void addSystemHashDecl(TypedHashDecl* hashdecl);
119 
121 
124  DLLEXPORT QoreNamespace* copy(int po) const;
125 
127 
130  DLLEXPORT QoreNamespace* copy(int64 po = PO_DEFAULT) const;
131 
133 
138  DLLEXPORT QoreHashNode* getClassInfo() const;
139 
141 
146  DLLEXPORT QoreHashNode* getConstantInfo() const;
147 
149 
155  DLLEXPORT QoreHashNode* getInfo() const;
156 
158 
161  DLLEXPORT const char* getName() const;
162 
164 
167  DLLEXPORT void addNamespace(QoreNamespace* ns);
168 
170 
175  DLLEXPORT void addInitialNamespace(QoreNamespace* ns);
176 
178 
186  DLLEXPORT QoreNamespace* findCreateNamespacePath(const char* nspath);
187 
189 
194  DLLEXPORT QoreClass* findLocalClass(const char* cname) const;
195 
197 
201  DLLEXPORT QoreNamespace* findLocalNamespace(const char* nsname) const;
202 
204 
207  DLLEXPORT void setClassHandler(q_ns_class_handler_t class_handler);
208 
210 
212  DLLEXPORT const QoreNamespace* getParent() const;
213 
215  DLLEXPORT void deleteData(ExceptionSink* xsink);
216 
218  DLLEXPORT void addBuiltinVariant(const char* name, q_func_n_t f, int64 code_flags = QCF_NO_FLAGS, int64 functional_domain = QDOM_DEFAULT, const QoreTypeInfo* returnTypeInfo = 0, unsigned num_params = 0, ...);
219 
221 
223  DLLEXPORT const QoreExternalFunction* findLocalFunction(const char* name) const;
224 
226 
228  DLLEXPORT const QoreExternalConstant* findLocalConstant(const char* name) const;
229 
231 
233  DLLEXPORT const QoreExternalGlobalVar* findLocalGlobalVar(const char* name) const;
234 
236 
238  DLLEXPORT const TypedHashDecl* findLocalTypedHash(const char* name) const;
239 
241 
245  DLLEXPORT std::string getPath(bool anchored = false) const;
246 
248 
250  DLLEXPORT bool isModulePublic() const;
251 
253 
255  DLLEXPORT bool isBuiltin() const;
256 
258 
260  DLLEXPORT bool isImported() const;
261 
263 
265  DLLEXPORT bool isRoot() const;
266 
267 private:
269  QoreNamespace(const QoreNamespace&) = delete;
270 
272  QoreNamespace& operator=(const QoreNamespace&) = delete;
273 
274 protected:
275  class qore_ns_private* priv; // private implementation
276 
277  // protected, function not exported in the API
278  DLLLOCAL QoreNamespace(qore_ns_private* p);
279 };
280 
282 
287  friend class qore_ns_private;
288  friend class qore_root_ns_private;
289  friend class StaticSystemNamespace;
290 
291 public:
293 
296  DLLEXPORT QoreNamespace* rootGetQoreNamespace() const;
297 
299  DLLLOCAL virtual ~RootQoreNamespace();
300 
301 protected:
302  // private implementation
303  class qore_root_ns_private* rpriv;
304 
305 private:
307  RootQoreNamespace(const RootQoreNamespace&) = delete;
308 
310  RootQoreNamespace& operator=(const RootQoreNamespace&) = delete;
311 
312  DLLLOCAL RootQoreNamespace(class qore_root_ns_private* p);
313 };
314 
315 class QorePrivateNamespaceIterator;
316 
318 
321 public:
323  DLLEXPORT QoreNamespaceIterator(QoreNamespace& ns);
324 
326  DLLEXPORT virtual ~QoreNamespaceIterator();
327 
329  DLLEXPORT bool next();
330 
332  DLLEXPORT QoreNamespace* operator->();
333 
335  DLLEXPORT QoreNamespace* operator*();
336 
338  DLLEXPORT QoreNamespace& get();
339 
341  DLLEXPORT const QoreNamespace* operator->() const;
343  DLLEXPORT const QoreNamespace* operator*() const;
344 
346  DLLEXPORT const QoreNamespace& get() const;
347 
348 private:
350  QoreNamespaceIterator(const QoreNamespaceIterator&) = delete;
351 
353  QoreNamespaceIterator& operator=(const QoreNamespaceIterator&) = delete;
354 
355  QorePrivateNamespaceIterator* priv;
356 };
357 
359 
362 public:
364  DLLEXPORT QoreNamespaceConstIterator(const QoreNamespace& ns);
365 
367  DLLEXPORT virtual ~QoreNamespaceConstIterator();
368 
370  DLLEXPORT bool next();
371 
373  DLLEXPORT const QoreNamespace* operator->() const;
374 
376  DLLEXPORT const QoreNamespace* operator*() const;
377 
379  DLLEXPORT const QoreNamespace& get() const;
380 
381 private:
383  QoreNamespaceConstIterator(const QoreNamespaceConstIterator&) = delete;
384 
386  QoreNamespaceConstIterator& operator=(const QoreNamespaceConstIterator&) = delete;
387 
388  QorePrivateNamespaceIterator* priv;
389 };
390 
392 
395 public:
397  DLLEXPORT QoreNamespaceNamespaceIterator(const QoreNamespace& ns);
398 
400  DLLEXPORT virtual ~QoreNamespaceNamespaceIterator();
401 
403  DLLEXPORT bool next();
404 
406  DLLEXPORT const QoreNamespace& get() const;
407 
408 private:
410  QoreNamespaceNamespaceIterator(const QoreNamespaceNamespaceIterator&) = delete;
411 
413  QoreNamespaceNamespaceIterator& operator=(const QoreNamespaceNamespaceIterator&) = delete;
414 
415  class qore_namespace_namespace_iterator* priv;
416 };
417 
419 
422 public:
424  DLLEXPORT QoreNamespaceFunctionIterator(const QoreNamespace& ns);
425 
427  DLLEXPORT virtual ~QoreNamespaceFunctionIterator();
428 
430  DLLEXPORT bool next();
431 
433  DLLEXPORT const QoreExternalFunction& get() const;
434 
435 private:
437  QoreNamespaceFunctionIterator(const QoreNamespaceFunctionIterator&) = delete;
438 
440  QoreNamespaceFunctionIterator& operator=(const QoreNamespaceFunctionIterator&) = delete;
441 
442  class qore_namespace_function_iterator* priv;
443 };
444 
446 
449 public:
451  DLLEXPORT QoreNamespaceConstantIterator(const QoreNamespace& ns);
452 
454  DLLEXPORT virtual ~QoreNamespaceConstantIterator();
455 
457  DLLEXPORT bool next();
458 
460  DLLEXPORT const QoreExternalConstant& get() const;
461 
462 private:
464  QoreNamespaceConstantIterator(const QoreNamespaceConstantIterator&) = delete;
465 
467  QoreNamespaceConstantIterator& operator=(const QoreNamespaceConstantIterator&) = delete;
468 
469  class qore_namespace_constant_iterator* priv;
470 };
471 
473 
476 public:
478  DLLEXPORT QoreNamespaceClassIterator(const QoreNamespace& ns);
479 
481  DLLEXPORT virtual ~QoreNamespaceClassIterator();
482 
484  DLLEXPORT bool next();
485 
487  DLLEXPORT const QoreClass& get() const;
488 
489 private:
491  QoreNamespaceClassIterator(const QoreNamespaceClassIterator&) = delete;
492 
494  QoreNamespaceClassIterator& operator=(const QoreNamespaceClassIterator&) = delete;
495 
496  class ConstClassListIterator* priv;
497 };
498 
500 
503 public:
505  DLLEXPORT QoreNamespaceGlobalVarIterator(const QoreNamespace& ns);
506 
508  DLLEXPORT virtual ~QoreNamespaceGlobalVarIterator();
509 
511  DLLEXPORT bool next();
512 
514  DLLEXPORT const QoreExternalGlobalVar& get() const;
515 
516 private:
518  QoreNamespaceGlobalVarIterator(const QoreNamespaceGlobalVarIterator&) = delete;
519 
521  QoreNamespaceGlobalVarIterator& operator=(const QoreNamespaceGlobalVarIterator&) = delete;
522 
523  class qore_namespace_globalvar_iterator* priv;
524 };
525 
527 
530 public:
532  DLLEXPORT QoreNamespaceTypedHashIterator(const QoreNamespace& ns);
533 
535  DLLEXPORT virtual ~QoreNamespaceTypedHashIterator();
536 
538  DLLEXPORT bool next();
539 
541  DLLEXPORT const TypedHashDecl& get() const;
542 
543 private:
545  QoreNamespaceTypedHashIterator(const QoreNamespaceTypedHashIterator&) = delete;
546 
548  QoreNamespaceTypedHashIterator& operator=(const QoreNamespaceTypedHashIterator&) = delete;
549 
550  class ConstHashDeclListIterator* priv;
551 };
552 
553 #endif // QORE_NAMESPACE_H
allows functions in a namespace to be iterated
Definition: QoreNamespace.h:421
#define PO_DEFAULT
no parse options set by default
Definition: Restrictions.h:105
allows all namespaces of a namespace to be iterated (including the namespace passed in the constructo...
Definition: QoreNamespace.h:320
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
DLLEXPORT void setClassHandler(q_ns_class_handler_t class_handler)
sets the namespace class handler
DLLEXPORT bool isRoot() const
returns true if the namespace is the root namespace
DLLEXPORT void addBuiltinVariant(const char *name, q_func_n_t f, int64 code_flags=QCF_NO_FLAGS, int64 functional_domain=QDOM_DEFAULT, const QoreTypeInfo *returnTypeInfo=0, unsigned num_params=0,...)
adds a function variant
DLLEXPORT void addNamespace(QoreNamespace *ns)
adds a namespace to the namespace tree
DLLEXPORT void clear(ExceptionSink *xsink)
clears the contents of the namespace before deleting
DLLEXPORT const TypedHashDecl * findLocalTypedHash(const char *name) const
find a typed hash (hashdecl) in the current namespace; returns nullptr if not found ...
DLLEXPORT bool isModulePublic() const
returns true if the namespace has its module public flag set
contains constants, classes, and subnamespaces in QoreProgram objects
Definition: QoreNamespace.h:64
DLLEXPORT const QoreExternalGlobalVar * findLocalGlobalVar(const char *name) const
find a global variable in the current namespace; returns nullptr if not found
DLLEXPORT QoreHashNode * getConstantInfo() const
a hash of all constants in the namespace, the hash keys are the constant names and the values are the...
DLLEXPORT QoreClass * findLocalClass(const char *cname) const
finds a class in this namespace, does not search child namespaces
allows all namespaces of a namespace to be iterated (including the namespace passed in the constructo...
Definition: QoreNamespace.h:361
DLLEXPORT const QoreNamespace * getParent() const
returns a pointer to the parent namespace or nullptr if there is no parent
allows typed hashes (hashdecls) in a namespace to be iterated
Definition: QoreNamespace.h:529
DLLEXPORT std::string getPath(bool anchored=false) const
returns the path for the namespace
the root namespace of a QoreProgram object
Definition: QoreNamespace.h:286
DLLEXPORT QoreNamespace(const char *n)
creates a namespace with the given name
defines a Qore-language class
Definition: QoreClass.h:239
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:262
DLLEXPORT void addInitialNamespace(QoreNamespace *ns)
adds a subnamespace to the namespace
DLLEXPORT QoreNamespace * findCreateNamespacePath(const char *nspath)
finds a Namespace based on the argument; creates it (or the whole path) if necessary ...
static unsigned num_params(const QoreListNode *n)
returns the number of arguments passed to the function
Definition: params.h:54
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:46
DLLEXPORT const char * getName() const
returns the name of the namespace
DLLEXPORT void addSystemHashDecl(TypedHashDecl *hashdecl)
adds a hashdecl to a namespace
DLLEXPORT bool isImported() const
returns true if the namespace was imported from another program object
allows classes in a namespace to be iterated
Definition: QoreNamespace.h:475
allows global variables in a namespace to be iterated
Definition: QoreNamespace.h:502
allows local namespaces to be iterated
Definition: QoreNamespace.h:394
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
virtual DLLEXPORT ~QoreNamespace()
destroys the object and frees memory
DLLEXPORT void addConstant(const char *name, QoreValue value)
adds a constant definition to the namespace
typed hash declaration
Definition: TypedHashDecl.h:44
DLLEXPORT QoreHashNode * getInfo() const
returns a hash giving information about the definitions in the namespace
DLLEXPORT QoreNamespace * findLocalNamespace(const char *nsname) const
finds a subnamespace in this namespace, does not search child namespaces
DLLEXPORT void deleteData(ExceptionSink *xsink)
this function must be called before the QoreNamespace object is deleted or a crash could result due i...
DLLEXPORT QoreNamespace * copy(int po) const
returns a deep copy of the namespace; DEPRECATED: use copy(int64) instead
DLLEXPORT void addSystemClass(QoreClass *oc)
adds a class to a namespace
DLLEXPORT const QoreExternalFunction * findLocalFunction(const char *name) const
find a function in the current namespace; returns nullptr if not found
DLLEXPORT QoreHashNode * getClassInfo() const
gets a hash of all classes in the namespace, the hash keys are the class names and the values are lis...
allows constants in a namespace to be iterated
Definition: QoreNamespace.h:448
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
DLLEXPORT bool isBuiltin() const
returns true if the namespace is builtin
DLLEXPORT const QoreExternalConstant * findLocalConstant(const char *name) const
find a constant in the current namespace; returns nullptr if not found
#define QDOM_DEFAULT
the default domain (no domain)
Definition: Restrictions.h:155