Qore Programming Language  1.10.0
QoreObject.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreObject.h
4 
5  thread-safe object definition
6 
7  Qore Programming Language
8 
9  Copyright (C) 2003 - 2022 Qore Technologies, s.r.o.
10 
11  Permission is hereby granted, free of charge, to any person obtaining a
12  copy of this software and associated documentation files (the "Software"),
13  to deal in the Software without restriction, including without limitation
14  the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  and/or sell copies of the Software, and to permit persons to whom the
16  Software is furnished to do so, subject to the following conditions:
17 
18  The above copyright notice and this permission notice shall be included in
19  all copies or substantial portions of the Software.
20 
21  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  DEALINGS IN THE SOFTWARE.
28 
29  Note that the Qore library is released under a choice of three open-source
30  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
31  information.
32 */
33 
34 #ifndef _QORE_QOREOBJECT_H
35 
36 #define _QORE_QOREOBJECT_H
37 
38 class AutoVLock;
39 class VRMutex;
40 class BuiltinCopy;
41 class BuiltinNormalMethodVariantBase;
42 class BuiltinCopyVariantBase;
44 class QoreProgram;
45 
47 
60 class QoreObject : public AbstractQoreNode {
61  friend class qore_object_private;
62  friend class ObjectRSetHelper;
63  friend class ObjectRSet;
64  friend class qore_object_dereference_helper;
65 
66 private:
68  class qore_object_private* priv;
69 
71  DLLLOCAL QoreObject(const QoreObject&);
72 
74  DLLLOCAL QoreObject& operator=(const QoreObject&);
75 
76 protected:
78 
82  DLLEXPORT virtual bool derefImpl(ExceptionSink* xsink);
83 
85 
87  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
88 
90  DLLLOCAL virtual void customRef() const;
91 
93  DLLLOCAL virtual void customDeref(ExceptionSink* xsink);
94 
96  DLLLOCAL virtual ~QoreObject();
97 
98  public:
100 
104  DLLEXPORT QoreObject(const QoreClass* oc, QoreProgram* p);
105 
107 
112  DLLEXPORT QoreObject(const QoreClass* oc, QoreProgram* p, AbstractPrivateData* data);
113 
115  DLLEXPORT bool hasMember(const char* mem, ExceptionSink* xsink) const;
116 
118 
120  DLLEXPORT virtual bool getAsBoolImpl() const;
121 
123 
129  DLLEXPORT virtual int getAsString(QoreString &str, int foff, ExceptionSink* xsink) const;
130 
132 
139  DLLEXPORT virtual QoreString* getAsString(bool& del, int foff, ExceptionSink* xsink) const;
140 
142  DLLEXPORT virtual AbstractQoreNode* realCopy() const;
143 
145 
149  DLLEXPORT virtual bool is_equal_soft(const AbstractQoreNode* v, ExceptionSink* xsink) const;
150 
152 
156  DLLEXPORT virtual bool is_equal_hard(const AbstractQoreNode* v, ExceptionSink* xsink) const;
157 
159  DLLEXPORT virtual const char* getTypeName() const;
160 
162  DLLLOCAL static const char* getStaticTypeName() {
163  return "object";
164  }
165 
167  DLLLOCAL static qore_type_t getStaticTypeCode() {
168  return NT_OBJECT;
169  }
170 
172 
176  DLLEXPORT bool validInstanceOf(qore_classid_t cid) const;
177 
179 
181  DLLEXPORT bool validInstanceOf(const QoreClass& qc) const;
182 
184 
188  DLLEXPORT bool validInstanceOfStrict(const QoreClass& qc) const;
189 
191 
196  DLLEXPORT void setValue(const char* key, QoreValue val, ExceptionSink* xsink);
197 
199 
202  DLLEXPORT QoreListNode* getMemberList(ExceptionSink* xsink) const;
203 
205 
209  DLLEXPORT void deleteMemberValue(const QoreString* key, ExceptionSink* xsink);
210 
212 
216  DLLEXPORT void deleteMemberValue(const char* key, ExceptionSink* xsink);
217 
219 
223  DLLEXPORT void removeMember(const QoreString* key, ExceptionSink* xsink);
224 
226 
230  DLLEXPORT void removeMember(const char* key, ExceptionSink* xsink);
231 
233 
238  DLLEXPORT QoreValue takeMember(const QoreString* key, ExceptionSink* xsink);
239 
241 
246  DLLEXPORT QoreValue takeMember(const char* key, ExceptionSink* xsink);
247 
249 
252  DLLEXPORT int size(ExceptionSink* xsink) const;
253 
255 
259  DLLEXPORT bool compareSoft(const QoreObject* obj, ExceptionSink* xsink) const;
260 
262 
266  DLLEXPORT bool compareHard(const QoreObject* obj, ExceptionSink* xsink) const;
267 
269 
274  DLLEXPORT QoreValue getReferencedMemberNoMethod(const char* mem, ExceptionSink* xsink) const;
275 
277 
283  DLLEXPORT int64 getMemberAsBigInt(const char* mem, bool& found, ExceptionSink* xsink) const;
284 
286 
289  DLLEXPORT QoreHashNode* copyData(ExceptionSink* xsink) const;
290 
292 
297 
299 
304 
306 
313 
315 
320  DLLEXPORT QoreValue evalMethod(const QoreString* name, const QoreListNode* args, ExceptionSink* xsink);
321 
323 
328  DLLEXPORT QoreValue evalMethod(const char* name, const QoreListNode* args, ExceptionSink* xsink);
329 
331 
336  DLLEXPORT QoreValue evalMethod(const QoreMethod& method, const QoreListNode* args, ExceptionSink* xsink);
337 
339 
349  DLLEXPORT QoreValue evalMethod(const QoreMethod& method, const QoreClass* class_ctx, const QoreListNode* args, ExceptionSink* xsink);
350 
352 
355  DLLEXPORT void doDelete(ExceptionSink* xsink);
356 
358 
362  DLLEXPORT const QoreClass* getClass(qore_classid_t cid) const;
363 
365 
372  DLLEXPORT const QoreClass* getClass(qore_classid_t cid, bool& priv) const;
373 
375 
381  DLLEXPORT ClassAccess getClassAccess(const QoreClass& cls) const;
382 
384 
387  DLLEXPORT const QoreClass* getClass() const;
388 
390 
393  DLLEXPORT const char* getClassName() const;
394 
396 
403  DLLEXPORT const QoreClass* getSurfaceClass() const;
404 
406 
413  DLLEXPORT const char* getSurfaceClassName() const;
414 
416 
419  DLLEXPORT bool isValid() const;
420 
422 
425  DLLEXPORT QoreProgram* getProgram() const;
426 
428 
431  DLLEXPORT bool isSystemObject() const;
432 
434 
439  DLLEXPORT void tRef() const;
440 
442 
445  DLLEXPORT void tDeref();
446 
448  DLLEXPORT void realRef();
449 
451  DLLEXPORT void realDeref(ExceptionSink* xsink);
452 
454 
462  DLLEXPORT QoreValue getMemberValueNoMethod(const QoreString* key, AutoVLock* vl, ExceptionSink* xsink) const;
463 
465 
471  DLLEXPORT QoreValue getMemberValueNoMethod(const char* key, AutoVLock* vl, ExceptionSink* xsink) const;
472 
474 
480  DLLEXPORT QoreValue getReferencedMemberNoMethod(const char* key, const QoreClass* cls, ExceptionSink* xsink) const;
481 
483 
492  DLLEXPORT int setMemberValue(const char* key, const QoreClass* cls, const QoreValue val, ExceptionSink* xsink);
493 
495 
499  DLLEXPORT void externalDelete(qore_classid_t key, ExceptionSink* xsink);
500 
502  DLLEXPORT QoreValue evalMethodVariant(const QoreMethod& method, const QoreExternalMethodVariant* variant, const QoreListNode* args, ExceptionSink* xsink);
503 
505 
507  DLLEXPORT QoreValue evalMethodVariant(const QoreMethod& method, const QoreClass* class_ctx, const QoreExternalMethodVariant* variant, const QoreListNode* args, ExceptionSink* xsink);
508 
510 
512  DLLEXPORT static QoreValue evalStaticMethod(const QoreMethod& method, const QoreListNode* args, ExceptionSink* xsink);
513 
515 
517  DLLEXPORT static QoreValue evalStaticMethod(const QoreMethod& method, const QoreClass* class_ctx, const QoreListNode* args, ExceptionSink* xsink);
518 
520 
522  DLLEXPORT static QoreValue evalStaticMethodVariant(const QoreMethod& method, const QoreExternalMethodVariant* variant, const QoreListNode* args, ExceptionSink* xsink);
523 
525 
527  DLLEXPORT static QoreValue evalStaticMethodVariant(const QoreMethod& method, const QoreClass* class_ctx, const QoreExternalMethodVariant* variant, const QoreListNode* args, ExceptionSink* xsink);
528 
530 
536  DLLEXPORT QoreValue evalMember(const QoreString* member, ExceptionSink* xsink);
537 
539 
546  DLLEXPORT QoreValue evalMember(const char* member, ExceptionSink* xsink);
547 
548  DLLLOCAL int getStatus() const;
549 
550  DLLLOCAL class KeyNode* getReferencedPrivateDataNode(qore_classid_t key);
551 
553 
558 
560 
566  DLLLOCAL QoreValue evalBuiltinMethodWithPrivateData(const QoreMethod& method, const BuiltinNormalMethodVariantBase* meth, const QoreListNode* args, ExceptionSink* xsink);
567 
569  DLLLOCAL void evalCopyMethodWithPrivateData(const QoreClass &thisclass, const BuiltinCopyVariantBase* meth, QoreObject* self, ExceptionSink* xsink);
570 
572 
576  DLLLOCAL void addPrivateDataToString(QoreString* str, ExceptionSink* xsink) const;
577 
579 
582  DLLLOCAL void obliterate(ExceptionSink* xsink);
583 
585 
589  DLLLOCAL void defaultSystemDestructor(qore_classid_t classID, ExceptionSink* xsink);
590 
591  // returns a new hash consisting of just the members of value_list
592  DLLLOCAL QoreHashNode* getSlice(const QoreListNode* value_list, ExceptionSink* xsink) const;
593 
595  DLLLOCAL QoreObject(const QoreClass* oc, QoreProgram* p, QoreHashNode* d);
596 
598  DLLLOCAL bool hasMemberNotification() const;
599 
601  DLLLOCAL void execMemberNotification(const char* member, ExceptionSink* xsink);
602 };
603 
605 template <class T>
607 public:
608  DLLLOCAL PrivateDataRefHolder(ExceptionSink* xsink) : ReferenceHolder<T>(xsink) {
609  }
610 
611  DLLLOCAL PrivateDataRefHolder(const QoreObject* o, qore_classid_t cid, ExceptionSink* xsink) : ReferenceHolder<T>(reinterpret_cast<T*>(o->getReferencedPrivateData(cid, xsink)), xsink) {
612  }
613 
615  DLLLOCAL void operator=(T *nv) {
616  if (this->p)
617  this->p->deref(this->xsink);
618  this->p = nv;
619  }
620 };
621 
623 template <class T>
625 public:
626  DLLLOCAL TryPrivateDataRefHolder(const QoreObject* o, qore_classid_t cid, ExceptionSink* xsink) : ReferenceHolder<T>(reinterpret_cast<T*>(o->tryGetReferencedPrivateData(cid, xsink)), xsink) {
627  }
628 
630  DLLLOCAL void operator=(T *nv) {
631  if (this->p)
632  this->p->deref(this->xsink);
633  this->p = nv;
634  }
635 };
636 
637 class QorePrivateObjectAccessHelper {
638 public:
639  DLLLOCAL QorePrivateObjectAccessHelper(ExceptionSink* xs) : xsink(xs), ptr(0) {
640  }
641 
642  DLLLOCAL operator bool() const {
643  return (bool)ptr;
644  }
645 
646 private:
647  DLLLOCAL QorePrivateObjectAccessHelper(const QorePrivateObjectAccessHelper&) = delete;
648  DLLLOCAL QorePrivateObjectAccessHelper& operator=(const QorePrivateObjectAccessHelper&) = delete;
649  DLLLOCAL void* operator new(size_t) = delete;
650 
651 protected:
652  ExceptionSink* xsink;
653  void* ptr;
654 };
655 
656 #endif
the base class for all data to be used as private data of Qore objects
Definition: AbstractPrivateData.h:44
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:57
AutoVLock is a container for safely managing global variable and object lock handovers,...
Definition: AutoVLock.h:80
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
convenience class for holding AbstractPrivateData references
Definition: QoreObject.h:606
DLLLOCAL void operator=(T *nv)
assigns a new pointer to the holder, dereferences the current pointer if any
Definition: QoreObject.h:615
defines a Qore-language class
Definition: QoreClass.h:239
external wrapper class for method variants
Definition: QoreReflection.h:90
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
a method in a QoreClass
Definition: QoreClass.h:125
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition: QoreObject.h:60
DLLEXPORT AbstractPrivateData * getReferencedPrivateData(qore_classid_t key, ExceptionSink *xsink) const
returns the private data corresponding to the class ID passed with an incremented reference count,...
virtual DLLEXPORT const char * getTypeName() const
returns the type name as a c string
virtual DLLEXPORT bool derefImpl(ExceptionSink *xsink)
runs the destructor if necessary and dereferences all members
DLLLOCAL bool hasMemberNotification() const
returns true if the class has a memberNotification method
DLLEXPORT void tRef() const
increments the existence reference count
DLLLOCAL QoreValue evalBuiltinMethodWithPrivateData(const QoreMethod &method, const BuiltinNormalMethodVariantBase *meth, const QoreListNode *args, ExceptionSink *xsink)
called to evaluate a builtin method when private data is available
DLLEXPORT bool compareSoft(const QoreObject *obj, ExceptionSink *xsink) const
tests for equality ("deep compare" including all contained values) with possible type conversion of c...
virtual DLLLOCAL void customRef() const
custom reference handler
DLLEXPORT QoreValue evalMember(const QoreString *member, ExceptionSink *xsink)
returns the value of the member with an incremented reference count, or executes the memberGate() met...
virtual DLLLOCAL void customDeref(ExceptionSink *xsink)
custom dereference handler - with delete
DLLEXPORT void tDeref()
decrements the existence reference count, when it reaches 0 the C++ object ("this") will be deleted
virtual DLLEXPORT int getAsString(QoreString &str, int foff, ExceptionSink *xsink) const
concatenate the verbose string representation of the list (including all contained values) to an exis...
DLLEXPORT const char * getClassName() const
returns the name of the class
DLLEXPORT QoreValue evalMethod(const QoreString *name, const QoreListNode *args, ExceptionSink *xsink)
evaluates the given method with the arguments passed and returns the return value,...
DLLEXPORT bool isValid() const
returns true if the object is valid
DLLEXPORT bool isSystemObject() const
returns true if the object is a system object (created with the system constructor)
DLLEXPORT void removeMember(const QoreString *key, ExceptionSink *xsink)
removes a member from the object without explicitly calling destructors; the value is only dereferenc...
DLLEXPORT void realDeref(ExceptionSink *xsink)
decrements the standard reference count of the object for references that cannot be part of a recursi...
DLLEXPORT QoreHashNode * copyData(ExceptionSink *xsink) const
retuns all member data of the object (or 0 if there's an exception), caller owns the QoreHashNode ref...
DLLEXPORT bool compareHard(const QoreObject *obj, ExceptionSink *xsink) const
tests for equality ("deep compare" including all contained values) with possible type conversion of c...
DLLEXPORT int size(ExceptionSink *xsink) const
returns the number of members of the object
virtual DLLEXPORT AbstractQoreNode * realCopy() const
performs a the same object with its reference count increased
virtual DLLLOCAL ~QoreObject()
destructor
DLLLOCAL void defaultSystemDestructor(qore_classid_t classID, ExceptionSink *xsink)
runs the destructor for system objects
DLLEXPORT void setValue(const char *key, QoreValue val, ExceptionSink *xsink)
sets the value of the given member to the given value
static DLLLOCAL qore_type_t getStaticTypeCode()
returns the type code (useful in templates)
Definition: QoreObject.h:167
DLLEXPORT QoreProgram * getProgram() const
returns the QoreProgram object associated with this object
DLLLOCAL void addPrivateDataToString(QoreString *str, ExceptionSink *xsink) const
concatenates info about private data to a string
DLLLOCAL void execMemberNotification(const char *member, ExceptionSink *xsink)
executes the member notification on the object the given member
DLLEXPORT QoreValue getMemberValueNoMethod(const QoreString *key, AutoVLock *vl, ExceptionSink *xsink) const
returns the pointer to the value of the member
static DLLEXPORT QoreValue evalStaticMethodVariant(const QoreMethod &method, const QoreExternalMethodVariant *variant, const QoreListNode *args, ExceptionSink *xsink)
executes a static method variant
DLLEXPORT void deleteMemberValue(const QoreString *key, ExceptionSink *xsink)
removes a member from the object, if the member's value is an object it is deleted as well (destructo...
DLLEXPORT QoreValue evalMethodVariant(const QoreMethod &method, const QoreExternalMethodVariant *variant, const QoreListNode *args, ExceptionSink *xsink)
executes a normal object method variant
DLLLOCAL void evalCopyMethodWithPrivateData(const QoreClass &thisclass, const BuiltinCopyVariantBase *meth, QoreObject *self, ExceptionSink *xsink)
called on the old object (this) to acquire private data, copy method called with pointer to "self" (n...
DLLLOCAL void obliterate(ExceptionSink *xsink)
destroys all members and dereferences all private data structures
DLLLOCAL AbstractPrivateData * getAndClearPrivateData(qore_classid_t key, ExceptionSink *xsink)
retrieves the private data pointer and clears it from the object's private data store,...
DLLEXPORT void doDelete(ExceptionSink *xsink)
runs the destructor on the object (if it hasn't already been deleted)
DLLEXPORT QoreValue getReferencedMemberNoMethod(const char *mem, ExceptionSink *xsink) const
returns the value of the given member with the reference count incremented, the caller owns any refer...
DLLEXPORT bool validInstanceOf(qore_classid_t cid) const
returns true if this object is a valid instance of the classid passed
DLLEXPORT void externalDelete(qore_classid_t key, ExceptionSink *xsink)
call this function when an object's private data is deleted externally
static DLLEXPORT QoreValue evalStaticMethod(const QoreMethod &method, const QoreListNode *args, ExceptionSink *xsink)
executes a static method
DLLEXPORT ClassAccess getClassAccess(const QoreClass &cls) const
returns the accessibility of the class in the object's hierachy or Inaccessible the object does not i...
static DLLLOCAL const char * getStaticTypeName()
returns the type name (useful in templates)
Definition: QoreObject.h:162
DLLEXPORT void setPrivate(qore_classid_t key, AbstractPrivateData *pd)
sets private data for the object against the class ID passed, used in C++ functions implementing Qore...
virtual DLLEXPORT bool is_equal_hard(const AbstractQoreNode *v, ExceptionSink *xsink) const
tests for equality ("deep compare" including all contained values) without type conversions (hard com...
DLLEXPORT bool validInstanceOfStrict(const QoreClass &qc) const
returns true if this object is a valid instance of the classid passed; does not check for injected co...
DLLEXPORT int64 getMemberAsBigInt(const char *mem, bool &found, ExceptionSink *xsink) const
returns the value of the given member as an int64
DLLEXPORT AbstractPrivateData * tryGetReferencedPrivateData(qore_classid_t key, ExceptionSink *xsink) const
returns the private data corresponding to the class ID passed with an incremented reference count if ...
DLLEXPORT const QoreClass * getClass() const
returns a pointer to the QoreClass of this object
DLLEXPORT QoreValue takeMember(const QoreString *key, ExceptionSink *xsink)
removes a member from the object without explicitly calling destructors and returns the value removed...
DLLEXPORT QoreListNode * getMemberList(ExceptionSink *xsink) const
returns the list of members, caller owns the list returned
DLLEXPORT const char * getSurfaceClassName() const
returns the name of the class or to the name of the injection target class in case the class was inje...
DLLEXPORT void realRef()
increments the standard reference count of the object for references that cannot be part of a recursi...
virtual DLLEXPORT bool is_equal_soft(const AbstractQoreNode *v, ExceptionSink *xsink) const
tests for equality ("deep compare" including all contained values) with possible type conversion (sof...
DLLEXPORT int setMemberValue(const char *key, const QoreClass *cls, const QoreValue val, ExceptionSink *xsink)
sets the value of the given member as accessed from the given class
DLLEXPORT const QoreClass * getSurfaceClass() const
returns a pointer to the QoreClass of this object or to the injection target class in case the class ...
DLLEXPORT bool hasMember(const char *mem, ExceptionSink *xsink) const
returns true if the object has the given member (note that the member may not have a value)
virtual DLLLOCAL QoreValue evalImpl(bool &needs_deref, ExceptionSink *xsink) const
should never be called, does nothing
virtual DLLEXPORT bool getAsBoolImpl() const
returns false unless perl-boolean-evaluation is enabled, in which case it returns false only when emp...
supports parsing and executing Qore-language code, reference counted, dynamically-allocated only
Definition: QoreProgram.h:127
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
a templated class to manage a reference count of an object that can throw a Qore-language exception w...
Definition: ReferenceHolder.h:52
convenience class for holding AbstractPrivateData references
Definition: QoreObject.h:624
DLLLOCAL void operator=(T *nv)
assigns a new pointer to the holder, dereferences the current pointer if any
Definition: QoreObject.h:630
int16_t qore_type_t
used to identify unique Qore data and parse types (descendents of AbstractQoreNode)
Definition: common.h:70
unsigned qore_classid_t
used for the unique class ID for QoreClass objects
Definition: common.h:79
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
const qore_type_t NT_OBJECT
type value for QoreObject
Definition: node_types.h:52
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:275