aboutsummaryrefslogtreecommitdiffhomepage
path: root/tdutils/td/utils/Destructor.h
blob: 2afb6a8fa09755ada7296b2f7df72e3a62402984 (plain)
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
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once

#include "td/utils/common.h"

#include <memory>
#include <utility>

namespace td {

class Destructor {
 public:
  Destructor() = default;
  Destructor(const Destructor &) = delete;
  Destructor &operator=(const Destructor &) = delete;
  Destructor(Destructor &&) = default;
  Destructor &operator=(Destructor &&) = default;
  virtual ~Destructor() = default;
};

template <class F>
class LambdaDestructor final : public Destructor {
 public:
  explicit LambdaDestructor(F &&f) : f_(std::move(f)) {
  }
  LambdaDestructor(const LambdaDestructor &) = delete;
  LambdaDestructor &operator=(const LambdaDestructor &) = delete;
  LambdaDestructor(LambdaDestructor &&) = default;
  LambdaDestructor &operator=(LambdaDestructor &&) = default;
  ~LambdaDestructor() final {
    f_();
  }

 private:
  F f_;
};

template <class F>
auto create_destructor(F &&f) {
  return make_unique<LambdaDestructor<F>>(std::forward<F>(f));
}
template <class F>
auto create_shared_destructor(F &&f) {
  return std::make_shared<LambdaDestructor<F>>(std::forward<F>(f));
}

}  // namespace td