Qore Programming Language  0.9.2
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 <string.h>
47 #include <stdlib.h>
48 
49 #include <string>
50 
51 // forward declarations
52 class QoreExternalFunction;
54 class QoreExternalGlobalVar;
55 
57 
62 typedef QoreClass* (*q_ns_class_handler_t)(QoreNamespace* ns, const char* cname);
63 
66  friend class QoreNamespaceList;
67  friend class RootQoreNamespace;
68  friend class qore_ns_private;
69  friend class qore_root_ns_private;
70  friend hashdecl NSOInfoBase;
71 
72 public:
74 
77  DLLEXPORT QoreNamespace(const char* n);
78 
80  DLLEXPORT virtual ~QoreNamespace();
81 
83 
87  DLLEXPORT void clear(ExceptionSink* xsink);
88 
90 
96  DLLEXPORT void addConstant(const char* name, QoreValue value);
97 
99 
105  DLLEXPORT void addConstant(const char* name, QoreValue value, const QoreTypeInfo* typeInfo);
106 
108 
111  DLLEXPORT void addSystemClass(QoreClass* oc);
112 
114 
119  DLLEXPORT void addSystemHashDecl(TypedHashDecl* hashdecl);
120 
122 
125  DLLEXPORT QoreNamespace* copy(int po) const;
126 
128 
131  DLLEXPORT QoreNamespace* copy(int64 po = PO_DEFAULT) const;
132 
134 
139  DLLEXPORT QoreHashNode* getClassInfo() const;
140 
142 
147  DLLEXPORT QoreHashNode* getConstantInfo() const;
148 
150 
156  DLLEXPORT QoreHashNode* getInfo() const;
157 
159 
162  DLLEXPORT const char* getName() const;
163 
165 
168  DLLEXPORT void addNamespace(QoreNamespace* ns);
169 
171 
176  DLLEXPORT void addInitialNamespace(QoreNamespace* ns);
177 
179 
187  DLLEXPORT QoreNamespace* findCreateNamespacePath(const char* nspath);
188 
190 
195  DLLEXPORT QoreClass* findLocalClass(const char* cname) const;
196 
198 
202  DLLEXPORT QoreNamespace* findLocalNamespace(const char* nsname) const;
203 
205 
208  DLLEXPORT void setClassHandler(q_ns_class_handler_t class_handler);
209 
211 
213  DLLEXPORT const QoreNamespace* getParent() const;
214 
216  DLLEXPORT void deleteData(ExceptionSink* xsink);
217 
219  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, ...);
220 
222 
224  DLLEXPORT const QoreExternalFunction* findLocalFunction(const char* name) const;
225 
227 
229  DLLEXPORT const QoreExternalConstant* findLocalConstant(const char* name) const;
230 
232 
234  DLLEXPORT const QoreExternalGlobalVar* findLocalGlobalVar(const char* name) const;
235 
237 
239  DLLEXPORT const TypedHashDecl* findLocalTypedHash(const char* name) const;
240 
242 
246  DLLEXPORT std::string getPath(bool anchored = false) const;
247 
249 
251  DLLEXPORT bool isModulePublic() const;
252 
254 
256  DLLEXPORT bool isBuiltin() const;
257 
259 
261  DLLEXPORT bool isImported() const;
262 
264 
266  DLLEXPORT bool isRoot() const;
267 
268 private:
270  QoreNamespace(const QoreNamespace&) = delete;
271 
273  QoreNamespace& operator=(const QoreNamespace&) = delete;
274 
275 protected:
276  class qore_ns_private* priv; // private implementation
277 
278  // protected, function not exported in the API
279  DLLLOCAL QoreNamespace(qore_ns_private* p);
280 };
281 
283 
288  friend class qore_ns_private;
289  friend class qore_root_ns_private;
290  friend class StaticSystemNamespace;
291 
292 public:
294 
297  DLLEXPORT QoreNamespace* rootGetQoreNamespace() const;
298 
300  DLLLOCAL virtual ~RootQoreNamespace();
301 
302 protected:
303  // private implementation
304  class qore_root_ns_private* rpriv;
305 
306 private:
308  RootQoreNamespace(const RootQoreNamespace&) = delete;
309 
311  RootQoreNamespace& operator=(const RootQoreNamespace&) = delete;
312 
313  DLLLOCAL RootQoreNamespace(class qore_root_ns_private* p);
314 };
315 
316 class QorePrivateNamespaceIterator;
317 
319 
322 public:
324  DLLEXPORT QoreNamespaceIterator(QoreNamespace& ns);
325 
327  DLLEXPORT virtual ~QoreNamespaceIterator();
328 
330  DLLEXPORT bool next();
331 
333  DLLEXPORT QoreNamespace* operator->();
334 
336  DLLEXPORT QoreNamespace* operator*();
337 
339  DLLEXPORT QoreNamespace& get();
340 
342  DLLEXPORT const QoreNamespace* operator->() const;
344  DLLEXPORT const QoreNamespace* operator*() const;
345 
347  DLLEXPORT const QoreNamespace& get() const;
348 
349 private:
352 
354  QoreNamespaceIterator& operator=(const QoreNamespaceIterator&) = delete;
355 
356  QorePrivateNamespaceIterator* priv;
357 };
358 
360 
363 public:
365  DLLEXPORT QoreNamespaceConstIterator(const QoreNamespace& ns);
366 
368  DLLEXPORT virtual ~QoreNamespaceConstIterator();
369 
371  DLLEXPORT bool next();
372 
374  DLLEXPORT const QoreNamespace* operator->() const;
375 
377  DLLEXPORT const QoreNamespace* operator*() const;
378 
380  DLLEXPORT const QoreNamespace& get() const;
381 
382 private:
385 
387  QoreNamespaceConstIterator& operator=(const QoreNamespaceConstIterator&) = delete;
388 
389  QorePrivateNamespaceIterator* priv;
390 };
391 
393 
396 public:
398  DLLEXPORT QoreNamespaceNamespaceIterator(const QoreNamespace& ns);
399 
401  DLLEXPORT virtual ~QoreNamespaceNamespaceIterator();
402 
404  DLLEXPORT bool next();
405 
407  DLLEXPORT const QoreNamespace& get() const;
408 
409 private:
412 
415 
416  class qore_namespace_namespace_iterator* priv;
417 };
418 
420 
423 public:
425  DLLEXPORT QoreNamespaceFunctionIterator(const QoreNamespace& ns);
426 
428  DLLEXPORT virtual ~QoreNamespaceFunctionIterator();
429 
431  DLLEXPORT bool next();
432 
434  DLLEXPORT const QoreExternalFunction& get() const;
435 
436 private:
439 
442 
443  class qore_namespace_function_iterator* priv;
444 };
445 
447 
450 public:
452  DLLEXPORT QoreNamespaceConstantIterator(const QoreNamespace& ns);
453 
455  DLLEXPORT virtual ~QoreNamespaceConstantIterator();
456 
458  DLLEXPORT bool next();
459 
461  DLLEXPORT const QoreExternalConstant& get() const;
462 
463 private:
466 
469 
470  class qore_namespace_constant_iterator* priv;
471 };
472 
474 
477 public:
479  DLLEXPORT QoreNamespaceClassIterator(const QoreNamespace& ns);
480 
482  DLLEXPORT virtual ~QoreNamespaceClassIterator();
483 
485  DLLEXPORT bool next();
486 
488  DLLEXPORT const QoreClass& get() const;
489 
490 private:
493 
495  QoreNamespaceClassIterator& operator=(const QoreNamespaceClassIterator&) = delete;
496 
497  class ConstClassListIterator* priv;
498 };
499 
501 
504 public:
506  DLLEXPORT QoreNamespaceGlobalVarIterator(const QoreNamespace& ns);
507 
509  DLLEXPORT virtual ~QoreNamespaceGlobalVarIterator();
510 
512  DLLEXPORT bool next();
513 
515  DLLEXPORT const QoreExternalGlobalVar& get() const;
516 
517 private:
520 
523 
524  class qore_namespace_globalvar_iterator* priv;
525 };
526 
528 
531 public:
533  DLLEXPORT QoreNamespaceTypedHashIterator(const QoreNamespace& ns);
534 
536  DLLEXPORT virtual ~QoreNamespaceTypedHashIterator();
537 
539  DLLEXPORT bool next();
540 
542  DLLEXPORT const TypedHashDecl& get() const;
543 
544 private:
547 
550 
551  class ConstHashDeclListIterator* priv;
552 };
553 
554 #endif // QORE_NAMESPACE_H
DLLEXPORT QoreNamespaceConstIterator(const QoreNamespace &ns)
creates the iterator; the namespace given will also be included in the iteration set ...
allows functions in a namespace to be iterated
Definition: QoreNamespace.h:422
DLLEXPORT const QoreNamespace * operator*() const
returns the namespace
#define PO_DEFAULT
no parse options set by default
Definition: Restrictions.h:102
allows all namespaces of a namespace to be iterated (including the namespace passed in the constructo...
Definition: QoreNamespace.h:321
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 bool next()
moves to the next position; returns true if on a valid position
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 bool next()
moves to the next position; returns true if on a valid position
virtual DLLEXPORT ~QoreNamespaceConstantIterator()
destroys the object
DLLEXPORT QoreNamespaceClassIterator(const QoreNamespace &ns)
creates the iterator
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 QoreNamespace * operator*()
returns the namespace
virtual DLLEXPORT ~QoreNamespaceTypedHashIterator()
destroys the object
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:65
DLLEXPORT bool next()
moves to the next position; returns true if on a valid position
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:362
DLLEXPORT const QoreNamespace * getParent() const
returns a pointer to the parent namespace or nullptr if there is no parent
DLLEXPORT QoreNamespaceTypedHashIterator(const QoreNamespace &ns)
creates the iterator
virtual DLLEXPORT ~QoreNamespaceNamespaceIterator()
destroys the object
allows typed hashes (hashdecls) in a namespace to be iterated
Definition: QoreNamespace.h:530
DLLEXPORT std::string getPath(bool anchored=false) const
returns the path for the namespace
the root namespace of a QoreProgram object
Definition: QoreNamespace.h:287
DLLEXPORT QoreNamespace(const char *n)
creates a namespace with the given name
DLLEXPORT QoreNamespaceIterator(QoreNamespace &ns)
creates the iterator; the namespace given will also be included in the iteration set ...
virtual DLLLOCAL ~RootQoreNamespace()
destructor is not exported in the library&#39;s public API
defines a Qore-language class
Definition: QoreClass.h:234
DLLEXPORT QoreNamespace * operator->()
returns the namespace
virtual DLLEXPORT ~QoreNamespaceGlobalVarIterator()
destroys the object
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:476
allows global variables in a namespace to be iterated
Definition: QoreNamespace.h:503
virtual DLLEXPORT ~QoreNamespaceConstIterator()
destroys the object
DLLEXPORT const QoreNamespace * operator->() const
returns the namespace
allows local namespaces to be iterated
Definition: QoreNamespace.h:395
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
virtual DLLEXPORT ~QoreNamespaceClassIterator()
destroys the object
typed hash declaration
Definition: TypedHashDecl.h:44
DLLEXPORT QoreNamespace * rootGetQoreNamespace() const
returns a pointer to the QoreNamespace for the "Qore" namespace
DLLEXPORT QoreNamespaceFunctionIterator(const QoreNamespace &ns)
creates the iterator
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 bool next()
moves to the next position; returns true if on a valid position
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 bool next()
moves to the next position; returns true if on a valid position
DLLEXPORT bool next()
moves to the next position; returns true if on a valid position
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...
DLLEXPORT QoreNamespaceConstantIterator(const QoreNamespace &ns)
creates the iterator
DLLEXPORT bool next()
moves to the next position; returns true if on a valid position
DLLEXPORT QoreNamespaceGlobalVarIterator(const QoreNamespace &ns)
creates the iterator
virtual DLLEXPORT ~QoreNamespaceFunctionIterator()
destroys the object
allows constants in a namespace to be iterated
Definition: QoreNamespace.h:449
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
DLLEXPORT QoreNamespaceNamespaceIterator(const QoreNamespace &ns)
creates the iterator
DLLEXPORT bool next()
moves to the next position; returns true if on a valid position
#define QDOM_DEFAULT
the default domain (no domain)
Definition: Restrictions.h:152
virtual DLLEXPORT ~QoreNamespaceIterator()
destroys the object