Qore Programming Language  0.9.16
QoreClassList.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreClassList.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2019 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_QORECLASSLIST_H
33 
34 #define _QORE_QORECLASSLIST_H
35 
36 #include <qore/common.h>
37 
38 #include <qore/QoreClass.h>
39 
40 #include <cstdlib>
41 #include <cstring>
42 
43 hashdecl cl_rec_t {
44  QoreClass* cls = nullptr;
45  bool priv = false;
46 
47  DLLLOCAL cl_rec_t() {
48  }
49 
50  DLLLOCAL cl_rec_t(QoreClass* c, bool p) : cls(c), priv(p) {
51  }
52 };
53 
54 // we use a vector map as the number of classes is generally relatively small
55 #include <qore/vector_map>
56 typedef vector_map_t<const char*, cl_rec_t> hm_qc_t;
57 /*
58 #ifdef HAVE_QORE_HASH_MAP
59 //#warning compiling with hash_map
60 #include <qore/hash_map_include.h>
61 #include "qore/intern/xxhash.h"
62 
63 typedef HASH_MAP<const char*, cl_rec_t, qore_hash_str, eqstr> hm_qc_t;
64 #else
65 #include <map>
66 typedef std::map<const char*, cl_rec_t, ltstr> hm_qc_t;
67 #endif
68 */
69 
70 class QoreNamespaceList;
71 
72 class ClassListIterator;
73 class ConstClassListIterator;
74 
75 class QoreClassList {
76  friend class ClassListIterator;
77  friend class ConstClassListIterator;
78 
79 private:
80  hm_qc_t hm; // hash_map for name lookups
81  bool ns_const : 1;
82  bool ns_vars : 1;
83 
84  DLLLOCAL void deleteAll();
85 
86  DLLLOCAL void remove(hm_qc_t::iterator i);
87 
88  DLLLOCAL void addInternal(QoreClass* ot, bool priv);
89 
90 public:
91  DLLLOCAL QoreClassList() : ns_const(false), ns_vars(false) {}
92  DLLLOCAL ~QoreClassList();
93  DLLLOCAL QoreClassList(const QoreClassList& old, int64 po, qore_ns_private* ns);
94 
95  DLLLOCAL void mergeUserPublic(const QoreClassList& old, qore_ns_private* ns);
96 
97  // returns the number of classes imported
98  DLLLOCAL int importSystemClasses(const QoreClassList& source, qore_ns_private* ns, ExceptionSink* xsink);
99 
100  DLLLOCAL int add(QoreClass* ot);
101  DLLLOCAL QoreClass* find(const char* name);
102  DLLLOCAL const QoreClass* find(const char* name) const;
103  DLLLOCAL void resolveCopy();
104  DLLLOCAL void parseResolveHierarchy();
105  DLLLOCAL void parseResolveClassMembers();
106  DLLLOCAL void parseInit();
107  DLLLOCAL void parseResolveAbstract();
108  DLLLOCAL void parseRollback();
109  DLLLOCAL void parseCommit();
110  DLLLOCAL void parseCommitRuntimeInit(ExceptionSink* xsink);
111  DLLLOCAL void reset();
112  DLLLOCAL void assimilate(QoreClassList& n, qore_ns_private& ns);
113  DLLLOCAL QoreHashNode* getInfo();
114 
115  //DLLLOCAL QoreValue findConstant(const char* cname, const QoreTypeInfo*& typeInfo, bool& found);
116 
117  //DLLLOCAL AbstractQoreNode* parseResolveBareword(const QoreProgramLocation* loc, const char* name, const QoreTypeInfo*& typeInfo);
118 
119  DLLLOCAL bool empty() const {
120  return hm.empty();
121  }
122 
123  // clears static class vars
124  DLLLOCAL void clear(ExceptionSink* xsink);
125  DLLLOCAL void clearConstants(QoreListNode& l);
126  DLLLOCAL void clearConstants(ExceptionSink* xsink);
127  DLLLOCAL void deleteClassData(bool deref_vars, ExceptionSink* xsink);
128 };
129 
130 class ClassListIterator {
131 protected:
132  hm_qc_t& cl;
133  hm_qc_t::iterator i;
134 
135 public:
136  DLLLOCAL ClassListIterator(QoreClassList& n_cl) : cl(n_cl.hm), i(cl.end()) {
137  }
138 
139  DLLLOCAL bool next() {
140  if (i == cl.end())
141  i = cl.begin();
142  else
143  ++i;
144  return i != cl.end();
145  }
146 
147  DLLLOCAL const char* getName() const {
148  return i->first;
149  }
150 
151  DLLLOCAL QoreClass* get() const {
152  return i->second.cls;
153  }
154 
155  DLLLOCAL bool isPublic() const;
156 
157  DLLLOCAL bool isUserPublic() const;
158 };
159 
160 class ConstClassListIterator {
161 protected:
162  const hm_qc_t& cl;
163  hm_qc_t::const_iterator i;
164 
165 public:
166  DLLLOCAL ConstClassListIterator(const QoreClassList& n_cl) : cl(n_cl.hm), i(cl.end()) {
167  }
168 
169  DLLLOCAL bool next() {
170  if (i == cl.end())
171  i = cl.begin();
172  else
173  ++i;
174  return i != cl.end();
175  }
176 
177  DLLLOCAL const char* getName() const {
178  return i->first;
179  }
180 
181  DLLLOCAL const QoreClass* get() const {
182  return i->second.cls;
183  }
184 
185  DLLLOCAL bool isPublic() const;
186 
187  DLLLOCAL bool isUserPublic() const;
188 };
189 
190 #endif // _QORE_QORECLASSLIST_H
QoreHashNode
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition: QoreHashNode.h:50
QoreClass
defines a Qore-language class
Definition: QoreClass.h:239
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
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
common.h