Lumiera  0.pre.03
»edit your freedom«
unique-malloc-owner.hpp
Go to the documentation of this file.
1 /*
2  UNIQUE-MALLOC-OWNER.hpp - automatic management of C-malloced memory
3 
4  Copyright (C) Lumiera.org
5  2014, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
36 #ifndef LIB_UNIQUE_MALLOC_OWNER_H
37 #define LIB_UNIQUE_MALLOC_OWNER_H
38 
39 
40 #include "lib/error.hpp"
41 
42 #include <memory>
43 
44 
45 namespace lib {
46 
47 
56  template<class X =void>
58  : public std::unique_ptr<X, void(*)(void*)>
59  {
60  using _Parent = std::unique_ptr<X, void(*)(void*)>;
61 
62  public:
63  explicit
64  UniqueMallocOwner(void* memory =nullptr)
65  : _Parent(static_cast<X*>(memory), std::free)
66  { }
67 
68  explicit
69  UniqueMallocOwner(X* alloc)
70  : _Parent(alloc, std::free)
71  { }
72 
73 
74  bool
75  empty() const
76  {
77  return ! bool(*this);
78  }
79  };
80 
81 
82 
83 
84 
85 } // namespace lib
86 #endif /*LIB_UNIQUE_MALLOC_OWNER_H*/
Implementation namespace for support and library code.
Lumiera error handling (C++ interface).
Ownership token for a piece of heap memory allocated in plain-C style.