Qore Programming Language  1.12.0
WeakReferenceNode.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  WeakReferenceNode.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_INTERN_WEAKREFERENCENODE_H
33 
34 #define _QORE_INTERN_WEAKREFERENCENODE_H
35 
36 class WeakReferenceNode : public AbstractQoreNode {
37 public:
38  DLLLOCAL WeakReferenceNode(QoreObject* obj) : AbstractQoreNode(NT_WEAKREF, false, true), obj(obj) {
39  obj->tRef();
40  }
41 
42  DLLLOCAL WeakReferenceNode(const WeakReferenceNode& old) : AbstractQoreNode(old), obj(old.obj) {
43  obj->tRef();
44  }
45 
46  DLLLOCAL QoreObject* operator*() const {
47  return obj;
48  }
49 
50  DLLLOCAL QoreObject* operator->() const {
51  return obj;
52  }
53 
54  DLLLOCAL QoreObject* get() const {
55  return obj;
56  }
57 
58 protected:
59  QoreObject* obj;
60 
61  static QoreString nstr;
62 
63  DLLLOCAL virtual bool derefImpl(ExceptionSink* xsink) {
64  obj->tDeref();
65  return true;
66  }
67 
68  DLLLOCAL virtual ~WeakReferenceNode() = default;
69 
70  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const {
71  assert(needs_deref);
72  needs_deref = false;
73  return obj;
74  }
75 
76  DLLLOCAL virtual bool getAsBoolImpl() const {
77  return obj->getAsBoolImpl();
78  }
79 
80  DLLLOCAL virtual int getAsString(QoreString &str, int foff, ExceptionSink* xsink) const {
81  return obj->getAsString(str, foff, xsink);
82  }
83 
84  DLLLOCAL virtual QoreString* getAsString(bool& del, int foff, ExceptionSink* xsink) const {
85  return obj->getAsString(del, foff, xsink);
86  }
87 
88  DLLLOCAL virtual AbstractQoreNode* realCopy() const {
89  return new WeakReferenceNode(*this);
90  }
91 
92  DLLLOCAL virtual bool is_equal_soft(const AbstractQoreNode* v, ExceptionSink* xsink) const {
93  return obj->is_equal_soft(v, xsink);
94  }
95 
96  DLLLOCAL virtual bool is_equal_hard(const AbstractQoreNode* v, ExceptionSink* xsink) const {
97  return obj->is_equal_hard(v, xsink);
98  }
99 
100  DLLLOCAL virtual const char* getTypeName() const {
101  return "weak reference";
102  }
103 };
104 
105 #endif
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:57
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition: QoreObject.h:60
DLLEXPORT void tRef() const
increments the existence reference count
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...
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...
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...
virtual DLLEXPORT bool getAsBoolImpl() const
returns false unless perl-boolean-evaluation is enabled, in which case it returns false only when emp...
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
const qore_type_t NT_WEAKREF
type value for WeakReferenceNode
Definition: node_types.h:87
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:275