Bitcoin Core 29.99.0
P2P Digital Currency
util.h
Go to the documentation of this file.
1// Copyright (c) 2019 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef MP_UTIL_H
6#define MP_UTIL_H
7
8#include <capnp/schema.h>
9#include <cstddef>
10#include <functional>
11#include <future>
12#include <kj/common.h>
13#include <kj/exception.h>
14#include <kj/string-tree.h>
15#include <memory>
16#include <string.h>
17#include <string>
18#include <tuple>
19#include <type_traits>
20#include <utility>
21#include <vector>
22
23namespace mp {
24
26
31template <typename... Types>
33{
34 static constexpr size_t size = sizeof...(Types);
35};
36
45template <template <typename...> class Class, typename... Types, typename... Args>
46Class<Types..., std::remove_reference_t<Args>...> Make(Args&&... args)
47{
48 return Class<Types..., std::remove_reference_t<Args>...>{std::forward<Args>(args)...};
49}
50
56template <std::size_t index, typename List, typename _First = TypeList<>, bool done = index == 0>
57struct Split;
58
60template <typename _Second, typename _First>
61struct Split<0, _Second, _First, true>
62{
63 using First = _First;
64 using Second = _Second;
65};
66
68template <std::size_t index, typename Type, typename... _Second, typename... _First>
69struct Split<index, TypeList<Type, _Second...>, TypeList<_First...>, false>
70{
71 using _Next = Split<index - 1, TypeList<_Second...>, TypeList<_First..., Type>>;
72 using First = typename _Next::First;
73 using Second = typename _Next::Second;
74};
75
77template <typename Callable>
78using ResultOf = decltype(std::declval<Callable>()());
79
81template <typename T>
82using RemoveCvRef = std::remove_cv_t<std::remove_reference_t<T>>;
83
85template <typename T>
86using Decay = std::decay_t<T>;
87
89template <typename SfinaeExpr, typename Result_>
91{
92 using Result = Result_;
93};
94
96template <typename SfinaeExpr, typename Result = void>
98
107template <int priority>
108struct Priority : Priority<priority - 1>
109{
110};
111
113template <>
114struct Priority<0>
115{
116};
117
119template <typename T>
120const char* TypeName()
121{
122 // DisplayName string looks like
123 // "interfaces/capnp/common.capnp:ChainNotifications.resendWalletTransactions$Results"
124 // This discards the part of the string before the first ':' character.
125 // Another alternative would be to use the displayNamePrefixLength field,
126 // but this discards everything before the last '.' character, throwing away
127 // the object name, which is useful.
128 const char* display_name = ::capnp::Schema::from<T>().getProto().getDisplayName().cStr();
129 const char* short_name = strchr(display_name, ':');
130 return short_name ? short_name + 1 : display_name;
131}
132
134template <typename Lock>
136{
137 UnlockGuard(Lock& lock) : m_lock(lock) { m_lock.unlock(); }
138 ~UnlockGuard() { m_lock.lock(); }
139 Lock& m_lock;
140};
141
142template <typename Lock, typename Callback>
143void Unlock(Lock& lock, Callback&& callback)
144{
145 const UnlockGuard<Lock> unlock(lock);
146 callback();
147}
148
151template <typename T>
153{
155 template <typename... Params>
156 DestructorCatcher(Params&&... params) : value(kj::fwd<Params>(params)...)
157 {
158 }
160 } catch (const kj::Exception& e) { // NOLINT(bugprone-empty-catch)
161 }
162};
163
169template <typename Callable>
171{
172 AsyncCallable(Callable&& callable) : m_callable(std::make_shared<DestructorCatcher<Callable>>(std::move(callable)))
173 {
174 }
175 AsyncCallable(const AsyncCallable&) = default;
177 ~AsyncCallable() noexcept = default;
178 ResultOf<Callable> operator()() const { return (m_callable->value)(); }
179 mutable std::shared_ptr<DestructorCatcher<Callable>> m_callable;
180};
181
183template <typename Callable>
185{
186 return std::move(callable);
187}
188
190std::string ThreadName(const char* exe_name);
191
194std::string LogEscape(const kj::StringTree& string);
195
197using FdToArgsFn = std::function<std::vector<std::string>(int fd)>;
198
205int SpawnProcess(int& pid, FdToArgsFn&& fd_to_args);
206
208void ExecProcess(const std::vector<std::string>& args);
209
211int WaitProcess(int pid);
212
213inline char* CharCast(char* c) { return c; }
214inline char* CharCast(unsigned char* c) { return (char*)c; }
215inline const char* CharCast(const char* c) { return c; }
216inline const char* CharCast(const unsigned char* c) { return (const char*)c; }
217
218} // namespace mp
219
220#endif // MP_UTIL_H
ArgsManager & args
Definition: bitcoind.cpp:277
const CChainParams & Params()
Return the currently selected parameters.
#define T(expected, seed, data)
Functions to serialize / deserialize common bitcoin types.
Definition: common-types.h:57
void Unlock(Lock &lock, Callback &&callback)
Definition: util.h:143
int WaitProcess(int pid)
Wait for a process to exit and return its exit code.
Definition: util.cpp:145
const char * TypeName()
Return capnp type name with filename prefix removed.
Definition: util.h:120
std::string ThreadName(const char *exe_name)
Format current thread name as "{exe_name}-{$pid}/{thread_name}-{$tid}".
Definition: util.cpp:47
typename _Require< SfinaeExpr, Result >::Result Require
SFINAE helper, basically the same as to C++17's void_t, but allowing types other than void to be retu...
Definition: util.h:97
int SpawnProcess(int &pid, FdToArgsFn &&fd_to_args)
Spawn a new process that communicates with the current process over a socket pair.
Definition: util.cpp:103
AsyncCallable< std::remove_reference_t< Callable > > MakeAsyncCallable(Callable &&callable)
Construct AsyncCallable object.
Definition: util.h:184
Class< Types..., std::remove_reference_t< Args >... > Make(Args &&... args)
Construct a template class value by deducing template arguments from the types of constructor argumen...
Definition: util.h:46
std::decay_t< T > Decay
Type helper abbreviating std::decay.
Definition: util.h:86
std::remove_cv_t< std::remove_reference_t< T > > RemoveCvRef
Substitutue for std::remove_cvref_t.
Definition: util.h:82
std::string LogEscape(const kj::StringTree &string)
Escape binary string for use in log so it doesn't trigger unicode decode errors in python unit tests.
Definition: util.cpp:78
decltype(std::declval< Callable >()()) ResultOf
Type helper giving return type of a callable type.
Definition: util.h:78
char * CharCast(char *c)
Definition: util.h:213
std::function< std::vector< std::string >(int fd)> FdToArgsFn
Callback type used by SpawnProcess below.
Definition: util.h:197
void ExecProcess(const std::vector< std::string > &args)
Call execvp with vector args.
Definition: util.cpp:131
SFINAE helper, see using Require below.
Definition: util.h:91
Result_ Result
Definition: util.h:92
Wrapper around callback function for compatibility with std::async.
Definition: util.h:171
AsyncCallable(AsyncCallable &&)=default
AsyncCallable(Callable &&callable)
Definition: util.h:172
~AsyncCallable() noexcept=default
AsyncCallable(const AsyncCallable &)=default
std::shared_ptr< DestructorCatcher< Callable > > m_callable
Definition: util.h:179
Needed for libc++/macOS compatibility.
Definition: util.h:153
DestructorCatcher(Params &&... params)
Definition: util.h:156
~DestructorCatcher() noexcept
Definition: util.h:159
Function parameter type for prioritizing overloaded function calls that would otherwise be ambiguous.
Definition: util.h:109
Type helper splitting a TypeList into two halves at position index.
Definition: util.h:57
Generic utility functions used by capnp code.
Definition: util.h:33
static constexpr size_t size
Definition: util.h:34
Analog to std::lock_guard that unlocks instead of locks.
Definition: util.h:136
UnlockGuard(Lock &lock)
Definition: util.h:137
~UnlockGuard()
Definition: util.h:138
Lock & m_lock
Definition: util.h:139