Qore Programming Language - C/C++ Library  0.8.13.2
QoreNodeEvalOptionalRefHolder.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreNodeEvalOptionalRefHolder.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2017 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_QORENODEEVALOPTIONALREFHOLDER_H
33 
34 #define _QORE_QORENODEEVALOPTIONALREFHOLDER_H
35 
37 
46 private:
47  AbstractQoreNode* val;
48  ExceptionSink* xsink;
49  bool needs_deref;
50 
51  DLLLOCAL void discard_intern() {
52  if (needs_deref && val)
53  val->deref(xsink);
54  }
55 
58 
60  DLLLOCAL QoreNodeEvalOptionalRefHolder& operator=(const QoreNodeEvalOptionalRefHolder&);
61 
63 
65  DLLLOCAL void* operator new(size_t);
66 
67 public:
69  DLLLOCAL QoreNodeEvalOptionalRefHolder(ExceptionSink* n_xsink) : val(0), xsink(n_xsink), needs_deref(false) {
70  }
71 
73  DLLLOCAL QoreNodeEvalOptionalRefHolder(const AbstractQoreNode* exp, ExceptionSink* n_xsink) : xsink(n_xsink) {
74  if (exp)
75  val = exp->eval(needs_deref, xsink);
76  else {
77  val = 0;
78  needs_deref = false;
79  }
80  }
81 
84  discard_intern();
85  }
86 
88  DLLLOCAL void discard() {
89  discard_intern();
90  needs_deref = false;
91  val = 0;
92  }
93 
95  DLLLOCAL void assign(bool n_needs_deref, AbstractQoreNode* n_val) {
96  discard_intern();
97  needs_deref = n_needs_deref;
98  val = n_val;
99  }
100 
103  if (needs_deref)
104  needs_deref = false;
105  else if (val)
106  val->ref();
107  return val;
108  }
109 
111  DLLLOCAL const AbstractQoreNode* operator->() const { return val; }
112 
114  DLLLOCAL const AbstractQoreNode* operator*() const { return val; }
115 
117  DLLLOCAL operator bool() const { return val != 0; }
118 
120  DLLLOCAL bool isTemp() const { return needs_deref; }
121 
122 };
123 
124 #endif
DLLLOCAL void assign(bool n_needs_deref, AbstractQoreNode *n_val)
assigns a new value to this holder object
Definition: QoreNodeEvalOptionalRefHolder.h:95
DLLLOCAL void discard()
discards any temporary value evaluated by the constructor or assigned by "assign()" ...
Definition: QoreNodeEvalOptionalRefHolder.h:88
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:54
DLLLOCAL const AbstractQoreNode * operator*() const
returns the object being managed
Definition: QoreNodeEvalOptionalRefHolder.h:114
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:47
DLLEXPORT void deref(ExceptionSink *xsink)
decrements the reference count and calls derefImpl() if there_can_be_only_one is false, otherwise does nothing
DLLLOCAL const AbstractQoreNode * operator->() const
returns the object being managed
Definition: QoreNodeEvalOptionalRefHolder.h:111
DLLEXPORT void ref() const
increments the reference count
DLLLOCAL bool isTemp() const
returns true if the value is temporary (needs dereferencing)
Definition: QoreNodeEvalOptionalRefHolder.h:120
DLLLOCAL AbstractQoreNode * getReferencedValue()
returns a referenced value - the caller will own the reference
Definition: QoreNodeEvalOptionalRefHolder.h:102
DLLLOCAL ~QoreNodeEvalOptionalRefHolder()
discards any temporary value evaluated by the constructor or assigned by "assign()" ...
Definition: QoreNodeEvalOptionalRefHolder.h:83
this class manages reference counts for the optional evaluation of AbstractQoreNode objects ...
Definition: QoreNodeEvalOptionalRefHolder.h:45
DLLEXPORT AbstractQoreNode * eval(ExceptionSink *xsink) const
evaluates the object and returns a value (or 0)