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