c++ finally don't use std::function

This commit is contained in:
Andrei DAMIAN 2021-04-03 18:22:17 +03:00
parent 3590d4d5e1
commit e7bdb71a28
3 changed files with 7 additions and 10 deletions

View File

@ -34,7 +34,6 @@
#include <exception> #include <exception>
#include <bitset> #include <bitset>
#include <condition_variable> #include <condition_variable>
#include <functional>
#ifndef USE_UTF8_INSTEAD_OF_CODECVT #ifndef USE_UTF8_INSTEAD_OF_CODECVT
#include <codecvt> #include <codecvt>

View File

@ -202,12 +202,6 @@ namespace antlrcpp {
return result; return result;
} }
//----------------- FinallyAction ------------------------------------------------------------------------------------
FinalAction finally(std::function<void ()> f) {
return FinalAction(f);
}
//----------------- SingleWriteMultipleRead -------------------------------------------------------------------------- //----------------- SingleWriteMultipleRead --------------------------------------------------------------------------
void SingleWriteMultipleReadLock::readLock() { void SingleWriteMultipleReadLock::readLock() {

View File

@ -19,8 +19,9 @@ namespace antlrcpp {
std::string indent(const std::string &s, const std::string &indentation, bool includingFirst = true); std::string indent(const std::string &s, const std::string &indentation, bool includingFirst = true);
// Using RAII + a lambda to implement a "finally" replacement. // Using RAII + a lambda to implement a "finally" replacement.
template <typename OnEnd>
struct FinalAction { struct FinalAction {
FinalAction(std::function<void ()> f) : _cleanUp { f } {} FinalAction(OnEnd f) : _cleanUp { std::move(f) } {}
FinalAction(FinalAction &&other) : FinalAction(FinalAction &&other) :
_cleanUp(std::move(other._cleanUp)), _enabled(other._enabled) { _cleanUp(std::move(other._cleanUp)), _enabled(other._enabled) {
other._enabled = false; // Don't trigger the lambda after ownership has moved. other._enabled = false; // Don't trigger the lambda after ownership has moved.
@ -29,11 +30,14 @@ namespace antlrcpp {
void disable() { _enabled = false; } void disable() { _enabled = false; }
private: private:
std::function<void ()> _cleanUp; OnEnd _cleanUp;
bool _enabled {true}; bool _enabled {true};
}; };
ANTLR4CPP_PUBLIC FinalAction finally(std::function<void ()> f); template <typename OnEnd>
FinalAction<OnEnd> finally(OnEnd f) {
return FinalAction<OnEnd>(std::move(f));
}
// Convenience functions to avoid lengthy dynamic_cast() != nullptr checks in many places. // Convenience functions to avoid lengthy dynamic_cast() != nullptr checks in many places.
template <typename T1, typename T2> template <typename T1, typename T2>