2018-08-13 19:15:09 +02:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-08-13 19:15:09 +02:00
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
2018-09-07 02:41:21 +02:00
|
|
|
#pragma once
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
#include "td/utils/common.h"
|
|
|
|
|
2018-09-07 02:41:21 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class Destructor {
|
|
|
|
public:
|
|
|
|
Destructor() = default;
|
2023-05-05 12:51:19 +02:00
|
|
|
Destructor(const Destructor &) = delete;
|
|
|
|
Destructor &operator=(const Destructor &) = delete;
|
|
|
|
Destructor(Destructor &&) = default;
|
|
|
|
Destructor &operator=(Destructor &&) = default;
|
2018-08-13 19:15:09 +02:00
|
|
|
virtual ~Destructor() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class F>
|
2021-07-04 04:58:54 +02:00
|
|
|
class LambdaDestructor final : public Destructor {
|
2018-08-13 19:15:09 +02:00
|
|
|
public:
|
|
|
|
explicit LambdaDestructor(F &&f) : f_(std::move(f)) {
|
|
|
|
}
|
2023-05-05 12:51:19 +02:00
|
|
|
LambdaDestructor(const LambdaDestructor &) = delete;
|
|
|
|
LambdaDestructor &operator=(const LambdaDestructor &) = delete;
|
|
|
|
LambdaDestructor(LambdaDestructor &&) = default;
|
|
|
|
LambdaDestructor &operator=(LambdaDestructor &&) = default;
|
2021-07-03 22:51:36 +02:00
|
|
|
~LambdaDestructor() final {
|
2018-08-13 19:15:09 +02:00
|
|
|
f_();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
F f_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class F>
|
|
|
|
auto create_destructor(F &&f) {
|
2018-09-27 03:19:03 +02:00
|
|
|
return make_unique<LambdaDestructor<F>>(std::forward<F>(f));
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
template <class F>
|
|
|
|
auto create_shared_destructor(F &&f) {
|
|
|
|
return std::make_shared<LambdaDestructor<F>>(std::forward<F>(f));
|
|
|
|
}
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
} // namespace td
|