Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
funclog.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
5#ifndef INKSCAPE_UTIL_FUNCLOG_H
6#define INKSCAPE_UTIL_FUNCLOG_H
7
8#include <utility>
9#include <exception>
10#include "util/pool.h"
11
12namespace Inkscape {
13namespace Util {
14
23class FuncLog final
24{
25public:
26 FuncLog() = default;
27 FuncLog(FuncLog &&other) noexcept { movefrom(other); }
28 FuncLog &operator=(FuncLog &&other) noexcept { destroy(); movefrom(other); return *this; }
30
35 template <typename F>
36 void emplace(F &&f)
37 {
38 using Fd = typename std::decay<F>::type;
39 auto entry = pool.allocate<Entry<Fd>>();
40 new (entry) Entry<Fd>(std::forward<F>(f));
41 *lastnext = entry;
42 lastnext = &entry->next;
43 entry->next = nullptr;
44 }
45
51 void exec();
52
54 void operator()() { exec(); }
55
61 template <typename C>
62 void exec_while(C &&c)
63 {
64 for (auto h = first; h; destroy_and_advance(h)) {
65 try {
66 if (!c()) {
67 destroy_from(h);
68 break;
69 }
70 (*h)();
71 } catch (...) {
72 destroy_from(h);
73 reset();
74 std::rethrow_exception(std::current_exception());
75 }
76 }
77 reset();
78 }
79
84 void clear() { destroy(); reset(); }
85
86 bool empty() const { return !first; }
87
88private:
89 struct Header
90 {
92 virtual ~Header() = default;
93 virtual void operator()() = 0;
94 };
95
96 template <typename Fd>
97 struct Entry : Header
98 {
99 Fd f;
100 template <typename F>
101 Entry(F &&f) : f(std::forward<F>(f)) {}
102 void operator()() override { f(); }
103 };
104
106 Header *first = nullptr;
108
110 static void destroy_from(Header *h) { while (h) destroy_and_advance(h); }
111 static void destroy_and_advance(Header *&h) noexcept;
112 void reset() noexcept;
113 void movefrom(FuncLog &other) noexcept;
114};
115
116} // namespace Util
117} // namespace Inkscape
118
119#endif // INKSCAPE_UTIL_FUNCLOG_H
A FuncLog is effectively a std::vector<std::function<void()>>, with the ability to hold move-only fun...
Definition funclog.h:24
void exec()
Execute and destroy each callable in the log.
Definition funclog.cpp:7
FuncLog & operator=(FuncLog &&other) noexcept
Definition funclog.h:28
FuncLog(FuncLog &&other) noexcept
Definition funclog.h:27
void clear()
Destroy all callables in the log without executing them.
Definition funclog.h:84
void operator()()
Convenience alias for exec().
Definition funclog.h:54
void movefrom(FuncLog &other) noexcept
Definition funclog.cpp:35
static void destroy_and_advance(Header *&h) noexcept
Definition funclog.cpp:21
void emplace(F &&f)
Append a callable object to the log.
Definition funclog.h:36
bool empty() const
Definition funclog.h:86
static void destroy_from(Header *h)
Definition funclog.h:110
void exec_while(C &&c)
Execute and destroy each callable in the log while condition c() is true, then destroy the rest.
Definition funclog.h:62
void reset() noexcept
Definition funclog.cpp:28
A Pool is a block allocator with the following characteristics:
Definition pool.h:27
std::byte * allocate(std::size_t size, std::size_t alignment)
Allocate a block of the given size and alignment.
Definition pool.cpp:18
double c[8][4]
Miscellaneous supporting code.
Definition document.h:93
Helper class to stream background task notifications as a series of messages.
STL namespace.
void operator()() override
Definition funclog.h:102