Qore Programming Language  0.8.11.1
QoreObject.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreObject.h
4 
5  thread-safe object definition
6 
7  references: how many variables are pointing at this object?
8 
9  Qore Programming Language
10 
11  Copyright (C) 2003 - 2014 David Nichols
12 
13  Permission is hereby granted, free of charge, to any person obtaining a
14  copy of this software and associated documentation files (the "Software"),
15  to deal in the Software without restriction, including without limitation
16  the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  and/or sell copies of the Software, and to permit persons to whom the
18  Software is furnished to do so, subject to the following conditions:
19 
20  The above copyright notice and this permission notice shall be included in
21  all copies or substantial portions of the Software.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  DEALINGS IN THE SOFTWARE.
30 
31  Note that the Qore library is released under a choice of three open-source
32  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
33  information.
34 */
35 
36 #ifndef _QORE_QOREOBJECT_H
37 
38 #define _QORE_QOREOBJECT_H
39 
40 class AutoVLock;
41 class VRMutex;
42 class BuiltinCopy;
43 class BuiltinDeleteBlocker;
44 class BuiltinNormalMethodVariantBase;
45 class BuiltinCopyVariantBase;
46 class QoreExternalMethodVariant;
47 class QoreExternalStaticMethodVariant;
48 class QoreProgram;
49 
51 
64 class QoreObject : public AbstractQoreNode {
65  friend class qore_object_private;
66  friend class ObjectRSetHelper;
67  friend class ObjectRSet;
68 
69 private:
71  class qore_object_private* priv;
72 
74  DLLLOCAL QoreObject(const QoreObject&);
75 
77  DLLLOCAL QoreObject& operator=(const QoreObject&);
78 
79 protected:
81 
85  DLLEXPORT virtual bool derefImpl(ExceptionSink* xsink);
86 
88 
90  DLLLOCAL virtual AbstractQoreNode* evalImpl(ExceptionSink* xsink) const;
91 
93 
95  DLLLOCAL virtual AbstractQoreNode* evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
96 
98 
100  DLLLOCAL virtual int64 bigIntEvalImpl(ExceptionSink* xsink) const;
101 
103 
105  DLLLOCAL virtual int integerEvalImpl(ExceptionSink* xsink) const;
106 
108 
110  DLLLOCAL virtual bool boolEvalImpl(ExceptionSink* xsink) const;
111 
113 
115  DLLLOCAL virtual double floatEvalImpl(ExceptionSink* xsink) const;
116 
118  DLLLOCAL void customRefIntern() const;
119 
121  DLLLOCAL virtual void customRef() const;
122 
124  DLLLOCAL virtual void customDeref(ExceptionSink* xsink);
125 
127  DLLLOCAL virtual ~QoreObject();
128 
129 public:
131 
135  DLLEXPORT QoreObject(const QoreClass* oc, QoreProgram* p);
136 
138 
143  DLLEXPORT QoreObject(const QoreClass* oc, QoreProgram* p, AbstractPrivateData *data);
144 
146 
148  DLLEXPORT virtual bool getAsBoolImpl() const;
149 
151 
157  DLLEXPORT virtual int getAsString(QoreString &str, int foff, ExceptionSink* xsink) const;
158 
160 
167  DLLEXPORT virtual QoreString* getAsString(bool& del, int foff, ExceptionSink* xsink) const;
168 
170  DLLEXPORT virtual AbstractQoreNode* realCopy() const;
171 
173 
177  DLLEXPORT virtual bool is_equal_soft(const AbstractQoreNode* v, ExceptionSink* xsink) const;
178 
180 
184  DLLEXPORT virtual bool is_equal_hard(const AbstractQoreNode* v, ExceptionSink* xsink) const;
185 
187  DLLEXPORT virtual const char* getTypeName() const;
188 
190  DLLLOCAL static const char* getStaticTypeName() {
191  return "object";
192  }
193 
195  DLLLOCAL static qore_type_t getStaticTypeCode() {
196  return NT_OBJECT;
197  }
198 
200 
204  DLLEXPORT bool validInstanceOf(qore_classid_t cid) const;
205 
207 
209  DLLEXPORT bool validInstanceOf(const QoreClass& qc) const;
210 
212 
217  DLLEXPORT void setValue(const char* key, AbstractQoreNode* val, ExceptionSink* xsink);
218 
220 
223  DLLEXPORT QoreListNode* getMemberList(ExceptionSink* xsink) const;
224 
226 
230  DLLEXPORT void deleteMemberValue(const QoreString* key, ExceptionSink* xsink);
231 
233 
237  DLLEXPORT void deleteMemberValue(const char* key, ExceptionSink* xsink);
238 
240 
244  DLLEXPORT void removeMember(const QoreString* key, ExceptionSink* xsink);
245 
247 
251  DLLEXPORT void removeMember(const char* key, ExceptionSink* xsink);
252 
254 
259  DLLEXPORT AbstractQoreNode* takeMember(const QoreString* key, ExceptionSink* xsink);
260 
262 
267  DLLEXPORT AbstractQoreNode* takeMember(const char* key, ExceptionSink* xsink);
268 
270 
273  DLLEXPORT int size(ExceptionSink* xsink) const;
274 
276 
280  DLLEXPORT bool compareSoft(const QoreObject* obj, ExceptionSink* xsink) const;
281 
283 
287  DLLEXPORT bool compareHard(const QoreObject* obj, ExceptionSink* xsink) const;
288 
290 
295  DLLEXPORT AbstractQoreNode* getReferencedMemberNoMethod(const char* mem, ExceptionSink* xsink) const;
296 
298 
304  DLLEXPORT int64 getMemberAsBigInt(const char* mem, bool& found, ExceptionSink* xsink) const;
305 
307 
310  DLLEXPORT QoreHashNode* copyData(ExceptionSink* xsink) const;
311 
313 
317  DLLEXPORT void mergeDataToHash(QoreHashNode* hash, ExceptionSink* xsink);
318 
320 
324  DLLEXPORT void setPrivate(qore_classid_t key, AbstractPrivateData *pd);
325 
327 
332 
334 
339  DLLEXPORT AbstractQoreNode* evalMethod(const QoreString* name, const QoreListNode* args, ExceptionSink* xsink);
340 
342 
347  DLLEXPORT AbstractQoreNode* evalMethod(const char* name, const QoreListNode* args, ExceptionSink* xsink);
348 
350 
355  DLLEXPORT int64 bigIntEvalMethod(const char* name, const QoreListNode* args, ExceptionSink* xsink);
356 
358 
363  DLLEXPORT int intEvalMethod(const char* name, const QoreListNode* args, ExceptionSink* xsink);
364 
366 
371  DLLEXPORT bool boolEvalMethod(const char* name, const QoreListNode* args, ExceptionSink* xsink);
372 
374 
379  DLLEXPORT double floatEvalMethod(const char* name, const QoreListNode* args, ExceptionSink* xsink);
380 
382 
387  DLLEXPORT AbstractQoreNode* evalMethod(const QoreMethod& method, const QoreListNode* args, ExceptionSink* xsink);
388 
390 
395  DLLEXPORT int64 bigIntEvalMethod(const QoreMethod& method, const QoreListNode* args, ExceptionSink* xsink);
396 
398 
403  DLLEXPORT int intEvalMethod(const QoreMethod& method, const QoreListNode* args, ExceptionSink* xsink);
404 
406 
411  DLLEXPORT bool boolEvalMethod(const QoreMethod& method, const QoreListNode* args, ExceptionSink* xsink);
412 
414 
419  DLLEXPORT double floatEvalMethod(const QoreMethod& method, const QoreListNode* args, ExceptionSink* xsink);
420 
422 
425  DLLEXPORT void doDelete(ExceptionSink* xsink);
426 
428 
432  DLLEXPORT const QoreClass* getClass(qore_classid_t cid) const;
433 
435 
442  DLLEXPORT const QoreClass* getClass(qore_classid_t cid, bool& priv) const;
443 
445 
448  DLLEXPORT const QoreClass* getClass() const;
449 
451 
454  DLLEXPORT const char* getClassName() const;
455 
457 
460  DLLEXPORT bool isValid() const;
461 
463 
466  DLLEXPORT QoreProgram* getProgram() const;
467 
469 
472  DLLEXPORT bool isSystemObject() const;
473 
475 
480  DLLEXPORT void tRef() const;
481 
483 
486  DLLEXPORT void tDeref();
487 
489 
497  DLLEXPORT AbstractQoreNode* getMemberValueNoMethod(const QoreString* key, AutoVLock* vl, ExceptionSink* xsink) const;
498 
500 
506  DLLEXPORT AbstractQoreNode* getMemberValueNoMethod(const char* key, AutoVLock* vl, ExceptionSink* xsink) const;
507 
509 
511  DLLEXPORT void deleteBlockerRef() const;
512 
514 
518  DLLEXPORT void externalDelete(qore_classid_t key, ExceptionSink* xsink);
519 
521  DLLEXPORT AbstractQoreNode* evalMethodVariant(const QoreMethod& method, const QoreExternalMethodVariant* variant, const QoreListNode* args, ExceptionSink* xsink);
522 
523  DLLLOCAL int getStatus() const;
524 
526 
530  DLLLOCAL AbstractQoreNode* evalMember(const QoreString* member, ExceptionSink* xsink);
531 
533 
537  DLLLOCAL QoreHashNode* getRuntimeMemberHash(ExceptionSink* xsink) const;
538 
539  DLLLOCAL class KeyNode* getReferencedPrivateDataNode(qore_classid_t key);
540 
542 
547 
549 
555  DLLLOCAL AbstractQoreNode* evalBuiltinMethodWithPrivateData(const QoreMethod& method, const BuiltinNormalMethodVariantBase* meth, const QoreListNode* args, ExceptionSink* xsink);
556  DLLLOCAL int64 bigIntEvalBuiltinMethodWithPrivateData(const QoreMethod& method, const BuiltinNormalMethodVariantBase* meth, const QoreListNode* args, ExceptionSink* xsink);
557  DLLLOCAL int intEvalBuiltinMethodWithPrivateData(const QoreMethod& method, const BuiltinNormalMethodVariantBase* meth, const QoreListNode* args, ExceptionSink* xsink);
558  DLLLOCAL bool boolEvalBuiltinMethodWithPrivateData(const QoreMethod& method, const BuiltinNormalMethodVariantBase* meth, const QoreListNode* args, ExceptionSink* xsink);
559  DLLLOCAL double floatEvalBuiltinMethodWithPrivateData(const QoreMethod& method, const BuiltinNormalMethodVariantBase* meth, const QoreListNode* args, ExceptionSink* xsink);
560 
562  DLLLOCAL void evalCopyMethodWithPrivateData(const QoreClass &thisclass, const BuiltinCopyVariantBase* meth, QoreObject* self, ExceptionSink* xsink);
563 
565 
569  DLLLOCAL void addPrivateDataToString(QoreString* str, ExceptionSink* xsink) const;
570 
572 
575  DLLLOCAL void obliterate(ExceptionSink* xsink);
576 
578 
582  DLLLOCAL void defaultSystemDestructor(qore_classid_t classID, ExceptionSink* xsink);
583 
585 
593  DLLLOCAL AbstractQoreNode** getExistingValuePtr(const QoreString* mem, AutoVLock* vl, ExceptionSink* xsink) const;
594 
596 
603  DLLLOCAL AbstractQoreNode** getExistingValuePtr(const char* mem, AutoVLock* vl, ExceptionSink* xsink) const;
604 
605  // returns a new hash consisting of just the members of value_list
606  DLLLOCAL QoreHashNode* getSlice(const QoreListNode* value_list, ExceptionSink* xsink) const;
607 
609  DLLLOCAL QoreObject(const QoreClass* oc, QoreProgram* p, QoreHashNode* d);
610 
612  DLLLOCAL bool evalDeleteBlocker(qore_classid_t classid_for_method, BuiltinDeleteBlocker* meth);
613 
615  DLLLOCAL bool hasMemberNotification() const;
616 
618  DLLLOCAL void execMemberNotification(const char* member, ExceptionSink* xsink);
619 
621  DLLLOCAL AbstractQoreNode** getMemberValuePtrForInitialization(const char* member);
622 };
623 
625 template <class T>
627 public:
628  DLLLOCAL PrivateDataRefHolder(const QoreObject* o, qore_classid_t cid, ExceptionSink* xsink) : ReferenceHolder<T>(reinterpret_cast<T*>(o->getReferencedPrivateData(cid, xsink)), xsink) {
629  }
630 };
631 
632 class QorePrivateObjectAccessHelper {
633 private:
634  // not implemented
635  DLLLOCAL QorePrivateObjectAccessHelper(const QorePrivateObjectAccessHelper&);
636  DLLLOCAL QorePrivateObjectAccessHelper& operator=(const QorePrivateObjectAccessHelper&);
637  DLLLOCAL void* operator new(size_t);
638 
639 protected:
640  ExceptionSink* xsink;
641  void* ptr;
642 
643 public:
644  DLLLOCAL QorePrivateObjectAccessHelper(ExceptionSink* xs) : xsink(xs), ptr(0) {
645  }
646 
647  DLLLOCAL operator bool() const {
648  return (bool)ptr;
649  }
650 };
651 
652 #endif
DLLLOCAL QoreHashNode * getRuntimeMemberHash(ExceptionSink *xsink) const
retuns member data of the object (or 0 if there's an exception), private members are excluded if call...
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...
DLLEXPORT bool compareHard(const QoreObject *obj, ExceptionSink *xsink) const
tests for equality ("deep compare" including all contained values) with possible type conversion of c...
AutoVLock is a container for safely managing global variable and object lock handovers, required for functions accessing global variables and object data where locking is necessary.
Definition: AutoVLock.h:80
DLLEXPORT bool isSystemObject() const
returns true if the object is a system object (created with the system constructor) ...
DLLLOCAL void execMemberNotification(const char *member, ExceptionSink *xsink)
executes the member notification on the object the given member
This is the hash or associative list container type in Qore, dynamically allocated only...
Definition: QoreHashNode.h:49
DLLEXPORT void setValue(const char *key, AbstractQoreNode *val, ExceptionSink *xsink)
sets the value of the given member to the given value
DLLLOCAL AbstractQoreNode ** getMemberValuePtrForInitialization(const char *member)
returns a pointer to an object member during initialization (no locking; interal use only) ...
DLLEXPORT void doDelete(ExceptionSink *xsink)
runs the destructor on the object (if it hasn't already been deleted)
virtual DLLLOCAL bool boolEvalImpl(ExceptionSink *xsink) const
should never be called, does nothing
the base class for all data to be used as private data of Qore objects
Definition: AbstractPrivateData.h:44
DLLEXPORT void mergeDataToHash(QoreHashNode *hash, ExceptionSink *xsink)
copies all member data of the current object to the passed QoreHashNode
DLLEXPORT const char * getClassName() const
returns the name of the class
DLLEXPORT bool compareSoft(const QoreObject *obj, ExceptionSink *xsink) const
tests for equality ("deep compare" including all contained values) with possible type conversion of c...
DLLEXPORT void externalDelete(qore_classid_t key, ExceptionSink *xsink)
call this function when an object's private data is deleted externally
DLLEXPORT void removeMember(const QoreString *key, ExceptionSink *xsink)
removes a member from the object without explicitly calling destructors; the value is only dereferenc...
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...
DLLEXPORT int size(ExceptionSink *xsink) const
returns the number of members of the object
DLLEXPORT AbstractQoreNode * takeMember(const QoreString *key, ExceptionSink *xsink)
removes a member from the object without explicitly calling destructors and returns the value removed...
virtual DLLEXPORT bool getAsBoolImpl() const
returns false unless perl-boolean-evaluation is enabled, in which case it returns false only when emp...
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:55
const qore_type_t NT_OBJECT
type value for QoreObject
Definition: node_types.h:52
virtual DLLLOCAL ~QoreObject()
destructor
DLLEXPORT bool boolEvalMethod(const char *name, const QoreListNode *args, ExceptionSink *xsink)
evaluates the given method with the arguments passed and returns the return value as a bool ...
DLLEXPORT const QoreClass * getClass() const
returns a pointer to the QoreClass of this object
DLLEXPORT int64 bigIntEvalMethod(const char *name, const QoreListNode *args, ExceptionSink *xsink)
evaluates the given method with the arguments passed and returns the return value as an int64 ...
DLLEXPORT QoreListNode * getMemberList(ExceptionSink *xsink) const
returns the list of members, caller owns the list returned
virtual DLLEXPORT AbstractQoreNode * realCopy() const
performs a deep copy of the list and returns the new list
virtual DLLLOCAL AbstractQoreNode * evalImpl(ExceptionSink *xsink) const
should never be called, does nothing
DLLEXPORT AbstractQoreNode * evalMethod(const QoreString *name, const QoreListNode *args, ExceptionSink *xsink)
evaluates the given method with the arguments passed and returns the return value, caller owns the AbstractQoreNode (reference) returned
DLLLOCAL AbstractQoreNode * 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 QoreProgram * getProgram() const
returns the QoreProgram object associated with this object
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 QoreHashNode * copyData(ExceptionSink *xsink) const
retuns all member data of the object (or 0 if there's an exception), caller owns the QoreHashNode ref...
signed short qore_type_t
used to identify unique Qore data and parse types (descendents of AbstractQoreNode) ...
Definition: common.h:67
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...
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:50
DLLLOCAL void obliterate(ExceptionSink *xsink)
destroys all members and dereferences all private data structures
virtual DLLLOCAL void customRef() const
custom reference handler
static DLLLOCAL qore_type_t getStaticTypeCode()
returns the type code (useful in templates)
Definition: QoreObject.h:195
DLLEXPORT void tDeref()
decrements the existence reference count, when it reaches 0 the C++ object ("this") will be deleted ...
virtual DLLEXPORT const char * getTypeName() const
returns the type name as a c string
DLLEXPORT double floatEvalMethod(const char *name, const QoreListNode *args, ExceptionSink *xsink)
evaluates the given method with the arguments passed and returns the return value as a double ...
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
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...
convenience class for holding AbstractPrivateData references
Definition: QoreObject.h:626
DLLLOCAL bool evalDeleteBlocker(qore_classid_t classid_for_method, BuiltinDeleteBlocker *meth)
evaluates the delete blocker function for the managed private data
DLLLOCAL void addPrivateDataToString(QoreString *str, ExceptionSink *xsink) const
concatenates info about private data to a string
defines a Qore-language class
Definition: QoreClass.h:194
DLLEXPORT AbstractQoreNode * getReferencedMemberNoMethod(const char *mem, ExceptionSink *xsink) const
returns the value of the given member with the reference count incremented, the caller owns the Abstr...
supports parsing and executing Qore-language code, reference counted, dynamically-allocated only ...
Definition: QoreProgram.h:103
the implementation of Qore's object data type, reference counted, dynamically-allocated only ...
Definition: QoreObject.h:64
DLLEXPORT AbstractQoreNode * getMemberValueNoMethod(const QoreString *key, AutoVLock *vl, ExceptionSink *xsink) const
returns the pointer to the value of the member
DLLLOCAL void defaultSystemDestructor(qore_classid_t classID, ExceptionSink *xsink)
runs the destructor for system objects
unsigned qore_classid_t
used for the unique class ID for QoreClass objects
Definition: common.h:76
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:43
virtual DLLLOCAL int integerEvalImpl(ExceptionSink *xsink) const
should never be called, does nothing
virtual DLLLOCAL double floatEvalImpl(ExceptionSink *xsink) const
should never be called, does nothing
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...
DLLLOCAL AbstractPrivateData * getAndClearPrivateData(qore_classid_t key, ExceptionSink *xsink)
retrieves the private data pointer and clears it from the object's private data store, used when executing destructors
DLLLOCAL void customRefIntern() const
custom reference handler - unlocked
DLLLOCAL AbstractQoreNode ** getExistingValuePtr(const QoreString *mem, AutoVLock *vl, ExceptionSink *xsink) const
returns a pointer to a pointer to the value of the member only if it already exists, so it can be set externally
DLLEXPORT AbstractQoreNode * evalMethodVariant(const QoreMethod &method, const QoreExternalMethodVariant *variant, const QoreListNode *args, ExceptionSink *xsink)
executes a normal object method variant
virtual DLLLOCAL int64 bigIntEvalImpl(ExceptionSink *xsink) const
should never be called, does nothing
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...
DLLLOCAL AbstractQoreNode * evalBuiltinMethodWithPrivateData(const QoreMethod &method, const BuiltinNormalMethodVariantBase *meth, const QoreListNode *args, ExceptionSink *xsink)
called to evaluate a builtin method when private data is available
DLLEXPORT void deleteBlockerRef() const
increment the reference count of the object, to be called only from within a delete blocker ...
static DLLLOCAL const char * getStaticTypeName()
returns the type name (useful in templates)
Definition: QoreObject.h:190
a method in a QoreClass
Definition: QoreClass.h:91
DLLEXPORT void tRef() const
increments the existence reference count
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...
DLLEXPORT bool isValid() const
returns true if the object is valid
a templated class to manage a reference count of an object that can throw a Qore-language exception w...
Definition: ReferenceHolder.h:51
DLLEXPORT int64 getMemberAsBigInt(const char *mem, bool &found, ExceptionSink *xsink) const
returns the value of the given member as an int64
DLLEXPORT bool validInstanceOf(qore_classid_t cid) const
returns true if this object is a valid instance of the classid passed
DLLEXPORT int intEvalMethod(const char *name, const QoreListNode *args, ExceptionSink *xsink)
evaluates the given method with the arguments passed and returns the return value as an int ...