/* * lftp - file transfer program * * Copyright (c) 1996-2012 by Alexander V. Lukyanov (lav@yars.free.net) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef REF_H #define REF_H template class Ref { Ref(const Ref&); // disable cloning void operator=(const Ref&); // and assignment protected: T *ptr; public: Ref() { ptr=0; } Ref(T *p) { ptr=p; } ~Ref() { delete ptr; } void operator=(T *p) { delete ptr; ptr=p; } operator const T*() const { return ptr; } T *operator->() const { return ptr; } T *borrow() { return replace_value(ptr,(T*)0); } const T *get() const { return ptr; } T *get_non_const() const { return ptr; } template const Ref& Cast() const { void(static_cast(ptr)); return *(const Ref*)this; } static const Ref null; void _set(T *p) { ptr=p; } void _clear() { ptr=0; } void unset() { *this=0; } }; template const Ref Ref::null; template class RefToArray : public Ref { RefToArray(const RefToArray&); // disable cloning void operator=(const RefToArray&); // and assignment public: RefToArray() {} RefToArray(T *p) : Ref(p) {} ~RefToArray() { delete[] Ref::ptr; Ref::ptr=0; } void operator=(T *p) { delete[] Ref::ptr; Ref::ptr=p; } T& operator[](unsigned i) const { return Ref::ptr[i]; } static const RefToArray null; }; template const RefToArray RefToArray::null; #endif