aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/util.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/libutil/util.hh')
-rw-r--r--src/libutil/util.hh52
1 files changed, 12 insertions, 40 deletions
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 6850b5a7a..b8e201203 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -12,13 +12,9 @@
#include <signal.h>
#include <functional>
-#include <limits>
-#include <cstdio>
#include <map>
#include <sstream>
#include <optional>
-#include <future>
-#include <iterator>
#ifndef HAVE_STRUCT_DIRENT_D_TYPE
#define DT_UNKNOWN 0
@@ -464,6 +460,12 @@ string base64Encode(std::string_view s);
string base64Decode(std::string_view s);
+/* Remove common leading whitespace from the lines in the string
+ 's'. For example, if every line is indented by at least 3 spaces,
+ then we remove 3 spaces from the start of every line. */
+std::string stripIndentation(std::string_view s);
+
+
/* Get a value for the specified key from an associate container. */
template <class T>
std::optional<typename T::mapped_type> get(const T & map, const typename T::key_type & key)
@@ -474,43 +476,8 @@ std::optional<typename T::mapped_type> get(const T & map, const typename T::key_
}
-/* A callback is a wrapper around a lambda that accepts a valid of
- type T or an exception. (We abuse std::future<T> to pass the value or
- exception.) */
template<typename T>
-class Callback
-{
- std::function<void(std::future<T>)> fun;
- std::atomic_flag done = ATOMIC_FLAG_INIT;
-
-public:
-
- Callback(std::function<void(std::future<T>)> fun) : fun(fun) { }
-
- Callback(Callback && callback) : fun(std::move(callback.fun))
- {
- auto prev = callback.done.test_and_set();
- if (prev) done.test_and_set();
- }
-
- void operator()(T && t) noexcept
- {
- auto prev = done.test_and_set();
- assert(!prev);
- std::promise<T> promise;
- promise.set_value(std::move(t));
- fun(promise.get_future());
- }
-
- void rethrow(const std::exception_ptr & exc = std::current_exception()) noexcept
- {
- auto prev = done.test_and_set();
- assert(!prev);
- std::promise<T> promise;
- promise.set_exception(exc);
- fun(promise.get_future());
- }
-};
+class Callback;
/* Start a thread that handles various signals. Also block those signals
@@ -601,4 +568,9 @@ constexpr auto enumerate(T && iterable)
}
+// C++17 std::visit boilerplate
+template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
+template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
+
+
}