2018-08-13 19:15:09 +02:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
|
|
|
//
|
|
|
|
// 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;
|
|
|
|
Destructor(const Destructor &other) = delete;
|
|
|
|
Destructor &operator=(const Destructor &other) = delete;
|
|
|
|
Destructor(Destructor &&other) = default;
|
|
|
|
Destructor &operator=(Destructor &&other) = default;
|
|
|
|
virtual ~Destructor() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class F>
|
|
|
|
class LambdaDestructor : public Destructor {
|
|
|
|
public:
|
|
|
|
explicit LambdaDestructor(F &&f) : f_(std::move(f)) {
|
|
|
|
}
|
|
|
|
LambdaDestructor(const LambdaDestructor &other) = delete;
|
|
|
|
LambdaDestructor &operator=(const LambdaDestructor &other) = delete;
|
|
|
|
LambdaDestructor(LambdaDestructor &&other) = default;
|
|
|
|
LambdaDestructor &operator=(LambdaDestructor &&other) = default;
|
|
|
|
~LambdaDestructor() override {
|
|
|
|
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
|