yat/utility/Deleter.h

Code
Comments
Other
Rev Date Author Line
2858 28 Sep 12 peter 1 #ifndef _theplu_yat_utility_deleter
2858 28 Sep 12 peter 2 #define _theplu_yat_utility_deleter
2858 28 Sep 12 peter 3
2858 28 Sep 12 peter 4 // $Id$
2858 28 Sep 12 peter 5
2858 28 Sep 12 peter 6 /*
4089 07 Sep 21 peter 7   Copyright (C) 2012, 2021 Peter Johansson
2858 28 Sep 12 peter 8
2858 28 Sep 12 peter 9   This file is part of the yat library, http://dev.thep.lu.se/yat
2858 28 Sep 12 peter 10
2858 28 Sep 12 peter 11   The yat library is free software; you can redistribute it and/or
2858 28 Sep 12 peter 12   modify it under the terms of the GNU General Public License as
2858 28 Sep 12 peter 13   published by the Free Software Foundation; either version 3 of the
2858 28 Sep 12 peter 14   License, or (at your option) any later version.
2858 28 Sep 12 peter 15
2858 28 Sep 12 peter 16   The yat library is distributed in the hope that it will be useful,
2858 28 Sep 12 peter 17   but WITHOUT ANY WARRANTY; without even the implied warranty of
2858 28 Sep 12 peter 18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2858 28 Sep 12 peter 19   General Public License for more details.
2858 28 Sep 12 peter 20
2858 28 Sep 12 peter 21   You should have received a copy of the GNU General Public License
2858 28 Sep 12 peter 22   along with yat. If not, see <http://www.gnu.org/licenses/>.
2858 28 Sep 12 peter 23 */
2858 28 Sep 12 peter 24
4022 04 Jan 21 peter 25 #include <new> // for delete operator
4022 04 Jan 21 peter 26
2858 28 Sep 12 peter 27 namespace theplu {
2858 28 Sep 12 peter 28 namespace yat {
2858 28 Sep 12 peter 29 namespace utility {
2858 28 Sep 12 peter 30
2858 28 Sep 12 peter 31   /**
2858 28 Sep 12 peter 32      \brief Functor that deletes an object.
2858 28 Sep 12 peter 33
2858 28 Sep 12 peter 34      The class is typically used as second argument to a
4022 04 Jan 21 peter 35      std::shared_ptr, which enables to decide at runtime whether the
2858 28 Sep 12 peter 36      shared_ptr should own the pointee, i.e., should the pointee be
2858 28 Sep 12 peter 37      destructed at destruction of last shared_ptr pointing to the
2858 28 Sep 12 peter 38      pointee.
2858 28 Sep 12 peter 39
2858 28 Sep 12 peter 40      \since New in yat 0.10
2858 28 Sep 12 peter 41    */
2858 28 Sep 12 peter 42   class Deleter
2858 28 Sep 12 peter 43   {
2858 28 Sep 12 peter 44   public:
2858 28 Sep 12 peter 45     /**
2858 28 Sep 12 peter 46        \brief constructor
2858 28 Sep 12 peter 47
2858 28 Sep 12 peter 48        \param active decides whether functor will delete object or
2858 28 Sep 12 peter 49        not. See operator().
2858 28 Sep 12 peter 50      */
2858 28 Sep 12 peter 51     Deleter(bool active=true);
2858 28 Sep 12 peter 52
2858 28 Sep 12 peter 53     /**
2858 28 Sep 12 peter 54        \return true if class is activated.
2858 28 Sep 12 peter 55      */
2858 28 Sep 12 peter 56     bool active(void) const;
2858 28 Sep 12 peter 57
2858 28 Sep 12 peter 58     /**
2858 28 Sep 12 peter 59        Delete \a ptr if active() otherwise do nothing
2858 28 Sep 12 peter 60      */
2858 28 Sep 12 peter 61     template<typename T>
2858 28 Sep 12 peter 62     void operator()(T* ptr) const
2858 28 Sep 12 peter 63     {
2858 28 Sep 12 peter 64       if (active_)
2858 28 Sep 12 peter 65         delete ptr;
2858 28 Sep 12 peter 66     }
2858 28 Sep 12 peter 67
2858 28 Sep 12 peter 68   private:
2858 28 Sep 12 peter 69     bool active_;
2858 28 Sep 12 peter 70   };
2858 28 Sep 12 peter 71
2858 28 Sep 12 peter 72 }}} // of namespace utility, yat, and theplu
2858 28 Sep 12 peter 73
2858 28 Sep 12 peter 74 #endif