From de1516a8d822f72bddfb158126b6e609afc63c64 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 8 Jul 2025 11:52:44 +1000 Subject: Replace on_scope_end with CTAD with OnScopeEnd directly --- src/buffer_utils.cc | 4 ++-- src/command_manager.cc | 4 ++-- src/commands.cc | 4 ++-- src/debug.cc | 2 +- src/file.cc | 12 ++++++------ src/highlighters.cc | 2 +- src/hook_manager.cc | 2 +- src/input_handler.cc | 6 +++--- src/normal.cc | 4 ++-- src/remote.cc | 8 ++++---- src/shell_manager.cc | 2 +- src/terminal_ui.cc | 2 +- src/utils.hh | 10 ++-------- 13 files changed, 28 insertions(+), 34 deletions(-) diff --git a/src/buffer_utils.cc b/src/buffer_utils.cc index dca1a372..cd6cd939 100644 --- a/src/buffer_utils.cc +++ b/src/buffer_utils.cc @@ -221,7 +221,7 @@ void write_buffer_to_file(Buffer& buffer, StringView filename, } { - auto close_fd = on_scope_end([fd]{ close(fd); }); + auto close_fd = OnScopeEnd([fd]{ close(fd); }); write_buffer_to_fd(buffer, fd); if (flags & WriteFlags::Sync) ::fsync(fd); @@ -310,7 +310,7 @@ Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, AutoScroll const int fifo = fd(); { - auto restore_flags = on_scope_end([this, flags=m_buffer.flags()] { m_buffer.flags() = flags; }); + auto restore_flags = OnScopeEnd([this, flags=m_buffer.flags()] { m_buffer.flags() = flags; }); m_buffer.flags() &= ~Buffer::Flags::ReadOnly; do { diff --git a/src/command_manager.cc b/src/command_manager.cc index 0db97e94..28c9f075 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -82,7 +82,7 @@ void CommandManager::load_module(StringView module_name, Context& context) { module->value.state = Module::State::Loading; - auto restore_state = on_scope_end([&] { module->value.state = Module::State::Registered; }); + auto restore_state = OnScopeEnd([&] { module->value.state = Module::State::Registered; }); Context empty_context{Context::EmptyContextFlag{}}; execute(module->value.commands, empty_context); @@ -526,7 +526,7 @@ void CommandManager::execute_single_command(CommandParameters params, throw runtime_error("maximum nested command depth hit"); ++m_command_depth; - auto pop_depth = on_scope_end([this] { --m_command_depth; }); + auto pop_depth = OnScopeEnd([this] { --m_command_depth; }); auto command_it = m_commands.find(resolve_alias(context, params[0])); if (command_it == m_commands.end()) diff --git a/src/commands.cc b/src/commands.cc index 351134f2..a190bb7b 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -2069,7 +2069,7 @@ void context_wrap(const ParametersParser& parser, Context& context, StringView d const auto& register_manager = RegisterManager::instance(); auto make_register_restorer = [&](char c) { auto& reg = register_manager[c]; - return on_scope_end([&, c, save=reg.save(context), d=ScopedSetBool{reg.modified_hook_disabled()}] { + return OnScopeEnd([&, c, save=reg.save(context), d=ScopedSetBool{reg.modified_hook_disabled()}] { try { reg.restore(context, save); @@ -2328,7 +2328,7 @@ const CommandDesc prompt_cmd = { return; sc.env_vars["text"_sv] = String{String::NoCopy{}, str}; - auto remove_text = on_scope_end([&] { + auto remove_text = OnScopeEnd([&] { sc.env_vars.erase("text"_sv); }); diff --git a/src/debug.cc b/src/debug.cc index 55f7e4d3..19180f5f 100644 --- a/src/debug.cc +++ b/src/debug.cc @@ -23,7 +23,7 @@ void write_to_debug_buffer(StringView str) if (Buffer* buffer = BufferManager::instance().get_buffer_ifp(debug_buffer_name)) { buffer->flags() &= ~Buffer::Flags::ReadOnly; - auto restore = on_scope_end([buffer] { buffer->flags() |= Buffer::Flags::ReadOnly; }); + auto restore = OnScopeEnd([buffer] { buffer->flags() |= Buffer::Flags::ReadOnly; }); buffer->insert(buffer->back_coord(), eol_back ? str : str + "\n"); } diff --git a/src/file.cc b/src/file.cc index 67781044..eb17379c 100644 --- a/src/file.cc +++ b/src/file.cc @@ -194,7 +194,7 @@ String read_file(StringView filename, bool text) if (fd == -1) throw file_access_error(filename, strerror(errno)); - auto close_fd = on_scope_end([fd]{ close(fd); }); + auto close_fd = OnScopeEnd([fd]{ close(fd); }); return read_fd(fd, text); } @@ -204,7 +204,7 @@ MappedFile::MappedFile(StringView filename) int fd = open(filename.zstr(), O_RDONLY | O_NONBLOCK); if (fd == -1) throw file_access_error(filename, strerror(errno)); - auto close_fd = on_scope_end([&] { close(fd); }); + auto close_fd = OnScopeEnd([&] { close(fd); }); fstat(fd, &st); if (S_ISDIR(st.st_mode)) @@ -253,7 +253,7 @@ void write(int fd, StringView data) int flags = fcntl(fd, F_GETFL, 0); if (not atomic and EventManager::has_instance()) fcntl(fd, F_SETFL, flags | O_NONBLOCK); - auto restore_flags = on_scope_end([&] { fcntl(fd, F_SETFL, flags); }); + auto restore_flags = OnScopeEnd([&] { fcntl(fd, F_SETFL, flags); }); while (count) { @@ -291,7 +291,7 @@ void write_to_file(StringView filename, StringView data) int fd = create_file(filename.zstr()); if (fd == -1) throw file_access_error(filename, strerror(errno)); - auto close_fd = on_scope_end([fd]{ close(fd); }); + auto close_fd = OnScopeEnd([fd]{ close(fd); }); write(fd, data); } @@ -358,7 +358,7 @@ void make_directory(StringView dir, mode_t mode) else { auto old_mask = umask(0); - auto restore_mask = on_scope_end([old_mask]() { umask(old_mask); }); + auto restore_mask = OnScopeEnd([old_mask]() { umask(old_mask); }); if (mkdir(dirname.zstr(), mode) != 0) throw runtime_error(format("mkdir failed for directory '{}' errno {}", dirname, errno)); @@ -374,7 +374,7 @@ void list_files(StringView dirname, FunctionRef ShellManager::eval( sigemptyset(&mask); sigaddset(&mask, SIGCHLD); sigprocmask(SIG_BLOCK, &mask, &orig_mask); - auto restore_mask = on_scope_end([&] { sigprocmask(SIG_SETMASK, &orig_mask, nullptr); }); + auto restore_mask = OnScopeEnd([&] { sigprocmask(SIG_SETMASK, &orig_mask, nullptr); }); int status = 0; // check for termination now that SIGCHLD is blocked diff --git a/src/terminal_ui.cc b/src/terminal_ui.cc index c55d0763..3d963e82 100644 --- a/src/terminal_ui.cc +++ b/src/terminal_ui.cc @@ -637,7 +637,7 @@ void TerminalUI::check_resize(bool force) const int fd = open("/dev/tty", O_RDWR); if (fd < 0) return; - auto close_fd = on_scope_end([fd]{ ::close(fd); }); + auto close_fd = OnScopeEnd([fd]{ ::close(fd); }); DisplayCoord terminal_size{24_line, 80_col}; if (winsize ws; ioctl(fd, TIOCGWINSZ, &ws) == 0 and ws.ws_row > 0 and ws.ws_col > 0) diff --git a/src/utils.hh b/src/utils.hh index ba8cf737..5ec5b334 100644 --- a/src/utils.hh +++ b/src/utils.hh @@ -51,11 +51,11 @@ Singleton* Singleton::ms_instance = nullptr; // *** On scope end *** // -// on_scope_end provides a way to register some code to be +// OnScopeEnd provides a way to register some code to be // executed when current scope closes. // // usage: -// auto cleaner = on_scope_end([]() { ... }); +// auto cleaner = OnScopeEnd([]() { ... }); // // This permits to cleanup c-style resources without implementing // a wrapping class @@ -82,12 +82,6 @@ private: T m_func; }; -template -OnScopeEnd on_scope_end(T t) -{ - return OnScopeEnd{std::move(t)}; -} - // bool that can be set (to true) multiple times, and will // be false only when unset the same time; struct NestedBool -- cgit v1.2.3