Qore Programming Language  0.9.16
QoreNamespaceList.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreNamespaceList.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2019 Qore Technologies, s.r.o.
8 
9  namespaces are children of a program object. there is a parse
10  lock per program object to ensure that objects are added (or backed out)
11  atomically per program object. All the objects referenced here should
12  be safe to read & copied at all times. They will only be deleted when the
13  program object is deleted (except the pending structures, which will be
14  deleted any time there is a parse error, together with all other
15  pending structures)
16 
17  Permission is hereby granted, free of charge, to any person obtaining a
18  copy of this software and associated documentation files (the "Software"),
19  to deal in the Software without restriction, including without limitation
20  the rights to use, copy, modify, merge, publish, distribute, sublicense,
21  and/or sell copies of the Software, and to permit persons to whom the
22  Software is furnished to do so, subject to the following conditions:
23 
24  The above copyright notice and this permission notice shall be included in
25  all copies or substantial portions of the Software.
26 
27  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33  DEALINGS IN THE SOFTWARE.
34 
35  Note that the Qore library is released under a choice of three open-source
36  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
37  information.
38 */
39 
40 #ifndef _QORE_NAMESPACELIST_H
41 
42 #define _QORE_NAMESPACELIST_H
43 
44 // issue #3461: cannot use a vector map because we erase in the map while iterating
45 
46 #ifdef HAVE_QORE_HASH_MAP
47 //#warning compiling with hash_map
48 #include <qore/hash_map_include.h>
49 #include "qore/intern/xxhash.h"
50 
51 typedef HASH_MAP<std::string, QoreNamespace*> nsmap_t;
52 #else
53 #include <map>
54 typedef std::map<std::string, QoreNamespace*> nsmap_t;
55 #endif
56 
57 class qore_ns_private;
58 class qore_root_ns_private;
59 
60 hashdecl GVEntryBase {
61  NamedScope* name;
62  Var* var;
63 
64  DLLLOCAL GVEntryBase(const NamedScope& n, Var* v) : name(new NamedScope(n)), var(v) {
65  }
66 
67  DLLLOCAL GVEntryBase(const QoreProgramLocation* loc, char* n, const QoreTypeInfo* typeInfo, QoreParseTypeInfo* parseTypeInfo);
68 
69  DLLLOCAL GVEntryBase(const GVEntryBase& old) : name(old.name), var(old.var) {
70  }
71 
72  DLLLOCAL void clear();
73 
74  DLLLOCAL Var* takeVar() {
75  Var* rv = var;
76  var = nullptr;
77  return rv;
78  }
79 };
80 
81 hashdecl GVEntry : public GVEntryBase {
82  qore_ns_private* ns;
83 
84  DLLLOCAL GVEntry(qore_ns_private* n_ns, const NamedScope& n, Var* v) : GVEntryBase(n, v), ns(n_ns) {
85  }
86 
87  DLLLOCAL GVEntry(const GVEntry& old) : GVEntryBase(old), ns(old.ns) {
88  }
89 
90  DLLLOCAL GVEntry(const GVEntryBase& old, qore_ns_private* n_ns) : GVEntryBase(old), ns(n_ns) {
91  }
92 };
93 
94 template <class T>
95 hashdecl GVList : public std::vector<T> {
96  DLLLOCAL ~GVList() {
97  clear();
98  }
99 
100  DLLLOCAL void clear() {
101  for (typename GVList<T>::iterator i = std::vector<T>::begin(), e = std::vector<T>::end(); i != e; ++i)
102  (*i).clear();
103  std::vector<T>::clear();
104  }
105 
106  DLLLOCAL void zero() {
107  std::vector<T>::clear();
108  }
109 };
110 
111 typedef GVList<GVEntryBase> gvblist_t;
112 typedef GVList<GVEntry> gvlist_t;
113 
114 class QoreNamespaceList {
115 private:
116  DLLLOCAL void deleteAll();
117 
118  // not implemented
119  DLLLOCAL QoreNamespaceList(const QoreNamespaceList& old);
120  // not implemented
121  DLLLOCAL QoreNamespaceList& operator=(const QoreNamespaceList& nsl);
122 
123 public:
124  nsmap_t nsmap;
125 
126  DLLLOCAL QoreNamespaceList() {
127  }
128 
129  DLLLOCAL QoreNamespaceList(const QoreNamespaceList& old, int64 po, const qore_ns_private& parent);
130 
131  DLLLOCAL ~QoreNamespaceList() {
132  deleteAll();
133  }
134 
135  DLLLOCAL QoreNamespace *find(const char* name) {
136  nsmap_t::iterator i = nsmap.find(name);
137  return i == nsmap.end() ? 0 : i->second;
138  }
139 
140  DLLLOCAL QoreNamespace *find(const std::string &name) {
141  nsmap_t::iterator i = nsmap.find(name);
142  return i == nsmap.end() ? 0 : i->second;
143  }
144  DLLLOCAL const QoreNamespace* find(const std::string& name) const {
145  nsmap_t::const_iterator i = nsmap.find(name);
146  return i == nsmap.end() ? 0 : i->second;
147  }
148 
149  // do not delete the pointer returned from this function
150  DLLLOCAL qore_ns_private* parseAdd(QoreNamespace* ot, qore_ns_private* parent);
151 
152  DLLLOCAL qore_ns_private* runtimeAdd(QoreNamespace* ot, qore_ns_private* parent);
153 
154  DLLLOCAL void resolveCopy();
155  DLLLOCAL void parseInitConstants();
156 
157  DLLLOCAL void parseInitGlobalVars();
158  DLLLOCAL void clearConstants(QoreListNode& l);
159  DLLLOCAL void clearData(ExceptionSink* sink);
160  DLLLOCAL void deleteGlobalVars(ExceptionSink* sink);
161 
162  DLLLOCAL void parseResolveHierarchy();
163  DLLLOCAL void parseResolveClassMembers();
164  DLLLOCAL void parseInit();
165  DLLLOCAL void parseResolveAbstract();
166  DLLLOCAL void parseCommit();
167  DLLLOCAL void parseCommitRuntimeInit(ExceptionSink* sink);
168  DLLLOCAL void parseRollback(ExceptionSink* sink);
169  DLLLOCAL void deleteAllConstants(ExceptionSink *xsink);
170  DLLLOCAL void reset();
171 
172  DLLLOCAL void parseAssimilate(QoreNamespaceList& n, qore_ns_private* parent);
173  DLLLOCAL void runtimeAssimilate(QoreNamespaceList& n, qore_ns_private* parent);
174 
175  DLLLOCAL bool addGlobalVars(qore_root_ns_private& rns);
176 
177  DLLLOCAL void deleteData(bool deref_vars, ExceptionSink *xsink);
178 
179  DLLLOCAL bool empty() const {
180  return nsmap.empty();
181  }
182 
183  DLLLOCAL qore_size_t size() const {
184  return nsmap.size();
185  }
186 
187  DLLLOCAL void getGlobalVars(QoreHashNode& h) const;
188 };
189 
190 #endif
QoreHashNode
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition: QoreHashNode.h:50
qore_size_t
size_t qore_size_t
used for sizes (same range as a pointer)
Definition: common.h:73
QoreListNode
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
int64
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
QoreNamespace
contains constants, classes, and subnamespaces in QoreProgram objects
Definition: QoreNamespace.h:65
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48