File: Deleters.hpp

package info (click to toggle)
packagekit 1.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,704 kB
  • sloc: ansic: 56,209; cpp: 13,919; python: 4,970; xml: 4,945; sh: 313; perl: 60; makefile: 57
file content (95 lines) | stat: -rw-r--r-- 2,900 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright (C) Serenity Cybersecurity, LLC <license@futurecrew.ru>
 *               Author: Gleb Popov <arrowd@FreeBSD.org>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#pragma once

#include <vector>
#include <memory>

#include <glib.h>

// alias for an unique_ptr with custom deleter
template<typename T>
using deleted_unique_ptr = std::unique_ptr<T,std::function<void(T*)>>;


// a variant of std::default_delete that calls free() instead of delete
template <class T>
struct free_delete {
  inline constexpr free_delete() noexcept = default;
  template <class U>
  inline
  free_delete(const free_delete<U>&,
                 typename std::enable_if<std::is_convertible<U*, T*>::value>::type* =
                     0) noexcept {}

  inline void operator()(T* __ptr) const noexcept {
    free(__ptr);
  }
};

// alias for an unique_ptr with free() deleter
template<typename T>
using free_deleted_unique_ptr = std::unique_ptr<T,free_delete<T>>;


// a variant of std::default_delete that calls g_free() instead of delete
template <class T>
struct g_free_delete {
  inline constexpr g_free_delete() noexcept = default;
  template <class U>
  inline
  g_free_delete(const g_free_delete<U>&,
                 typename std::enable_if<std::is_convertible<U*, T*>::value>::type* =
                     0) noexcept {}

  inline void operator()(T* __ptr) const noexcept {
    g_free(__ptr);
  }
};

// alias for an unique_ptr with free() deleter
template<typename T>
using g_free_deleted_unique_ptr = std::unique_ptr<T,g_free_delete<T>>;


// a variant of std::default_delete that calls g_strfreev() instead of delete
struct g_strfreev_delete {
  inline constexpr g_strfreev_delete() noexcept = default;

  inline void operator()(gchar** __ptr) const noexcept {
    g_strfreev(__ptr);
  }
};

// alias for an unique_ptr with g_strfreev() deleter
template<typename T>
using g_strfreev_deleted_unique_ptr = std::unique_ptr<T,g_strfreev_delete>;

// a variant of std::vector that g_free()'s its elements on destruction

class gchar_ptr_vector : public std::vector<gchar*> {
public:
  ~gchar_ptr_vector() {
    for (gchar* ptr : *this)
      g_free (ptr);
  }
};