00001 /* 00002 QoreNodeEvalOptionalRefHolder.h 00003 00004 Qore Programming Language 00005 00006 Copyright 2003 - 2009 David Nichols 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Lesser General Public 00010 License as published by the Free Software Foundation; either 00011 version 2.1 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Lesser General Public License for more details. 00017 00018 You should have received a copy of the GNU Lesser General Public 00019 License along with this library; if not, write to the Free Software 00020 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #ifndef _QORE_QORENODEEVALOPTIONALREFHOLDER_H 00024 00025 #define _QORE_QORENODEEVALOPTIONALREFHOLDER_H 00026 00028 00036 class QoreNodeEvalOptionalRefHolder { 00037 private: 00038 AbstractQoreNode *val; 00039 ExceptionSink *xsink; 00040 bool needs_deref; 00041 00042 DLLLOCAL void discard_intern() { 00043 if (needs_deref && val) 00044 val->deref(xsink); 00045 } 00046 00048 DLLLOCAL QoreNodeEvalOptionalRefHolder(const QoreNodeEvalOptionalRefHolder&); 00049 00051 DLLLOCAL QoreNodeEvalOptionalRefHolder& operator=(const QoreNodeEvalOptionalRefHolder&); 00052 00054 00056 DLLLOCAL void *operator new(size_t); 00057 00058 public: 00060 DLLLOCAL QoreNodeEvalOptionalRefHolder(ExceptionSink *n_xsink) : val(0), xsink(n_xsink), needs_deref(false) { 00061 } 00062 00064 DLLLOCAL QoreNodeEvalOptionalRefHolder(const AbstractQoreNode *exp, ExceptionSink *n_xsink) : xsink(n_xsink) { 00065 if (exp) 00066 val = exp->eval(needs_deref, xsink); 00067 else { 00068 val = 0; 00069 needs_deref = false; 00070 } 00071 } 00072 00074 DLLLOCAL ~QoreNodeEvalOptionalRefHolder() { 00075 discard_intern(); 00076 } 00077 00079 DLLLOCAL void discard() { 00080 discard_intern(); 00081 needs_deref = false; 00082 val = 0; 00083 } 00084 00086 DLLLOCAL void assign(bool n_needs_deref, AbstractQoreNode *n_val) { 00087 discard_intern(); 00088 needs_deref = n_needs_deref; 00089 val = n_val; 00090 } 00091 00093 DLLLOCAL AbstractQoreNode *getReferencedValue() { 00094 if (needs_deref) 00095 needs_deref = false; 00096 else if (val) 00097 val->ref(); 00098 return val; 00099 } 00100 00102 DLLLOCAL const AbstractQoreNode *operator->() const { return val; } 00103 00105 DLLLOCAL const AbstractQoreNode *operator*() const { return val; } 00106 00108 DLLLOCAL operator bool() const { return val != 0; } 00109 00111 DLLLOCAL bool isTemp() const { return needs_deref; } 00112 00113 }; 00114 00115 #endif