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